Text Control Reporting Framework

The Text Control Reporting Framework consists of the classes part of the namespace TXTextControl.DocumentServer including the MailMerge component, field adapters and WYSIWYG template editors for the platforms ASP.NET, Windows Forms and WPF.

The MailMerge class is a very flexible framework and can be used as your basis for all types of document automation processes. Typically, a mail merge template is merged with data from a specified data source. But thanks to the flexibility, you can control the merge process and inject your custom code.

This sample shows how to replace a merge field with a hyperlink with a defined text and target. The template itself consists only of normal merge fields:

MailMerge: Merge hyperlinks into merge fields

The merge field link should be populated with a hyperlink that is defined in the data source. The sample data source is a simple XML file:

<report>
    <name>Bill Gates</name>
    <company>Microsoft</company>
    <link>%LINK%,http://www.microsoft.com/</link>
</report>

A unique identifier is used as part of the merge data. The string "%LINK%" indicates that this merge field should be replaced with a hyperlink. In the FieldMerged event, which returns the already merged field, the Text property is checked, whether it contains the unique identifier "%LINK%".

In case, the field contains the unique identifier, the content is loaded into a temporary ServerTextControl and converted into a hyperlink. The results are loaded back into the original field position of the mail merge process.

private void mailMerge1_FieldMerged(object sender,
    TXTextControl.DocumentServer.MailMerge.FieldMergedEventArgs e)
{
    byte[] data;

    if(e.MailMergeFieldAdapter.TypeName == "MERGEFIELD")
    {
        MergeField field = new MergeField(
            e.MailMergeFieldAdapter.ApplicationField);

        // if the merge field starts with "%LINK%", we want to convert it
        // to a hyperlink
        if (field.Text.StartsWith("%LINK%") == true)
        {
            // create a temporary ServerTextControl
            using (TXTextControl.ServerTextControl tx =
                new TXTextControl.ServerTextControl())
            {
                tx.Create();

                // load the merge field to clone the formatting
                tx.Load(e.MergedField,
                    TXTextControl.BinaryStreamType.InternalUnicodeFormat);

                // remove the content
                tx.ApplicationFields.Clear(true);
                tx.SelectAll();
                tx.Selection.Text = "";

                // add a new hyperlink with the text part of the hyperlink
                tx.HypertextLinks.Add(new TXTextControl.HypertextLink(
                    field.ApplicationField.Text.Split(',')[1],
                    field.ApplicationField.Text.Split(',')[1]));

                // save the content
                tx.Save(out data,
                    TXTextControl.BinaryStreamType.InternalUnicodeFormat);
            }

            // return the hyperlink to the merge process
            e.MergedField = data;
        }
    }
}

Download the sample from GitHub and test it on your own.