当我从 Microsoft Word 文档转换时,我需要更改PDF文件的边距。
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();
}
}
}
这不起作用。pdf中的边距不会改变
但是当我这样做时:
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();
无需转换为PDF,它就可以工作。
一只斗牛犬
相关分类