I am using the code below in order to convert a MS Word docx file to pdf.
private static ByteArrayOutputStream convertToPDFDocx4j(String inputfilepath) throws Exception {
WordprocessingMLPackage wordMLPackage =
WordprocessingMLPackage.load(new java.io.File(inputfilepath));
FieldUpdater updater = new FieldUpdater(wordMLPackage);
updater.update(true);
Mapper fontMapper = new IdentityPlusMapper();
wordMLPackage.setFontMapper(fontMapper);
FOSettings foSettings = Docx4J.createFOSettings();
foSettings.setWmlPackage(wordMLPackage);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Docx4J.toFO(foSettings, baos, Docx4J.FLAG_EXPORT_PREFER_XSL);
if (wordMLPackage.getMainDocumentPart().getFontTablePart() != null) {
wordMLPackage.getMainDocumentPart().getFontTablePart().deleteEmbeddedFontTempFiles();
}
updater = null;
foSettings = null;
wordMLPackage = null;
return baos;
}
Although the pdf is generated, it loses the text alignment.
I have uploaded the word and generated pdf in the links below: