Products Technologies Demo Docs Blog Support Company

MailMerge Class Settings Explained

The MailMerge class provides four properties to control merge output: RemoveEmptyFields, RemoveEmptyLines, RemoveEmptyBlocks, and RemoveEmptyImages. Each property handles unmatched or missing data by removing the corresponding elements from the final merged document output.

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

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.NETReportingWindows Forms

MailMerge: Conditional Table Cell Colors using Filter Instructions

The TX Text Control MailMerge FieldMerged event exposes TableCell instances during merge for conditional formatting. A CellFilterInstructions class parses filter rules from merge field names and…


ASP.NETReportingWindows Forms

MailMerge: Using Filters to Remove Unwanted Rows

TX Text Control merge block filters remove unwanted rows from repeating data during the mail merge process without modifying the underlying source. Filter conditions applied to merge blocks…


ASP.NETReportingWindows Forms

Different Ways to Create Documents using Text Control Products

Text Control offers four document creation paths: building from scratch via the ServerTextControl API, merging pre-designed templates with MailMerge using IEnumerable or DataSet sources, editing…


ASP.NETReportingGitHub

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

An MVC sample uses the MailMerge class to populate a signature template with the signer name and image, then inserts it into a named text frame in a contract document. An async controller method…


ASP.NETReportingGitHub

MVC: Loading a Data Source from a View Model

When merging templates with business objects, the Reports ribbon drop-downs in Web.TextControl can be populated from a serialized view model. The controller serializes a class model with the…

Share on this blog post on: