Products Technologies Demo Docs Blog Support Company

MailMerge Class Settings Explained

This article explains the different settings of the MailMerge class and how and when they should be used.

MailMerge Class Settings Explained

TX Text Control sample template

The TXTextControl.DocumentServer.MailMerge class provides several settings to control the merging process and formatting in the resulting document. There are various switches to control how text fields, blocks, images and blank lines are handled. This article gives an overview of when and how to use which option.

We will look into four different switches:

  1. TXTextControl.DocumentServer.MailMerge.RemoveEmptyFields property
  2. TXTextControl.DocumentServer.MailMerge.RemoveEmptyLines property
  3. TXTextControl.DocumentServer.MailMerge.RemoveEmptyBlocks property
  4. TXTextControl.DocumentServer.MailMerge.RemoveEmptyImages property

To show the various settings, the same data source basis and template is used and will be slightly changed to illustrate different scenarios. The following JSON is used as the data source:

[
  {
    "Id": 1,
    "Name": "Sales Report",
    "Customer": {
      "Id": 1,
      "Name": "Text Control, LLC",
      "Street": "6926 Shannon-Willow Rd",
      "ZipCode": "28226",
      "City": "Charlotte",
      "Country": "United States"
    },
    "Contacts": [
      {
        "Name": "Paulsen",
        "FirstName": "Carl",
        "Email": "sales@textcontrol.com",
        "Phone": "7045447445"
      },
      {
        "Name": "Franklin",
        "FirstName": "Paul",
        "Email": "support@textcontrol.com",
        "Phone": "7045447445"
      }
    ]
  }
]

The following screenshot shows the simple template with text fields and a merge block highlighted in red:

TX Text Control sample template

Removing Empty Fields

In the first scenario, we would like to remove fields that are empty. This happens, if no data field can be found in the data source that matches the field name in the template. By default, the RemoveEmptyFields property is true, so that the text of empty fields is removed automatically.

The following code is used to merge the template with the sample JSON data source:

textControl1.Load("template.tx", TXTextControl.StreamType.InternalUnicodeFormat);
string data = System.IO.File.ReadAllText("data.json");

using (TXTextControl.DocumentServer.MailMerge mailMerge =
       new TXTextControl.DocumentServer.MailMerge())
{
    mailMerge.TextComponent = textControl1;
    mailMerge.MergeJsonData(data);
}

When passing all field values from the data source, the results would look similar to the following screenshot:

TX Text Control sample template

Now, we will remove one field Street from the data source:

[
  {
    "Id": 1,
    "Name": "Sales Report",
    "Customer": {
      "Id": 1,
      "Name": "Text Control, LLC",
      "ZipCode": "28226",
      "City": "Charlotte",
      ...

In code, the RemoveEmptyFields property is set to false:

textControl1.Load("template.tx", TXTextControl.StreamType.InternalUnicodeFormat);
string data = System.IO.File.ReadAllText("data.json");

using (TXTextControl.DocumentServer.MailMerge mailMerge = new TXTextControl.DocumentServer.MailMerge())
{
    mailMerge.TextComponent = textControl1;

    mailMerge.RemoveEmptyFields = false;
    mailMerge.MergeJsonData(data);
}

The resulting document is shown in the next screenshot:

TX Text Control sample template

Removing Empty Lines

Based on the previous example, we keep the Street field deleted from the data source and the RemoveEmptyFields is not set (by default true). The resulting document would contain an empty line where the empty field was:

TX Text Control sample template

By setting the RemoveEmptyLines property to true, the complete empty line will be removed. This is done, if no other content exists in the same line and an empty line would be created:

textControl1.Load("template.tx", TXTextControl.StreamType.InternalUnicodeFormat);
string data = System.IO.File.ReadAllText("data.json");

using (TXTextControl.DocumentServer.MailMerge mailMerge = new TXTextControl.DocumentServer.MailMerge())
{
    mailMerge.TextComponent = textControl1;
    mailMerge.RemoveEmptyLines = true;
    mailMerge.MergeJsonData(data);
}

TX Text Control sample template

Removing Empty Blocks

In case, the data is missing a block, the RemoveEmptyBlocks property can be used to determine whether a block should be removed from the document completely. This concept can be used to conditionally remove content from a document.

In our data source, we will remove the complete merge block data:

[
  {
    "Id": 1,
    "Name": "Sales Report",
    "Customer": {
      "Id": 1,
      "Name": "Text Control, LLC",
      "Street": "6926 Shannon-Willow Rd",
      "ZipCode": "28226",
      "City": "Charlotte",
      "Country": "United States"
    }
  }
]

The following code is used to create the document and to set the RemoveEmptyBlocks to true:

textControl1.Load("template.tx", TXTextControl.StreamType.InternalUnicodeFormat);
string data = System.IO.File.ReadAllText("data.json");

using (TXTextControl.DocumentServer.MailMerge mailMerge = new TXTextControl.DocumentServer.MailMerge())
{
    mailMerge.TextComponent = textControl1;
    mailMerge.RemoveEmptyBlocks = true;
    mailMerge.MergeJsonData(data);
}

The complete block part is removed. In our sample, the table header is still visible. In order to remove the table header as well, the header should be part of the merge block or should be part of a hierarchical nested block. If the data of the parent table is missing, the complete merge blocks (including child blocks) are removed.

TX Text Control sample template

Removing Empty Images

For the next sample, we will insert an image placeholder into the template. If merged without any settings, the image placeholder will be still part of the template, even if there is no explicit data in the data source matching the image placeholder name:

TX Text Control sample template

When setting the RemoveEmptyImages property to true, the image will be removed after the merge process, if no associated data field has been found in the data source:

textControl1.Load("template.tx", TXTextControl.StreamType.InternalUnicodeFormat);
string data = System.IO.File.ReadAllText("data.json");

using (TXTextControl.DocumentServer.MailMerge mailMerge = new TXTextControl.DocumentServer.MailMerge())
{
    mailMerge.TextComponent = textControl1;
    mailMerge.RemoveEmptyImages = true;
    mailMerge.MergeJsonData(data);
}

TX Text Control sample template

Test this on your own with any .NET product of TX Text Control. The MailMerge class is available for all platforms including Windows Forms, WPF and ASP.NET.

Stay in the loop!

Subscribe to the newsletter to receive the latest updates.

Also See

This post references the following in the documentation:

  • TXTextControl.DocumentServer.MailMerge Class
  • TXTextControl.DocumentServer.MailMerge.RemoveEmptyBlocks Property
  • TXTextControl.DocumentServer.MailMerge.RemoveEmptyFields Property
  • TXTextControl.DocumentServer.MailMerge.RemoveEmptyImages Property
  • TXTextControl.DocumentServer.MailMerge.RemoveEmptyLines Property

Related Posts

ASP.NETReportingWindows Forms

MailMerge: Conditional Table Cell Colors using Filter Instructions

The MailMerge class provides an extensible framework to inject custom logic to the merge process. This sample shows how to implement conditional table cell colors.


ASP.NETReportingWindows Forms

MailMerge: Using Filters to Remove Unwanted Rows

Data in merge blocks can be filtered and sorted. This article explains how to use filters to remove unwanted lines.


ASP.NETReportingWindows Forms

Different Ways to Create Documents using Text Control Products

There are many ways to create documents using Text Control products. This article gives an overview of different approaches using our products.


ASP.NETReportingGitHub

MVC: Adding an Electronic Signature to Documents in Web.TextControl

An electronic signature is in many processes legally sufficient to prove an identity. According to the U.S. Federal ESIGN Act passed in 2000, an electronic signature is an: Electronic sound,…


ASP.NETReportingGitHub

MVC: Loading a Data Source from a View Model

The most typical application for the Web.TextControl is the creation of templates for the Text Control Reporting engine DocumentServer.MailMerge. The ribbon tab Reports is designed to insert merge…