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 handled. This article shows how to merge hyperlinks into templates.

The Mail
The Template
In the following screenshot, a simple template is created using TX Text Control that consists of 2 merge fields:
- company
- website
The Data Source
The following class is used to create the data source object for this sample merge process:
private class Report {
public string Company { get; set; }
public Hyperlink Website { get; set; }
}
private class Hyperlink {
public string Target { get; set; }
public string Text { get; set; }
public override string ToString() {
return Text;
}
}
Pay attention to the Hyperlink class that contains the actual display Text and the Target. The overridden ToString method returns the text, so that without any custom handling, the merge field would contain the display text after the merge process.
After creating an instance of the data source, the MailMerge class is used to merge the data object into the template:
// create a data object
var dummyData = new Report() {
Company = "Text Control, LLC",
Website = new Hyperlink() {
Text = "Visit us",
Target = "https://www.textcontrol.com"
}
};
// merge using MailMerge
using (TXTextControl.DocumentServer.MailMerge mm =
new TXTextControl.DocumentServer.MailMerge() { TextComponent = textControl1 }) {
// magic happens in the FieldMerged event
mm.FieldMerged += Mm_FieldMerged;
mm.MergeObject(dummyData);
}
Custom Event Handling
In our custom handling, we want to replace the merge field website with a clickable hyperlink. Therefore, the Field
private void Mm_FieldMerged(
object sender,
TXTextControl.DocumentServer.MailMerge.FieldMergedEventArgs e) {
// check if the current field is a merge field
if (e.MailMergeFieldAdapter.TypeName == "MERGEFIELD") {
MergeField field = (MergeField)e.MailMergeFieldAdapter;
// the DataRow returns the object of the actual data source
if (e.DataRow[field.Name].GetType() == typeof(Hyperlink)) {
// cast the object to a Hyperlink
Hyperlink hyperlink = (Hyperlink)e.DataRow[field.Name];
// use a temporary ServerTextControl to generate HypertextLink
using (TXTextControl.ServerTextControl tx = new TXTextControl.ServerTextControl()) {
tx.Create();
// the load the field to apply the formatting
tx.Load(e.MergedField, TXTextControl.BinaryStreamType.InternalUnicodeFormat);
// remove the field
tx.ApplicationFields.Clear(true);
// remove the text, but keep formatting
tx.SelectAll();
tx.Selection.Text = "";
// create and insert a new hypertext link based on the data row object
TXTextControl.HypertextLink hypertextLink =
new TXTextControl.HypertextLink(hyperlink.Text, hyperlink.Target);
tx.HypertextLinks.Add(hypertextLink);
// save the content and return to the event arguments
byte[] data;
tx.Save(out data, TXTextControl.BinaryStreamType.InternalUnicodeFormat);
e.MergedField = data;
}
}
}
}
The FieldMerged event is triggered for all merged fields. The event not only returns the Mail
if (e.DataRow[field.Name].GetType() == typeof(Hyperlink)) { } //...
In case, the object is a Hyperlink, a temporary Server
In the resulting document, the website merge field is converted into a clickable hypertext link:
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…
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.
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…
X19 Sneak Peek: Manipulating MergeBlockInfo Objects
TX Text Control X19 provides the possibility to retrieve and manipulate information of an existing MailMerge merge block. This enables the editing of filters and sorting instructions programmatically.