How to Extract Text from DOCX

This article was translated from English: Does it need improvement?
Translated
View the article in English

Text extraction from DOCX files is a common requirement for document processing and data analysis. IronWord provides a straightforward way to read and extract text content from existing DOCX files, allowing you to access paragraphs, tables, and other text elements programmatically.

In this tutorial, the ExtractText() method will be talked about in detail and how it can help access text from various document elements.

Get started with IronWord

今天在您的项目中使用 IronWord,免费试用。

第一步:
green arrow pointer


Text Extraction Example

The ExtractText() method allows you to retrieve text content from an entire Word document. In this example, we create a new document, add text to it, extract the text using ExtractText(), and display it in the console. This demonstrates the primary text extraction workflow.

:path=/static-assets/word/content-code-examples/how-to/extract-text-simple.cs
using IronWord;

// Instantiate a new DOCX file
WordDocument doc = new WordDocument();

// Add text
doc.AddText("Hello, World!");

// Print extracted text from the document to the console
Console.WriteLine(doc.ExtractText());
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

Output

Code example for basic text extraction

Console Log

Console output showing extracted text

Extract Text from a Paragraph

For more control, you can extract text from specific paragraphs instead of the entire document. By accessing the Paragraphs collection, you can target and process any paragraph you need. In this example, we’ll extract text from the first and last paragraphs, combine them, and save the result to a .txt file.

:path=/static-assets/word/content-code-examples/how-to/extract-text-paragraphs.cs
using IronWord;
using System.IO;

// Load an existing DOCX file
WordDocument doc = new WordDocument("document.docx");

// Extract text and assign variables
string firstParagraph = doc.Paragraphs[0].ExtractText();
string lastParagraph = doc.Paragraphs.Last().ExtractText();

// Combine the texts
string newText = firstParagraph + " " + lastParagraph;

// Export the combined text as a new .txt file
File.WriteAllText("output.txt", newText);
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

First Paragraph

First paragraph extraction result

Last Paragraph

Last paragraph extraction result

Text File Output

Combined text output in text file

The screenshots above show the first paragraph extraction, last paragraph extraction, and the combined output saved to a text file.

Text Extraction from a Table

Tables often contain structured data that needs to be extracted for processing or analysis. IronWord allows you to access table data by navigating through rows and cells. In this example, we load a document containing an API statistics table and extract a specific cell value from the 4th column of the 2nd row.

:path=/static-assets/word/content-code-examples/how-to/extract-text-table.cs
using IronWord;

// Load the API statistics document
WordDocument apiStatsDoc = new WordDocument("api-statistics.docx");

// Extract text from the 1st table, 4th column and 2nd row
string extractedValue = apiStatsDoc.Tables[0].Rows[2].Cells[3].ExtractText();

// Print extracted value
Console.WriteLine($"Target success rate: {extractedValue}");
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

Example Table

API statistics table in Word document

Console Log

Extracted table cell value in console

常见问题解答

在 IronWord 中,从 DOCX 文件中提取文本的主要方法是什么?

使用 IronWord 从 DOCX 文件中提取文本的主要方法是 `ExtractText()` 方法,该方法允许您从各种文档元素(例如段落和表格)中检索文本内容。

如何使用 IronWord 从特定段落中提取文本?

您可以通过访问 IronWord 中的“段落”集合,从特定段落中提取文本。这样,您可以定位并处理所需的任何段落,从而更好地控制文本提取过程。

是否可以使用 IronWord 从 DOCX 文档中的表格中提取数据?

是的,IronWord 允许您通过浏览行和单元格从表格中提取数据,从而轻松访问结构化数据以进行处理或分析。

我可以使用 IronWord 将提取的文本导出到文件吗?

是的,使用 IronWord 提取文本后,您可以对其进行进一步处理,并将其导出为各种格式,例如 .txt 文件,以便存储或进一步使用。

使用 IronWord 进行文本提取的步骤有哪些?

要开始使用 IronWord 进行文本提取,请下载 C# 库,创建一个新的 Word 文档,使用 `ExtractText()` 方法访问和提取文本内容,然后根据需要处理或导出提取的文本。

IronWord是否支持从整个DOCX文档中提取数据?

是的,IronWord 支持从整个 DOCX 文档中提取数据,允许您使用 `ExtractText()` 方法检索所有文本内容,包括段落和表格。

IronWord如何处理从Word文档的首尾段落中提取文本?

IronWord 允许您通过访问“段落”集合来提取特定段落(包括首尾段落)中的文本,并根据需要处理文本。

有没有办法查看 IronWord 中提取文本的控制台输出?

是的,IronWord 提供了在控制台中显示提取文本的功能,使您可以在提取过程中直接验证输出。

如何使用 IronWord 从 DOCX 文件中的表格中提取特定单元格的值?

IronWord 允许您通过导航行和列从表格中提取特定单元格的值,从而可以定位并检索表格中任何单元格的数据。

IronWord 可以从 DOCX 文件中提取哪些类型的文本元素?

IronWord 可以从 DOCX 文件中提取各种文本元素,包括段落、表格和其他文本组件,提供全面的文本提取功能。

Ahmad Sohail
全栈开发者

Ahmad 是一名全栈开发人员,拥有扎实的 C#、Python 和 Web 技术基础。他对构建可扩展的软件解决方案深感兴趣,并喜欢探索设计和功能在实际应用中如何结合。

在加入 Iron Software 团队之前,Ahmad 致力于自动化项目和 API 集成,专注于提高性能和开发人员体验。

在业余时间,他喜欢尝试 UI/UX 想法,贡献开源工具,并偶尔从事技术写作和文档工作,以便让复杂主题更易于理解。

准备开始了吗?
Nuget 下载 25,807 | 版本: 2025.11 刚刚发布