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の「Paragraphs」コレクションにアクセスすることで、特定の段落からテキストを抽出できます。これにより、必要な段落をターゲットにして処理できるため、テキスト抽出プロセスをより細かく制御できます。

IronWord を使用して DOCX ドキュメント内のテーブルからデータを抽出することは可能ですか?

はい、IronWord を使用すると、行やセルをナビゲートしてテーブルからデータを抽出できるため、処理や分析のために構造化されたデータに簡単にアクセスできます。

抽出したテキストを IronWord を使用してファイルにエクスポートできますか?

はい、IronWord を使用してテキストを抽出したら、それをさらに処理して、.txt ファイルなどのさまざまな形式でエクスポートし、保存したり、さらに使用したりすることができます。

テキスト抽出に IronWord を使い始める手順は何ですか?

テキスト抽出に IronWord を使い始めるには、C# ライブラリをダウンロードし、新しい Word 文書を作成し、`ExtractText()` メソッドを使用してテキスト コンテンツにアクセスして抽出し、必要に応じて抽出したテキストを処理またはエクスポートします。

IronWord は DOCX ドキュメント全体からのデータの抽出をサポートしていますか?

はい、IronWord は DOCX ドキュメント全体からのデータの抽出をサポートしており、`ExtractText()` メソッドを使用して段落や表を含むすべてのテキスト コンテンツを取得できます。

IronWord は、Word 文書の最初と最後の段落からのテキスト抽出をどのように処理しますか?

IronWord では、`Paragraphs` コレクションを通じて特定の段落にアクセスし、必要に応じてテキストを処理することで、最初と最後の段落を含む特定の段落からテキストを抽出できます。

IronWord で抽出されたテキストのコンソール出力を表示する方法はありますか?

はい、IronWord は抽出されたテキストをコンソールに表示する機能を提供しており、抽出プロセス中に出力を直接確認できます。

IronWord を使用して DOCX ファイル内のテーブルから特定のセルの値を抽出するにはどうすればよいですか?

IronWord を使用すると、行と列をナビゲートしてテーブルから特定のセルの値を抽出できるため、テーブル内の任意のセルからデータをターゲットにして取得することができます。

IronWord は DOCX ファイルからどのようなテキスト要素を抽出できますか?

IronWord は、段落、表、その他のテキスト コンポーネントを含むさまざまなテキスト要素を DOCX ファイルから抽出し、包括的なテキスト抽出機能を提供します。

Ahmad Sohail
フルスタックデベロッパー

Ahmadは、C#、Python、およびウェブ技術に強い基盤を持つフルスタック開発者です。彼はスケーラブルなソフトウェアソリューションの構築に深い関心を持ち、デザインと機能が実際のアプリケーションでどのように融合するかを探求することを楽しんでいます。

Iron Softwareチームに参加する前、Ahmadは自動化プロジェクトやAPI統合に取り組み、パフォーマンスの向上と開発者の体験向上に注力してきました。

彼の自由時間には、UI/UXのアイデアを試したり、オープンソースツールに貢献したり、時折テクニカルライティングやドキュメンテーションに取り組んで、複雑なトピックを理解しやすくすることを目指しています。

準備はいいですか?
Nuget ダウンロード 25,807 | バージョン: 2025.11 ただ今リリースされました