using IronWord;// Quick example: Extract all text from DOCXWordDocument doc = new WordDocument("sample.docx");string allText = doc.ExtractText();Console.WriteLine(allText);
using IronWord;
// Quick example: Extract all text from DOCX
WordDocument doc = new WordDocument("sample.docx");
string allText = doc.ExtractText();
Console.WriteLine(allText);
using System;using IronWord;// Instantiate a new DOCX fileWordDocument doc = new WordDocument();// Add textdoc.AddText("Hello, World!");// Print extracted text from the document to the consoleConsole.WriteLine(doc.ExtractText());
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());
ImportsSystemImportsIronWord' Instantiate a new DOCX fileDim doc As New WordDocument()' Add textdoc.AddText("Hello, World!")' Print extracted text from the document to the consoleConsole.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())
using System.IO;using System.Linq;using IronWord;// Load an existing DOCX fileWordDocument doc = new WordDocument("document.docx");// Extract text and assign variablesstring firstParagraph = doc.Paragraphs[0].ExtractText();string lastParagraph = doc.Paragraphs.Last().ExtractText();// Combine the textsstring newText = firstParagraph + " " + lastParagraph;// Export the combined text as a new .txt fileFile.WriteAllText("output.txt", newText);
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);
ImportsSystem.IOImportsSystem.LinqImportsIronWord' Load an existing DOCX fileDim doc As New WordDocument("document.docx")' Extract text and assign variablesDim firstParagraph AsString = doc.Paragraphs(0).ExtractText()Dim lastParagraph AsString = doc.Paragraphs.Last().ExtractText()' Combine the textsDim newText AsString = firstParagraph & " " & lastParagraph' Export the combined text as a new .txt fileFile.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)
using System;using IronWord;using IronWord.Models;// Load the API statistics documentWordDocument apiStatsDoc = new WordDocument("api-statistics.docx");// Extract text from the 1st table, 4th column and 3rd rowstring extractedValue = ((TableCell)apiStatsDoc.Tables[0].Rows[2].Cells[3]).ExtractText();// Print extracted valueConsole.WriteLine($"Target success rate: {extractedValue}");
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}");
using IronWord;using IronWord.Models;using System.Text;using System.Linq;// Load a complex documentWordDocument complexDoc = new WordDocument("report.docx");// Create a StringBuilder for efficient string concatenationStringBuilder 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 summariesforeach (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 extractionSystem.IO.File.WriteAllText("structured-extract.txt", extractedContent.ToString());
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());
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.