翻譯|使用教程|編輯:胡濤|2022-05-10 11:43:47.990|閱讀 370 次
概述:在本文中,您將學習如何使用 C# 以編程方式從 Word 文檔中提取文本。此外,我們將介紹如何動態提取段落、表格等特定元素之間的內容。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
從 Word 文檔中提取文本通常在不同的場景中執行。例如,分析文本,提取文檔的特定部分并將它們組合成單個文檔,等等。在本文中,您將學習如何使用 C# 以編程方式從 Word 文檔中提取文本。此外,我們將介紹如何動態提取段落、表格等特定元素之間的內容。
提示:您可能需要檢查 Aspose PowerPoint to Word Converter,因為它演示了流行的演示文稿到 Word 文檔的轉換過程。
Aspose.Words for .NET是一個功能強大的庫,可讓您從頭開始創建 MS Word 文檔。此外,它可以讓您操作現有的 Word 文檔進行加密、轉換、文本提取等。我們將使用這個庫從 Word DOCX 或 DOC 文檔中提取文本。您可以 下載 API 的 DLL 或 使用包管理器控制臺直接從NuGet安裝它。
PM> Install-Package Aspose.Words
MS Word 文檔由各種元素組成,包括段落、表格、圖像等。因此,文本提取的要求可能因一種情況而異。例如,您可能需要在段落、書簽、評論等之間提取文本。
Word 文檔中的每種類型的元素都表示為一個節點。因此,要處理文檔,您將不得不使用節點。那么讓我們開始看看如何在不同的場景下從 Word 文檔中提取文本。
在本節中,我們將為 Word 文檔實現一個 C# 文本提取器,文本提取的工作流程如下:
現在讓我們編寫一個名為ExtractContent的方法,我們將向該方法傳遞節點和一些其他參數來執行文本提取。此方法將解析文檔并克隆節點。以下是我們將傳遞給此方法的參數。
以下是提取傳遞的節點之間的內容的ExtractContent方法的完整實現.
現在我們準備好使用這些方法并從 Word 文檔中提取文本。
讓我們看看如何在 Word DOCX 文檔的兩個段落之間提取內容。以下是在 C# 中執行此操作的步驟。
以下代碼示例展示了如何在 C# 中提取 Word 文檔中第 7 段和第 11 段之間的文本。
// Load Word document Document doc = new Document("document.docx"); // Gather the nodes (the GetChild method uses 0-based index) Paragraph startPara = (Paragraph)doc.FirstSection.Body.GetChild(NodeType.Paragraph, 6, true); Paragraph endPara = (Paragraph)doc.FirstSection.Body.GetChild(NodeType.Paragraph, 10, true); // Extract the content between these nodes in the document. Include these markers in the extraction. ArrayList extractedNodes = ExtractContent(startPara, endPara, true); // Insert the content into a new document and save it to disk. Document dstDoc = GenerateDocument(doc, extractedNodes); dstDoc.Save("output.docx");
您還可以在不同類型的節點之間提取內容。為了演示,讓我們提取段落和表格之間的內容并將其保存到新的 Word 文檔中。以下是執行此操作的步驟。
以下代碼示例演示如何在 C# 中提取段落和表格之間的文本。
// Load Word document Document doc = new Document("document.docx"); Paragraph startPara = (Paragraph)doc.LastSection.GetChild(NodeType.Paragraph, 2, true); Table endTable = (Table)doc.LastSection.GetChild(NodeType.Table, 0, true); // Extract the content between these nodes in the document. Include these markers in the extraction. ArrayList extractedNodes = ExtractContent(startPara, endTable, true); // Insert the content into a new document and save it to disk. Document dstDoc = GenerateDocument(doc, extractedNodes); dstDoc.Save("output.docx");
現在讓我們看看如何根據樣式提取段落之間的內容。為了演示,我們將提取 Word 文檔中第一個“標題 1”和第一個“標題 3”之間的內容。以下步驟演示了如何在 C# 中實現此目的。
以下代碼示例展示了如何根據樣式提取段落之間的內容。
// Load Word document Document doc = new Document("document.docx"); // Gather a list of the paragraphs using the respective heading styles. List<Paragraph> parasStyleHeading1 = ParagraphsByStyleName(doc, "Heading 1"); List<Paragraph> parasStyleHeading3 = ParagraphsByStyleName(doc, "Heading 3"); // Use the first instance of the paragraphs with those styles. Node startPara1 = (Node)parasStyleHeading1[0]; Node endPara1 = (Node)parasStyleHeading3[0]; // Extract the content between these nodes in the document. Don't include these markers in the extraction. ArrayList extractedNodes = ExtractContent(startPara1, endPara1, false); // Insert the content into a new document and save it to disk. Document dstDoc = GenerateDocument(doc, extractedNodes); dstDoc.Save("output.docx");
在本文中,您學習了如何使用 C# 從 MS Word 文檔中提取文本。此外,您還了解了如何以編程方式在 Word 文檔中相似或不同類型的節點之間提取內容。因此,您可以在 C# 中構建自己的 MS Word 文本提取器。此外,您可以使用文檔探索 Aspose.Words for .NET 的其他功能 。如果您有任何問題,請隨時告訴我們。
歡迎下載|體驗更多Aspose產品
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn