Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Apache POI将docx转换为PDF raise java.lang.RuntimeException:表格宽度必须大于零_Java_Apache Poi_Pdf Generation - Fatal编程技术网

Apache POI将docx转换为PDF raise java.lang.RuntimeException:表格宽度必须大于零

Apache POI将docx转换为PDF raise java.lang.RuntimeException:表格宽度必须大于零,java,apache-poi,pdf-generation,Java,Apache Poi,Pdf Generation,我正在尝试使用ApachePOI生成Word文档(这没问题),然后我想将此docx转换为PDF 我正在使用PdfConverter进行此操作,但在生成它时出现了一个非常奇怪的错误 代码如下: public static void main(String[] args)throws Exception { //Blank Document XWPFDocument document = new XWPFDocument(); FileOutp

我正在尝试使用ApachePOI生成Word文档(这没问题),然后我想将此docx转换为PDF

我正在使用PdfConverter进行此操作,但在生成它时出现了一个非常奇怪的错误

代码如下:

    public static void main(String[] args)throws Exception {
        //Blank Document
        XWPFDocument document = new XWPFDocument();
        FileOutputStream out = new FileOutputStream(new File("C:\\temp\\Rapport" + "_" + "00" + ".docx"));
        document.createNumbering();

        CTDocument1 doc = document.getDocument();
        CTBody body = doc.getBody();

        XWPFStyles styles = document.createStyles();
        if (!body.isSetSectPr()) {
             body.addNewSectPr();
        }
        CTSectPr section = body.getSectPr();

        if(!section.isSetPgSz()) {
            section.addNewPgSz();
        }
        CTPageSz pageSize = section.getPgSz();
        pageSize.setOrient(STPageOrientation.PORTRAIT);
        pageSize.setW(BigInteger.valueOf(595));
        pageSize.setH(BigInteger.valueOf(842));

        XWPFParagraph paragraph0 = document.createParagraph();
        paragraph0.setAlignment(ParagraphAlignment.CENTER );
        XWPFRun run0 = paragraph0.createRun();
        run0.setBold(true);  
        run0.setFontSize(14);
        run0.addBreak();
        run0.addBreak();
        run0.setText("RAPPORT\n");

        //Close document
        document.write(out);
        out.close();
        System.out.println("word document written successfully");

        try {
            InputStream docu = new FileInputStream(new File("C:\\temp\\Rapport" + "_" + "00" + ".docx"));
            PdfOptions options = PdfOptions.create();
            OutputStream outWord = new FileOutputStream(new File("C:\\temp\\Rapport" + "_" + "00" + ".pdf"));
            PdfConverter.getInstance().convert(document, outWord, options);
            System.out.println("Done");
        } catch (FileNotFoundException ex) {
            System.out.println(ex.getMessage());
        } catch (IOException ex) {

            System.out.println(ex.getMessage());
        }

        System.out.println("PDF document written successfully");

}
以下是提出的例外情况:

Exception in thread "main" fr.opensagres.poi.xwpf.converter.core.XWPFConverterException: fr.opensagres.poi.xwpf.converter.core.XWPFConverterException: com.lowagie.text.DocumentException: java.lang.RuntimeException: The table width must be greater than zero.
at fr.opensagres.poi.xwpf.converter.pdf.PdfConverter.doConvert(PdfConverter.java:71)
at fr.opensagres.poi.xwpf.converter.pdf.PdfConverter.doConvert(PdfConverter.java:39)
at fr.opensagres.poi.xwpf.converter.core.AbstractXWPFConverter.convert(AbstractXWPFConverter.java:46)
at com.jre.word.wordGeneration2.main(wordGeneration2.java:69)
我的Word文档生成正确,但我在生成PDF时遇到了多个问题: -我必须创造风格 -定义页面大小的步骤

现在我有一个错误链接到一个链接到样式的表(即使我没有创建表):

Caused by: fr.opensagres.poi.xwpf.converter.core.XWPFConverterException: com.lowagie.text.DocumentException: java.lang.RuntimeException: The table width must be greater than zero.
at fr.opensagres.poi.xwpf.converter.pdf.internal.elements.StylableDocument.flushTable(StylableDocument.java:378)
at fr.opensagres.poi.xwpf.converter.pdf.internal.elements.StylableDocument.close(StylableDocument.java:179)
at fr.opensagres.poi.xwpf.converter.pdf.internal.PdfMapper.endVisitDocument(PdfMapper.java:177)
at fr.opensagres.poi.xwpf.converter.core.XWPFDocumentVisitor.start(XWPFDocumentVisitor.java:217)
at fr.opensagres.poi.xwpf.converter.pdf.PdfConverter.doConvert(PdfConverter.java:57)

知道我为什么会遇到这样的异常吗?

根据异常:java.lang.RuntimeException:表的宽度必须大于零。谢谢Razva,但正如你在代码中看到的,我没有管理任何表……嗨,Tiamat,我不熟悉这个库,但我在我推荐的java项目中使用org.apache.pdfbox。回到您的错误,您可能需要创建和管理该表。如果您使用
Word
打开
Rapport\u 00.docx
,会发生什么情况?它看起来真的合适吗?我对此表示怀疑。它只有595个twips==0.41英寸宽,842个twips==0.58英寸高。有关从头开始创建
Word
PDF
的完整示例,请参阅。尝试
pageSize.setW(biginger.valueOf(12240));setH(biginger.valueOf(15840))在您的代码中,它将工作。是的,文档可以正确打开,但您是对的,大小不正确。我试着按照Axel的建议更改大小,结果成功了……我认为这是A4格式的正确值……谢谢alex!