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 TX Text Control .NET Server for ASP.NET
DocumentServer Namespace
MailMerge Class
The MailMerge class is a .NET component that can be used to effortlessly merge template documents with database content in .NET projects, such as ASP.NET web applications, web services or Windows services.
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.

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

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

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

Template and Data

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

  2. 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
    }
    ]
    }
    ]
    view raw test.json hosted with ❤ by GitHub
  3. Download and unzip the following template and add it to your project. Set the Copy to Output Directory property to Copy always.

    template.zip

Adding the Code

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

Running the Application

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