Aspose.Words for .NET是一种高级Word文档处理API,用于执行各种文档管理和操作任务。API支持生成,修改,转换,呈现和打印文档,而无需在跨平台应用程序中直接使用Microsoft Word。此外,API支持所有流行的Word处理文件格式,并允许将Word文档导出或转换为固定布局文件格式和最常用的图像/多媒体格式。
>>Aspose.Words for .NET已经更新至v20.7,添加了新节点以处理多节结构化文档标签,改进了SmartArt冷渲染的性能,RevisionOptions类扩展了新的属性,体验
- 如何插入全新的目录
- 更新文档中的新目录或现有目录。
- 指定开关以控制目录的格式和整体结构。
- 如何修改目录的样式和外观。
- 如何删除整个目录字段以及所有条目形成文档。
以编程方式插入目录
可以通过调用DocumentBuilder.InsertTableOfContents 方法将TOC(目录)字段插入当前位置的文档中。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();
对Document.UpdateFields的第一次调用将构建TOC,将填充所有文本条目,并且TOC几乎完成。唯一缺少的是现在用“显示的页码。 第二次调用Document.UpdatePageLayout将在内存中构建文档的布局。需要这样做以收集条目的页码。然后将从此调用中计算出的正确页码插入到目录中。
修改目录
TOC中条目的格式不使用标记条目的原始样式,而是使用等效的TOC样式来格式化每个级别。例如,TOC中的第一级使用TOC1样式设置格式,第二级使用TOC2样式设置,等等。这意味着要更改目录的外观,必须修改这些样式。在Aspose.Words中,这些样式由独立于语言环境的StyleIdentifier.TOC1到StyleIdentifier.TOC9表示,并且可以使用这些标识符从Document.Styles集合中检索。
一旦检索到适当的文档样式,就可以修改该样式的格式。对这些样式的任何更改将自动反映在文档的目录中。下面的示例更改在第一级TOC样式中使用的格式设置属性。
Document doc = new Document(); // Retrieve the style used for the first level of the TOC and change the formatting of the style.doc.Styles[StyleIdentifier.Toc1].Font.Bold = true;
还需要注意的是,标记为包含在TOC中的段落的任何直接格式(在段落本身上定义,而不是在样式中定义)都将被复制到TOC中的条目中。例如,如果标题1样式用于标记目录的内容,并且此样式具有粗体格式,而段落也具有直接应用于其的斜体格式。生成的TOC条目将不是粗体的,因为这是样式格式的一部分,但是由于它是直接在段落中设置的格式,因此将是斜体的。
使用为要修改的特定TOC级别检索到的Style类,还可以修改它们在文档中的显示方式。若要更改其显示方式,首先必须调用Style.ParagraphFormat来检索样式的段落格式。通过调用ParagraphFormat.TabStops可以从中检索制表位,并修改相应的制表位。使用相同的技术,选项卡本身可以一起移动或删除。下例显示了如何在TOC相关段落中修改右制表位的位置。
// The path to the documents directory.string dataDir = RunExamples.GetDataDir_WorkingWithStyles();string fileName = ""Document.TableOfContents.doc"";// Open the document.Document doc = new Document(dataDir + fileName);// Iterate through all paragraphs in the documentforeach (Paragraph para in doc.GetChildNodes(NodeType.Paragraph声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!