需求是把doc根据Header,拆分几份小的PDF
发现复制doc内容和页眉成新文档, 再转化PDF就乱了
// <version>3.9.0</version>
Document doc1 = new Document();
doc1.loadFromFile("C:\\Users\\admin\\OneDrive\\桌面\\说明书.doc");
//对比 -----------------1
doc1.saveToFile("C:\\Users\\admin\\OneDrive\\桌面\\五pdf\\" + "完整转化-说明书.pdf", FileFormat.PDF);
Document doc2 = new Document();
doc2.loadFromFile("C:\\Users\\admin\\OneDrive\\桌面\\说明书.doc");
//需求:把doc根据Header,拆分几份小的PDF
SectionCollection sections = doc2.getSections();
for (Object objSelect : sections) {
Section select = (Section) objSelect;
HeaderFooter headerFooter = select.getHeadersFooters().getHeader();
if (headerFooter != null) {
IParagraph firstParagraph = headerFooter.getFirstParagraph();
if (firstParagraph != null) {
String paragraphText = firstParagraph.getText();
System.out.println("text--->" + paragraphText);
Document document = new Document();
document.addSection();
//复制页眉
Object object1 = headerFooter.getChildObjects().get(0);
document.getSections().get(0).getHeadersFooters().getHeader().getChildObjects().add(((DocumentObject) object1).deepClone());
//复制word内容
for (int j = 0; j < select.getBody().getChildObjects().getCount(); j++) {
Object object = select.getBody().getChildObjects().get(j);
document.getSections().get(0).getBody().getChildObjects().add(((DocumentObject) object).deepClone());
}
//对比 -----------------2
document.saveToFile("C:\\Users\\admin\\OneDrive\\桌面\\拆分pdf\\" + paragraphText + ".pdf", FileFormat.PDF);
}
}
附件
完整转化是好的.png
复制内容和页眉成新文档再转化就乱了.png
doc文档