# Generate PDF Documents from MS Word DOCX Templates in ASP.NET Core C#

> A very efficient way to create pixel-perfect, error-free documents in an automated process is to create PDFs from MS Word templates. This article shows how to use the TX Text Control libraries to merge MS Word templates with JSON data to generate PDFs in an ASP.NET Core application.

- **Author:** Bjoern Meyer
- **Published:** 2023-10-20
- **Modified:** 2026-07-17
- **Description:** A very efficient way to create pixel-perfect, error-free documents in an automated process is to create PDFs from MS Word templates. This article shows how to use the TX Text Control libraries to merge MS Word templates with JSON data to generate PDFs in an ASP.NET Core application.
- **4 min read** (668 words)
- **Tags:**
  - ASP.NET
  - ServerTextControl
  - PDF
  - MS Word
- **Web URL:** https://www.textcontrol.com/blog/2023/10/20/generate-pdf-documents-from-ms-word-docx-templates-in-aspnet-core-csharp/
- **LLMs URL:** https://www.textcontrol.com/blog/2023/10/20/generate-pdf-documents-from-ms-word-docx-templates-in-aspnet-core-csharp/llms.txt
- **LLMs-Full URL:** https://www.textcontrol.com/blog/2023/10/20/generate-pdf-documents-from-ms-word-docx-templates-in-aspnet-core-csharp/llms-full.txt

---

Creating PDFs from MS Word templates is a very efficient way to create pixel-perfect, error-free documents in an automated process. Unlike other PDF tools, TX Text Control can be used to create PDF documents from scratch or to programmatically prepare them from existing MS Word templates by merging placeholders and JSON data.

TX Text Control provides MS Word-inspired Document Editor components for Angular, JavaScript, and ASP.NET Core for template editing and creation. You can also use pre-designed MS Word DOC, RTF, and DOCX templates to create PDF files.

### Mail Merge Engine

The MailMerge class provides efficient ways to merge data into merge field placeholders, repeating blocks, form fields, bar codes, images, and chart objects.

Typically, an MS Word template is composed of static data, a layout design, and dynamic placeholders known as merge fields. Consider the following simple MS Word DOCX template displayed in the TX Text Control Document Editor.

[![Creating documents with TX Text Control](https://s1-www.textcontrol.com/assets/dist/blog/2023/10/20/b/assets/template.webp "Creating documents with TX Text Control")](https://s1-www.textcontrol.com/assets/dist/blog/2023/10/20/b/assets/template.webp)

After designing a template, the *MailMerge* reporting engine is used to populate merge fields with actual hierarchical data from JSON or business objects. A document is created for each data row of the master table in the data source. Merge fields are populated with column values. Repeating blocks are merged with data rows from related child tables in the data source.

### JSON Hierarchical Data

The following is an example of the JSON data that is used for the merge process.

```
[
  {
    "name": "Peter",
    "firstname": "Parker",
    "age": 18,
    "city": "New York",
    "items":  [
      {
        "name": "Spiderman",
        "price": 1000
      },
      {
        "name": "Web",
        "price": 10
      },
      {
        "name": "Spider",
        "price": 1
      }
    ]
  }
]
```

After the JSON data has been merged into the template, all of the placeholders and merge blocks will be filled in. The following screenshot shows the result PDF document.

![Creating documents with TX Text Control](https://s1-www.textcontrol.com/assets/dist/blog/2023/10/20/b/assets/results.webp "Creating documents with TX Text Control")

### Preparing the Application

For the purposes of this demo, a .NET 6 console application is built.

1. In Visual Studio, create a new Console App using .NET 6.
2. 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
    
    ![Create PDF](https://s1-www.textcontrol.com/assets/dist/blog/2023/10/20/b/assets/step1.webp "Create PDF")

### Template and Data

3. In the *Solution Explorer*, select the project and click *Project -> Add New Item...*. Select *JSON File*, name it *data.json* and confirm with *Add*. Set the *Copy to Output Directory* property to *Copy always*.
4. Copy the following JSON data into the file:
    
    ```
    [
      {
        "name": "Peter",
        "firstname": "Parker",
        "age": 18,
        "city": "New York",
        "items":  [
          {
            "name": "Spiderman",
            "price": 1000
          },
          {
            "name": "Web",
            "price": 10
          },
          {
            "name": "Spider",
            "price": 1
          }
        ]
      }
    ]
    ```
5. Download and unzip the following template and add it to your project. Set the *Copy to Output Directory* property to *Copy always*.
    
    [template.zip](https://s1-www.textcontrol.com/assets/dist/blog/2023/10/20/b/assets/template.zip)

### Adding the Code

6. Open the *Program.cs* file and add the following code:
    
    ```
    using TXTextControl.DocumentServer.Fields;
    
    using (TXTextControl.ServerTextControl tx = new TXTextControl.ServerTextControl())
    {
        tx.Create();
    
        TXTextControl.LoadSettings ls = new TXTextControl.LoadSettings()
        {
            ApplicationFieldFormat = TXTextControl.ApplicationFieldFormat.MSWord,
            LoadSubTextParts = true
        };
    
        tx.Load("template.docx", TXTextControl.StreamType.WordprocessingML, ls);
    
        using (TXTextControl.DocumentServer.MailMerge mailMerge =
          new TXTextControl.DocumentServer.MailMerge())
        {
            var jsonData = System.IO.File.ReadAllText("data.json");
    
            mailMerge.TextComponent = tx;
            mailMerge.MergeJsonData(jsonData);
        }
    
        tx.Save("output.pdf", TXTextControl.StreamType.AdobePDF);
    }
    ```

### Running the Application

7. Run the application and check the output folder for the generated PDF document.

> **Learn More**
> 
> The MailMerge class provides very effective ways to merge data into MS Word compatible templates. This updated ultimate guide provides an overview of all the important features and functionalities of the mail merge process.
> 
> [An Ultimate Guide to Mail Merge with MS Word Documents in C# ](https://www.textcontrol.com/blog/2023/06/07/an-ultimate-guide-to-mail-merge-with-ms-word-documents-in-csharp/llms-full.txt)

---

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

- [Sign Documents with a Self-Signed Digital ID From Adobe Acrobat Reader in .NET C#](https://www.textcontrol.com/blog/2024/08/12/sign-documents-with-a-self-signed-digital-id-from-adobe-acrobat-reader-in-net-c-sharp/llms.txt)
- [Programmatically Convert MS Word DOCX Documents to PDF in .NET C#](https://www.textcontrol.com/blog/2024/08/09/programmatically-convert-ms-word-docx-documents-to-pdf-in-net-c-sharp/llms.txt)
- [Chat PDF - A Generative AI Application for PDF Documents using TX Text Control and OpenAI Functions in C#](https://www.textcontrol.com/blog/2024/02/23/ask-pdf-a-generative-ai-application-for-pdf-documents-using-tx-text-control-and-openai-functions-in-c-sharp/llms.txt)
- [Document Viewer: Save the Values of Form Fields in Documents](https://www.textcontrol.com/blog/2023/12/19/document-viewer-save-the-values-of-form-fields-in-documents/llms.txt)
- [Store Documents as PDF/A using C# - A Future-Proof Archiving Format](https://www.textcontrol.com/blog/2023/10/24/store-documents-as-pdfaa-using-csharp-a-futureproof-archiving-format/llms.txt)
- [Convert HTML to PDF in ASP.NET Core C#](https://www.textcontrol.com/blog/2023/10/23/convert-html-to-pdf-in-aspnet-core-csharp/llms.txt)
- [How to Load and View PDF Documents in ASP.NET Core C#](https://www.textcontrol.com/blog/2023/10/20/how-to-load-and-view-pdf-documents-in-aspnet-core-csharp/llms.txt)
- [Document Viewer: Save the Values of Form Fields in Documents](https://www.textcontrol.com/blog/2023/10/19/document-viewer-save-the-values-of-form-fields-in-documents/llms.txt)
- [How to Create and Deploy PDF Forms in ASP.NET Core C#](https://www.textcontrol.com/blog/2023/10/17/how-to-create-and-deploy-pdf-forms-in-aspnet-core-csharp/llms.txt)
- [Converting MS Word DOCX Documents to PDF in C#](https://www.textcontrol.com/blog/2023/10/16/converting-ms-word-docx-documents-to-pdf-in-csharp/llms.txt)
- [AI Natural Language Document Generation with MCP and TX Text Control .NET](https://www.textcontrol.com/blog/2026/07/16/ai-natural-language-document-generation-with-mcp-server-and-tx-text-control-dotnet/llms.txt)
- [C# Document Generation: A Developer's Guide for .NET](https://www.textcontrol.com/blog/2026/07/08/csharp-document-generation-developer-guide-for-dotnet/llms.txt)
- [Validating PDF/UA Documents in .NET C#: A Practical Guide](https://www.textcontrol.com/blog/2026/07/06/validating-pdf-ua-documents-in-dotnet-csharp/llms.txt)
- [TX Text Control vs IronPDF for Enterprise PDF Workflows: Complete Comparison Guide](https://www.textcontrol.com/blog/2026/04/28/tx-text-control-vs-ironpdf-for-enterprise-pdf-workflows-complete-comparison-guide/llms.txt)
- [Using QR Codes in PDF Documents in C# .NET](https://www.textcontrol.com/blog/2026/04/21/using-qr-codes-in-pdf-documents-in-csharp-dotnet/llms.txt)
- [Programmatically Fill, Flatten, and Export DOCX Form Templates to PDF in C# .NET](https://www.textcontrol.com/blog/2026/04/10/programmatically-fill-flatten-and-export-docx-form-templates-to-pdf-in-csharp-dotnet/llms.txt)
- [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)
- [From Legacy Microsoft Office Automation to a Future-Ready Document Pipeline with C# .NET](https://www.textcontrol.com/blog/2026/03/02/from-legacy-microsoft-office-automation-to-a-future-ready-document-pipeline-with-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)
