在 Word 文档中创建目录可以帮助读者快速了解文档的结构和内容,提高文档的可读性。创建目录还有助于作者组织文档内容,确保文档结构清晰,逻辑性强。在需要对文档进行修改或补充内容时,目录也能够帮助作者快速定位到需要编辑的部分。本文将介绍如何使用 Spire.Doc for Java 在 Java 项目中给新建的 Word 文档创建目录。
安装 Spire.Doc for Java
首先,您需要在 Java 程序中添加 Spire.Doc.jar 文件作为依赖项。您可以从 这个链接 下载 JAR 文件;如果您使用 Maven,则可以通过在 pom.xml 文件中添加以下代码导入 JAR 文件。
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>https://repo.e-iceblue.cn/repository/maven-public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.doc</artifactId>
<version>12.4.6</version>
</dependency>
</dependencies>
Java 利用标题样式创建目录
在 Spire.Doc 中,利用标题样式创建目录是生成目录的默认方法。通过为文档中的章节和子章节应用不同级别的标题样式,来自动生成目录。以下是详细的步骤:
- 创建一个 Document 对象。
- 使用 Document.addSection() 方法添加一个节。
- 使用 Section.addParagraph() 方法添加一个段落。
- 使用 Paragraph.appendTOC(int lowerLevel, int upperLevel) 方法创建一个目录对象。
- 创建一个 CharacterFormat 字符格式对象并设置字体。
- 使用 Paragraph.applyStyle(BuiltinStyle.Heading_1) 方法给段落应用标题样式。
- 使用 Paragraph.appendTex() 方法添加文本内容。
- 使用 TextRange.applyCharacterFormat() 方法给文本设置字符格式。
- 使用 Document.updateTableOfContents() 方法更新目录。
- 使用 Document.saveToFile() 方法保存到文档。
- Java
import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.*;
import com.spire.doc.formatting.*;
public class CreateTOCByHeadingStyle {
public static void main(String[] args) {
// 创建一个新的文档对象
Document doc = new Document();
// 在文档中添加一个节
Section section = doc.addSection();
// 添加目录段落
Paragraph TOCparagraph = section.addParagraph();
TOCparagraph.appendTOC(1, 3);
// 创建字符格式对象并设置字体
CharacterFormat characterFormat1 = new CharacterFormat(doc);
characterFormat1.setFontName("微软雅黑");
// 创建另一个字符格式对象并设置字体、字体大小
CharacterFormat characterFormat2 = new CharacterFormat(doc);
characterFormat2.setFontName("微软雅黑");
characterFormat2.setFontSize(12);
// 添加标题1样式的段落
Paragraph paragraph = section.getBody().addParagraph();
paragraph.applyStyle(BuiltinStyle.Heading_1);
// 添加文本并应用字符格式
TextRange textRange1 = paragraph.appendText("概述");
textRange1.applyCharacterFormat(characterFormat1);
// 添加普通内容
paragraph = section.getBody().addParagraph();
TextRange textRange2 = paragraph.appendText("Spire.Doc for Java 是一款专业的 Java Word 组件,开发人员使用它可以轻松地将 Word 文档创建、读取、编辑、转换和打印等功能集成到自己的 Java 应用程序中。作为一款完全独立的组件,Spire.Doc for Java 的运行环境无需安装 Microsoft Office。同时兼容大部分国产操作系统,能够在中标麒麟和中科方德等国产操作系统中正常运行。Spire.Doc for Java 支持 WPS 生成的 Word 格式文档(.wps, .wpt)。");
textRange2.applyCharacterFormat(characterFormat2);
// 添加标题1样式的段落
paragraph = section.getBody().addParagraph();
paragraph.applyStyle(BuiltinStyle.Heading_1);
textRange1 = paragraph.appendText("主要功能");
textRange1.applyCharacterFormat(characterFormat1);
// 添加标题2样式的段落
paragraph = section.getBody().addParagraph();
paragraph.applyStyle(BuiltinStyle.Heading_2);
textRange1 = paragraph.appendText("仅需 Spire.Doc,无需 Microsoft Office Automation");
textRange1.applyCharacterFormat(characterFormat1);
// 添加普通内容
paragraph = section.getBody().addParagraph();
textRange2 = paragraph.appendText("Spire.Doc for Java 是一款完全独立的 Java Word 组件,它的运行环境无需安装 Microsoft Office。");
textRange2.applyCharacterFormat(characterFormat2);
// 添加标题3样式的段落
paragraph = section.getBody().addParagraph();
paragraph.applyStyle(BuiltinStyle.Heading_3);
textRange1 = paragraph.appendText("Word 版本");
textRange1.applyCharacterFormat(characterFormat1);
paragraph = section.getBody().addParagraph();
textRange2 = paragraph.appendText("Word97-03 Word2007 Word2010 Word2013 Word2016 Word2019");
textRange2.applyCharacterFormat(characterFormat2);
// 添加标题2样式的段落
paragraph = section.getBody().addParagraph();
paragraph.applyStyle(BuiltinStyle.Heading_2);
textRange1 = paragraph.appendText("高质量的文档转换");
textRange1.applyCharacterFormat(characterFormat1);
// 添加普通内容
paragraph = section.getBody().addParagraph();
textRange2 = paragraph.appendText("Spire.Doc for Java 支持将常见文件格式例如 Html、Rtf、Odt、Txt、WordML、WordXML 等高质量的转换为 Word 文档,也支持将 Word 文档高质量的导出为其他文件格式,例如 PDF、XPS、Image、OFD、Epub、Html、Txt、Odt、Rtf、WordML、WordXML 等。此外,它还支持将 Doc 和 Docx 相互转换。");
textRange2.applyCharacterFormat(characterFormat2);
// 添加标题2样式的段落
paragraph = section.getBody().addParagraph();
paragraph.applyStyle(BuiltinStyle.Heading_2);
textRange1 = paragraph.appendText("多样化的 Word 文档功能");
textRange1.applyCharacterFormat(characterFormat1);
// 添加普通内容
paragraph = section.getBody().addParagraph();
textRange2 = paragraph.appendText("Spire.Doc for Java 支持处理几乎所有的 Word 文档元素,包括页面、节、页眉和页脚、脚注、尾注、段落、项目符号和编号、表格、文本、域、超链接、书签、注释、图片、样式、背景设置、打印功能、数字签名、文档设置和文档保护等。同时,也支持形状、文本框、图片、OLE 对象和内容控件等。");
textRange2.applyCharacterFormat(characterFormat2);
// 更新目录
doc.updateTableOfContents();
// 保存文档
doc.saveToFile("利用标题样式创建目录.docx", FileFormat.Docx_2016);
// 释放资源
doc.dispose();
}
}
Java 利用大纲层级样式创建目录
您也可以利用大纲层级样式来创建 Word 文档目录。在 Spire.Doc 中,通过设置段落的 OutlineLevel 属性,可以指定段落在大纲中的层级样式。接着,通过调用 TableOfContent.setTOCLevelStyle() 方法,将这些大纲层级样式应用到目录的生成规则中。以下是详细的步骤:
- 创建一个 Document 对象。
- 使用 Document.addSection() 方法添加一个节。
- 创建一个 ParagraphStyle 对象,并使用 ParagraphStyle.getParagraphFormat().setOutlineLevel(OutlineLevel.Level_1) 设置大纲层级。
- 使用 Document.getStyles().add() 方法将创建好的 ParagraphStyle 对象添加到文档中。
- 使用 Section.addParagraph() 方法添加一个段落。
- 使用 Paragraph.appendTOC(int lowerLevel, int upperLevel) 方法创建一个目录对象。
- 将使用标题样式创建目录的默认设置改为 false, TableOfContent.setUseHeadingStyles(false)。
- 使用 TableOfContent.setTOCLevelStyle(int levelNumber, string styleName) 方法将大纲层级样式应用到目录规则中。
- 创建一个 CharacterFormat 字符格式对象并设置字体。
- 使用 Paragraph.applyStyle(ParagraphStyle.getName()) 方法给段落应用样式。
- 使用 Paragraph.appendTex() 方法添加文本内容。
- 使用 TextRange.applyCharacterFormat() 方法给文本设置字符格式。
- 使用 Document.updateTableOfContents() 方法更新目录。
- 使用 Document.saveToFile() 方法保存到文档。
- Java
import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.*;
import com.spire.doc.formatting.*;
public class CreateTOCByOutlineLevelStyle {
public static void main(String[] args) {
// 创建一个新的文档对象
Document doc = new Document();
Section section = doc.addSection();
// 定义大纲级别1
ParagraphStyle titleStyle1 = new ParagraphStyle(doc);
titleStyle1.setName("T1S");
titleStyle1.getParagraphFormat().setOutlineLevel(OutlineLevel.Level_1);
titleStyle1.getCharacterFormat().setBold(true);
titleStyle1.getCharacterFormat().setFontName("微软雅黑");
titleStyle1.getCharacterFormat().setFontSize(18f);
titleStyle1.getParagraphFormat().setHorizontalAlignment(HorizontalAlignment.Left);
doc.getStyles().add(titleStyle1);
// 定义大纲级别2
ParagraphStyle titleStyle2 = new ParagraphStyle(doc);
titleStyle2.setName("T2S");
titleStyle2.getParagraphFormat().setOutlineLevel(OutlineLevel.Level_2);
titleStyle2.getCharacterFormat().setBold(true);
titleStyle2.getCharacterFormat().setFontName("微软雅黑");
titleStyle2.getCharacterFormat().setFontSize(16f);
titleStyle2.getParagraphFormat().setHorizontalAlignment(HorizontalAlignment.Left);
doc.getStyles().add(titleStyle2);
// 定义大纲级别3
ParagraphStyle titleStyle3 = new ParagraphStyle(doc);
titleStyle3.setName("T3S");
titleStyle3.getParagraphFormat().setOutlineLevel(OutlineLevel.Level_3);
titleStyle3.getCharacterFormat().setBold(true);
titleStyle3.getCharacterFormat().setFontName("微软雅黑");
titleStyle3.getCharacterFormat().setFontSize(14f);
titleStyle3.getParagraphFormat().setHorizontalAlignment(HorizontalAlignment.Left);
doc.getStyles().add(titleStyle3);
// 添加目录段落
Paragraph TOCparagraph = section.addParagraph();
TableOfContent toc = TOCparagraph.appendTOC(1, 3);
toc.setUseHeadingStyles(false);
toc.setUseHyperlinks(true);
toc.setUseTableEntryFields(false);
toc.setRightAlignPageNumbers(true);
toc.setTOCLevelStyle(1, titleStyle1.getName());
toc.setTOCLevelStyle(2, titleStyle2.getName());
toc.setTOCLevelStyle(3, titleStyle3.getName());
// 定义字符格式
CharacterFormat characterFormat = new CharacterFormat(doc);
characterFormat.setFontName("微软雅黑");
characterFormat.setFontSize(12);
// 添加段落并应用大纲级别样式1
Paragraph paragraph = section.getBody().addParagraph();
paragraph.applyStyle(titleStyle1.getName());
paragraph.appendText("概述");
// 添加段落并设置文本内容
paragraph = section.getBody().addParagraph();
TextRange textRange = paragraph.appendText("Spire.Doc for Java 是一款专业的 Java Word 组件,开发人员使用它可以轻松地将 Word 文档创建、读取、编辑、转换和打印等功能集成到自己的 Java 应用程序中。作为一款完全独立的组件,Spire.Doc for Java 的运行环境无需安装 Microsoft Office。同时兼容大部分国产操作系统,能够在中标麒麟和中科方德等国产操作系统中正常运行。Spire.Doc for Java 支持 WPS 生成的 Word 格式文档(.wps, .wpt)。");
textRange.applyCharacterFormat(characterFormat);
// 添加段落并应用大纲级别样式1
paragraph = section.getBody().addParagraph();
paragraph.applyStyle(titleStyle1.getName());
paragraph.appendText("主要功能");
// 添加段落并应用大纲级别样式2
paragraph = section.getBody().addParagraph();
paragraph.applyStyle(titleStyle2.getName());
paragraph.appendText("仅需 Spire.Doc,无需 Microsoft Office Automation");
// 添加段落并设置文本内容
paragraph = section.getBody().addParagraph();
textRange = paragraph.appendText("Spire.Doc for Java 是一款完全独立的 Java Word 组件,它的运行环境无需安装 Microsoft Office。");
textRange.applyCharacterFormat(characterFormat);
// 添加段落并应用大纲级别样式3
paragraph = section.getBody().addParagraph();
paragraph.applyStyle(titleStyle3.getName());
paragraph.appendText("Word 版本");
// 添加段落并设置文本内容
paragraph = section.getBody().addParagraph();
textRange = paragraph.appendText("Word97-03 Word2007 Word2010 Word2013 Word2016 Word2019");
textRange.applyCharacterFormat(characterFormat);
// 添加段落并应用大纲级别样式2
paragraph = section.getBody().addParagraph();
paragraph.applyStyle(titleStyle2.getName());
paragraph.appendText("高质量的文档转换");
// 添加段落并设置文本内容
paragraph = section.getBody().addParagraph();
textRange = paragraph.appendText("Spire.Doc for Java 支持将常见文件格式例如 Html、Rtf、Odt、Txt、WordML、WordXML 等高质量的转换为 Word 文档,也支持将 Word 文档高质量的导出为其他文件格式,例如 PDF、XPS、Image、OFD、Epub、Html、Txt、Odt、Rtf、WordML、WordXML 等。此外,它还支持将 Doc 和 Docx 相互转换。");
textRange.applyCharacterFormat(characterFormat);
// 添加段落并应用大纲级别样式2
paragraph = section.getBody().addParagraph();
paragraph.applyStyle(titleStyle2.getName());
paragraph.appendText("多样化的 Word 文档功能");
// 添加段落并设置文本内容
paragraph = section.getBody().addParagraph();
textRange = paragraph.appendText("Spire.Doc for Java 支持处理几乎所有的 Word 文档元素,包括页面、节、页眉和页脚、脚注、尾注、段落、项目符号和编号、表格、文本、域、超链接、书签、注释、图片、样式、背景设置、打印功能、数字签名、文档设置和文档保护等。同时,也支持形状、文本框、图片、OLE 对象和内容控件等。");
textRange.applyCharacterFormat(characterFormat);
// 更新目录内容
doc.updateTableOfContents();
// 保存文档
doc.saveToFile("利用大纲级别样式创建目录.docx", FileFormat.Docx_2016);
// 释放资源
doc.dispose();
}
}
Java 利用图片标题创建目录
使用 Spire.Doc 库,您可以利用 TableOfContent tocForImage = new TableOfContent(Document, " \\h \\z \\c \"图片\"") 方法通过图片标题来创建目录。以下是详细的步骤:
- 创建一个 Document 对象。
- 使用 Document.addSection() 方法添加一个节。
- 创建一个目录对象 TableOfContent tocForImage = new TableOfContent(Document, " \\h \\z \\c \"图片\"") 并指定目录的样式。
- 使用 Section.addParagraph() 方法添加一个段落。
- 使用 Paragraph.getItems().add(tocForImage) 方法将目录对象添加到段落中 。
- 使用 Paragraph.appendFieldMark(FieldMarkType.Field_Separator) 方法添加字段分隔符。
- 使用 Paragraph.appendText("TOC") 方法添加文本内容 "TOC"。
- 使用 Paragraph.appendFieldMark(FieldMarkType.Field_End) 方法添加字段结束标记。
- 使用 Paragraph.appendPicture() 方法添加一个图片。
- 使用 DocPicture.addCaption() 方法添加图片标题段落,包括产品信息和格式设置.
- 利用 Document.updateTableOfContents(tocForImage) 方法更新目录以反映文档中的更改。
- 使用 Document.saveToFile() 方法保存到文档。
- Java
import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.*;
public class CreateTOCByImageCaption {
public static void main(String[] args) {
// 创建一个新文档对象
Document doc = new Document();
// 在文档中添加一个节
Section section = doc.addSection();
// 创建一个图片目录对象
TableOfContent tocForImage = new TableOfContent(doc, " \\h \\z \\c \"图片\"");
// 在节中添加一个段落
Paragraph tocParagraph = section.getBody().addParagraph();
// 将图片目录对象添加到段落中
tocParagraph.getItems().add(tocForImage);
// 添加字段分隔符
tocParagraph.appendFieldMark(FieldMarkType.Field_Separator);
// 添加文本内容
tocParagraph.appendText("TOC");
// 添加字段结束标记
tocParagraph.appendFieldMark(FieldMarkType.Field_End);
// 在节中添加一个空白段落
section.getBody().addParagraph();
// 在节中添加一个段落
Paragraph paragraph = section.getBody().addParagraph();
// 添加一个图片
DocPicture docPicture = paragraph.appendPicture("images/Doc-Java.png");
docPicture.setWidth(100);
docPicture.setHeight(100);
// 添加图片标题段落
Paragraph pictureCaptionParagraph = (Paragraph)docPicture.addCaption("图片", CaptionNumberingFormat.Number, CaptionPosition.Below_Item);
pictureCaptionParagraph.appendText(" Spire.Doc for Java 产品 ");
pictureCaptionParagraph.getFormat().setAfterSpacing(20);
// 继续在节中添加段落
paragraph = section.getBody().addParagraph();
docPicture = paragraph.appendPicture("images/PDF-Java.png");
docPicture.setWidth(100);
docPicture.setHeight(100);
pictureCaptionParagraph = (Paragraph)docPicture.addCaption("图片", CaptionNumberingFormat.Number, CaptionPosition.Below_Item);
pictureCaptionParagraph.appendText(" Spire.PDF for Java 产品 ");
pictureCaptionParagraph.getFormat().setAfterSpacing(20);
paragraph = section.getBody().addParagraph();
docPicture = paragraph.appendPicture("images/XLS-Java.png");
docPicture.setWidth(100);
docPicture.setHeight(100);
pictureCaptionParagraph = (Paragraph)docPicture.addCaption("图片", CaptionNumberingFormat.Number, CaptionPosition.Below_Item);
pictureCaptionParagraph.appendText(" Spire.XLS for Java 产品 ");
pictureCaptionParagraph.getFormat().setAfterSpacing(20);
paragraph = section.getBody().addParagraph();
docPicture = paragraph.appendPicture("images/PPT-Java.png");
docPicture.setWidth(100);
docPicture.setHeight(100);
pictureCaptionParagraph = (Paragraph)docPicture.addCaption("图片", CaptionNumberingFormat.Number, CaptionPosition.Below_Item);
pictureCaptionParagraph.appendText(" Spire.Presentation for Java 产品 ");
// 更新目录
doc.updateTableOfContents(tocForImage);
// 将文档保存为文件
doc.saveToFile("利用图片标题创建目录.docx", FileFormat.Docx_2016);
// 释放文档对象
doc.dispose();
}
}
Java 利用表格标题创建目录
同样也可以通过 TableOfContent tocForImage = new TableOfContent(Document, " \\h \\z \\c \"表格\"") 方法利用表格标题来创建目录。以下是详细的步骤:
- 创建一个 Document 对象。
- 使用 Document.addSection() 方法添加一个节。
- 创建一个目录对象 TableOfContent tocForTable = new TableOfContent(Document, " \\h \\z \\c \"表格\"") 并指定目录的样式。
- 使用 Section.addParagraph() 方法添加一个段落。
- 使用 Paragraph.getItems().add(tocForTable) 方法将目录对象添加到段落中 。
- 使用 Paragraph.appendFieldMark(FieldMarkType.Field_Separator) 方法添加字段分隔符。
- 使用 Paragraph.appendText("TOC") 方法添加文本内容 "TOC"。
- 使用 Paragraph.appendFieldMark(FieldMarkType.Field_End) 方法添加字段结束标记。
- 使用 Section.addTable() 方法添加一个表格并通过 Table.resetCells(int rowsNum, int columnsNum) 方法设置行数和列数。
- 使用 Table.addCaption() 方法添加表格片标题段落,包括产品信息和格式设置.
- 利用 Document.updateTableOfContents(tocForTable) 方法更新目录以反映文档中的更改。
- 使用 Document.saveToFile() 方法保存到文档。
- Java
import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.*;
public class CreateTOCByTableCaption {
public static void main(String[] args) {
// 创建一个新文档
Document doc = new Document();
// 在文档中添加一个节
Section section = doc.addSection();
// 创建一个表格目录对象
TableOfContent tocForTable = new TableOfContent(doc, " \\h \\z \\c \"表格\"");
// 在节中添加一个段落用于放置表格目录
Paragraph tocParagraph = section.getBody().addParagraph();
tocParagraph.getItems().add(tocForTable);
tocParagraph.appendFieldMark(FieldMarkType.Field_Separator);
tocParagraph.appendText("TOC");
tocParagraph.appendFieldMark(FieldMarkType.Field_End);
// 在节中添加两个空白段落
section.getBody().addParagraph();
section.getBody().addParagraph();
// 在节中添加一个表格
Table table = section.getBody().addTable(true);
table.resetCells(1, 3);
// 添加一个表格的标题
Paragraph tableCaptionParagraph = (Paragraph)table.addCaption("表格", CaptionNumberingFormat.Number, CaptionPosition.Below_Item);
tableCaptionParagraph.appendText(" 一行三列");
tableCaptionParagraph.getFormat().setAfterSpacing(18);
// 在节中添加一个新的表格
table = section.getBody().addTable(true);
table.resetCells(3, 3);
// 添加第二个表格的标题
tableCaptionParagraph = (Paragraph)table.addCaption("表格", CaptionNumberingFormat.Number, CaptionPosition.Below_Item);
tableCaptionParagraph.appendText(" 三行三列");
tableCaptionParagraph.getFormat().setAfterSpacing(18);
// 在节中添加另一个新的表格
table = section.getBody().addTable(true);
table.resetCells(5, 3);
// 添加第三个表格的标题
tableCaptionParagraph = (Paragraph)table.addCaption("表格", CaptionNumberingFormat.Number, CaptionPosition.Below_Item);
tableCaptionParagraph.appendText(" 五行三列");
// 更新表格目录
doc.updateTableOfContents(tocForTable);
// 将文档保存为指定文件
doc.saveToFile("利用表格标题创建目录.docx", FileFormat.Docx_2016);
// 释放资源
doc.dispose();
}
}
申请临时 License
如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。获取有效期 30 天的临时许可证。