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 TX Text Control .NET Server for ASP.NET
TXTextControl Namespace
ServerTextControl Class
The ServerTextControl class implements a component that provide high-level text processing features for server-based applications.
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 for ASP.NET.

  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"
    }
    ]
    }
    ]
    view raw data.json hosted with ❤ by GitHub

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");
    }
    }
    view raw test.cs hosted with ❤ by GitHub

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();
    }
    view raw test.cshtml hosted with ❤ by GitHub

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