I need to change the margin of the PDF file when I convert from Microsoft Word document.
public class TestCon {
public static final String DEST = "./test.pdf";
public static final String SRC = "./test.docx";
public static void main(String[] args) {
try {
InputStream doc = new FileInputStream(new File(SRC));
XWPFDocument document = new XWPFDocument(doc );
CTSectPr addNewSectPr = document.getDocument().getBody().addNewSectPr();
CTPageMar addNewPgMar = addNewSectPr.addNewPgMar();
addNewPgMar.setLeft(BigInteger.valueOf(720L));
addNewPgMar.setTop(BigInteger.valueOf(720L));
addNewPgMar.setRight(BigInteger.valueOf(720L));
addNewPgMar.setBottom(BigInteger.valueOf(720L));
OutputStream out = new FileOutputStream(new File(DEST));
PdfOptions options = PdfOptions.create();
PdfConverter.getInstance().convert(document, out, options);
} catch (Throwable e) {
e.printStackTrace();
}
}
}
This does not work. The margin does not change in pdf
But when I do this:
FileOutputStream out = new FileOutputStream(new File(SRC1));
InputStream doc = new FileInputStream(new File(SRC));
XWPFDocument document = new XWPFDocument(doc );
CTSectPr addNewSectPr = document.getDocument().getBody().addNewSectPr();
CTPageMar addNewPgMar = addNewSectPr.addNewPgMar();
addNewPgMar.setLeft(BigInteger.valueOf(720L));
addNewPgMar.setTop(BigInteger.valueOf(720L));
addNewPgMar.setRight(BigInteger.valueOf(720L));
addNewPgMar.setBottom(BigInteger.valueOf(720L));
document.write(out);
out.close();
Without converting to PDF, it works.