Skip to content

Instantly share code, notes, and snippets.

@bjoerntx
Created June 8, 2022 09:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bjoerntx/5b010209074f78e5675386703e4f2741 to your computer and use it in GitHub Desktop.
Save bjoerntx/5b010209074f78e5675386703e4f2741 to your computer and use it in GitHub Desktop.
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;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment