MailMerge: Field Mapping and Handling of Unmerged Fields
Text Control's MailMerge API maps merge fields in templates with columns in supported data sources. This article explains the structure and how to handle unmerged fields.

Text Control's Mail
The following illustration shows how merge field names are mapped to hierarchical data.
company_name comes from the root table directly while the address is coming from a child table. These fields can be accessed using the "dot notation". For example, to access the street, the merge field name is address.street.
Learn More
MailMerge: Data Structures Explained with a Sample Template and JSON Data
Removing Empty Fields
But what is happening to a field in case an appropriate column name cannot be found in the given data source?
By default, empty fields are removed. This behavior can be controlled by setting the Remove
The following sample template contains 3 fields while the third field MERGEFIELD doesn't have an associated data field in the used sample JSON data source.
In the resulting document, the third field is removed:
In the following code, a new instance of the MailMerge class is created and the RemoveEmptyFields is set to false, so that empty fields are not removed from the document.
string jsonData = System.IO.File.ReadAllText("data.json");
using (MailMerge mm = new MailMerge() { TextComponent = textControl1 }) {
mm.RemoveEmptyFields = false;
mm.MergeJsonData(jsonData);
}
Handling Empty Fields
For each merged field, the Field
string jsonData = System.IO.File.ReadAllText("data.json");
using (MailMerge mm = new MailMerge() { TextComponent = textControl1 }) {
mm.FieldMerged += Mm_FieldMerged;
mm.RemoveEmptyFields = false;
mm.MergeJsonData(jsonData);
}
In the event handler, the text of the unmapped field is changed.
private void Mm_FieldMerged(object sender, MailMerge.FieldMergedEventArgs e) {
if (e.Merged == false) {
if (e.MailMergeFieldAdapter.TypeName == "MERGEFIELD") {
MergeField mf = new MergeField(e.MailMergeFieldAdapter.ApplicationField);
mf.Text = "[Field not merged!]";
}
}
}
The unhandled field contains the custom text after the merge process.
In case there is a mismatch, this event can be also used to add custom mapping lists or dictionaries that represent the mapping between names in the given data source and merge fields in the template.
Removing Empty Paragraphs
In case a paragraph contains only unhandled merge fields and becomes empty during the merge process, the resulting empty line might not be wanted in the resulting document. These empty lines can be removed automatically by setting the Remove
string jsonData = System.IO.File.ReadAllText("data.json");
using (MailMerge mm = new MailMerge() { TextComponent = textControl1 }) {
mm.FieldMerged += Mm_FieldMerged;
mm.RemoveEmptyFields = true;
mm.RemoveEmptyLines = true;
mm.MergeJsonData(jsonData);
}
Related Posts
Mail Merge MS Word Office Open XML (DOCX) Templates in C#
TX Text Control's MailMerge engine allows you to perform MS Word compatible mail merge processes in .NET based applications. This article gives an overview of the basic functionality and shows how…
Generating Hierarchical Tables from JSON Data in .NET C#
Using TX Text Control, you can generate complex hierarchical tables directly from JSON data. This article explains the code and logic behind it.
MailMerge: Merging Hyperlinks using the FieldMerged Event
The MailMerge class is used to merge data into merge fields, image placeholders, barcodes and other reporting elements. With the help of the flexible FieldMerged event, additional elements can be…
MailMerge: Data Structures Explained with a Sample Template and JSON Data
The MailMerge class is used to merge JSON data into templates including merge fields and repeating merge blocks. This article gives an overview of the data structure based on a sample template…
Adding Sorting and Filter Instructions to Existing MergeBlocks
Merge blocks can be filtered and sorted with linked instructions that help to shape the used data in each merge block. This article shows how to add sorting and filter instructions to existing…