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 ?