赞
踩
从 Word 文档中提取文本通常在不同的场景中执行。例如,分析文本,提取文档的特定部分并将它们组合成单个文档,等等。在本文中,您将学习如何使用 C# 以编程方式从 Word 文档中提取文本。此外,我们将介绍如何动态提取段落、表格等特定元素之间的内容。
Aspose.Words for .NET 最新下载https://www.evget.com/product/564/download
提示:您可能需要检查 Aspose PowerPoint to Word Converter,因为它演示了流行的演示文稿到 Word 文档的转换过程。
从 Word 文档中提取文本的 C# 库
Aspose.Words for .NET是一个功能强大的库,可让您从头开始创建 MS Word 文档。此外,它可以让您操作现有的 Word 文档进行加密、转换、文本提取等。我们将使用这个库从 Word DOCX 或 DOC 文档中提取文本。您可以 下载 API 的 DLL 或 使用包管理器控制台直接从NuGet安装它。
PM> Install-Package Aspose.Words
使用 C# 在 Word 文档中提取文本
MS Word 文档由各种元素组成,包括段落、表格、图像等。因此,文本提取的要求可能因一种情况而异。例如,您可能需要在段落、书签、评论等之间提取文本。
Word 文档中的每种类型的元素都表示为一个节点。因此,要处理文档,您将不得不使用节点。那么让我们开始看看如何在不同的场景下从 Word 文档中提取文本。
在 C# 中从 Word 文档中提取文本
在本节中,我们将为 Word 文档实现一个 C# 文本提取器,文本提取的工作流程如下:
现在让我们编写一个名为ExtractContent的方法,我们将向该方法传递节点和一些其他参数来执行文本提取。此方法将解析文档并克隆节点。以下是我们将传递给此方法的参数。
以下是提取传递的节点之间的内容的ExtractContent方法的完整实现.
现在我们准备好使用这些方法并从 Word 文档中提取文本。
在 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 文档中不同类型的节点之间提取文本
您还可以在不同类型的节点之间提取内容。为了演示,让我们提取段落和表格之间的内容并将其保存到新的 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 的其他功能 。如果您有任何问题,请随时告诉我们。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。