Test in a live environment
Test in production without watermarks.
Works wherever you need it to.
Microsoft Word documents are widely used for various purposes, from formal business reports to personal letters. In C#, developers often need to generate Microsoft Word documents programmatically. Windows application developers have traditionally used the Microsoft Office Interop to generate and create Word documents using C#.
However, this approach isn't accessible to everyone, and developers can be found using an OS or on a Linux machine where the Microsoft Office interface isn't available. In these cases, developers must explore other libraries that can independently work on different platforms. One such powerful library for working with Word files programmatically is IronWord, from Iron Software.
IronWord provides robust functionality for working with Word documents in .NET applications and can run on different platforms and docker images/containers based on Linux. With IronWord, which has intuitive C#, VB.NET Word, and Docx Document API, there is no need to install Microsoft Office, Office automation, or Word Interop, to build, edit, and export Word document files. IronWord fully supports .NET 8, 7, 6, Framework, Core, and Azure.
This article will explore the creation of Word documents in C# using the IronWord library.
Prerequisites:
Create a new C# console application or use an existing project where you want to generate the Word document.
Select the console application template and click next.
In the next step, you can provide the solution and project names.
Select the .NET Version and click "Create".
Open your C# project and install the IronWord library using the NuGet Package Manager Console:
Install-Package IronWord -Version 2024.1.2
The NuGet package can also be installed using Visual Studio Package Manager, as shown below.
Let's start with the simple example of creating a Word document using the IronWord library. The following code demonstrates how to create an essential Word document with a single paragraph containing the text "Hello, World!"
using IronWord;
using IronWord.Models;
// Create textrun instance
TextRun textRun = new TextRun("Hello, World!");
Paragraph paragraph = new Paragraph();
paragraph.AddTextRun(textRun);
// Create a new Word document object
WordDocument doc = new WordDocument(paragraph);
// Export docx
doc.SaveAs("example.docx"); // save docx file to disk
using IronWord;
using IronWord.Models;
// Create textrun instance
TextRun textRun = new TextRun("Hello, World!");
Paragraph paragraph = new Paragraph();
paragraph.AddTextRun(textRun);
// Create a new Word document object
WordDocument doc = new WordDocument(paragraph);
// Export docx
doc.SaveAs("example.docx"); // save docx file to disk
Imports IronWord
Imports IronWord.Models
' Create textrun instance
Private textRun As New TextRun("Hello, World!")
Private paragraph As New Paragraph()
paragraph.AddTextRun(textRun)
' Create a new Word document object
Dim doc As New WordDocument(paragraph)
' Export docx
doc.SaveAs("example.docx") ' save docx file to disk
After executing the above code example, the new document file, example.docx, is created and the output is as shown below.
This is a fundamental example of generating a Word document file using IronWord. For more information, you can refer to the documents here.
Now that we know how to create a simple Word document using IronWord, let's add paragraphs and styled text.
TextRun can also take style data, enhancing the text's visual representation. Text can be set with styles such as strikethrough, bold, italic, and underline.
Modify and add the code below to the program you have made previously.
using IronWord;
using IronWord.Models;
// Create textrun
TextRun textRun = new TextRun("Hello, World!"); // add string
Paragraph paragraph = new Paragraph();
paragraph.AddTextRun(textRun);
// Configure text
TextRun introText = new TextRun("This is an example paragraph with italic and bold styling."); // add string or text
TextStyle italicStyle = new TextStyle()
{
IsItalic = true
};
TextRun italicText = new TextRun("Italic example sentence.", italicStyle);
// add italic string
TextStyle boldStyle = new TextStyle()
{
IsBold = true
};
TextRun boldText = new TextRun("Bold example sentence.", boldStyle);
// add bold string
// Add text
paragraph.AddTextRun(introText);
paragraph.AddTextRun(italicText);
paragraph.AddTextRun(boldText);
// Create a new Word document
WordDocument doc = new WordDocument(paragraph);
// Export docx document
doc.SaveAs("example.docx");// docx file save in EXE location
using IronWord;
using IronWord.Models;
// Create textrun
TextRun textRun = new TextRun("Hello, World!"); // add string
Paragraph paragraph = new Paragraph();
paragraph.AddTextRun(textRun);
// Configure text
TextRun introText = new TextRun("This is an example paragraph with italic and bold styling."); // add string or text
TextStyle italicStyle = new TextStyle()
{
IsItalic = true
};
TextRun italicText = new TextRun("Italic example sentence.", italicStyle);
// add italic string
TextStyle boldStyle = new TextStyle()
{
IsBold = true
};
TextRun boldText = new TextRun("Bold example sentence.", boldStyle);
// add bold string
// Add text
paragraph.AddTextRun(introText);
paragraph.AddTextRun(italicText);
paragraph.AddTextRun(boldText);
// Create a new Word document
WordDocument doc = new WordDocument(paragraph);
// Export docx document
doc.SaveAs("example.docx");// docx file save in EXE location
Imports IronWord
Imports IronWord.Models
' Create textrun
Private textRun As New TextRun("Hello, World!") ' add string
Private paragraph As New Paragraph()
paragraph.AddTextRun(textRun)
' Configure text
Dim introText As New TextRun("This is an example paragraph with italic and bold styling.") ' add string or text
Dim italicStyle As New TextStyle() With {.IsItalic = True}
Dim italicText As New TextRun("Italic example sentence.", italicStyle)
' add italic string
Dim boldStyle As New TextStyle() With {.IsBold = True}
Dim boldText As New TextRun("Bold example sentence.", boldStyle)
' add bold string
' Add text
paragraph.AddTextRun(introText)
paragraph.AddTextRun(italicText)
paragraph.AddTextRun(boldText)
' Create a new Word document
Dim doc As New WordDocument(paragraph)
' Export docx document
doc.SaveAs("example.docx") ' docx file save in EXE location
For clear representation of data, it can also be represented in a grid. When data is arranged as a grid, it is called a table. Using IronWord, we can add tables and images to the Word document as shown below:
using IronWord;
using IronWord.Models;
// Create table cell
TableCell cell = new TableCell();
TextRun textRun = new TextRun();
textRun.Text = "Sample text";
// Add textrun to the cell
cell.AddContent(new Paragraph(textRun));
// Configure border style
BorderStyle borderStyle = new BorderStyle();
borderStyle.BorderColor = new IronColor(IronSoftware.Drawing.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); // add cell
row.AddCell(cell); // add cell
// Create table and add row
Table table = new Table();
table.AddRow(row);
// Generate document from the table
WordDocument doc = new WordDocument(table);
// Export Word document
doc.SaveAs("Document.docx"); // docx file save in EXE location
using IronWord;
using IronWord.Models;
// Create table cell
TableCell cell = new TableCell();
TextRun textRun = new TextRun();
textRun.Text = "Sample text";
// Add textrun to the cell
cell.AddContent(new Paragraph(textRun));
// Configure border style
BorderStyle borderStyle = new BorderStyle();
borderStyle.BorderColor = new IronColor(IronSoftware.Drawing.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); // add cell
row.AddCell(cell); // add cell
// Create table and add row
Table table = new Table();
table.AddRow(row);
// Generate document from the table
WordDocument doc = new WordDocument(table);
// Export Word document
doc.SaveAs("Document.docx"); // docx file save in EXE location
Imports IronWord
Imports IronWord.Models
' Create table cell
Private cell As New TableCell()
Private textRun As New TextRun()
textRun.Text = "Sample text"
' Add textrun to the cell
cell.AddContent(New Paragraph(textRun))
' Configure border style
Dim borderStyle As New BorderStyle()
borderStyle.BorderColor = New IronColor(IronSoftware.Drawing.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) ' add cell
row.AddCell(cell) ' add cell
' Create table and add row
Dim table As New Table()
table.AddRow(row)
' Generate document from the table
Dim doc As New WordDocument(table)
' Export Word document
doc.SaveAs("Document.docx") ' docx file save in EXE location
In the example above, we added a table to a Word document using borders.
Images enhance the document's presentation and can add more colors and visual appeal.
Images can be added programmatically to a Word document using IronWord as shown in the code below:
using IronWord;
using IronWord.Models;
// Load new document
WordDocument doc = new WordDocument();
// Configure image
IronWord.Models.Image image = new IronWord.Models.Image("SalesChart.jpg");
image.Width = 200; // In unit pixel (px)
image.Height = 200; // In unit pixel (px)
Paragraph paragraph = new Paragraph();
// Add image
paragraph.AddImage(image);
// Add paragraph
doc.AddParagraph(paragraph);
// Export docx
doc.SaveAs("save_document.docx"); // docx file
using IronWord;
using IronWord.Models;
// Load new document
WordDocument doc = new WordDocument();
// Configure image
IronWord.Models.Image image = new IronWord.Models.Image("SalesChart.jpg");
image.Width = 200; // In unit pixel (px)
image.Height = 200; // In unit pixel (px)
Paragraph paragraph = new Paragraph();
// Add image
paragraph.AddImage(image);
// Add paragraph
doc.AddParagraph(paragraph);
// Export docx
doc.SaveAs("save_document.docx"); // docx file
Imports IronWord
Imports IronWord.Models
' Load new document
Private doc As New WordDocument()
' Configure image
Private image As New IronWord.Models.Image("SalesChart.jpg")
image.Width = 200 ' In unit pixel (px)
image.Height = 200 ' In unit pixel (px)
Dim paragraph As New Paragraph()
' Add image
paragraph.AddImage(image)
' Add paragraph
doc.AddParagraph(paragraph)
' Export docx
doc.SaveAs("save_document.docx") ' docx file
Here, we are adding an image using IronWord.Models.Image with a Height and Width of 200 pixels to a paragraph.
IronWord requires a license to use. A trial key can be obtained from the Iron Software website here. This key needs to be placed in appsettings.json.
{
"IronWord.LicenseKey":"IRONWORD.MYLICENSE.KEY.TRIAL"
}
{
"IronWord.LicenseKey":"IRONWORD.MYLICENSE.KEY.TRIAL"
}
If True Then
"IronWord.LicenseKey":"IRONWORD.MYLICENSE.KEY.TRIAL"
End If
Provide your email to get a trial license. After you submit your email ID, the key will be delivered via email.
Creating Word documents in C# using the IronWord library provides a flexible and efficient way to generate documents without relying on Microsoft Office. Whether you need to create simple letters or complex reports with tables and images, IronWord allows you to achieve this programmatically. This article provides a comprehensive tutorial on creating Word documents. With IronWord, you have the power to automate the creation of Word documents, making your C# applications more versatile and productive.
And for developers looking for PDF file manipulation to work in conjunction with their generated Word documents, look no further than IronPDF another C# library produced by Iron Software. To check it out, click here.
9 .NET API products for your office documents