# 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.

- **Author:** Bjoern Meyer
- **Published:** 2025-02-07
- **Modified:** 2025-11-16
- **Description:** This article shows how to merge data into MS Word DOCX documents and convert them to PDF using TX Text Control .NET Server.
- **5 min read** (868 words)
- **Tags:**
  - ASP.NET
  - ASP.NET Core
  - PDF
  - Mail Merge
- **Web URL:** https://www.textcontrol.com/blog/2025/02/07/mail-merge-ms-word-docx-documents-and-convert-to-pdf-in-net-c-sharp/
- **LLMs URL:** https://www.textcontrol.com/blog/2025/02/07/mail-merge-ms-word-docx-documents-and-convert-to-pdf-in-net-c-sharp/llms.txt
- **LLMs-Full URL:** https://www.textcontrol.com/blog/2025/02/07/mail-merge-ms-word-docx-documents-and-convert-to-pdf-in-net-c-sharp/llms-full.txt

---

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](https://dotnet.microsoft.com/download/dotnet/8.0).

> ### Prerequisites
> 
> The following tutorial requires a trial version of TX Text Control .NET Server.
> 
> - [Download Trial Version](https://www.textcontrol.com/product/tx-text-control-dotnet-server/)

1. In Visual Studio 2022, create a new project by choosing *Create a new project*.
2. Select *Console App* as the project template and confirm with *Next*.
3. Choose a name for your project and confirm with *Next*.
4. In the next dialog, choose *.NET 8 (Long-term support)* as the *Framework* and confirm with *Create*.

### Adding the NuGet Package

5. 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*
    
    ![ASP.NET Core Web Application](https://s1-www.textcontrol.com/assets/dist/blog/2025/02/07/a/assets/visualstudio1.webp "ASP.NET Core Web Application")

### 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.

6. 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.

![MS Word Mail Merge Template](https://s1-www.textcontrol.com/assets/dist/blog/2025/02/07/a/assets/template.webp "MS Word Mail Merge Template")

### Mail Merge

7. Copy your MS Word template into the project folder and name it *template.docx*.
8. 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

9. 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:

![Generated PDF Document](https://s1-www.textcontrol.com/assets/dist/blog/2025/02/07/a/assets/invoice.webp "Generated 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.

---

## About Bjoern Meyer

As CEO, Bjoern is the visionary behind our strategic direction and business development, bridging the gap between our customers and engineering teams. His deep passion for coding and web technologies drives the creation of innovative products. If you're at a tech conference, be sure to stop by our booth - you'll most likely meet Bjoern in person. With an advanced graduate degree (Dipl. Inf.) in Computer Science, specializing in AI, from the University of Bremen, Bjoern brings significant expertise to his role. In his spare time, Bjoern enjoys running, paragliding, mountain biking, and playing the piano.

- [LinkedIn](https://www.linkedin.com/in/bjoernmeyer/)
- [X](https://x.com/txbjoern)
- [GitHub](https://github.com/bjoerntx)

---

## Related Posts

- [Why Structured E-Invoices Still Need Tamper Protection using C# and .NET](https://www.textcontrol.com/blog/2026/03/24/why-structured-e-invoices-still-need-tamper-protection-using-csharp-and-dotnet/llms.txt)
- [Create Fillable PDFs from HTML Forms in C# ASP.NET Core Using a WYSIWYG Template](https://www.textcontrol.com/blog/2026/03/17/create-fillable-pdfs-from-html-forms-in-csharp-aspnet-core-using-a-wysiwyg-template/llms.txt)
- [Why HTML to PDF Conversion is Often the Wrong Choice for Business Documents in C# .NET](https://www.textcontrol.com/blog/2026/03/13/why-html-to-pdf-conversion-is-often-the-wrong-choice-for-business-documents-in-csharp-dot-net/llms.txt)
- [A Complete Guide to Converting Markdown to PDF in .NET C#](https://www.textcontrol.com/blog/2026/01/07/a-complete-guide-to-converting-markdown-to-pdf-in-dotnet-csharp/llms.txt)
- [Why PDF Creation Belongs at the End of the Business Process](https://www.textcontrol.com/blog/2026/01/02/why-pdf-creation-belongs-at-the-end-of-the-business-process/llms.txt)
- [Designing the Perfect PDF Form with TX Text Control in .NET C#](https://www.textcontrol.com/blog/2025/12/16/designing-the-perfect-pdf-form-with-tx-text-control-in-dotnet-csharp/llms.txt)
- [Why Defining MIME Types for PDF/A Attachments Is Essential](https://www.textcontrol.com/blog/2025/12/10/why-defining-mime-types-for-pdfa-attachments-is-essential/llms.txt)
- [Validate Digital Signatures and the Integrity of PDF Documents in C# .NET](https://www.textcontrol.com/blog/2025/11/14/validate-digital-signatures-and-the-integrity-of-pdf-documents-in-csharp-dotnet/llms.txt)
- [Validate PDF/UA Documents and Verify Electronic Signatures in C# .NET](https://www.textcontrol.com/blog/2025/11/13/validate-pdf-ua-documents-and-verify-electronic-signatures-in-csharp-dotnet/llms.txt)
- [How To Choose the Right C# PDF Generation Library: Developer Checklist](https://www.textcontrol.com/blog/2025/11/12/how-to-choose-the-right-csharp-pdf-generation-library-developer-checklist/llms.txt)
- [Why Digitally Signing your PDFs is the Only Reliable Way to Prevent Tampering](https://www.textcontrol.com/blog/2025/10/30/why-digitally-signing-your-pdfs-is-the-only-reliable-way-to-prevent-tampering/llms.txt)
- [Automating PDF/UA Accessibility with AI: Describing DOCX Documents Using TX Text Control and LLMs](https://www.textcontrol.com/blog/2025/10/16/automating-pdf-ua-accessibility-with-ai-describing-docx-documents-using-tx-text-control-and-llms/llms.txt)
- [Converting Office Open XML (DOCX) to PDF in Java](https://www.textcontrol.com/blog/2025/10/14/converting-office-open-xml-docx-to-pdf-in-java/llms.txt)
- [Extending DS Server with Custom Digital Signature APIs](https://www.textcontrol.com/blog/2025/10/09/extending-ds-server-with-custom-digital-signature-apis/llms.txt)
- [Why PDF/UA and PDF/A-3a Matter: Accessibility, Archiving, and Legal Compliance](https://www.textcontrol.com/blog/2025/10/07/why-pdf-ua-and-pdf-a-3a-matter-accessibility-archiving-and-legal-compliance/llms.txt)
- [Convert Markdown to PDF in a Console Application on Linux and Windows](https://www.textcontrol.com/blog/2025/09/23/convert-markdown-to-pdf-in-a-console-application-on-linux-and-windows/llms.txt)
- [Mining PDFs with Regex in C#: Practical Patterns, Tips, and Ideas](https://www.textcontrol.com/blog/2025/08/12/mining-pdfs-with-regex-in-csharp-practical-patterns-tips-and-ideas/llms.txt)
- [Streamline Data Collection with Embedded Forms in C# .NET](https://www.textcontrol.com/blog/2025/08/02/streamline-data-collection-with-embedded-forms-in-csharp-dotnet/llms.txt)
- [Adding QR Codes to PDF Documents in C# .NET](https://www.textcontrol.com/blog/2025/07/15/adding-qr-codes-to-pdf-documents-in-csharp-dotnet/llms.txt)
- [Adding SVG Graphics to PDF Documents in C# .NET](https://www.textcontrol.com/blog/2025/07/08/adding-svg-graphics-to-pdf-documents-in-csharp-dotnet/llms.txt)
- [Enhancing PDF Searchability in Large Repositories by Adding and Reading Keywords Using C# .NET](https://www.textcontrol.com/blog/2025/06/24/enhancing-pdf-searchability-in-large-repositories-by-adding-and-reading-keywords-using-csharp-dotnet/llms.txt)
- [How to Verify PDF Encryption Programmatically in C# .NET](https://www.textcontrol.com/blog/2025/06/20/how-to-verify-pdf-encryption-programmatically-in-csharp-dotnet/llms.txt)
- [PDF Security for C# Developers: Encryption and Permissions in .NET](https://www.textcontrol.com/blog/2025/06/16/pdf-security-for-csharp-developers-encryption-and-permissions-in-dotnet/llms.txt)
- [Add JavaScript to PDFs with TX Text Control in C# .NET: Time-Based Alerts Made Easy](https://www.textcontrol.com/blog/2025/06/13/add-javascript-to-pdfs-with-tx-text-control-in-c-dot-net-time-based-alerts-made-easy/llms.txt)
- [Convert MS Word DOCX to PDF including Text Reflow using .NET C# on Linux](https://www.textcontrol.com/blog/2025/06/10/convert-ms-word-docx-to-pdf-including-text-reflow-using-dotnet-csharp-on-linux/llms.txt)
