Java 添加Word目錄的2種方法示例代碼詳解
目錄是一種能夠快速、有效地幫助讀者了解文檔或書籍主要內(nèi)容的方式。在Word中,插入目錄首先需要設(shè)置相應(yīng)段落的大綱級別,根據(jù)大綱級別來生成目錄表。本文中生成目錄分2種情況來進(jìn)行:
1.文檔沒有設(shè)置大綱級別,生成目錄前需要手動設(shè)置
2.文檔已設(shè)置大綱級別,通過域代碼生成目錄
使用工具:
•Free Spire.Doc for Java 2.0.0 (免費(fèi)版)
•IntelliJ IDEA
工具獲取途徑1:通過官網(wǎng)下載jar文件包,解壓并導(dǎo)入jar文件到IDEA程序。
工具獲取途徑2:通過Maven倉庫導(dǎo)入到Maven項(xiàng)目中,參考導(dǎo)入方法。
Java示例代碼(供參考)
【示例1】手動設(shè)置大綱級別并生成目錄
import com.spire.doc.*;
import com.spire.doc.documents.BuiltinStyle;
import com.spire.doc.documents.HorizontalAlignment;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.fields.TextRange;
import java.awt.*;
public class AddToc {
public static void main(String[]args){
//加載測試文檔
Document doc = new Document("test.docx");
//在文檔最前面插入一個段落,寫入文本并格式化
Paragraph parainserted = new Paragraph(doc);
TextRange tr= parainserted.appendText("目 錄");
tr.getCharacterFormat().setBold(true);
tr.getCharacterFormat().setTextColor(Color.gray);
doc.getSections().get(0).getParagraphs().insert(0,parainserted);
parainserted.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
//設(shè)置文檔中指定段落的大綱級別
doc.getSections().get(0).getParagraphs().get(2).applyStyle(BuiltinStyle.Heading_1);
doc.getSections().get(0).getParagraphs().get(3).applyStyle(BuiltinStyle.Heading_2);
doc.getSections().get(0).getParagraphs().get(5).applyStyle(BuiltinStyle.Heading_2);
doc.getSections().get(0).getParagraphs().get(7).applyStyle(BuiltinStyle.Heading_2);
doc.getSections().get(0).getParagraphs().get(13).applyStyle(BuiltinStyle.Heading_2);
doc.getSections().get(0).getParagraphs().get(14).applyStyle(BuiltinStyle.Heading_3);
doc.getSections().get(0).getParagraphs().get(15).applyStyle(BuiltinStyle.Heading_3);
//添加目錄
doc.getSections().get(0).getParagraphs().get(0).appendTOC(1,3);
//更新目錄表
doc.updateTableOfContents();
//保存文檔
doc.saveToFile("AddToc.docx",FileFormat.Docx_2010);
}
}
目錄生成效果:

【示例2】已設(shè)置大綱級別,通過域代碼直接生成目錄
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.documents.HorizontalAlignment;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.fields.TableOfContent;
import com.spire.doc.fields.TextRange;
import java.awt.*;
public class AddTOC2 {
public static void main (String[] args){
//加載已設(shè)置大綱級別的測試文檔
Document doc = new Document("sample.docx");
//在文檔最前面插入一個段落,寫入文本并格式化
Paragraph parainserted = new Paragraph(doc);
TextRange tr= parainserted.appendText("目 錄");
tr.getCharacterFormat().setBold(true);
tr.getCharacterFormat().setTextColor(Color.gray);
doc.getSections().get(0).getParagraphs().insert(0,parainserted);
parainserted.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
//通過域代碼添加目錄表
TableOfContent toc = new TableOfContent(doc, "{\\o \"1-3\" \\h \\z \\u}");
doc.getSections().get(0).getParagraphs().get(0).appendTOC(1,3);
doc.updateTableOfContents();
//保存文檔
doc.saveToFile("AddToc2.docx", FileFormat.Docx_2010);
}
}
目錄生成效果:

PS:關(guān)于通過域代碼生成目錄,可參考這篇文章,獲取更多目錄設(shè)置方法
總結(jié)
以上所述是小編給大家介紹的Java 添加Word目錄的2種方法示例代碼詳解,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉(zhuǎn)載,煩請注明出處,謝謝!
相關(guān)文章
Springboot2 session設(shè)置超時時間無效的解決
這篇文章主要介紹了Springboot2 session設(shè)置超時時間無效的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07
MybatisPlus處理四種表與實(shí)體的映射及id自增策略分析
在最近的工作中,碰到一個比較復(fù)雜的返回結(jié)果,發(fā)現(xiàn)簡單映射已經(jīng)解決不了這個問題了,只好去求助百度,學(xué)習(xí)mybatis表與實(shí)體的映射應(yīng)該怎么寫,將學(xué)習(xí)筆記結(jié)合工作碰到的問題寫下本文,供自身查漏補(bǔ)缺,同時已被不時之需2022-10-10
java中volatile不能保證線程安全(實(shí)例講解)
下面小編就為大家?guī)硪黄猨ava中volatile不能保證線程安全(實(shí)例講解)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-09-09
Java通過切面實(shí)現(xiàn)統(tǒng)一處理Token設(shè)置用戶信息
這篇文章主要介紹了Java切面統(tǒng)一處理Token設(shè)置用戶信息,常見的后端開發(fā)中,接口請求中一般前端都是先通過用戶登錄獲取token,每次接口請求都需要在頭信息中攜帶token信息,后端每次都需要手動處理token信息,從token信息中解析獲取用戶信息,需要的朋友可以參考下2023-10-10
使用Springboot自定義轉(zhuǎn)換器實(shí)現(xiàn)參數(shù)去空格功能
這篇文章主要介紹了使用Springboot自定義轉(zhuǎn)換器實(shí)現(xiàn)參數(shù)去空格功能,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08

