The code creates a .docx and then creates a PDF from it. When open, the margins and styles do not match the .docx, but the main problem is that the text has moved and some images are missed. The code that cretes the PDF:
InputStream doc = new FileInputStream(new File(sourcePath + name));
XWPFDocument document = new XWPFDocument(doc);
PdfOptions options = PdfOptions.getDefault();
OutputStream out = new FileOutputStream(new File(destinationPath + name.replace("." + MimeUtil2.getExtension(name), "") + ".pdf"));
PdfConverter.getInstance().convert(document, out, options);
About missed images I have no clue, with margins I have tried to add them adding the following code between instantiating the document and creating PdfOptions:
CTSectPr sectPr = document.getDocument().getBody().addNewSectPr();
CTPageMar pageMar = sectPr.addNewPgMar();
pageMar.setLeft(BigInteger.valueOf(720L));
pageMar.setTop(BigInteger.valueOf(1440L));
pageMar.setRight(BigInteger.valueOf(720L));
pageMar.setBottom(BigInteger.valueOf(1440L));
But do not work.
I found a way to modify the margins: adding the same code above in the creation of the .docx, where it seems to work. But that does not fix the problem of the missed image and moved texts. Also it makes the code uglier and does not feel right because the styles are added correctly after (in the writing of the .docx).
How can i keep the styles from the .docx or adding them to make the PDF the closest to the .docx?
Java 1.8 , Apache POI 3.15 and Pdfbox 2.0.9 .