Java 添加Word目錄的2種方法示例代碼詳解
目錄是一種能夠快速、有效地幫助讀者了解文檔或書籍主要內(nèi)容的方式。在Word中,插入目錄首先需要設(shè)置相應(yīng)段落的大綱級(jí)別,根據(jù)大綱級(jí)別來生成目錄表。本文中生成目錄分2種情況來進(jìn)行:
1.文檔沒有設(shè)置大綱級(jí)別,生成目錄前需要手動(dòng)設(shè)置
2.文檔已設(shè)置大綱級(jí)別,通過域代碼生成目錄
使用工具:
•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】手動(dòng)設(shè)置大綱級(jí)別并生成目錄
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){ //加載測(cè)試文檔 Document doc = new Document("test.docx"); //在文檔最前面插入一個(gè)段落,寫入文本并格式化 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è)置文檔中指定段落的大綱級(jí)別 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è)置大綱級(jí)別,通過域代碼直接生成目錄
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è)置大綱級(jí)別的測(cè)試文檔 Document doc = new Document("sample.docx"); //在文檔最前面插入一個(gè)段落,寫入文本并格式化 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種方法示例代碼詳解,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!
相關(guān)文章
Springboot2 session設(shè)置超時(shí)時(shí)間無效的解決
這篇文章主要介紹了Springboot2 session設(shè)置超時(shí)時(shí)間無效的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07Java tomcat手動(dòng)配置servlet詳解
這篇文章主要為大家介紹了tomcat手動(dòng)配置servlet,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2021-11-11MybatisPlus處理四種表與實(shí)體的映射及id自增策略分析
在最近的工作中,碰到一個(gè)比較復(fù)雜的返回結(jié)果,發(fā)現(xiàn)簡(jiǎn)單映射已經(jīng)解決不了這個(gè)問題了,只好去求助百度,學(xué)習(xí)mybatis表與實(shí)體的映射應(yīng)該怎么寫,將學(xué)習(xí)筆記結(jié)合工作碰到的問題寫下本文,供自身查漏補(bǔ)缺,同時(shí)已被不時(shí)之需2022-10-10java中volatile不能保證線程安全(實(shí)例講解)
下面小編就為大家?guī)硪黄猨ava中volatile不能保證線程安全(實(shí)例講解)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-09-09Java通過切面實(shí)現(xiàn)統(tǒng)一處理Token設(shè)置用戶信息
這篇文章主要介紹了Java切面統(tǒng)一處理Token設(shè)置用戶信息,常見的后端開發(fā)中,接口請(qǐng)求中一般前端都是先通過用戶登錄獲取token,每次接口請(qǐng)求都需要在頭信息中攜帶token信息,后端每次都需要手動(dòng)處理token信息,從token信息中解析獲取用戶信息,需要的朋友可以參考下2023-10-10詳解SpringBoot啟動(dòng)代碼和自動(dòng)裝配源碼分析
這篇文章主要介紹了SpringBoot啟動(dòng)代碼和自動(dòng)裝配源碼分析,使用SpringBoot很簡(jiǎn)單,在主類中添加一個(gè)@SpringBootApplication,以及調(diào)用SpringApplication.run()并傳入主類,本文通過示例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-07-07使用Springboot自定義轉(zhuǎn)換器實(shí)現(xiàn)參數(shù)去空格功能
這篇文章主要介紹了使用Springboot自定義轉(zhuǎn)換器實(shí)現(xiàn)參數(shù)去空格功能,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08