USING IRONWORD

How to Create Word Documents Without Office Interop in C#

Published March 12, 2024
Share:

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.

How to Create Word Documents Without Office Interop in C#

  1. Create a new C# project.
  2. Install the IronWord library.
  3. Create a Word document using the IronWord library.
  4. Add content to the now existing document.
  5. Save the created Word document.
  6. Open and display the created Word document.

Prerequisites:

  1. Visual Studio: Ensure you have Visual Studio or any other C# development environment installed.
  2. NuGet Package Manager: Make sure you can use NuGet to manage packages in your project

Step 1: Create a New C# Project

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.

How to Create Word Documents Without Office Interop in C#: Figure 1 - Console App template for creating a new project

In the next step, you can provide the solution and project names.

How to Create Word Documents Without Office Interop in C#: Figure 2 - Configuring project with solution and project name

Select the .NET Version and click "Create".

How to Create Word Documents Without Office Interop in C#: Figure 3 - Creating project with correct .NET version

Step 2: Install the IronWord Library

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.

How to Create Word Documents Without Office Interop in C#: Figure 4 - Installing IronWord with the VS package manager

Step 3: Create and Save a Word Document using the IronWord Library

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
VB   C#

After executing the above code example, the new document file, example.docx, is created and the output is as shown below.

How to Create Word Documents Without Office Interop in C#: Figure 5 - Word document created through the code above.

This is a fundamental example of generating a Word document file using IronWord. For more information, you can refer to the documents here.

Adding Paragraphs with Style to a Word document.

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
VB   C#

Adding a Table to a Word document

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
VB   C#

In the example above, we added a table to a Word document using borders.

Adding Images to a Word document

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
VB   C#

Here, we are adding an image using IronWord.Models.Image with a Height and Width of 200 pixels to a paragraph.

Licensing (Free Trial Available)

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
VB   C#

Provide your email to get a trial license. After you submit your email ID, the key will be delivered via email.

How to Create Word Documents Without Office Interop in C#: Figure 6 - Popup upon a successfully submitted trial form

Conclusion

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.

< PREVIOUS
How to Create Word Document Programmatically in VB .NET
NEXT >
How to Read Word Document With Formatting in C#

Ready to get started? Version: 2024.10 just released

Free NuGet Download Total downloads: 6,643 View Licenses >