3

I'm trying to use Apache POI to generate a Word Document (this is ok) and then I would like to convert this docx to PDF.

I'm using PdfConverter for this but I have a very strange error while generating it....

Here is the code:

    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");

}

and here is the Exception raised:

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)

My Word document is generated correctly but I'm facong multiple issues while generating the PDF: - I had to create styles - to define the pageSize

and now I have an error linked to a Table (even if I do not create a Table) linked to the Styles:

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)

any Idea why I'm getting such Exception ?

tiamat
  • 879
  • 2
  • 12
  • 35
  • According to th exception : java.lang.RuntimeException: The table width must be greater than zero. – ibercode Jun 25 '19 at 13:02
  • Thanks Razva, but as you can see in the code I'm not managing any table... – tiamat Jun 25 '19 at 13:29
  • Hi Tiamat, I´m not familiar with this library, but I work with org.apache.pdfbox in my java projects which I recommend. Going back to your error, maybe you need to create and manage the table. – ibercode Jun 25 '19 at 14:13
  • 2
    What happens if you open the `Rapport_00.docx` using `Word`? Does it really look properly? I doubt that. It is only 595 twips == 0.41 inches width and 842 twips == 0.58 inches height. See https://stackoverflow.com/questions/51330192/trying-to-make-simple-pdf-document-with-apache-poi/51337157#51337157 for an complete example to create `Word` to `PDF` from scratch. Try `pageSize.setW(BigInteger.valueOf(12240)); pageSize.setH(BigInteger.valueOf(15840));` in you code and it will work. – Axel Richter Jun 25 '19 at 14:51
  • yes the document can be opened correctly but you're right the size is not correct. I tried to change the size as suggested by Axel and it works....I thought it was the correct values for a A4 format....Thanks alex ! – tiamat Jun 25 '19 at 15:29
  • A4 would be 11907 twips width and 16840 twips height. – Axel Richter Jun 25 '19 at 15:44

0 Answers0