Products Technologies Demo Docs Blog Support Company

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.

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

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

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

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
          }
        ]
      }
    ]
  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);
    }

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#

Stay in the loop!

Subscribe to the newsletter to receive the latest updates.

ASP.NET

Integrate document processing into your applications to create documents such as PDFs and MS Word documents, including client-side document editing, viewing, and electronic signatures.

ASP.NET Core
Angular
Blazor
JavaScript
React
  • Angular
  • Blazor
  • React
  • JavaScript
  • ASP.NET MVC, ASP.NET Core, and WebForms

Learn more Trial token Download trial

Related Posts

ASP.NETASP.NET CoreDOCX

Sign Documents with a Self-Signed Digital ID From Adobe Acrobat Reader in…

This article shows how to create a self-signed digital ID using Adobe Acrobat Reader and how to use it to sign documents in .NET C#. The article also shows how to create a PDF document with a…


ASP.NETASP.NET CoreConvert

Programmatically Convert MS Word DOCX Documents to PDF in .NET C#

This article shows how to convert MS Word DOCX documents to PDF in .NET C# using the ServerTextControl component. The example shows how to load a DOCX file from a file or from a variable and how…


ASP.NETGenerative AIOpenAI

Chat PDF - A Generative AI Application for PDF Documents using TX Text…

This article shows how to create a generative AI application for PDF documents using TX Text Control and OpenAI functions in C#. The application uses the OpenAI GPT-3 engine to answer questions on…


ASP.NETForm FieldsPDF

Document Viewer: Save the Values of Form Fields in Documents

The TX Text Control Document Viewer is used to allow users to fill in form fields in documents. This article explains how to save a document with the values of the filled in form fields.


ASP.NETPDFPDF/A

Store Documents as PDF/A using C# - A Future-Proof Archiving Format

PDF/A is an international ISO standard for the preservation of electronic and digital documents. This article will explain the reasons and motivations and will show you how to create PDF/A…