Test in a live environment
Test in production without watermarks.
Works wherever you need it to.
In modern-day applications, it is crucial to generate Word documents on the fly for various purposes like billing, invoices, letters, etc. The Microsoft Word template document feature offers a powerful way to ensure consistency and efficiency. However, manually populating these templates can be time-consuming and prone to errors. That’s where IronWord from Iron Software comes in—a robust .NET library designed to automate the process of filling Word templates programmatically. In this article, we’ll walk through how to use IronWord to fill a Word document template and provide a practical example to illustrate the process.
IronWord is a .NET library from Iron Software designed to facilitate the creation, manipulation, and management of Microsoft Word documents programmatically. It allows developers to automate the process of generating Word documents, making it easier to dynamically create reports, invoices, letters, and other types of documents within their applications.
IronWord enables the use of Word templates to define placeholders in a template document and replace them with actual data at runtime.
You can easily insert, replace, or delete text within a Word document.
The library supports various formatting options, including font styles, sizes, colors, and paragraph alignment.
IronWord allows you to insert and manipulate tables and images within your documents.
It works seamlessly with different versions of Microsoft Word, ensuring compatibility and ease of use.
IronWord simplifies working with Word documents in .NET applications, making it a valuable tool for developers who want to automate document generation and management tasks.
A quick reminder to make sure you have the following before we get started:
Now, let us begin by creating a new Visual Studio project.
Select the console application template on the screen below.
Provide the project name and location.
Select the .NET Version, preferably the latest one with support, and click Create.
Install IronWord NuGet package from NuGet package manager as below in Visual Studio.
Alternatively, please install it using CLI directly using the command below.
dotnet add package IronWord --version 2024.9.1
dotnet add package IronWord --version 2024.9.1
'INSTANT VB TODO TASK: The following line uses invalid syntax:
'dotnet add package IronWord --version 2024.9.1
Now, generate a Word template document with one or two pages to be used during the Word document generation process.
Dear {Name},
Thanks for Purchasing {product}, happy to serve you always. Your application dated {Date} has been approved. The product comes with an expiry date of {expiryDate}. Renew the product on or before expiry date.
Fell Free to contact {phone} or {email} for further queries.
Address: {Address}
Thank you,
{Sender}
Dear {Name},
Thanks for Purchasing {product}, happy to serve you always. Your application dated {Date} has been approved. The product comes with an expiry date of {expiryDate}. Renew the product on or before expiry date.
Fell Free to contact {phone} or {email} for further queries.
Address: {Address}
Thank you,
{Sender}
Dear
If True Then
Name
End If
'INSTANT VB TODO TASK: Local functions are not converted by Instant VB:
', Thanks for Purchasing
'{
' product
'}
'INSTANT VB TODO TASK: Local functions are not converted by Instant VB:
', happy @to serve you always.Your application dated
'{
' @Date
'}
'INSTANT VB TODO TASK: Local functions are not converted by Instant VB:
'has been approved.The product comes @with an expiry @date @of
'{
' expiryDate
'}
'INSTANT VB TODO TASK: Local functions are not converted by Instant VB:
'.Renew the product on @or before expiry @date.Fell Free @to contact
'{
' phone
'}
'INSTANT VB TODO TASK: Local functions are not converted by Instant VB:
'@or {email} for further queries.Address:
'{
' Address
'}
Thank you,
If True Then
'INSTANT VB TODO TASK: The following line uses invalid syntax:
' Sender}
Now save the document above as Template.docx.
using IronWord;
class Program
{
static void Main()
{
License.LicenseKey = "your key";
// Define the path to the template and the output file object sender
string templatePath = "Template.docx";
string outputPath = "FilledDocument.docx";
// Create a new instance of the WordDocument class
WordDocument doc = new WordDocument(templatePath);
// Define a dictionary/ first table of placeholders and their replacements
var replacements = new Dictionary<string, string>
{
{ "{Name}", "John Doe" },
{ "{Date}", DateTime.Now.ToString("MMMM d, yyyy") },
{ "{Address}", "123 Iron Street, Iron Software" },
{ "{product}", "IronWord" },
{ "{Sender}", "IronSoftware" },
{ "{phone}", "+123 456789" },
{ "{email}", "sale@ironsoftware.com" },
{ "{expiryDate}", DateTime.Now.AddYears(1).ToString("MMMM d, yyyy") },
};
// Replace placeholders with actual data
foreach (var replacement in replacements)
{
doc.Texts.ForEach(x=>x.Replace(replacement.Key, replacement.Value));
}
// Save the filled document
doc.Save(outputPath);
Console.WriteLine("Document filled and saved successfully.");
}
}
using IronWord;
class Program
{
static void Main()
{
License.LicenseKey = "your key";
// Define the path to the template and the output file object sender
string templatePath = "Template.docx";
string outputPath = "FilledDocument.docx";
// Create a new instance of the WordDocument class
WordDocument doc = new WordDocument(templatePath);
// Define a dictionary/ first table of placeholders and their replacements
var replacements = new Dictionary<string, string>
{
{ "{Name}", "John Doe" },
{ "{Date}", DateTime.Now.ToString("MMMM d, yyyy") },
{ "{Address}", "123 Iron Street, Iron Software" },
{ "{product}", "IronWord" },
{ "{Sender}", "IronSoftware" },
{ "{phone}", "+123 456789" },
{ "{email}", "sale@ironsoftware.com" },
{ "{expiryDate}", DateTime.Now.AddYears(1).ToString("MMMM d, yyyy") },
};
// Replace placeholders with actual data
foreach (var replacement in replacements)
{
doc.Texts.ForEach(x=>x.Replace(replacement.Key, replacement.Value));
}
// Save the filled document
doc.Save(outputPath);
Console.WriteLine("Document filled and saved successfully.");
}
}
Imports IronWord
Friend Class Program
Shared Sub Main()
License.LicenseKey = "your key"
' Define the path to the template and the output file object sender
Dim templatePath As String = "Template.docx"
Dim outputPath As String = "FilledDocument.docx"
' Create a new instance of the WordDocument class
Dim doc As New WordDocument(templatePath)
' Define a dictionary/ first table of placeholders and their replacements
Dim replacements = New Dictionary(Of String, String) From {
{"{Name}", "John Doe"},
{"{Date}", DateTime.Now.ToString("MMMM d, yyyy")},
{"{Address}", "123 Iron Street, Iron Software"},
{"{product}", "IronWord"},
{"{Sender}", "IronSoftware"},
{"{phone}", "+123 456789"},
{"{email}", "sale@ironsoftware.com"},
{"{expiryDate}", DateTime.Now.AddYears(1).ToString("MMMM d, yyyy")}
}
' Replace placeholders with actual data
For Each replacement In replacements
doc.Texts.ForEach(Function(x) x.Replace(replacement.Key, replacement.Value))
Next replacement
' Save the filled document
doc.Save(outputPath)
Console.WriteLine("Document filled and saved successfully.")
End Sub
End Class
The provided code demonstrates using the IronWord library to fill a Word document template with specific data. Here’s a concise explanation:
Output
IronWord also allows adding various text effects, as shown in the table below.
In the following example, we add text effects to the Word Iron Software.
using IronWord;
using IronWord.Models;
class Program
{
static void Main()
{
License.LicenseKey = "your key";
// Define the path to the template and the output file
string templatePath = "Template.docx";
string outputPath = "FilledDocument.docx";
// Create a new instance of the WordDocument class
WordDocument doc = new WordDocument(templatePath);
// Define a dictionary of placeholders and their replacements
var replacements = new Dictionary<string, string>
{
{ "{Name}", "John Doe" },
{ "{Date}", DateTime.Now.ToString("MMMM d, yyyy") },
{ "{Address}", "123 Iron Street, Iron Software" },
{ "{product}", "IronWord" },
{ "{Sender}", "Sale," },
{ "{phone}", "+123 456789" },
{ "{email}", "sale@ironsoftware.com" },
{ "{expiryDate}", DateTime.Now.AddYears(1).ToString("MMMM d, yyyy") },
};
// Replace placeholders with actual data
foreach (var replacement in replacements)
{
doc.Texts.ForEach(x=>x.Replace(replacement.Key, replacement.Value));
}
// Save the filled document
//doc.Save(outputPath);
//Console.WriteLine("Document filled and saved successfully.");
// Create and configure text style methods
TextStyle textStyle = new TextStyle();
textStyle.TextEffect = new TextEffect()
{
GlowEffect = new Glow()
{
GlowColor = IronWord.Models.Color.Aqua,
GlowRadius = 10,
},
};
// Add text with style or image
doc.AddText(" IronSoftware").Style = textStyle;
// Export new Word document
doc.SaveAs("glowEffect.docx");
}
}
using IronWord;
using IronWord.Models;
class Program
{
static void Main()
{
License.LicenseKey = "your key";
// Define the path to the template and the output file
string templatePath = "Template.docx";
string outputPath = "FilledDocument.docx";
// Create a new instance of the WordDocument class
WordDocument doc = new WordDocument(templatePath);
// Define a dictionary of placeholders and their replacements
var replacements = new Dictionary<string, string>
{
{ "{Name}", "John Doe" },
{ "{Date}", DateTime.Now.ToString("MMMM d, yyyy") },
{ "{Address}", "123 Iron Street, Iron Software" },
{ "{product}", "IronWord" },
{ "{Sender}", "Sale," },
{ "{phone}", "+123 456789" },
{ "{email}", "sale@ironsoftware.com" },
{ "{expiryDate}", DateTime.Now.AddYears(1).ToString("MMMM d, yyyy") },
};
// Replace placeholders with actual data
foreach (var replacement in replacements)
{
doc.Texts.ForEach(x=>x.Replace(replacement.Key, replacement.Value));
}
// Save the filled document
//doc.Save(outputPath);
//Console.WriteLine("Document filled and saved successfully.");
// Create and configure text style methods
TextStyle textStyle = new TextStyle();
textStyle.TextEffect = new TextEffect()
{
GlowEffect = new Glow()
{
GlowColor = IronWord.Models.Color.Aqua,
GlowRadius = 10,
},
};
// Add text with style or image
doc.AddText(" IronSoftware").Style = textStyle;
// Export new Word document
doc.SaveAs("glowEffect.docx");
}
}
Imports IronWord
Imports IronWord.Models
Friend Class Program
Shared Sub Main()
License.LicenseKey = "your key"
' Define the path to the template and the output file
Dim templatePath As String = "Template.docx"
Dim outputPath As String = "FilledDocument.docx"
' Create a new instance of the WordDocument class
Dim doc As New WordDocument(templatePath)
' Define a dictionary of placeholders and their replacements
Dim replacements = New Dictionary(Of String, String) From {
{"{Name}", "John Doe"},
{"{Date}", DateTime.Now.ToString("MMMM d, yyyy")},
{"{Address}", "123 Iron Street, Iron Software"},
{"{product}", "IronWord"},
{"{Sender}", "Sale,"},
{"{phone}", "+123 456789"},
{"{email}", "sale@ironsoftware.com"},
{"{expiryDate}", DateTime.Now.AddYears(1).ToString("MMMM d, yyyy")}
}
' Replace placeholders with actual data
For Each replacement In replacements
doc.Texts.ForEach(Function(x) x.Replace(replacement.Key, replacement.Value))
Next replacement
' Save the filled document
'doc.Save(outputPath);
'Console.WriteLine("Document filled and saved successfully.");
' Create and configure text style methods
Dim textStyle As New TextStyle()
textStyle.TextEffect = New TextEffect() With {
.GlowEffect = New Glow() With {
.GlowColor = IronWord.Models.Color.Aqua,
.GlowRadius = 10
}
}
' Add text with style or image
doc.AddText(" IronSoftware").Style = textStyle
' Export new Word document
doc.SaveAs("glowEffect.docx")
End Sub
End Class
Explanation
The revised code illustrates using the IronWord library to fill out a Word document template, style text, and save the modified document. Here's a concise explanation:
This code demonstrates IronWord's document automation and customization features, including text replacement and styling.
Output
IronWord. Once the data is entered, the license is delivered to the email ID provided. This license needs to be placed at the beginning of the code, before using the IronWord library, as below.
License.LicenseKey = "your Key Here"
License.LicenseKey = "your Key Here"
'INSTANT VB TODO TASK: The following line uses invalid syntax:
'License.LicenseKey = "your Key Here"
IronWord offers several advantages for generating Word documents using templates. It simplifies document creation automation by allowing developers to programmatically fill out templates with specific data, reducing the need for manual input. This increases efficiency and accuracy, as the risk of human error is minimized. Additionally, IronWord helps maintain consistency across documents, ensuring that each generated file adheres to the same format and structure. Automating repetitive tasks saves time and resources, making it ideal for quickly producing large volumes of documents. IronWord enhances productivity and streamlines workflows in scenarios requiring frequent or complex document generation.
By following the steps outlined in this article and leveraging the provided example with IronWord, you can efficiently manage your document generation needs and streamline your workflow.
9 .NET API products for your office documents