/*** 先临时生成一个合并完成后的docx格式文档,doc会出现乱码。
*@parampathList 所有需要合并的文档的绝对路径
*@paramsavePath 一个路径,但是没有文件的后缀,之后进行拼接。
*@return状态,是否保存成功*/
public static boolean mergeWordToPdf(ListpathList, String savePath){//判断是否为pdf文件后缀的路径//String[] split = savePath.split("\\.");//if (!"pdf".equals(split[split.length-1])) {//System.out.println("请给一个以pdf保存路径结尾的路径");//return false;//}//保存合并完成后临时存放的文件
String file = savePath + ".docx";
File newfile= newFile(file);try{//判断是否存在,存在则删除
if(newfile.exists()) {
newfile.delete();
}
newfile.createNewFile();//创建一个新的doc文件
Document doc = newDocument(file);int count = 0;//进行合并
for(String filePath : pathList) {//获取文档的路径,然后合并
count++;
Document doc2= newDocument();
doc2.loadFromFile(filePath);for (int j = 0; j < doc2.getSections().getCount(); j++) {
doc.getSections().add(doc2.getSections().get(j).deepClone());
}
}//在开头创建一个目录页
ParagraphStyle title1style = newParagraphStyle(doc);
title1style.setName("TL1");
title1style.getParagraphFormat().setOutlineLevel(OutlineLevel.Level_1);
doc.getStyles().add(title1style);
Section sec= doc.getSections().get(0);//设置边距
sec.getPageSetup().getMargins().setTop(71.882f);
sec.getPageSetup().getMargins().setBottom(71.882f);
sec.getPageSetup().getMargins().setLeft(90f);
sec.getPageSetup().getMargins().setRight(90f);
sec.getParagraphs().get(0).applyStyle(title1style.getName());//循环遍历每一页的标题,并添加到目录页中
for (int i = 1; i <= count; i++) {
sec=doc.getSections().get(i);
sec.getParagraphs().get(0).applyStyle(title1style.getName());
}
sec= doc.getSections().get(0);
Paragraph para= newParagraph(doc);
sec.getParagraphs().insert(0, para);
TableOfContent toc= para.appendTOC(1, 3);
toc.setUseHeadingStyles(false);
toc.setUseHyperlinks(true);
toc.setUseTableEntryFields(false);
toc.setRightAlignPageNumbers(true);
toc.setTOCLevelStyle(1, title1style.getName());
doc.isUpdateFields();
doc.updateTableOfContents();//设置目录的字体
TextRange range = para.appendText("目录");
range.getCharacterFormat().setFontName("宋体");
range.getCharacterFormat().setFontSize(16);
para.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
sec.getParagraphs().insert(0, para);for (int i = 0; i < sec.getParagraphs().getCount(); i++) {
Paragraph p=sec.getParagraphs().get(i);if (p.getStyleName().equals("TOC1")) {for (int j = 0; j < p.getChildObjects().getCount(); j++) {if(p.getChildObjects().get(j).getDocumentObjectType().equals(DocumentObjectType.Text_Range)) {
TextRange range0=(TextRange) p.getChildObjects().get(j);
range0.getCharacterFormat().setFontName("宋体");
range0.getCharacterFormat().setBold(false);
}
}
}
}//删除页眉
for (int i = 1; i <= count; i++) {
ParagraphCollection paragraphsHeader=doc.getSections().get(i).getHeadersFooters().getHeader().getParagraphs();if (paragraphsHeader.getCount() > 0) {
paragraphsHeader.removeAt(0);
}
doc.getSections().get(i).getHeadersFooters().getFirstPageFooter().getChildObjects().clear();
doc.getSections().get(i).getHeadersFooters().getOddFooter().getChildObjects().clear();
}//添加文字、页码域和总页数域到段落
Paragraph paragraph = doc.getSections().get(0).getHeadersFooters().getFirstPageFooter().addParagraph();
paragraph.appendField("page number", FieldType.Field_Page);
paragraph.getFormat().setHorizontalAlignment(HorizontalAlignment.Right);
Paragraph paragraph1= doc.getSections().get(0).getHeadersFooters().getOddFooter().addParagraph();
paragraph1.appendField("page number", FieldType.Field_Page);
paragraph1.getFormat().setHorizontalAlignment(HorizontalAlignment.Right);//在转换为pdf时出现字体便乱的情况,格式化字体后解决。如果不需要转换为pdf,此操作可以删除。
for (int a = 1; a <= count; a++) {
Section s=doc.getSections().get(a);//更新全文的字体(不包括tbale里的)
for (int i = 1; i < s.getParagraphs().getCount(); i++) {
Paragraph p=s.getParagraphs().get(i);for (int j = 0; j < p.getChildObjects().getCount(); j++) {if(p.getChildObjects().get(j).getDocumentObjectType().equals(DocumentObjectType.Text_Range)) {
TextRange range0=(TextRange) p.getChildObjects().get(j);
range0.getCharacterFormat().setFontName("宋体");
range0.getCharacterFormat().setBold(false);
}
}
}
TableCollection tables=s.getTables();//更新table里字体
if (tables.getCount() > 0) {
updateTable(tables);
}
}//保存word文件
doc.saveToFile(file, FileFormat.Docx);//转换为pdf,转换的代码在下一篇文章里,使用的不是同一个jar包,因为这个jar对生成pdf没有限制,准确的说是破*了。
//WordToPdfUtil.wordToPdf(file, savePath + ".pdf");return true;
}catch(Exception e){
e.printStackTrace();
}return false;
}