# How to Mail Merge MS Word DOCX Documents in ASP.NET Core C#

> Mail merge is the process of merging data, such as Json or IEnumerable objects, into a template document, such as a DOC or DOCX file. This tutorial is a walkthrough of the steps necessary to create an ASP.NET Core MVC application.

- **Author:** Bjoern Meyer
- **Published:** 2023-10-16
- **Modified:** 2026-07-17
- **Description:** Mail merge is the process of merging data, such as Json or IEnumerable objects, into a template document, such as a DOC or DOCX file. This tutorial is a walkthrough of the steps necessary to create an ASP.NET Core MVC application.
- **3 min read** (533 words)
- **Tags:**
  - ASP.NET
  - ASP.NET Core
  - MailMerge
  - Tutorial
- **Web URL:** https://www.textcontrol.com/blog/2023/10/16/how-to-mail-merge-ms-word-docx-documents-in-aspnet-core-csharp/
- **LLMs URL:** https://www.textcontrol.com/blog/2023/10/16/how-to-mail-merge-ms-word-docx-documents-in-aspnet-core-csharp/llms.txt
- **LLMs-Full URL:** https://www.textcontrol.com/blog/2023/10/16/how-to-mail-merge-ms-word-docx-documents-in-aspnet-core-csharp/llms-full.txt

---

This tutorial is a walkthrough of the steps necessary to create an ASP.NET Core MVC application. Mail merge is the process of merging data, such as Json or IEnumerable objects, into a template document, such as a DOC or DOCX file.

The following tutorial shows how to create a simple .NET 6 C# Console App to convert MS Word documents to PDF that uses the ServerTextControl class.

### Creating the Application

Make sure that you downloaded the latest version of Visual Studio 2022 that comes with the [.NET 6 SDK](https://dotnet.microsoft.com/download/dotnet/6.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/download/)

1. In Visual Studio 2022, create a new project by choosing *Create a new project*.
2. Select *ASP.NET Core Web App (Model-View-Controller)* 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 6 (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/2023/10/16/b/assets/visualstudio1.webp "ASP.NET Core Web Application")

#### Adding the Template and Data

6. In the *Solution Explorer*, select the project and click *New Folder* from the *Project* main menu and name it *App\_Data*.
7. Download the following sample template and save it in the newly created folder:
    
    [template.tx](https://s1-www.textcontrol.com/assets/dist/blog/2023/10/16/b/assets/template.tx)
8. In the same folder, create a new JSON file, name it *data.json* and add the following content to it:
    
    ```
    [
      {
        "name": "Jackson",
        "firstname": "Peter",
        "city": "Charlotte",
        "zip": "28226",
        "street": "6920 Shannon Willow Rd",
        "product": [
          {
            "name": "TX Text Control .NET Server for ASP.NET",
            "price": "2998"
          },
          {
            "name": "TX Text Control .NET for Windows Forms",
            "price": "1459"
          }
        ]
      }
    ]
    ```

#### Adding a Web API Endpoint

9. Open the *HomeController.cs* and add the following method:
    
    ```
    [HttpGet]
    public IActionResult MergeDocument()
    {
        // create a ServerTextControl
        using (TXTextControl.ServerTextControl tx = new TXTextControl.ServerTextControl())
        {
    
            tx.Create();
    
            // load the template
            tx.Load("App_Data/template.tx", TXTextControl.StreamType.InternalUnicodeFormat);
    
            // create the mail merge engine
            using (TXTextControl.DocumentServer.MailMerge mm = new TXTextControl.DocumentServer.MailMerge())
            {
                // connect to ServerTextControl instance
                mm.TextComponent = tx;
    
                // merge data
                var jsonData = System.IO.File.ReadAllText("App_Data/data.json");
                mm.MergeJsonData(jsonData);
            }
    
            byte[] bDocument = null;
    
            // save in the internal format
            tx.Save(out bDocument, TXTextControl.BinaryStreamType.AdobePDF);
    
            // create memory stream from byte array
            MemoryStream stream = new MemoryStream(bDocument);
    
            stream.Position = 0;
            //Download Word document in the browser
            return File( stream, "application/pdf", "results.pdf");
        }
    }
    ```

#### Calling the Web API

10. Open the *Views -> Home -> Index.cshtml* and replace the code with the following code:
    
    ```
    @{
        Html.BeginForm("MergeDocument", "Home", FormMethod.Get);
        {
            <div>
                <input type="submit" value="Merge Document" />
            </div>
        }
        Html.EndForm();
    }
    ```

Now, compile and start the application. Click the button *Merge Document* to call the Web API and to download the merged document.

![ASP.NET Core Web Application](https://s1-www.textcontrol.com/assets/dist/blog/2023/10/16/b/assets/results.webp "ASP.NET Core Web Application")

---

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

- [Adding SVG Watermarks to Documents](https://www.textcontrol.com/blog/2022/01/28/adding-svg-watermarks-to-documents/llms.txt)
- [Using MailMerge in ASP.NET Core 6 Web Applications](https://www.textcontrol.com/blog/2022/01/27/using-mailmerge-in-aspnet-core-6-web-applications/llms.txt)
- [Building a Production-Ready PDF Generation Pipeline in C# with ASP.NET Core](https://www.textcontrol.com/blog/2026/10/12/reliable-pdf-generation-pipeline-csharp-aspnet-core/llms.txt)
- [Create SignFabric Envelopes from Mail Merge Templates in .NET C#](https://www.textcontrol.com/blog/2026/06/23/create-signfabric-envelopes-from-mail-merge-templates-using-dotnet-csharp/llms.txt)
- [Use MailMerge in .NET on Linux to Generate Pixel-Perfect PDFs from DOCX Templates](https://www.textcontrol.com/blog/2025/05/27/use-mailmerge-in-dotnet-on-linux-to-generate-pixel-perfect-pdfs-from-docx-templates/llms.txt)
- [Generating Dynamic NDAs Using TX Text Control MailMerge in C# .NET](https://www.textcontrol.com/blog/2025/04/08/generating-dynamic-ndas-using-tx-text-control-mailmerge-in-csharp-dotnet/llms.txt)
- [Designing a Maintainable PDF Generation Web API in ASP.NET Core (Linux) C# with Clean Architecture and TX Text Control](https://www.textcontrol.com/blog/2025/03/27/designing-a-maintainable-pdf-generation-web-api-in-asp-net-core-linux-c-sharp-with-clean-architecture-and-tx-text-control/llms.txt)
- [Manipulating Table Cells During the MailMerge Process in .NET C#](https://www.textcontrol.com/blog/2024/12/03/manipulating-table-cells-during-the-mailmerge-process-in-net-csharp/llms.txt)
- [When to Generate Documents Server-Side Instead of Client-Side: A Focus on Data Security](https://www.textcontrol.com/blog/2024/10/04/when-to-generate-documents-server-side-instead-of-client-side-a-focus-on-data-security/llms.txt)
- [Mail Merge: Inserting Merge Blocks using the DataSourceManager in C#](https://www.textcontrol.com/blog/2024/10/02/mail-merge-inserting-merge-blocks-using-the-datasourcemanager-in-csharp/llms.txt)
- [Creating Advanced Tables in PDF and DOCX Documents with C#](https://www.textcontrol.com/blog/2024/09/30/creating-advanced-tables-in-pdf-and-docx-documents-with-csharp/llms.txt)
- [Designing the Perfect Contract Template for MailMerge in C#](https://www.textcontrol.com/blog/2024/09/26/designing-the-perfect-contract-template-for-mailmerge-in-csharp/llms.txt)
- [Video Tutorial: Creating a MailMerge Template and JSON Data Structure](https://www.textcontrol.com/blog/2024/08/16/video-tutorial-creating-a-mailmerge-template-and-json-data-structure/llms.txt)
- [Getting Started Video Tutorial: How to use the MailMerge and ServerTextControl Classes in ASP.NET Core C#](https://www.textcontrol.com/blog/2024/08/05/getting-started-video-tutorial-how-to-use-the-mailmerge-and-servertextcontrol-classes-in-asp-net-core-c/llms.txt)
- [Getting Started Videos: New Text Control YouTube Channel](https://www.textcontrol.com/blog/2024/08/02/getting-started-videos-new-text-control-youtube-channel/llms.txt)
- [Best Practices for Mail Merge and Form Field Processing in ASP.NET Core C# Applications](https://www.textcontrol.com/blog/2024/07/30/best-practices-for-mail-merge-and-form-field-processing-in-asp-net-core-csharp-applications/llms.txt)
- [Advantages of Flow Type Layout Reporting vs. Banded Reporting or PDF Template Engines in .NET C#](https://www.textcontrol.com/blog/2024/07/29/advantages-of-flow-type-layout-reporting-vs-banded-reporting-or-pdf-template-engines-in-net-c-sharp/llms.txt)
- [Designing a MailMerge Web API Endpoint with ASP.NET Core in C#](https://www.textcontrol.com/blog/2024/07/12/designing-a-mailmerge-web-api-endpoint-with-asp-net-core-in-c-sharp/llms.txt)
- [Enhancing Documents with QR Codes and Barcodes in .NET C#: A Comprehensive Guide](https://www.textcontrol.com/blog/2024/07/11/enhancing-documents-with-qr-codes-and-barcodes-in-net-csharp-a-comprehensive-guide/llms.txt)
- [Document Automation 101: Leveraging TX Text Control for Business Efficiency in .NET C# Applications](https://www.textcontrol.com/blog/2024/07/09/document-automation-101-leveraging-tx-text-control-for-business-efficiency-in-net-c-applications/llms.txt)
- [Merging Templates with MailMerge with Different Merge Field Settings in C#](https://www.textcontrol.com/blog/2023/12/16/merging-templates-with-mailmerge-with-different-merge-field-settings/llms.txt)
- [MailMerge: Working with Image Placeholders](https://www.textcontrol.com/blog/2022/12/22/mailmerge-working-with-image-placeholders/llms.txt)
- [Getting Started: ServerTextControl and MailMerge with ASP.NET Core](https://www.textcontrol.com/blog/2022/09/01/getting-started-servertextcontrol-and-mailmerge-with-aspnet-core/llms.txt)
- [code.talks 2026: Two Fantastic Days of Developers, Innovation, and Great Conversations in Hamburg](https://www.textcontrol.com/blog/2026/11/06/code-talks-2026-hamburg/llms.txt)
- [Meet Text Control at code.talks 2026: Spreadsheets, Documents, and More](https://www.textcontrol.com/blog/2026/10/27/meet-text-control-at-code-talks-2026/llms.txt)
