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 Document.
  • 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 the IronWord is quick and easy, please install the package like this:

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.

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:

:path=/static-assets/word/content-code-examples/get-started/get-started-license.cs
IronWord.License.LicenseKey = "IRONWORD.MYLICENSE.KEY.1EF01";
IronWord.License.LicenseKey = "IRONWORD.MYLICENSE.KEY.1EF01"
VB   C#

Code Examples

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.

:path=/static-assets/word/content-code-examples/get-started/get-started-1.cs
using IronWord;
using IronWord.Models;

// Create textrun
Text textRun = new Text("Sample text");

Paragraph paragraph = new Paragraph();
paragraph.AddText(textRun);

// Create a new Word document
WordDocument doc = new WordDocument(paragraph);

// Export docx
doc.SaveAs("document.docx");
Imports IronWord
Imports IronWord.Models

' Create textrun
Private textRun As New Text("Sample text")

Private paragraph As New Paragraph()
paragraph.AddText(textRun)

' Create a new Word document
Dim doc As New WordDocument(paragraph)

' Export docx
doc.SaveAs("document.docx")
VB   C#

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.

:path=/static-assets/word/content-code-examples/get-started/get-started-2.cs
using IronWord;
using IronWord.Models;

// Load docx
WordDocument doc = new WordDocument("document.docx");

// Configure image
IronWord.Models.Image image = new IronWord.Models.Image("image.jpg");
image.Width = 250; // In unit pixel
image.Height = 200; // In unit pixel
Paragraph paragraph = new Paragraph();

// Add image
paragraph.AddImage(image);

// Add paragraph
doc.AddParagraph(paragraph);

// Export docx
doc.SaveAs("save_document.docx");
Imports IronWord
Imports IronWord.Models

' Load docx
Private doc As New WordDocument("document.docx")

' Configure image
Private image As New IronWord.Models.Image("image.jpg")
image.Width = 250 ' In unit pixel
image.Height = 200 ' In unit pixel
Dim paragraph As New Paragraph()

' Add image
paragraph.AddImage(image)

' Add paragraph
doc.AddParagraph(paragraph)

' Export docx
doc.SaveAs("save_document.docx")
VB   C#

Add Table

Adding a table requires a little more work, as the table, rows, columns, and table cells have to be created. However, with this setup, there are significant configuration opportunities. Each cell can have a different style. Explore the various border styles, offering a vast selection of 24 types.

:path=/static-assets/word/content-code-examples/get-started/get-started-3.cs
using IronWord;
using IronWord.Models;

// Create table cell
TableCell cell = new TableCell();

Text textRun = new Text();
textRun.Text = "Sample text";

// Add textrun to the cell
cell.AddChild(new Paragraph(textRun));

// Configure border style
BorderStyle borderStyle = new BorderStyle();
borderStyle.BorderColor = Color.Black;
borderStyle.BorderValue = IronWord.Models.Enums.BorderValues.Thick;
borderStyle.BorderSize = 5;

// Configure table border
TableBorders tableBorders = new TableBorders() {
    TopBorder = borderStyle,
    RightBorder = borderStyle,
    BottomBorder = borderStyle,
    LeftBorder = borderStyle,
};

cell.Borders = tableBorders;

// Create row and add cell
TableRow row = new TableRow();
row.AddCell(cell);
row.AddCell(cell);

// Create table and add row
Table table = new Table();
table.AddRow(row);

// Create new Word document from the table
WordDocument doc = new WordDocument(table);

// Export Word document
doc.SaveAs("Document.docx");
Imports IronWord
Imports IronWord.Models

' Create table cell
Private cell As New TableCell()

Private textRun As New Text()
textRun.Text = "Sample text"

' Add textrun to the cell
cell.AddChild(New Paragraph(textRun))

' Configure border style
Dim borderStyle As New BorderStyle()
borderStyle.BorderColor = Color.Black
borderStyle.BorderValue = IronWord.Models.Enums.BorderValues.Thick
borderStyle.BorderSize = 5

' Configure table border
Dim tableBorders As New TableBorders() With {
	.TopBorder = borderStyle,
	.RightBorder = borderStyle,
	.BottomBorder = borderStyle,
	.LeftBorder = borderStyle
}

cell.Borders = tableBorders

' Create row and add cell
Dim row As New TableRow()
row.AddCell(cell)
row.AddCell(cell)

' Create table and add row
Dim table As New Table()
table.AddRow(row)

' Create new Word document from the table
Dim doc As New WordDocument(table)

' Export Word document
doc.SaveAs("Document.docx")
VB   C#

Licensing & Support Available

IronWord is a paid library, however free trial licenses are also 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: support@ironsoftware.com