Getting Started with IronWord

IronWord: Word Document Library for .NET

IronWord is a Word document library developed by Iron Software. IronWord excels in providing robust functionality for working with Word documents in .NET applications.

  • Load, Manipulate, and Save Word and Docx Documents.
  • PageSetup: Configuring paper size, page orientation, margins, and background color.
  • TextRun: Handling text content, styles, splitting, appending text, and adding images.
  • TextStyle: Managing font family, size, color, bold, italic, strikethrough, underline, superscript, and subscript.
  • Paragraph: Adding text runs, images, shapes, setting styles, alignments, bullets, and numbering lists.
  • Table: Manipulating table structure, including adding rows, getting and setting cell values, removing rows, merging cells, and more.
  • Image: Loading images from files or streams, setting wrap text, position offset, width, height, and other properties.
  • Shape: Setting wrap text, position offset, width, height, shape type, and rotation.

Installation

IronWord Library

Installing IronWord is quick and easy. You can install the package using NuGet with the following command:

Install-Package IronWord

Alternatively, download directly from the official IronWord NuGet website.

Once installed, you can get started by adding using IronWord; to the top of your C# code file.

Applying License Key

Next, apply a valid license or trial key to IronWord by assigning the license key to the LicenseKey property of the License class. Include the following code right after the import statement, before using any IronWord methods:

using IronWord;

// Assign your license key
License.LicenseKey = "YOUR_LICENSE_KEY_HERE";
using IronWord;

// Assign your license key
License.LicenseKey = "YOUR_LICENSE_KEY_HERE";
Imports IronWord

' Assign your license key
License.LicenseKey = "YOUR_LICENSE_KEY_HERE"
$vbLabelText   $csharpLabel

Code Examples

Let’s explore some code examples and the features available.

Please note
The DOCX file produced by IronWord, when opened in a certain version of Microsoft Word, may be in Compatibility Mode, causing some of the styling to be unavailable. To convert a Word document out of compatibility mode

  1. Select 'File' > 'Info' and click "Convert."
  2. You will be prompted with a message saying that your document will be upgraded to the newest file format. Click "OK."

Create Word and Docx Document

Create the Word document by instantiating the WordDocument class using one of its constructors. After that, use the SaveAs method to export the Word document. Example:

using IronWord;

class Program
{
    static void Main()
    {
        // Create a new Word document
        var document = new WordDocument();

        // Save the document as a .docx file
        document.SaveAs("example.docx");
    }
}
using IronWord;

class Program
{
    static void Main()
    {
        // Create a new Word document
        var document = new WordDocument();

        // Save the document as a .docx file
        document.SaveAs("example.docx");
    }
}
Imports IronWord

Friend Class Program
	Shared Sub Main()
		' Create a new Word document
		Dim document = New WordDocument()

		' Save the document as a .docx file
		document.SaveAs("example.docx")
	End Sub
End Class
$vbLabelText   $csharpLabel

Add Image

An image cannot be added by itself; instead, it should be added to one of the document structures, such as a Paragraph, TableCell, or Section. Use the AddImage method to add an image. Example:

using IronWord;
using System.Drawing;

class Program
{
    static void Main()
    {
        var document = new WordDocument();
        var section = document.Sections.Add();

        // Add an image to a paragraph
        var paragraph = section.Paragraphs.Add();
        paragraph.AddImage("path/to/image.jpg", new Rectangle(0, 0, 100, 100));

        document.SaveAs("example_with_image.docx");
    }
}
using IronWord;
using System.Drawing;

class Program
{
    static void Main()
    {
        var document = new WordDocument();
        var section = document.Sections.Add();

        // Add an image to a paragraph
        var paragraph = section.Paragraphs.Add();
        paragraph.AddImage("path/to/image.jpg", new Rectangle(0, 0, 100, 100));

        document.SaveAs("example_with_image.docx");
    }
}
Imports IronWord
Imports System.Drawing

Friend Class Program
	Shared Sub Main()
		Dim document = New WordDocument()
		Dim section = document.Sections.Add()

		' Add an image to a paragraph
		Dim paragraph = section.Paragraphs.Add()
		paragraph.AddImage("path/to/image.jpg", New Rectangle(0, 0, 100, 100))

		document.SaveAs("example_with_image.docx")
	End Sub
End Class
$vbLabelText   $csharpLabel

Add Table

Adding a table requires creating the table, rows, columns, and table cells. This enables significant configuration opportunities, as each cell can have different styles. Example:

using IronWord;

class Program
{
    static void Main()
    {
        var document = new WordDocument();
        var section = document.Sections.Add();
        var table = section.Tables.Add(3, 3); // 3x3 table

        // Iterate over cells and set their content
        for (int i = 0; i < table.Rows.Count; i++)
        {
            for (int j = 0; j < table.Rows[i].Cells.Count; j++)
            {
                table.Rows[i].Cells[j].Paragraphs.Add().AppendText($"Cell {i+1},{j+1}");
            }
        }

        document.SaveAs("example_with_table.docx");
    }
}
using IronWord;

class Program
{
    static void Main()
    {
        var document = new WordDocument();
        var section = document.Sections.Add();
        var table = section.Tables.Add(3, 3); // 3x3 table

        // Iterate over cells and set their content
        for (int i = 0; i < table.Rows.Count; i++)
        {
            for (int j = 0; j < table.Rows[i].Cells.Count; j++)
            {
                table.Rows[i].Cells[j].Paragraphs.Add().AppendText($"Cell {i+1},{j+1}");
            }
        }

        document.SaveAs("example_with_table.docx");
    }
}
Imports IronWord

Friend Class Program
	Shared Sub Main()
		Dim document = New WordDocument()
		Dim section = document.Sections.Add()
		Dim table = section.Tables.Add(3, 3) ' 3x3 table

		' Iterate over cells and set their content
		For i As Integer = 0 To table.Rows.Count - 1
			Dim j As Integer = 0
			Do While j < table.Rows(i).Cells.Count
				table.Rows(i).Cells(j).Paragraphs.Add().AppendText($"Cell {i+1},{j+1}")
				j += 1
			Loop
		Next i

		document.SaveAs("example_with_table.docx")
	End Sub
End Class
$vbLabelText   $csharpLabel

Licensing & Support Available

IronWord is a paid library; however, free trial licenses are available here.

For more information about Iron Software, please visit our website: https://ironsoftware.com/. For more support and inquiries, please ask our team.

Support from Iron Software

For general support and technical inquiries, please email us at: [email protected]