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
Using QR Codes in PDF Documents in C# .NET
QR codes are a powerful tool for embedding machine-readable information in documents. In this article, we will explore how to generate and insert them into PDF documents using C# .NET with TX Text…
ASP.NETASP.NET CoreE-Invoicing
Why Structured E-Invoices Still Need Tamper Protection using C# and .NET
ZUGFeRD, Factur-X, German e-invoicing rules, and how to seal PDF invoices with TX Text Control to prevent tampering. Learn how to create compliant e-invoices with C# and .NET.
Create Fillable PDFs from HTML Forms in C# ASP.NET Core Using a WYSIWYG Template
Learn how to generate PDFs from HTML forms in ASP.NET Core using a pixel-perfect WYSIWYG template. Extract form fields from a document, render a dynamic HTML form, and merge the data server-side…
Why HTML to PDF Conversion is Often the Wrong Choice for Business Documents…
In this article, we explore the challenges of HTML to PDF conversion for business documents in C# .NET and present alternative solutions that offer better performance and reliability. Discover why…
A Complete Guide to Converting Markdown to PDF in .NET C#
Learn how to convert Markdown to PDF in .NET C# using Text Control's ServerTextControl component. This guide covers setup, conversion process, and customization options for generating high-quality…
