Test in a live environment
Test in production without watermarks.
Works wherever you need it to.
Documents are increasingly becoming digital in the digital age. Documents in the business setting are often associated with Microsoft Word and its document-based Word file. Writing and editing Microsoft Word documents has become the foundation for passing information around in large organizations. However, manually creating and editing documents can be a painful process; to automate Word generation programmatically is no easy feat either, as many open-source libraries rely on Microsoft Office Word for dependency.
However, managing and creating Word documents doesn't need to be this difficult. IronWord is a C# Word Library that is not dependent on Microsoft Office Word and allows users to customize the entire document while also allowing document generation and creating document templates programmatically.
In today's tutorial, I'll briefly explain how to programmatically create a Microsoft Word Document using IronWord and provide brief examples.
Broken image Add from Pixabay, select from your files or drag and drop an image here.
IronWord is an exceptionally reliable and user-friendly C# Docx library that empowers developers to construct and modify Word documents using C, all without needing traditional dependencies such as Microsoft Office or Word Interlope. Additionally, it boasts extensive documentation and provides full support for .NET 8, 7, 6, Framework, Core, and Azure, making it universally compatible with most applications. This level of flexibility makes it an excellent choice for any application you may be dealing with.
For this example, we'll create a console app using Visual Studio and showcase how to create blank Word documents and manipulate them using the IronWord Library. Before we proceed with this next step, ensure that you have Visual Studio installed.
First, let's create a new console app.
Broken image Add from Pixabay, select from your files or drag and drop an image here.
Then, provide a project name and save the location, as shown below.
Broken image Add from Pixabay, select from your files or drag and drop an image here.
Finally, select the desired framework.
Broken image Add from Pixabay, select from your files or drag and drop an image here.
After creating the blank console app, we'll download IronWord through NuGet Package Manager.
Click on "Manage NuGet Packages," then search IronWord in the "Browse Tab" as shown below.
Broken image Add from Pixabay, select from your files or drag and drop an image here.
Afterward, install it onto the newly created project.
Alternatively, you can type the following command in the command line to install IronWord.
Install-Package IronWord
Install-Package IronWord
'INSTANT VB TODO TASK: The following line uses invalid syntax:
'Install-Package IronWord
Now that everything is set up and ready to go, let's move to an example of creating Word documents.
Please remember that IronWord requires a licensing key for operation. You can get a key as part of a free trial by visiting this link.
//Replace the license key variable with the trial key you obtained
IronWord.License.LicenseKey = "REPLACE-WITH-YOUR-KEY";
//Replace the license key variable with the trial key you obtained
IronWord.License.LicenseKey = "REPLACE-WITH-YOUR-KEY";
IRON VB CONVERTER ERROR developers@ironsoftware.com
After receiving a trial key, set this variable in your project.
using IronWord;
using IronWord.Models;
// Create textrun
Text textRun = new Text("Sample text");
Paragraph paragraph = new Paragraph();
paragraph.AddChild(textRun);
// Create a new Word document
WordDocument doc = new WordDocument(paragraph);
// Export docx
doc.SaveAs("document.docx");
using IronWord;
using IronWord.Models;
// Create textrun
Text textRun = new Text("Sample text");
Paragraph paragraph = new Paragraph();
paragraph.AddChild(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.AddChild(textRun)
' Create a new Word document
Dim doc As New WordDocument(paragraph)
' Export docx
doc.SaveAs("document.docx")
In the previous example, we created a Word document with basic text. Let's do an example with more advanced features, such as customizing the text and adding text effects.
using IronWord;
using IronWord.Models;
#region
IronWord.License.LicenseKey = "YOUR-KEY";
#endregion
// Load docx
WordDocument doc = new WordDocument("document.docx");
// Configure text
Text introText = new Text("This is an example paragraph with italic and bold styling.");
TextStyle italicStyle = new TextStyle()
{
IsItalic = true
};
Text italicText = new Text("Italic example sentence.");
italicText.Style = italicStyle;
TextStyle boldStyle = new TextStyle()
{
IsBold = true
};
Text boldText = new Text("Bold example sentence.");
boldText.Style = boldStyle;
Paragraph paragraph = new Paragraph();
// Add text
paragraph.AddText(introText);
paragraph.AddText(italicText);
paragraph.AddText(boldText);
// Add paragraph
doc.AddParagraph(paragraph);
// Export docx
doc.SaveAs("save_document.docx");
using IronWord;
using IronWord.Models;
#region
IronWord.License.LicenseKey = "YOUR-KEY";
#endregion
// Load docx
WordDocument doc = new WordDocument("document.docx");
// Configure text
Text introText = new Text("This is an example paragraph with italic and bold styling.");
TextStyle italicStyle = new TextStyle()
{
IsItalic = true
};
Text italicText = new Text("Italic example sentence.");
italicText.Style = italicStyle;
TextStyle boldStyle = new TextStyle()
{
IsBold = true
};
Text boldText = new Text("Bold example sentence.");
boldText.Style = boldStyle;
Paragraph paragraph = new Paragraph();
// Add text
paragraph.AddText(introText);
paragraph.AddText(italicText);
paragraph.AddText(boldText);
// Add paragraph
doc.AddParagraph(paragraph);
// Export docx
doc.SaveAs("save_document.docx");
Imports IronWord
Imports IronWord.Models
#Region ""
IronWord.License.LicenseKey = "YOUR-KEY"
'#End Region
' Load docx
Dim doc As New WordDocument("document.docx")
' Configure text
Dim introText As New Text("This is an example paragraph with italic and bold styling.")
Dim italicStyle As New TextStyle() With {.IsItalic = True}
Dim italicText As New Text("Italic example sentence.")
italicText.Style = italicStyle
Dim boldStyle As New TextStyle() With {.IsBold = True}
Dim boldText As New Text("Bold example sentence.")
boldText.Style = boldStyle
Dim paragraph As New Paragraph()
' Add text
paragraph.AddText(introText)
paragraph.AddText(italicText)
paragraph.AddText(boldText)
' Add paragraph
doc.AddParagraph(paragraph)
' Export docx
doc.SaveAs("save_document.docx")
The example above showcases the fonts and styles developers can use using IronWord, aside from italics and bold text effects. IronWord also offers other features to ensure developers have all the options to create unique Word documents in all scenarios.
Our demonstrations showed how easy it is to use the IronWord library to create Word documents in C# programmatically. The library's flexibility and scalability make it a valuable tool for developers in real-life scenarios, such as customizing text, font, and style in a Word document. Understanding how Word integrates with other applications provides developers additional solutions to their challenges.
IronWord provides a free trial license.
9 .NET API products for your office documents