Products Technologies Demo Docs Blog Support Company

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.

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

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.

Prerequisites

The following tutorial requires a trial version of TX Text Control .NET Server.

  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

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

Adding the Template and Data

  1. In the Solution Explorer, select the project and click New Folder from the Project main menu and name it App_Data.

  2. Download the following sample template and save it in the newly created folder:

    template.tx

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

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

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

Stay in the loop!

Subscribe to the newsletter to receive the latest updates.

Related Posts

ASP.NETASP.NET CoreMailMerge

Adding SVG Watermarks to Documents

This article shows how to add SVG images to document section headers that repeat automatically on each page. This watermark will be inserted vertically and horizontally centered on each section page.


ASP.NETASP.NET CoreMailMerge

Using MailMerge in ASP.NET Core 6 Web Applications

This article shows how to use the TX Text Control ASP.NET MailMerge class to merge templates with JSON data within a .NET 6 application in Visual Studio 2022.


ASP.NETASP.NET CoreDOCX

Use MailMerge in .NET on Linux to Generate Pixel-Perfect PDFs from DOCX…

This article explores how to use the TX Text Control MailMerge feature in .NET applications on Linux to generate pixel-perfect PDFs from DOCX templates. This powerful combination enables…


ASP.NETASP.NET CoreContract

Generating Dynamic NDAs Using TX Text Control MailMerge in C# .NET

This article demonstrates how to generate dynamic NDAs using TX Text Control MailMerge in C# .NET. It covers the process of creating a template, binding data, and generating the final document.


ASP.NETASP.NET CoreBackend

Designing a Maintainable PDF Generation Web API in ASP.NET Core (Linux) C#…

This article shows how to create a PDF generation Web API in ASP.NET Core on Linux using TX Text Control .NET Server. The clean architecture is used to create a maintainable and testable solution.