Aspose.Words For .Net是一种高级Word文档处理API,用于执行各种文档管理和操作任务。API支持生成,修改,转换,呈现和打印文档,而无需在跨平台应用程序中直接使用Microsoft Word。此外,API支持所有流行的Word处理文件格式,并允许将Word文档导出或转换为固定布局文件格式和最常用的图像/多媒体格式。
接下来我们将进入关于“样式处理”的介绍,在Aspose.Words中学会如何插入和使用目录字段。
>>Aspose.Words for .NET迎来2020第一次更新v20.1,支持插入LINQ Reporting Engine标签,体验
如何插入和使用目录字段
- 如何插入全新的目录
- 更新文档中的新目录或现有目录。
- 指定开关以控制目录的格式和整体结构。
- 如何修改目录的样式和外观。
- 如何从文档中删除整个TOC字段以及所有条目。
以编程方式插入目录
调用DocumentBuilder.InsertTableOfContents方法将TOC字段插入DocumentBuilder当前位置的文档中。 Word文档中的目录可以通过多种方式构建,并使用各种选项进行格式化。您将切换到该方法的字段切换,以控制表的构建方式和在文档中显示的方式。
// The path to the documents directory.string dataDir = RunExamples.GetDataDir_WorkingWithDocument();// Initialize document.Document doc = new Document();DocumentBuilder builder = new DocumentBuilder(doc);// Insert a table of contents at the beginning of the document.builder.InsertTableOfContents("\o "1-3"" \h \z \u"");// The newly inserted table of contents will be initially empty.// It needs to be populated by updating the fields in the document.doc.UpdateFields();dataDir = dataDir + ""DocumentBuilderInsertTOC_out.doc"";doc.Save(dataDir);
下面的示例演示如何使用标题样式作为条目将目录(TOC)插入文档。
Document doc = new Document();DocumentBuilder builder = new DocumentBuilder(doc);// Insert a table of contents at the beginning of the document.builder.InsertTableOfContents(""\o ""1-3"" \h \z \u"");// Start the actual document content on the second page.builder.InsertBreak(BreakType.PageBreak);// Build a document with complex structure by applying different heading styles thus creating TOC entries.builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading1;builder.Writeln(""Heading 1"");builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading2;builder.Writeln(""Heading 1.1"");builder.Writeln(""Heading 1.2"");builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading1;builder.Writeln(""Heading 2"");builder.Writeln(""Heading 3"");builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading2;builder.Writeln(""Heading 3.1"");builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading3;builder.Writeln(""Heading 3.1.1"");builder.Writeln(""Heading 3.1.2"");builder.Writeln(""Heading 3.1.3"");builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading2;builder.Writeln(""Heading 3.2"");builder.Writeln(""Heading 3.3"");doc.UpdateFields();dataDir = dataDir + ""DocumentBuilderInsertTableOfContents_out.doc"";doc.Save(dataDir);
该代码演示了新目录插入到空白文档中。然后,使用DocumentBuilder类插入具有适当标题样式的一些样本内容格式,这些样式用于标记要包含在TOC中的内容。接下来的几行通过更新文档的字段和页面布局来填充目录。

更新目录
Aspose.Words允许您仅用几行代码即可完全更新TOC。在对文档进行更改后,可以执行此操作以填充新插入的目录或更新现有目录。必须使用以下两种方法来更新文档中的TOC字段:
- Document.UpdateFields
- Document.UpdatePageLayout
请注意,这两个更新方法必须按该顺序调用。如果反转,将填充目录,但不会显示页码。可以更新任意数量的不同目录。这些方法将自动更新文档中找到的所有目录。下例显示了如何通过调用字段更新来完全重建文档中的TOC字段。
doc。UpdateFields();
插入TC字段
可以通过调用DocumentBuilder.InsertField方法并将字段名称指定为“ TC”以及所需的任何开关,在DocumentBuilder的当前位置插入新的TC字段。下例显示了如何使用DocumentBuilder将TC字段插入文档中。
// The path to the documents directory.string dataDir = RunExamples.GetDataDir_WorkingWithDocument();// Initialize document.Document doc = new Document();// Create a document builder to insert content with.DocumentBuilder builder = new DocumentBuilder(doc);// Insert a TC field at the current document builder position.builder.InsertField(""TC ""Entry Text"" \f t"");dataDir = dataDir + ""DocumentBuilderInsertTCField_out.doc"";doc.Save(dataDir);
Document doc = new Document();FindReplaceOptions options = new FindReplaceOptions();// Highlight newly inserted content.options.ApplyFont.HighlightColor = Color.DarkOrange;options.ReplacingCallback = new InsertTCFieldHandler(""Chapter 1""声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!