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
Mining PDFs with Regex in C#: Practical Patterns, Tips, and Ideas
Mining PDFs with Regex in C# can be a powerful technique for extracting information from documents. This article explores practical patterns, tips, and ideas for effectively using regular…
Streamline Data Collection with Embedded Forms in C# .NET
Discover how to enhance your C# .NET applications by embedding forms for data collection. This article explores the benefits of using Text Control's ASP.NET and ASP.NET Core components to create…
Adding QR Codes to PDF Documents in C# .NET
This article explains how to add QR codes to PDF documents with the Text Control .NET Server component in C#. It provides the necessary steps and code snippets for effectively implementing this…
Adding SVG Graphics to PDF Documents in C# .NET
In this article, we will explore how to add SVG graphics to PDF documents using C# .NET. We will use the TX Text Control .NET Server component to demonstrate the process of rendering SVG images in…
Enhancing PDF Searchability in Large Repositories by Adding and Reading…
This article explores how to improve the searchability of PDF documents in large repositories by adding and reading keywords with C# .NET. This is especially helpful for applications that manage…