docx4j includes code for creating a PDF from docx using iText. It can also use POI to convert a document to docx.
There was a time when we supported both methods the same way (as well as PDF via XHTML), but we decided to focus on XSL-FO.
If this is an option, you would be much better off using docx4j to convert docx to PDF via XSL-FO and FOP.
Use it like this:
wordMLPackage = WordprocessingMLPackage.load(new java.io.File(inputfilepath)); // Set up font mapper Mapper fontMapper = new IdentityPlusMapper(); wordMLPackage.setFontMapper(fontMapper); // Example of mapping missing font Algerian to installed font Comic Sans MS PhysicalFont font = PhysicalFonts.getPhysicalFonts().get("Comic Sans MS"); fontMapper.getFontMappings().put("Algerian", font); org.docx4j.convert.out.pdf.PdfConversion c = new org.docx4j.convert.out.pdf.viaXSLFO.Conversion(wordMLPackage); // = new org.docx4j.convert.out.pdf.viaIText.Conversion(wordMLPackage); OutputStream os = new java.io.FileOutputStream(inputfilepath + ".pdf"); c.output(os);
Update July 2016
As with docx4j 3.3.0, the commercial rendering of a Plutext PDF file is the default docx4j option for converting docx to PDF. You can try the online demo at converter-eval.plutext.com
If you want to use the existing docx for XSL-FO for PDF (or other targeted support supported by Apache FOP), then simply add the jar docx4j-export-FO to your classpath.
In any case, to convert docx to PDF, you can use the Docx4J facade toPDF method .
The old docx in PDF via iText code can be found at https://github.com/plutext/docx4j-export-FO/.../docx4j-additions/PdfViaIText/
source share