翻譯|使用教程|編輯:胡濤|2022-10-27 14:14:39.193|閱讀 290 次
概述:本文主要介紹如何在 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@fc6vip.cn