Products Technologies Demo Docs Blog Support Company

Using MailMerge Events to Remove Trailing Text in a Merge Block

Consider a repeating block that contains a comma separated list and the last comma should be removed. This article shows how to implement this functionality using MailMerge events.

Using MailMerge Events to Remove Trailing Text in a Merge Block

Consider the following comma separated list that is created dynamically using a merge block:

MailMerge event order

Pay attention to the last circle where the comma is intentionally missing. In you define the comma as part of the TextAfter property of a MergeField, the comma would be added to the last entry in the merge block as well.

MailMerge event order

As a result, an unwanted comma is added at the end of the list.

MailMerge event order

In order to solve that problem, the flexible events of MailMerge are used:

  • BlockMerging
  • FieldMerged
  • BlockRowMerged

For merge blocks, these events are fired in the following order:

MailMerge event order

To define conditional text after processing, a unique keyword can be defined that is part of the TextAfter property:

MailMerge event order

The TextBefore property value starts with %REMOVELAST%: followed by the actual string that should be rendered. In our case the comma (",").

When merging the template with data, the above events are attached before calling one of the merge methods:

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

  // attach events
  mm.BlockMerging += Mm_BlockMerging;
  mm.FieldMerged += Mm_FieldMerged;
  mm.BlockRowMerged += Mm_BlockRowMerged;

  // merge template
  mm.MergeJsonData(jsonData);
}

The flag bLastBlockRow that indicates that the last row of a block is processed is reset for each new merge block:

bool bLastBlockRow = false;

private void Mm_BlockMerging(object sender, MailMerge.BlockMergingEventArgs e) {
  // reset counter for new block
  bLastBlockRow = false;
}

bLastBlockRow is updated in the BlockRowMerged event:

private void Mm_BlockRowMerged(object sender, MailMerge.BlockRowMergedEventArgs e) {
  // before last row gets merged
  if (e.DataRowNumber == e.DataRowCount - 2) {
    bLastBlockRow = true;
  }
}

The actual processing is done in the FieldMerged event. If the field contains the unique keyword and the last row of the merge block is being processed, the complete text after is removed. Otherwise, it is rendered.

private void Mm_FieldMerged(object sender, MailMerge.FieldMergedEventArgs e) {

  var keyword = "%REMOVELAST%:";

  // return if field is outside block
  if (e.MergeBlockName == "" || e.MailMergeFieldAdapter.TypeName != "MERGEFIELD")
    return;

  // convert to the MergeField
  MergeField field = ((MergeField)e.MailMergeFieldAdapter);

  // if "text after" contains keyword
  if (field.TextAfter.StartsWith(keyword)) {
    if (bLastBlockRow == true) { // remove "text after" when last row
      field.TextAfter = "";
    }
    else { // else keep "text after" string
      field.TextAfter = field.TextAfter.Substring(
        keyword.Length, 
        field.TextAfter.Length - keyword.Length);
    }
  }

}

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 CoreFiltering

Filtering and Sorting Repeating Blocks in MailMerge using C#

TX Text Control MailMerge's ability to filter and sort repeating merge blocks is a key strength, making it ideal for creating dynamic reports, lists, and catalogs.


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.


ASP.NETJSON

Adjusting the Maximum Request Length for ASP.NET Core and ASP.NET Applications

Learn how to adjust the maximum request length for ASP.NET Core and ASP.NET applications to allow loading large files into the Document Viewer. This article shows how to configure the maximum…


ASP.NETWindows FormsWPF

Renaming Merge Blocks and Merge Fields Programmatically in C#

This article shows how to programmatically rename merge blocks and merge fields in a document using TX Text Control .NET for Windows Forms, WPF, and ASP.NET. It implements a helper class that can…


ASP.NETJSONTables

How to Detect and Extract Table Data as JSON from PDF Documents in C#

A common use case is to extract tabular data from tables in a PDF document to create a structured and reusable data format. This article describes how to convert tables recognized by the PDF…