Test in production without watermarks.
Works wherever you need it to.
Get 30 days of fully functional product.
Have it up and running in minutes.
Full access to our support engineering team during your product trial
In this tutorial, we explore how to read Word documents using Iron Word in C#. The process begins with ensuring Iron Word is installed via NuGet in your C# console application. After installation, open your Program.cs
file and use the Iron Word library to create an instance of a Word document class. By specifying the document's path, you initialize an object representing the loaded Word document.
The tutorial then guides you through reading the content by iterating through paragraphs and text runs within the document. This involves traversing each paragraph and nested text run, retrieving the text content, and displaying it on the console. Additionally, it shows how to save any changes to a new file using the save-as method.
Upon running the program, you will see the Word document's contents displayed on the console, demonstrating successful implementation. The tutorial emphasizes the breadth of functionalities Iron Word offers beyond reading documents, encouraging further exploration. Subscribe for more tutorials and check the description for a link to download and install the package. Enjoy discovering the power of Iron Word!
Here's a sample implementation:
using System;
using IronWordLibrary; // Make sure to replace with the actual namespace of Iron Word
class Program
{
static void Main(string[] args)
{
// Specify the path to the Word document
string filePath = @"path\to\your\document.docx";
// Load the Word document
var document = new IronWord.Document(filePath);
// Iterate through each paragraph in the document
foreach (var paragraph in document.Paragraphs)
{
// Get the text content of the paragraph
Console.WriteLine($"Paragraph: {paragraph.Text}");
// Iterate through each text run in the paragraph
foreach (var textRun in paragraph.TextRuns)
{
// Display the text run's content
Console.WriteLine($"Text Run: {textRun.Text}");
}
}
// Save the document as a new file
document.SaveAs(@"path\to\your\new-document.docx");
Console.WriteLine("Document has been saved to a new file.");
}
}
using System;
using IronWordLibrary; // Make sure to replace with the actual namespace of Iron Word
class Program
{
static void Main(string[] args)
{
// Specify the path to the Word document
string filePath = @"path\to\your\document.docx";
// Load the Word document
var document = new IronWord.Document(filePath);
// Iterate through each paragraph in the document
foreach (var paragraph in document.Paragraphs)
{
// Get the text content of the paragraph
Console.WriteLine($"Paragraph: {paragraph.Text}");
// Iterate through each text run in the paragraph
foreach (var textRun in paragraph.TextRuns)
{
// Display the text run's content
Console.WriteLine($"Text Run: {textRun.Text}");
}
}
// Save the document as a new file
document.SaveAs(@"path\to\your\new-document.docx");
Console.WriteLine("Document has been saved to a new file.");
}
}
Imports System
Imports IronWordLibrary ' Make sure to replace with the actual namespace of Iron Word
Friend Class Program
Shared Sub Main(ByVal args() As String)
' Specify the path to the Word document
Dim filePath As String = "path\to\your\document.docx"
' Load the Word document
Dim document = New IronWord.Document(filePath)
' Iterate through each paragraph in the document
For Each paragraph In document.Paragraphs
' Get the text content of the paragraph
Console.WriteLine($"Paragraph: {paragraph.Text}")
' Iterate through each text run in the paragraph
For Each textRun In paragraph.TextRuns
' Display the text run's content
Console.WriteLine($"Text Run: {textRun.Text}")
Next textRun
Next paragraph
' Save the document as a new file
document.SaveAs("path\to\your\new-document.docx")
Console.WriteLine("Document has been saved to a new file.")
End Sub
End Class
Code Explanation:
IronWord.Document
class is used to load the Word document.Further Reading: How to open Word Document in C#