Extract Text from DOCX with IronWord
IronWord's ExtractText() method enables you to extract text from DOCX files by accessing entire documents, specific paragraphs, or table cells, providing a simple API for document processing and data analysis tasks in C#.
-
1Install IronWord with NuGet Package Manager
-
2Copy and run this code snippet.
using IronWord; // Quick example: Extract all text from DOCX WordDocument doc = new WordDocument("sample.docx"); string allText = doc.ExtractText(); Console.WriteLine(allText);C# -
3Deploy to test on your live environment
Start using IronWord in your project today with a free trial
Minimal Workflow (5 steps)
- Install the IronWord C# library
- Load an existing Word document with
new WordDocument() - Call
ExtractText()on the document to retrieve all text - Extract text from specific paragraphs using the
Paragraphscollection - Process or export the extracted text content
How Do I Extract All Text from a DOCX Document?
The ExtractText() method retrieves 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.
The extracted text maintains the logical reading order of the document. The method processes headers, paragraphs, lists, and other text elements in sequence, making it ideal for content analysis and search indexing applications.
using System;
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());Imports System
Imports IronWord
' Instantiate a new DOCX file
Dim doc As New WordDocument()
' Add text
doc.AddText("Hello, World!")
' Print extracted text from the document to the console
Console.WriteLine(doc.ExtractText())What Does the Extracted Text Look Like?

What Output Should I Expect in the Console?

How Can I Extract Text from Specific Paragraphs?
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. This granular approach is useful when dealing with documents that have structured content or when you need to process specific sections independently.
In this example, we extract text from the first and last paragraphs, combine them, and save the result to a .txt file. This technique is commonly used in document summarization tools where you might want to extract the introduction and conclusion of a document. Similar to how you might use license keys to unlock features, the Paragraphs collection gives you access to specific document elements.
using System.IO;
using System.Linq;
using IronWord;
// 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);Imports System.IO
Imports System.Linq
Imports IronWord
' Load an existing DOCX file
Dim doc As New WordDocument("document.docx")
' Extract text and assign variables
Dim firstParagraph As String = doc.Paragraphs(0).ExtractText()
Dim lastParagraph As String = doc.Paragraphs.Last().ExtractText()
' Combine the texts
Dim newText As String = firstParagraph & " " & lastParagraph
' Export the combined text as a new .txt file
File.WriteAllText("output.txt", newText)The ability to extract specific paragraphs becomes powerful when combined with document analysis requirements. For instance, you might extract key paragraphs based on their formatting, position, or content patterns. This selective extraction approach helps reduce processing time and focuses on the most relevant content.
What Content Is Extracted from the First Paragraph?

What Content Is Extracted from the Last Paragraph?

How Does the Combined Text Appear in the Output File?

The screenshots above show the first paragraph extraction, last paragraph extraction, and the combined output saved to a text file. Notice how the extraction process preserves the text content while removing formatting information, making it suitable for plain text processing.
How Do I Extract Data from Tables in DOCX?
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 3rd row.
Table extraction is essential for data migration projects, report generation, and automated data collection workflows. When working with tabular data, understanding the zero-based indexing system is crucial - the first table is Tables[0], the first row is Rows[0], and so on. This systematic approach, similar to licensing structures, provides predictable access patterns.
using System;
using IronWord;
using IronWord.Models;
// Load the API statistics document
WordDocument apiStatsDoc = new WordDocument("api-statistics.docx");
// Extract text from the 1st table, 4th column and 3rd row
string extractedValue = ((TableCell)apiStatsDoc.Tables[0].Rows[2].Cells[3]).ExtractText();
// Print extracted value
Console.WriteLine($"Target success rate: {extractedValue}");
The code demonstrates accessing table cells using the collection properties Tables, Rows, and Cells. Note that the Cells collection returns ITableCell interface objects, which must be cast to TableCell to access the ExtractText method: ((TableCell)cell).ExtractText(). This requires adding using IronWord.Models; to your namespace declarations.
What Does the Source Table Look Like?

What Value Is Retrieved from the Table Cell?

Advanced Text Extraction Scenarios
When working with complex documents, you may need to combine multiple extraction techniques. Here's an example that demonstrates extracting text from multiple elements and processing them differently:
using IronWord;
using IronWord.Models;
using System.Text;
using System.Linq;
// Load a complex document
WordDocument complexDoc = new WordDocument("report.docx");
// Create a StringBuilder for efficient string concatenation
StringBuilder extractedContent = new StringBuilder();
// Extract and process headers (assuming they're in the first few paragraphs)
var headers = complexDoc.Paragraphs
.Take(3)
.Select(p => p.ExtractText())
.Where(text => !string.IsNullOrWhiteSpace(text));
foreach (var header in headers)
{
extractedContent.AppendLine($"HEADER: {header}");
}
// Extract table summaries
foreach (var table in complexDoc.Tables)
{
// Get first cell as table header/identifier
string tableIdentifier = ((TableCell)table.Rows[0].Cells[0]).ExtractText();
extractedContent.AppendLine($"\nTABLE: {tableIdentifier}");
// Extract key metrics (last row often contains totals)
if (table.Rows.Count > 1)
{
var lastRow = table.Rows.Last();
var totals = lastRow.Cells.Select(cell => ((TableCell)cell).ExtractText());
extractedContent.AppendLine($"Totals: {string.Join(", ", totals)}");
}
}
// Save the structured extraction
System.IO.File.WriteAllText("structured-extract.txt", extractedContent.ToString());
This advanced example shows how to create structured extractions by combining different document elements. This approach is useful for generating document summaries, creating indexes, or preparing data for further processing. Just as upgrades enhance software capabilities, combining extraction methods enhances your document processing capabilities.
Best Practices for Text Extraction
When implementing text extraction in production applications, consider these best practices:
- Error Handling: Always wrap extraction code in try-catch blocks to handle documents that might be corrupted or have unexpected structures.
- Performance Optimization: For large documents or batch processing, consider extracting only the necessary portions rather than the entire document content.
- Character Encoding: Be aware of character encoding when saving extracted text, especially for documents containing special characters or multiple languages.
- Memory Management: When processing multiple documents, properly dispose of
WordDocumentobjects to prevent memory leaks.
Remember that text extraction preserves the logical reading order but removes formatting. If you need to maintain formatting information, consider using additional IronWord features or storing metadata separately. For production deployments, review the changelog to stay updated with the latest features and improvements.
Summary
IronWord's ExtractText() method provides a powerful and flexible way to extract text from DOCX files. Whether you need to extract entire documents, specific paragraphs, or table data, the API offers straightforward methods to accomplish your goals. By combining these techniques with proper error handling and optimization strategies, you can build robust document processing applications that efficiently handle various text extraction scenarios.
For more advanced scenarios and to explore additional features, check out extensions and other documentation resources to enhance your document processing capabilities.
Frequently Asked Questions
How do I extract all text from a DOCX document using IronWord?
You can extract all text from a DOCX document using IronWord with the `ExtractText()` method. This method retrieves text content from the entire document, maintaining logical reading order by processing headers, paragraphs, lists, and other elements sequentially. It is useful for content analysis and search indexing applications.
Can I extract text from specific paragraphs in a DOCX file?
Yes, IronWord allows you to extract text from specific paragraphs by accessing the `Paragraphs` collection. This method provides control over which sections to process, making it suitable for documents with structured content or when you need to handle specific sections independently.
How does IronWord handle text extraction from table cells in DOCX files?
IronWord enables extraction from table cells by navigating through the `Tables`, `Rows`, and `Cells` collections. By using zero-based indexing, you can access specific cells, allowing for efficient data migration, report generation, or collection workflows in structured documents.
What are some advanced text extraction scenarios with IronWord?
Advanced scenarios include extracting headers, table summaries, and combining extracted content from multiple elements. This approach, useful for generating document summaries or indexes, involves using a combination of `ExtractText()` and specific document element methods.
What best practices should I follow for text extraction in IronWord?
Implementing text extraction with IronWord involves handling errors using try-catch blocks, optimizing performance by targeting necessary content, managing character encoding, and ensuring proper memory management to prevent leaks during multiple document processing.
How does IronWord help in maintaining text order during extraction?
IronWord maintains the logical reading order by processing text elements such as headers, paragraphs, and lists sequentially. This extraction structure is especially valuable for applications requiring content analysis and indexing.
How can I export extracted text to a file using IronWord?
Once you've extracted the text, you can use C# file handling methods like `File.WriteAllText` to save the text to a file, as demonstrated in the tutorial with paragraph extraction where combined text is saved to an output.txt file.
Is it possible to extract introduction and conclusion sections separately?
Yes, you can extract specific paragraphs such as the introduction and conclusion by targeting them within the `Paragraphs` collection. This method is effective for document summarization tools requiring distinct extraction of key sections.
What encoding considerations should be taken when saving extracted text?
IronWord users should be aware of character encoding, especially when dealing with documents containing special characters or multiple languages. Ensuring the correct encoding is crucial when saving extracted text to maintain text integrity.
How can IronWord's text extraction be optimized for large documents?
For large documents, optimize text extraction by targeting only the necessary portions instead of extracting the entire content. This ensures efficiency, reduces processing time, and leverages the full capabilities of IronWord.

Ahmad is a full-stack developer with a strong foundation in C#, Python, and web technologies. He has a deep interest in building scalable software solutions and enjoys exploring how design and functionality meet in real-world applications.