日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java word文档生成目录_JAVA合并word文档生成目录

發(fā)布時間:2023/12/13 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java word文档生成目录_JAVA合并word文档生成目录 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

/*** 先臨時生成一個合并完成后的docx格式文檔,doc會出現(xiàn)亂碼。

*@parampathList 所有需要合并的文檔的絕對路徑

*@paramsavePath 一個路徑,但是沒有文件的后綴,之后進(jìn)行拼接。

*@return狀態(tài),是否保存成功*/

public static boolean mergeWordToPdf(ListpathList, String savePath){//判斷是否為pdf文件后綴的路徑//String[] split = savePath.split("\\.");//if (!"pdf".equals(split[split.length-1])) {//System.out.println("請給一個以pdf保存路徑結(jié)尾的路徑");//return false;//}//保存合并完成后臨時存放的文件

String file = savePath + ".docx";

File newfile= newFile(file);try{//判斷是否存在,存在則刪除

if(newfile.exists()) {

newfile.delete();

}

newfile.createNewFile();//創(chuàng)建一個新的doc文件

Document doc = newDocument(file);int count = 0;//進(jìn)行合并

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());

}

}//在開頭創(chuàng)建一個目錄頁

ParagraphStyle title1style = newParagraphStyle(doc);

title1style.setName("TL1");

title1style.getParagraphFormat().setOutlineLevel(OutlineLevel.Level_1);

doc.getStyles().add(title1style);

Section sec= doc.getSections().get(0);//設(shè)置邊距

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());//循環(huán)遍歷每一頁的標(biāo)題,并添加到目錄頁中

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();//設(shè)置目錄的字體

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();

}//添加文字、頁碼域和總頁數(shù)域到段落

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);//在轉(zhuǎn)換為pdf時出現(xiàn)字體便亂的情況,格式化字體后解決。如果不需要轉(zhuǎn)換為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);//轉(zhuǎn)換為pdf,轉(zhuǎn)換的代碼在下一篇文章里,使用的不是同一個jar包,因為這個jar對生成pdf沒有限制,準(zhǔn)確的說是破*了。

//WordToPdfUtil.wordToPdf(file, savePath + ".pdf");return true;

}catch(Exception e){

e.printStackTrace();

}return false;

}

總結(jié)

以上是生活随笔為你收集整理的java word文档生成目录_JAVA合并word文档生成目录的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。