Products Technologies Demo Docs Blog Support Company

Mail Merge: The Pre-Shaped Data Concept Explained

This article explains the concept of pre-shaped data and how it can be used to merge data into templates using TX Text Control MailMerge. Pre-shaped data is a powerful concept to merge data into templates where the data is already structured in a way that it can be directly merged into the template.

Mail Merge: The Pre-Shaped Data Concept Explained

Mail merge is a concept that is used to create multiple documents from a single template. It is a process in which a template is merged with a data source to create a document. The data source can be a database, JSON, XML, or any other data source that contains the data that needs to be merged with the template.

TX Text Control provides a powerful MailMerge feature that allows you to create documents from templates that contain merge fields. Merge fields are placeholders in the template that are replaced with data from the data source when the document is generated.

In contrast to other reporting tools, TX Text Control uses the pre-shaped data approach to merge data into templates. This means that the data is prepared in the correct hierarchical structure before it is merged into the template. This approach has several advantages:

  • Because the data is already structured correctly, the template is easier to understand and maintain.
  • Merging of data into the template is faster because the data is already in the correct format.
  • It is more flexible. You can use any data source that can be converted into the correct hierarchical structure.

TX Text Control expects a pre-selected data structure instead of implicitly accessing a data source with queries. This allows you to generate a proxy data structure, such as a business object or a JSON file, that has been generated from many different data sources. This makes it possible for you to use any data source with TX Text Control.

Hierarchical Data

TX Text Control uses a hierarchical data structure to merge data into templates. Let's take a look at this example JSON data structure:

[
  {
    "company_name": "Text Control, LLC",
    "address":
    {
      "street": "1111 Text Control Way",
      "zip": "28226",
      "city": "Charlotte",
      "country": "United States"
    },
    "contacts": [
      {
        "name": "Tim Typer",
        "email": "tim@textcontrol.com"
      },
      {
        "name": "Karl Keyboard",
        "email": "karl@textcontrol.com"
      },
      {
        "name": "Petra Paragraph",
        "email": "petra@textcontrol.com"
      }
    ],
    "orders": [
      {
        "id": 123,
        "articles": [
          {
            "id": 1,
            "product": {
              "name": "Product A",
              "description": "Description of product A",
              "price": 200
            },
            "qty": 5,
            "discount": 0.2
          },
          {
            "id": 2,
            "product": {
              "name": "Product B",
              "description": "Description of product B",
              "price": 244
            },
            "qty": 50,
            "discount": 0.0
          },
          {
            "id": 3,
            "product": {
              "name": "Product C",
              "description": "Description of product C",
              "price": 677
            },
            "qty": 2,
            "discount": 0.5
          }
        ]
      },
      {
        "id": 321,
        "articles": [
          {
            "id": 1,
            "product": {
              "name": "Product B",
              "description": "Description of product B",
              "price": 123
            },
            "qty": 12,
            "discount": 0.0
          },
          {
            "id": 2,
            "product": {
              "name": "Product D",
              "description": "Description of product D",
              "price": 556
            },
            "qty": 2,
            "discount": 0.7
          },
          {
            "id": 3,
            "product": {
              "name": "Product C",
              "description": "Description of product C",
              "price": 677
            },
            "qty": 20,
            "discount": 0.3
          }
        ]
      }
    ]
  }
]

The internal hierarchy structure is converted to tables:

Hierarchical data

The following screenshot shows a typical template consisting of merge fields and repeating merge blocks.

Template with merge fields and repeating merge blocks

Child Tables

The company_name is taken directly from the root table, while the address is taken from a child table. The Contacts section contains a merge block (shown in red below) to list all contacts.

Child tables

The following animation shows the header section before the merge process and the header section after the merge process:

Merge process

Merge Blocks

Merge blocks are used to repeat a section of a document for each record in a child table. The details section lists all orders and their items. Therefore, two nested merge blocks are inserted. The outer block repeats the order objects and the inner block lists all items:

Merge blocks

The outer block is repeated 2 times based on the actual data in the given JSON, as you can see in the animation below. The inner row of the table is defined as the nested block of elements and contains all of the elements:

Merge blocks animation

Data Controls the Output

TX Text Control merges the data into the template based on the structure of the data. The data controls the output of the document. If no matching data is found for a merge field, the merge field is removed from the output document. If a merge block is empty, the block is removed from the output document.

The RemoveEmptyFields property is used to remove empty merge fields and the RemoveEmptyBlocks property is used to remove empty merge blocks.

In other words, the output document is generated based on the data structure. If the complete orders table is missing in the data, the output document will not contain any orders. And this is valid for all hierarchical levels of merge blocks and structures.

Conclusion

Mail merge is a powerful feature to create documents from templates that contain merge fields. TX Text Control uses a hierarchical data structure to merge data into templates. The data controls the output of the document. If no matching data is found for a merge field, the merge field is removed from the output document. If a merge block is empty, the block is removed from the output document.

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

AngularASP.NETASP.NET Core

Creating Advanced Tables in PDF and DOCX Documents with C#

This article shows how to create advanced tables in PDF and DOCX documents using the TX Text Control .NET for ASP.NET Server component. This article shows how to create tables from scratch,…


AngularASP.NETASP.NET Core

Video Tutorial: Creating a MailMerge Template and JSON Data Structure

This video tutorial shows how to create a MailMerge template and a JSON data structure to merge data into a document. This tutorial will walk you through the steps necessary to create a template…


AngularASP.NETJavaScript

New JavaScript API Calls for Typical MailMerge Tasks

This article shows how to use the improved JavaScript API for typical MailMerge tasks such as inserting merge blocks.


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.