没有找到合适的产品?
联系客服协助选型:023-68661681
提供3000多款全球软件/控件产品
针对软件研发的各个阶段提供专业培训与技术咨询
根据客户需求提供定制化的软件开发服务
全球知名设计软件,显著提升设计质量
打造以经营为中心,实现生产过程透明化管理
帮助企业合理产能分配,提高资源利用率
快速打造数字化生产线,实现全流程追溯
生产过程精准追溯,满足企业合规要求
以六西格玛为理论基础,实现产品质量全数字化管理
通过大屏电子看板,实现车间透明化管理
对设备进行全生命周期管理,提高设备综合利用率
实现设备数据的实时采集与监控
利用数字化技术提升油气勘探的效率和成功率
钻井计划优化、实时监控和风险评估
提供业务洞察与决策支持实现数据驱动决策
翻译|使用教程|编辑:胡涛|2022-10-27 14:14:39.193|阅读 303 次
概述:本文主要介绍如何在 C# 中的 Word 文档中添加、编辑和删除目录,欢迎查阅~
# 界面/图表报表/文档/IDE等千款热门软控件火热销售中 >>
相关链接:
Aspose.Words 是一种高级Word文档处理API,用于执行各种文档管理和操作任务。API支持生成,修改,转换,呈现和打印文档,而无需在跨平台应用程序中直接使用Microsoft Word。此外,API支持所有流行的Word处理文件格式,并允许将Word文档导出或转换为固定布局文件格式和最常用的图像/多媒体格式。
Word 文档中的目录(TOC) 可让您大致了解文档包含的内容。此外,它还允许您导航到文档的特定部分。在本文中,您将学习如何以编程方式处理 Word 文档中的目录。特别是,本文介绍了如何在 C# 中添加、提取、更新或删除 Word 文档中的目录。
为了使用 Word 文档中的目录,我们将使用Aspose.Words for .NET。所述 API 旨在从 .NET 应用程序中执行基本和高级 Word 自动化功能。此外,它使您可以很好地控制 Word 文档中目录的操作。您可以下载API 或使用NuGet安装它。
PM> Install-Package Aspose.Words
以下是使用 Aspose.Words for .NET 将目录添加到 Word 文档的步骤。
以下代码示例演示如何在 C# 中的 Word 文档中添加目录。
// For complete examples and data files, please go to //github.com/aspose-words/Aspose.Words-for-.NET
// 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 文档时,都需要更新目录。为了以编程方式执行此操作,您只需在保存文档之前调用方法。
Aspose.Words for .NET 还允许您从 Word 文档中删除目录。以下是执行此操作的步骤。
以下代码示例演示如何从 C# 中的 Word 文档中删除目录。
// For complete examples and data files, please go to //github.com/aspose-words/Aspose.Words-for-.NET
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);
}
/// <summary>
/// Removes the specified table of contents field from the document.
/// </summary>
/// <param name="doc">The document to remove the field from.</param>
/// <param name="index">The zero-based index of the TOC to remove.</param>
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();
}
}
以上便是在 C# 中的 Word 文档中添加、编辑和删除目录详细步骤 ,要是您还有其他关于产品方面的问题,欢迎咨询我们,或者加入我们官方技术交流群。
欢迎下载|体验更多Aspose产品
本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@ke049m.cn




大型SaaS系统的自动化测试常常受制于界面变化快、结构复杂、加载机制多变等因素。从元素识别到脚本管理,SmartBear TestComplete帮助Salesforce建了可靠的自动化测试体系。
BarTender 标签管理系统,正是帮助企业轻松实现 GS1 标准化标签设计、编码生成与信息联动的强大工具。
Parasoft C/C++test 是一款功能强大的 C/C++ 软件测试工具,集成了静态代码分析、单元测试、集成测试和覆盖率分析等功能,单元测试作为其关键功能之一,为了适配多样化的目标部署环境,C/C++test 设计了灵活的测试结果收集机制。通过Socket通讯方式来收集单元测试结果,从而扩展其测试覆盖范围与应用场景。
Parasoft C/C++test作为一款功能全面的自动化代码质量保障工具,为C/C++开发者提供了静态分析、单元测试和运行时错误检测等核心功能。通过将其与轻量级且广受欢迎的VScode编辑器集成,开发团队可以在熟悉的编码环境中实时获得代码质量反馈,有效提升开发效率与代码可靠性。
相关产品
专业的电子表格控件,无需MS Excel也可满足一切Excel表格功能。
Aspose.Words for .NET无需Microsoft Word也可在任何平台上满足Word文档的一切操作需求。
Aspose.PDF for .NETPDF文档创建组件,无需Adobe Acrobat,也可以在任何平台上操作PDF文档。
最新文章 MORE
半岛外围网上直营相关的文章 MORE
服务电话
重庆/ 023-68661681
华东/ 13452821722
华南/ 18100878085
华北/ 17347785263
客户支持
技术支持咨询服务
服务热线:400-700-1020
邮箱:sales@ke049m.cn
关注我们
地址 : 重庆市九龙坡区火炬大道69号6幢
半岛外围网上直营