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.

Consider the following comma separated list that is created dynamically using a merge block:
Pay attention to the last circle where the comma is intentionally missing. In you define the comma as part of the Text
As a result, an unwanted comma is added at the end of the list.
In order to solve that problem, the flexible events of Mail
- Block
Merging - Field
Merged - Block
Row Merged
For merge blocks, these events are fired in the following order:
To define conditional text after processing, a unique keyword can be defined that is part of the TextAfter property:
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);
}
}
}
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.
- Angular
- Blazor
- React
- JavaScript
- ASP.NET MVC, ASP.NET Core, and WebForms
Related Posts
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.
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.
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…
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…
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…