目录(TOC)为您的文档提供了快速参考点,并为读者提供了从何处查找内容的简要概述。
在本文中,我们将向您展示如何使用免费的Java库Spire.Doc在Java应用程序中创建和更新Word文档中的目录(TOC)。
下面的示例演示如何创建具有默认外观的目录,其中包括以内置样式(标题1,标题2,标题3)和与制表符前导符右对齐的页码设置的所有文本。
import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.*;
import java.awt.*;
public class TableofContents {
public static void main(String[] args){
//instantiate a Document object
Document doc = new Document();
//add a section
Section section = doc.addSection();
//add a paragraph
Paragraph para = section.addParagraph();
TextRange tr = para.appendText("Table of Contents");
//set font size and text color
tr.getCharacterFormat().setFontSize(11);
tr.getCharacterFormat().setTextColor(Color.blue);
//set the space after the paragraph
para.getFormat().setAfterSpacing(10);
//add a paragraph
para = section.addParagraph();
//add a table of contents with default appearance by specifying lower heading level and upper heading level. The heading level range must be from 1 to 9.
para.appendTOC(1, 3);
//add a new section
section = doc.addSection();
//add a paragraph
para = section.addParagraph();
para.appendText("Heading 1");
//apply Heading 1 style to the paragraph
para.applyStyle(BuiltinStyle.Heading_1);
section.addParagraph();
//add a paragraph
para = section.addParagraph();
para.appendText("Heading 2");
//apply Heading 2 style to the paragraph
para.applyStyle(BuiltinStyle.Heading_2);
section.addParagraph();
//add a paragraph
para = section.addParagraph();
para.appendText("Heading 3");
//apply Heading 3 style to the paragraph
para.applyStyle(BuiltinStyle.Heading_3);
section.addParagraph();
//update Table of Contents
doc.updateTableOfContents();
//save the resultant document
doc.saveToFile("createTableOfContents.docx", FileFormat.Docx);
}
}
我们还可以创建一个自定义目录,并使用TOC开关确定要在目录中显示的条目。
序列号。 | 开关 | 描述 |
---|---|---|
1个 | \ o | 根据段落的样式构建目录,表格的样式包括轮廓级别(最常见的是标题样式)。 |
2 | \ t | 根据使用内置样式以外的样式设置格式的段落构建目录。 |
3 | \ u | 根据段落建立目录,其格式包括在段落设置中直接应用的大纲级别。 |
4 | \C | 列出由SEQ(序列)字段编号的图形,表格,图表或其他项目。 |
5 | \一个 | 列出使用“字幕”命令添加字幕的项目(“参考”>“插入字幕”),但省略字幕标签和数字。 |
6 | \F | 从TC字段构建表。 |
7 | \ l | 从TC字段构建目录,这些目录将条目分配给指定级别之一。 |
8 | \ b | 仅从指定书签标记的文档部分收集条目。 |
9 | \ s | 在页码之前包含一个数字,例如章节号。 |
10 | \ d | 与\ s开关一起使用时,指定分隔序列号和页码的字符。 |
11 | \ p | 指定分隔条目及其页码的字符。 |
12 | \ n | 省略目录中的页码。 |
13 | \ w | 在表条目中保留选项卡条目。 |
14 | \X | 保留表条目中的手动换行符。 |
15 | \ z | 在Web版式视图中隐藏制表符标题和页码。 |
16 | \H | 将TOC条目作为超链接插入。 |
有关TOC开关的更多信息,请在此处检查。
下面的示例演示如何创建一个自定义目录,其中包括以内置样式标题1,标题2和标题3格式化的所有文本,但是省略了标题级别1-3中的页码。
import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.*;
import java.awt.*;
public class TableofContents {
public static void main(String[] args){
//instantiate a Document object
Document doc = new Document();
//add a section
Section section = doc.addSection();
//add a paragraph
Paragraph para = section.addParagraph();
TextRange tr = para.appendText("Table of Contents");
//set font size and text color
tr.getCharacterFormat().setFontSize(11);
tr.getCharacterFormat().setTextColor(Color.blue);
//set the space after the paragraph
para.getFormat().setAfterSpacing(10);
//create a custom table of contents that omits page numbers from heading levels 1-3.
TableOfContent toc = new TableOfContent(doc, "{\\o \"1-3\" \\n 1-3}");
para = section.addParagraph();
para.getItems().add(toc);
para.appendFieldMark(FieldMarkType.Field_Separator);
para.appendText("TOC");
para.appendFieldMark(FieldMarkType.Field_End);
doc.setTOC(toc);
//add a new section
section = doc.addSection();
//add a paragraph
para = section.addParagraph();
para.appendText("Heading 1");
//apply Heading 1 style to the paragraph
para.applyStyle(BuiltinStyle.Heading_1);
section.addParagraph();
//add a paragraph
para = section.addParagraph();
para.appendText("Heading 2");
//apply Heading 2 style to the paragraph
para.applyStyle(BuiltinStyle.Heading_2);
section.addParagraph();
//add a paragraph
para = section.addParagraph();
para.appendText("Heading 3");
//apply Heading 3 style to the paragraph
para.applyStyle(BuiltinStyle.Heading_3);
section.addParagraph();
//update Table of Contents
doc.updateTableOfContents();
//save the resultant document
doc.saveToFile("customTableOfContents.docx", FileFormat.Docx);
}
}