How to open Word Document in C#

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
$vbLabelText   $csharpLabel

Code Explanation:

  • The code starts with importing the necessary namespaces.
  • We define the file path of the Word document.
  • The IronWord.Document class is used to load the Word document.
  • We iterate through all paragraphs in the document. For each paragraph, we print its text content.
  • We further iterate through the text runs in each paragraph and print their text content.
  • Finally, the document is saved to a new specified file path.

Further Reading: How to open Word Document in C#

Regan Pun
Software Engineer
Regan graduated from the University of Reading, with a BA in Electronic Engineering. Before joining Iron Software, his previous job roles had him laser-focused on single tasks; and what he most enjoys at Iron Software is the spectrum of work he gets to undertake, whether it’s adding value to sales, technical support, product development or marketing. He enjoys understanding the way developers are using the Iron Software library, and using that knowledge to continually improve documentation and develop the products.
< PREVIOUS
How to Work with Document Elements in C#

Ready to get started? Version: 2025.6 just released

View Licenses >