Java现有Word文档添加目录

本文已参与「新人创作礼」活动,一起开启掘金创作之路。

Java现有Word文档添加目录

本篇文章将介绍如何使用Spire.Doc for Java添加Word目录。一般在Word中添加目录时,需要先设置文字大纲级别,因此以下示例将分两种情况来添加目录,即:

  • 源文档没有设置大纲级别:添加目录前需要先手动设置大纲级别
  • 源文档已设置大纲级别:通过域代码添加目录

使用工具:Free Spire.Doc for Java (免费版) 工具获取途径:www.e-iceblue.cn/Downloads/F…

1.手动设置大纲级别并添加目录

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("测试文件.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);
        
        //手动设置文档中指定段落的大纲级别
        doc.getSections().get(0).getParagraphs().get(1).applyStyle(BuiltinStyle.Heading_1);
        doc.getSections().get(0).getParagraphs().get(2).applyStyle(BuiltinStyle.Heading_2);
        doc.getSections().get(0).getParagraphs().get(4).applyStyle(BuiltinStyle.Heading_2);
        doc.getSections().get(0).getParagraphs().get(6).applyStyle(BuiltinStyle.Heading_2);
        doc.getSections().get(0).getParagraphs().get(12).applyStyle(BuiltinStyle.Heading_2);
        doc.getSections().get(0).getParagraphs().get(13).applyStyle(BuiltinStyle.Heading_3);
        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(17).applyStyle(BuiltinStyle.Heading_1);
        doc.getSections().get(0).getParagraphs().get(18).applyStyle(BuiltinStyle.Heading_2);

        //添加目录
        doc.getSections().get(0).getParagraphs().get(0).appendTOC(1,3);

        //更新目录表
        doc.updateTableOfContents();

        //保存文档
        doc.saveToFile("目录.docx",FileFormat.Docx_2010);
    }
}
复制代码

效果:

在这里插入图片描述

2.通过域代码添加目录

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){
        //加载已设置大纲级别的测试文档
        Document doc = new Document("测试文件2.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("目录2.docx", FileFormat.Docx_2010);
    }
}
复制代码

效果: 在这里插入图片描述 PS:关于通过域代码生成目录,可参考微软官网,获取更多目录设置方法。

猜你喜欢

转载自juejin.im/post/7110405626135576583
今日推荐