Products Technologies Demo Docs Blog Support Company

MailMerge: Rendering Conditional Table Rows

The MailMerge class supports repeating merge blocks that are repeated based on the given data rows. Sub-blocks can be rendered conditionally based on value comparisons of the parent data table. This article shows how to add such a condition.

MailMerge: Rendering Conditional Table Rows

Typically, a merge block is used to repeat content such as table rows or paragraphs. Another purpose of a merge block is to control the rendering of a complete sub-block such as a table row. Those sub-blocks can be rendered conditionally based on value comparisons of the parent data table.

Consider the following JSON data that is merged into the template that is shown further below:

[
  {
    "order_id": 123,
    "products": [
      {
        "name": "Product A",
        "price": 123.12,
        "available": false,
        "outofstock": [
          {
            "leadtime": 5
          }
        ]
      },
      {
        "name": "Product B",
        "price": 223.00,
        "available": false,
        "outofstock": [
          {
            "leadtime": 10
          }
        ]
      },
      {
        "name": "Product C",
        "price": 555.00,
        "available": true,
        "outofstock": [
          {
            "leadtime": 7
          }
        ]
      }
    ]
  }
]

The following screenshot shows the template with 2 nested merge blocks that are highlighted in blue and purple.

Merge Block Template

The blue merge block shall be rendered in case the available value equals false. Otherwise the complete merge block should be removed.

Based on the above JSON data, the following result should be rendered:

Merge Block Template

The outofstock merge block is rendered for the first 2 rows and not rendered for the last row.

To show how to add the conditions programmatically, the template contains both merge blocks without the condition. The following code shows how to add the condition to the merge block outofstock:

// load the template
textControl1.Load("template.tx", TXTextControl.StreamType.InternalUnicodeFormat);

// get the "outofstock" SubTextPart
SubTextPart subTextPart = textControl1.SubTextParts.GetItem("txmb_outofstock");

// create a MergeBlockInfo object
MergeBlockInfo mergeBlockInfo = new MergeBlockInfo(subTextPart);

// add the condition
mergeBlockInfo.BlockMergingCondition =
  new List<TXTextControl.DocumentServer.DataShaping.FilterInstruction>()
  {
       new TXTextControl.DocumentServer.DataShaping.FilterInstruction(
          "available",
          TXTextControl.DocumentServer.DataShaping.RelationalOperator.Equals,
          false
          )
  };

// store the condition in the SubTextPart
mergeBlockInfo.Apply();

The MergeBlockInfo class can be initialized with any merge block SubTextPart that is part of the template. Each merge block SubTextPart has the prefix txmb_, so that the outofstock merge block has the name txmb_outofstock. Using the BlockMergingCondition property, a new FilterInstruction is added.

Finally, the settings are applied and stored in the SubTextPart.

These conditions are persistently stored when the template is saved and are used automatically by the MailMerge class when merging the template with the JSON data:

string data = System.IO.File.ReadAllText("data.json");

using (MailMerge mm = new MailMerge()) {
  mm.TextComponent = textControl1;
  mm.MergeJsonData(data);
}

Additionally, the merge field leadtime of the conditional block is populated with values from the outofstock table.

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.DataShaping.FilterInstruction Class
  • TXTextControl.DocumentServer.DataSources.MergeBlockInfo.BlockMergingCondition Property
  • TXTextControl.DocumentServer.DataSources.MergeBlockInfo Class
  • TXTextControl.DocumentServer.MailMerge Class
  • TXTextControl.SubTextPart Class

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.NETWindows FormsWPF

Merging Form Fields using the MailMerge Class

Usually, the MailMerge class is used to merge data into document templates to replace merge fields. Since version 30.0, it is possible to merge data into form fields.


ASP.NETWindows FormsWPF

TX Text Control 33.0 SP3 is Now Available: What's New in the Latest Version

TX Text Control 33.0 Service Pack 3 is now available, offering important updates and bug fixes for all platforms. If you use TX Text Control in your document processing applications, this service…


ASP.NETWindows FormsWPF

TX Text Control 33.0 SP2 is Now Available: What's New in the Latest Version

TX Text Control 33.0 Service Pack 2 is now available, offering important updates and bug fixes for all platforms. If you use TX Text Control in your document processing applications, this service…


ASP.NETWindows FormsWPF

Document Lifecycle Optimization: Leveraging TX Text Control's Internal Format

Maintaining the integrity and functionality of documents throughout their lifecycle is paramount. TX Text Control provides a robust ecosystem that focuses on preserving documents in their internal…


ActiveXASP.NETWindows Forms

Expert Implementation Services for Legacy System Modernization

We are happy to officially announce our partnership with Quality Bytes, a specialized integration company with extensive experience in modernizing legacy systems with TX Text Control technologies.