Mail Merge MS Word DOCX Documents and Convert to PDF in .NET C#
This article shows how to merge data into MS Word DOCX documents and convert them to PDF using TX Text Control .NET Server.

TX Text Control's mail merge feature enables automated document generation by merging templates with structured data, such as from databases or JSON. It supports merge fields, conditional logic, and barcode integration, allowing users to dynamically create personalized reports, invoices, or letters.
This allows business users to create WYSIWYG templates using standard MS Word DOCX documents, eliminating the need for complex reporting tools or developer expertise. These templates can include merge fields, tables, conditional content, and images, making them highly flexible for creating personalized documents.
Why Use TX Text Control for Mail Merge?
The key benefit is pixel-perfect PDF output - the resulting documents look exactly like the original DOCX template, ensuring 100% layout fidelity. This means that business users can visually design templates in MS Word or the TX Text Control Document Editor available for any browser, preview them as they will appear, and then merge structured data (e.g., from a database, JSON, or XML) to create consistent, professional documents.
Because the rendering engine faithfully reproduces document layout in both screen and print output, it eliminates the common formatting inconsistencies found in traditional reporting solutions. The result is high-quality, accurate PDF documents that stay true to the original design - ideal for invoices, contracts, reports, and business correspondence.
This tutorial shows how to perform mail merge on an MS Word document with JSON data and convert the results to a PDF document.
Creating the Application
To demonstrate how easy this is with the TX Text Control library, we will use a .NET console application.
Make sure that you downloaded the latest version of Visual Studio 2022 that comes with the .NET 8 SDK.
Prerequisites
The following tutorial requires a trial version of TX Text Control .NET Server.
-
In Visual Studio 2022, create a new project by choosing Create a new project.
-
Select Console App as the project template and confirm with Next.
-
Choose a name for your project and confirm with Next.
-
In the next dialog, choose .NET 8 (Long-term support) as the Framework and confirm with Create.
Adding the NuGet Package
-
In the Solution Explorer, select your created project and choose Manage NuGet Packages... from the Project main menu.
Select Text Control Offline Packages from the Package source drop-down.
Install the latest versions of the following package:
- TXTextControl.TextControl.ASP.SDK

Data Object
For this tutorial, we will use a simple Invoice object that represents an invoice with a customer name, address, and a list of items with a description and price.
-
Create a new class file in your project and name it InvoiceData.cs. Copy and paste the following code into the file:
public class Invoice { public string InvoiceNumber { get; set; } public DateTime InvoiceDate { get; set; } public DateTime DueDate { get; set; } public decimal AmountDue { get; set; } public Customer Customer { get; set; } = new Customer(); public List<LineItem> LineItems { get; set; } = new List<LineItem>(); } public class Customer { public string CustomerName { get; set; } public string CustomerAddress { get; set; } } public class LineItem { public string Item { get; set; } public string Description { get; set; } public int Quantity { get; set; } public decimal Price { get; set; } public decimal Total { get; set; } }
Template
For this tutorial, we will use a simple MS Word template that includes merge fields for the invoice data. The template contains simple text fields and a table for the line items.

Mail Merge
-
Copy your MS Word template into the project folder and name it template.docx.
-
Find the Program.cs file in your project and replace the content with the following code:
using TXTextControl.DocumentServer; using TXTextControl; // Create invoice object Invoice invoice = CreateInvoice(); // Process document generation GenerateInvoiceDocument(invoice, "template.docx", "output.pdf"); static Invoice CreateInvoice() { return new Invoice { InvoiceNumber = "12345", InvoiceDate = DateTime.Parse("2020-01-01"), DueDate = DateTime.Parse("2020-01-31"), AmountDue = 123.45m, // Use decimal for currency Customer = new Customer { CustomerName = "John Doe", CustomerAddress = "123 Main St., Springfield, IL 62701" }, LineItems = new List<LineItem> { new LineItem { Item = "1", Description = "Widget", Quantity = 2, // Use integer for quantity Price = 45.00m, // Use decimal for price Total = 90.00m }, new LineItem { Item = "2", Description = "Gadget", Quantity = 1, Price = 78.45m, Total = 78.45m } } }; } static void GenerateInvoiceDocument(Invoice invoice, string templatePath, string outputPath) { using (ServerTextControl tx = new ServerTextControl()) { tx.Create(); var loadSettings = new LoadSettings { ApplicationFieldFormat = ApplicationFieldFormat.MSWord, LoadSubTextParts = true }; tx.Load(templatePath, StreamType.WordprocessingML, loadSettings); using (MailMerge mailMerge = new MailMerge { TextComponent = tx }) { mailMerge.MergeObject(invoice); } tx.Save(outputPath, StreamType.AdobePDF); } }
Run the Application
-
Run the application by pressing F5. When the application is finished, you will find the generated PDF document in the project folder.
The following screenshot shows the resulting PDF document:

Conclusion
TX Text Control's mail merge feature allows business users to create personalized documents by merging templates with structured data. This tutorial demonstrates how to perform a mail merge on an MS Word document with JSON data and convert the results to a PDF document using TX Text Control .NET Server.
ASP.NET
Integrate document processing into your applications to create documents such as PDFs and MS Word documents, including client-side document editing, viewing, and electronic signatures.
- Angular
- Blazor
- React
- JavaScript
- ASP.NET MVC, ASP.NET Core, and WebForms
Related Posts
Automating PDF/UA Accessibility with AI: Describing DOCX Documents Using TX…
This article shows how to use TX Text Control together with the OpenAI API to automatically add descriptive texts (alt text and labels) to images, links, and tables in a DOCX. The resulting…
Converting Office Open XML (DOCX) to PDF in Java
Learn how to convert Office Open XML (DOCX) documents to PDF in Java using the powerful ServerTextControl library. This guide provides step-by-step instructions and code examples to help you…
Extending DS Server with Custom Digital Signature APIs
In this article, we will explore how to extend the functionality of DS Server by integrating custom digital signature APIs. We will cover the necessary steps to create a plugin that allows DS…
Why PDF/UA and PDF/A-3a Matter: Accessibility, Archiving, and Legal Compliance
It is more important than ever to ensure that documents are accessible, archivable, and legally compliant. PDF/UA and PDF/A-3a are two effective standards for addressing these needs. This article…
Convert Markdown to PDF in a Console Application on Linux and Windows
Learn how to convert Markdown files to PDF in a console application on Linux and Windows using TX Text Control .NET Server for ASP.NET. This tutorial provides step-by-step instructions and code…
