How to generate invoices with dynamic text color using mail merge in a Word document?
Syncfusion® Essential® DocIO is a .NET Word library used to create, read, edit, and convert Word documents programmatically without Microsoft Word or interop dependencies. Using this library, you can generate invoices with dynamic text color using mail merge in a Word document using C#.
To achieve this, use the MergeField event to customize the formatting of merge fields during the mail merge process. Store the desired text color in a column (e.g., FontColor) within your data table and apply it dynamically based on the current row index.
Steps to generate invoices with dynamic text color using mail merge in a Word document:
- Create a new .NET Core console application project.
- Install the Syncfusion.DocIO.Net.Core NuGet package as a reference to your project from NuGet.org.
Note:
Starting with v16.2.0.x, if you reference Syncfusion® assemblies from a trial setup or from the NuGet feed, include a license key in your projects. Refer to the link to learn about generating and registering a Syncfusion® license key in your application to use the components without a trial message.
- Include the following namespaces in the Program.cs file.
C#
using Syncfusion.DocIO.DLS;
using Syncfusion.DocIO;
- Use the following code example to generate invoices with dynamic text color using mail merge in a Word document.
C#
// Holds row index to color mapping
Dictionary<int, object> TextColors = new Dictionary<int, object>();
// Load the Word document
using (WordDocument document = new WordDocument(Path.GetFullPath(@"Data/Template.docx")))
{
// Get invoice data
DataTable invoiceTable = GetInvoiceData();
// Store color info by row index
for (int i = 0; i < invoiceTable.Rows.Count; i++)
TextColors.Add(i, invoiceTable.Rows[i]["FontColor"]);
// Hook merge event to apply font color
document.MailMerge.MergeField += ApplyColorToFields;
// Enable separate page for each invoice
document.MailMerge.StartAtNewPage = true;
// Perform mail merge
document.MailMerge.ExecuteGroup(invoiceTable);
// Save the modified document
document.Save(Path.GetFullPath(@"Output/Result.docx"), FormatType.Docx);
}
- Use the following code example to apply text color based on the row.
C#
// Event handler to apply text color based on row
void ApplyColorToFields(object sender, MergeFieldEventArgs args)
{
if (TextColors.TryGetValue(args.RowIndex, out object color))
args.TextRange.CharacterFormat.TextColor = (Color)color;
}
- Use the following code example to generate invoice data.
C#
DataTable GetInvoiceData()
{
DataTable table = new DataTable("Invoice");
table.Columns.Add("InvoiceNumber");
table.Columns.Add("InvoiceDate");
table.Columns.Add("CustomerName");
table.Columns.Add("ItemDescription");
table.Columns.Add("Amount");
table.Columns.Add("FontColor", typeof(Color));
// First Invoice
table.Rows.Add("INV001", "2024-05-01", "Andy Bernard", "Consulting Services", "$3000.00", Color.Teal);
// Second Invoice
table.Rows.Add("INV002", "2024-05-05", "Stanley Hudson", "Software Development", "$4500.00", Color.DarkOrange);
// Third Invoice
table.Rows.Add("INV003", "2024-05-10", "Margaret Peacock", "UI Design Services", "$2000.00", Color.Indigo);
return table;
}
You can download a complete working sample to generate invoices with dynamic text color using mail merge in a Word document from GitHub.
Take a moment to peruse the documentation where you can find basic Word document processing options along with features like mail merge, merge, split, and compare Word documents, find and replace text in the Word document, protect the Word documents, and most importantly, the PDF and Image conversions with code examples.
Conclusion
I hope you enjoyed learning about how to generate invoices with dynamic text color using mail merge in a Word document in a .NET Core Word document.
You can refer to our ASP.NET Core DocIO feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started with configuration specifications. You can also explore our ASP.NET Core DocIO example to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!