# MailMerge: Merge Hyperlinks into Merge Fields

> The MailMerge FieldMerged event allows replacing standard merge fields with hyperlinks during the merge process. A unique identifier in the XML data source marks fields for conversion. A temporary ServerTextControl creates the hyperlink and returns it to the MailMerge engine.

- **Author:** Bjoern Meyer
- **Published:** 2015-05-15
- **Modified:** 2026-07-17
- **Description:** The MailMerge FieldMerged event allows replacing standard merge fields with hyperlinks during the merge process. A unique identifier in the XML data source marks fields for conversion. A temporary ServerTextControl creates the hyperlink and returns it to the MailMerge engine.
- **3 min read** (475 words)
- **Tags:**
  - ASP.NET
  - GitHub
  - Mail Merge
  - Sample
- **Web URL:** https://www.textcontrol.com/blog/2015/05/15/mailmerge-merge-hyperlinks-into-merge-fields/
- **LLMs URL:** https://www.textcontrol.com/blog/2015/05/15/mailmerge-merge-hyperlinks-into-merge-fields/llms.txt
- **LLMs-Full URL:** https://www.textcontrol.com/blog/2015/05/15/mailmerge-merge-hyperlinks-into-merge-fields/llms-full.txt
- **GitHub Repository:** https://github.com/TextControl/TextControl.MailMerge.Hyperlinks

---

![Text Control Reporting Framework](https://s1-www.textcontrol.com/assets/dist/blog/2015/05/15/a/assets/reporting_framework.webp "Text Control Reporting Framework")The [Text Control Reporting Framework](https://www.textcontrol.com/technology/reporting/) consists of the classes part of the namespace [TXTextControl.DocumentServer](https://docs.textcontrol.com/textcontrol/asp-dotnet/ref.txtextcontrol.documentserver.namespace.htm) including the [MailMerge](https://docs.textcontrol.com/textcontrol/asp-dotnet/ref.txtextcontrol.documentserver.mailmerge.class.htm) component, field adapters and WYSIWYG template editors for the platforms ASP.NET, Windows Forms and WPF.

The [MailMerge](https://docs.textcontrol.com/textcontrol/asp-dotnet/ref.txtextcontrol.documentserver.mailmerge.class.htm) 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](https://s1-www.textcontrol.com/assets/dist/blog/2015/05/15/a/assets/template.webp "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](https://docs.textcontrol.com/textcontrol/asp-dotnet/ref.txtextcontrol.documentserver.mailmerge.fieldmerged.event.htm) 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.

---

## About Bjoern Meyer

As CEO, Bjoern is the visionary behind our strategic direction and business development, bridging the gap between our customers and engineering teams. His deep passion for coding and web technologies drives the creation of innovative products. If you're at a tech conference, be sure to stop by our booth - you'll most likely meet Bjoern in person. With an advanced graduate degree (Dipl. Inf.) in Computer Science, specializing in AI, from the University of Bremen, Bjoern brings significant expertise to his role. In his spare time, Bjoern enjoys running, paragliding, mountain biking, and playing the piano.

- [LinkedIn](https://www.linkedin.com/in/bjoernmeyer/)
- [X](https://x.com/txbjoern)
- [GitHub](https://github.com/bjoerntx)

---

## Related Posts

- [Official TX Text Control .NET Sample Applications Are Now Hosted on GitHub](https://www.textcontrol.com/blog/2023/01/08/official-tx-text-control-net-sample-applications-are-now-hosted-on-github/llms.txt)
- [MVC: Adding an Electronic Signature to Documents in Web.TextControl](https://www.textcontrol.com/blog/2016/02/18/mvc-adding-an-electronic-signature-to-documents-in-webtextcontrol/llms.txt)
- [MVC: Loading a Data Source from a View Model](https://www.textcontrol.com/blog/2016/01/29/mvc-loading-a-data-source-from-a-view-model/llms.txt)
- [MVC: Merging Templates in a Controller HttpPost Method](https://www.textcontrol.com/blog/2016/01/19/mvc-merging-templates-in-a-controller-httppost-method/llms.txt)
- [Reporting: Styling the DocumentViewer for ASP.NET](https://www.textcontrol.com/blog/2015/06/14/reporting-styling-the-documentviewer-for-aspnet/llms.txt)
- [Building a Touch-enabled Button Bar with Javascript](https://www.textcontrol.com/blog/2015/05/27/building-a-touch-enabled-button-bar-with-javascript/llms.txt)
- [Building a TX Text Control Project with GitHub Actions and the Text Control NuGet Feed](https://www.textcontrol.com/blog/2026/02/09/building-a-tx-text-control-project-with-github-actions-and-the-text-control-nuget-feed/llms.txt)
- [TX Text Control for Blazor: Mail Merge Integration Tutorial](https://www.textcontrol.com/blog/2025/03/25/tx-text-control-for-blazor-mail-merge-integration-tutorial/llms.txt)
- [Mail Merge: Skipping Records During the Merge Process in .NET C#](https://www.textcontrol.com/blog/2025/02/10/mail-merge-skipping-records-during-the-merge-process-in-net-c-sharp/llms.txt)
- [Mail Merge MS Word DOCX Documents and Convert to PDF in .NET C#](https://www.textcontrol.com/blog/2025/02/07/mail-merge-ms-word-docx-documents-and-convert-to-pdf-in-net-c-sharp/llms.txt)
- [Merging Self-Calculating Business Objects with TX Text Control MailMerge in C#](https://www.textcontrol.com/blog/2024/12/27/merging-self-calculating-business-objects-with-tx-text-control-mailmerge-in-csharp/llms.txt)
- [Creating an ASP.NET Core Web App with Docker Support and GitHub Packages](https://www.textcontrol.com/blog/2024/01/05/creating-an-asp-net-core-web-app-with-docker-support-and-github-packages/llms.txt)
- [Use Case: Create, Deploy and Process Insurance Claim Forms](https://www.textcontrol.com/blog/2023/06/09/use-case-create-deploy-and-process-insurance-claim-forms/llms.txt)
- [Table Extension: Remove Empty Columns After Mail Merge](https://www.textcontrol.com/blog/2023/06/08/table-extension-remove-empty-columns-after-mail-merge/llms.txt)
- [An Ultimate Guide to Mail Merge with MS Word Documents in C#](https://www.textcontrol.com/blog/2023/06/07/an-ultimate-guide-to-mail-merge-with-ms-word-documents-in-csharp/llms.txt)
- [Preparing Projects for GitHub Actions](https://www.textcontrol.com/blog/2022/06/27/preparing-projects-for-github-actions/llms.txt)
- [Mail Merge with MS Word Documents in C# - An Ultimate Guide](https://www.textcontrol.com/blog/2022/05/25/mail-merge-with-ms-word-documents-in-csharp-an-ultimate-guide/llms.txt)
- [Combining MailMerge and Table of Contents](https://www.textcontrol.com/blog/2022/03/22/combining-mailmerge-and-table-of-contents/llms.txt)
- [Merging Form Fields using the MailMerge Class](https://www.textcontrol.com/blog/2022/02/21/merging-form-fields-using-the-mailmerge-class/llms.txt)
- [MailMerge: Rendering Conditional Table Rows](https://www.textcontrol.com/blog/2022/02/17/mailmerge-rendering-conditional-table-rows/llms.txt)
- [Merging Barcodes with JSON Data in C#](https://www.textcontrol.com/blog/2022/02/14/merging-barcodes-with-json-data-in-csharp/llms.txt)
- [Detect Toggle Button Changes Using a MutationObserver](https://www.textcontrol.com/blog/2021/11/11/detect-toggle-button-changes-using-a-mutationobserver/llms.txt)
- [Merging Merge Block Cells Vertically with Matching Content](https://www.textcontrol.com/blog/2021/10/20/vertically-merge-table-cells-of-merge-blocks-for-matching-content/llms.txt)
- [Creating PDF Documents from MS Word DOCX in C#](https://www.textcontrol.com/blog/2021/02/26/creating-pdf-documents-from-ms-word-docx-in-csharp/llms.txt)
- [ASP.NET Core: Server-Side MailMerge](https://www.textcontrol.com/blog/2020/04/28/aspnet-core-server-side-mailmerge/llms.txt)
