没有找到合适的产品?
联系客服协助选型:023-68661681
提供3000多款全球软件/控件产品
针对软件研发的各个阶段提供专业培训与技术咨询
根据客户需求提供定制化的软件开发服务
全球知名设计软件,显著提升设计质量
打造以经营为中心,实现生产过程透明化管理
帮助企业合理产能分配,提高资源利用率
快速打造数字化生产线,实现全流程追溯
生产过程精准追溯,满足企业合规要求
以六西格玛为理论基础,实现产品质量全数字化管理
通过大屏电子看板,实现车间透明化管理
对设备进行全生命周期管理,提高设备综合利用率
实现设备数据的实时采集与监控
利用数字化技术提升油气勘探的效率和成功率
钻井计划优化、实时监控和风险评估
提供业务洞察与决策支持实现数据驱动决策
翻译|使用教程|编辑:李显亮|2021-03-03 11:35:17.657|阅读 442 次
概述:Word文档中的目录(TOC)概述了文档中包含的内容。此外,它还允许导航到文档的特定部分。在本文中,将学习如何使用C#以编程方式在Word文档中使用目录。
# 界面/图表报表/文档/IDE等千款热门软控件火热销售中 >>
相关链接:
Word文档中的目录(TOC)概述了文档中包含的内容。此外,它还允许导航到文档的特定部分。在本文中,将学习如何使用C#以编程方式在Word文档中使用目录。特别是,本文介绍如何在Word文档中添加,提取,更新或删除目录。
为了处理Word文档中的目录,将使用Aspose.Words for .NET API。旨在执行.NET应用程序中的基本和高级Word自动化功能。此外,它可以熟练掌握Word文档中目录的操作。点击下方按钮下载最新版。
以下是使用Aspose.Words for .NET将目录添加到Word文档的步骤。
下面的代码示例演示如何在C#中的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);
以下是从Word文档中的目录提取字段的步骤。
下面的代码示例演示如何使用C#从Word文档中提取目录。
// The path to the documents directory. string dataDir = RunExamples.GetDataDir_WorkingWithDocument(); string fileName = "TOC.doc"; Aspose.Words.Document doc = new Aspose.Words.Document(dataDir + fileName); foreach (Field field in doc.Range.Fields) { if (field.Type.Equals(Aspose.Words.Fields.FieldType.FieldHyperlink)) { FieldHyperlink hyperlink = (FieldHyperlink)field; if (hyperlink.SubAddress != null && hyperlink.SubAddress.StartsWith("_Toc")) { Paragraph tocItem = (Paragraph)field.Start.GetAncestor(NodeType.Paragraph); Console.WriteLine(tocItem.ToString(SaveFormat.Text).Trim()); Console.WriteLine("------------------"); if (tocItem != null) { Bookmark bm = doc.Range.Bookmarks[hyperlink.SubAddress]; // Get the location this TOC Item is pointing to Paragraph pointer = (Paragraph)bm.BookmarkStart.GetAncestor(NodeType.Paragraph); Console.WriteLine(pointer.ToString(SaveFormat.Text)); } } // End If }// End If }// End Foreach
每当将某些内容附加到Word文档时,都需要更新目录。为了以编程方式执行此操作,您只需要在保存文档之前调用Document.UpdateFields()方法即可。
Aspose.Words for .NET还允许您从Word文档中删除目录。以下是执行此操作的步骤。
下面的代码示例演示如何从C#中的Word文档中删除目录。
public static void Run() { // The path to the documents directory. string dataDir = RunExamples.GetDataDir_WorkingWithStyles(); // Open a document which contains a TOC. Document doc = new Document(dataDir + "Document.TableOfContents.doc"); // Remove the first table of contents from the document. RemoveTableOfContents(doc, 0); dataDir = dataDir + "Document.TableOfContentsRemoveToc_out.doc"; // Save the output. doc.Save(dataDir); Console.WriteLine("\nSpecified TOC from a document removed successfully.\nFile saved at " + dataDir); } ////// Removes the specified table of contents field from the document. //////The document to remove the field from.///The zero-based index of the TOC to remove.public static void RemoveTableOfContents(Document doc, int index) { // Store the FieldStart nodes of TOC fields in the document for quick access. ArrayList fieldStarts = new ArrayList(); // This is a list to store the nodes found inside the specified TOC. They will be removed // At the end of this method. ArrayList nodeList = new ArrayList(); foreach (FieldStart start in doc.GetChildNodes(NodeType.FieldStart, true)) { if (start.FieldType == FieldType.FieldTOC) { // Add all FieldStarts which are of type FieldTOC. fieldStarts.Add(start); } } // Ensure the TOC specified by the passed index exists. if (index > fieldStarts.Count - 1) throw new ArgumentOutOfRangeException("TOC index is out of range"); bool isRemoving = true; // Get the FieldStart of the specified TOC. Node currentNode = (Node)fieldStarts[index]; while (isRemoving) { // It is safer to store these nodes and delete them all at once later. nodeList.Add(currentNode); currentNode = currentNode.NextPreOrder(doc); // Once we encounter a FieldEnd node of type FieldTOC then we know we are at the end // Of the current TOC and we can stop here. if (currentNode.NodeType == NodeType.FieldEnd) { FieldEnd fieldEnd = (FieldEnd)currentNode; if (fieldEnd.FieldType == FieldType.FieldTOC) isRemoving = false; } } // Remove all nodes found in the specified TOC. foreach (Node node in nodeList) { node.Remove(); } }
如果你想试用Aspose的全部完整功能,可 联系在线客服获取30天临时授权体验。
本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@evget.com
本文主要介绍如何使用DevExpress WPF Grid控件实现展开和折叠节点,欢迎下载最新版组件体验!
本文将介绍两种方法,讲述如何使用 C# 从 PDF 中提取条码信息:一种是提取 PDF 页面中的嵌入图片后识别条码,另一种是将整个页面渲染为图像再进行识别。这两种方法都支持识别多种 1D 和 2D 条码类型。
本教程主要为大家介绍DevExpress WinForms WinExplorer视图的项目自定义,欢迎下载最新版组件体验!
将复杂的 DGN 文件转换为可缩放矢量图形 (SVG),SVG 广泛应用于 Web 应用程序和数字媒体,以及建筑、工程和施工等行业,本教程将展示如何使用Aspose.CAD 将DGN导出为SVG。
服务电话
重庆/ 023-68661681
华东/ 13452821722
华南/ 18100878085
华北/ 17347785263
客户支持
技术支持咨询服务
服务热线:400-700-1020
邮箱:sales@evget.com
关注我们
地址 : 重庆市九龙坡区火炬大道69号6幢
慧都科技 版权所有 Copyright 2003-
2025 渝ICP备12000582号-13 渝公网安备
50010702500608号