Test in production without watermarks.
Works wherever you need it to.
Get 30 days of fully functional product.
Have it up and running in minutes.
Full access to our support engineering team during your product trial
Text alignment is crucial when displaying text in a GUI application like a Microsoft Word document. Indeed, there are many libraries that can align text. Still, the IronWord NuGet package emerges as a powerful tool to streamline this process, providing a simple and effective way to manage format or alignment in your C# applications. Developers often encounter the need to align text in various ways, whether for reports or console applications, in all these places IronWord NuGet can be readily used. Now let's dig deep into the IronWord NuGet package and how to align text or paragraphs using this package.
IronWord is a C# NuGet library from Iron Software developed to facilitate the creation, manipulation, and management of Microsoft Word files programmatically. It allows developers to automate the process of generating Word documents with aligned text, making it easier to dynamically generate reports, invoices, letters, and other types of documents within their applications.
IronWord supports multiple alignment types, including left, right, center, and justified text alignment. This versatility allows developers to choose the most appropriate format for their specific requirements.
The package simplifies the process of aligning text in columns, making it particularly useful for generating reports or displaying data in a structured format.
IronWord allows developers to customize the width of the text fields, ensuring that the aligned text fits neatly within specified boundaries.
You can easily insert, replace, or delete text within a Word document.
The library supports various formatting options, including font name styles, font sizes, colors, and 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 the IronWord NuGet package from the 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
The below code example shows how to generate a Word document with center-aligned text.
using IronWord;
using IronWord.Models;
class Program
{
static void Main()
{
// Set your license key for IronWord
License.LicenseKey = "your key";
// Create a new document
var document = new WordDocument();
// Create a paragraph and add text
Paragraph paragraph = new Paragraph();
Text text = new Text() { Text = "IronWord, From IronSoftware" };
paragraph.AddText(text); // Add text to the paragraph
// Set text alignment to center aligned
paragraph.Alignment = IronWord.Models.Enums.TextAlignment.Center;
// Add the first paragraph to the document
document.AddParagraph(paragraph);
// Save the document with the specified filename
document.SaveAs("output.docx");
}
}
using IronWord;
using IronWord.Models;
class Program
{
static void Main()
{
// Set your license key for IronWord
License.LicenseKey = "your key";
// Create a new document
var document = new WordDocument();
// Create a paragraph and add text
Paragraph paragraph = new Paragraph();
Text text = new Text() { Text = "IronWord, From IronSoftware" };
paragraph.AddText(text); // Add text to the paragraph
// Set text alignment to center aligned
paragraph.Alignment = IronWord.Models.Enums.TextAlignment.Center;
// Add the first paragraph to the document
document.AddParagraph(paragraph);
// Save the document with the specified filename
document.SaveAs("output.docx");
}
}
Imports IronWord
Imports IronWord.Models
Friend Class Program
Shared Sub Main()
' Set your license key for IronWord
License.LicenseKey = "your key"
' Create a new document
Dim document = New WordDocument()
' Create a paragraph and add text
Dim paragraph As New Paragraph()
Dim text As New Text() With {.Text = "IronWord, From IronSoftware"}
paragraph.AddText(text) ' Add text to the paragraph
' Set text alignment to center aligned
paragraph.Alignment = IronWord.Models.Enums.TextAlignment.Center
' Add the first paragraph to the document
document.AddParagraph(paragraph)
' Save the document with the specified filename
document.SaveAs("output.docx")
End Sub
End Class
Code Explanation
WordDocument
instance.IronWord.Models.Enums.TextAlignment.Center
.WordDocument
instance and save the document to output.docx
.Output
Now let us try different options.
using IronWord;
using IronWord.Models;
using IronWord.Models.Enums;
class Program
{
static void Main()
{
// Set your license key for IronWord
License.LicenseKey = "your key";
// Create a new DOCX document
var doc = new WordDocument();
// Sample text for alignment
Text text = new Text() { Text = "IronWord, From IronSoftware" };
// Add paragraphs with different alignments
AddPara(doc, text, TextAlignment.Center);
AddPara(doc, text, TextAlignment.Left);
AddPara(doc, text, TextAlignment.Right);
AddPara(doc, text, TextAlignment.Justified);
// Save the document with all alignment options
doc.SaveAs("outputAllOptions.docx");
}
// Method to add a paragraph to the document with specified alignment
private static void AddPara(WordDocument doc, Text text, TextAlignment alignment)
{
Paragraph paragraph = new Paragraph();
paragraph.AddText(text);
paragraph.Alignment = alignment;
doc.AddParagraph(paragraph);
}
}
using IronWord;
using IronWord.Models;
using IronWord.Models.Enums;
class Program
{
static void Main()
{
// Set your license key for IronWord
License.LicenseKey = "your key";
// Create a new DOCX document
var doc = new WordDocument();
// Sample text for alignment
Text text = new Text() { Text = "IronWord, From IronSoftware" };
// Add paragraphs with different alignments
AddPara(doc, text, TextAlignment.Center);
AddPara(doc, text, TextAlignment.Left);
AddPara(doc, text, TextAlignment.Right);
AddPara(doc, text, TextAlignment.Justified);
// Save the document with all alignment options
doc.SaveAs("outputAllOptions.docx");
}
// Method to add a paragraph to the document with specified alignment
private static void AddPara(WordDocument doc, Text text, TextAlignment alignment)
{
Paragraph paragraph = new Paragraph();
paragraph.AddText(text);
paragraph.Alignment = alignment;
doc.AddParagraph(paragraph);
}
}
Imports IronWord
Imports IronWord.Models
Imports IronWord.Models.Enums
Friend Class Program
Shared Sub Main()
' Set your license key for IronWord
License.LicenseKey = "your key"
' Create a new DOCX document
Dim doc = New WordDocument()
' Sample text for alignment
Dim text As New Text() With {.Text = "IronWord, From IronSoftware"}
' Add paragraphs with different alignments
AddPara(doc, text, TextAlignment.Center)
AddPara(doc, text, TextAlignment.Left)
AddPara(doc, text, TextAlignment.Right)
AddPara(doc, text, TextAlignment.Justified)
' Save the document with all alignment options
doc.SaveAs("outputAllOptions.docx")
End Sub
' Method to add a paragraph to the document with specified alignment
Private Shared Sub AddPara(ByVal doc As WordDocument, ByVal text As Text, ByVal alignment As TextAlignment)
Dim paragraph As New Paragraph()
paragraph.AddText(text)
paragraph.Alignment = alignment
doc.AddParagraph(paragraph)
End Sub
End Class
Code Explanation
WordDocument
instance.WordDocument
instance.outputAllOptions.docx
.Output
IronWord library is one of the products of Iron Suite from Iron Software. It needs a license to run. A user can download a trial license using their email ID from the trial license page. Once the data is entered, the license is delivered to the email ID of the user. This license needs to be placed at the beginning of the user code before using the IronWord library, as below.
License.LicenseKey = "your Key Here";
License.LicenseKey = "your Key Here";
License.LicenseKey = "your Key Here"
The IronWord NuGet package simplifies the process of working with Word documents in .NET applications. Its intuitive API allows developers to easily manipulate text alignment and other document elements. Whether you need to align text to the left, center, or right, or justify it, IronWord provides the tools you need to create professional and well-formatted documents.
IronWord is a C# NuGet library developed by Iron Software to facilitate the creation, manipulation, and management of Microsoft Word files programmatically. It enables developers to automate the process of generating Word documents with aligned text.
You can install IronWord through the NuGet package manager in Visual Studio or by using the CLI command: dotnet add package IronWord --version 2024.9.1.
IronWord supports multiple text alignment options, including left, right, center, and justified alignment.
Yes, IronWord simplifies the process of aligning text in columns, which is useful for generating structured data reports.
IronWord can be used for various applications, such as report generation, invoice creation, contract management, and generating personalized letters and notices.
Yes, IronWord allows you to insert and manipulate tables and images within your Word documents.
IronWord works seamlessly with different versions of Microsoft Word, ensuring compatibility and ease of use.
Before using IronWord, ensure that Visual Studio and the latest .NET Framework are installed on your machine.
You can set the license key for IronWord in your code by using: License.LicenseKey = "your Key Here".
Key features of IronWord include support for multiple text alignments, customizable formatting, text manipulation, and compatibility with different Word versions.