# Reporting: Merge Formatted HTML into Merge Fields

> The FieldMerged event in the TX Text Control MailMerge component allows injecting formatted HTML content into merge fields during the merge process. HTML stored in XML CDATA sections is loaded through ServerTextControl and converted using the PreserveFormatting property flag.

- **Author:** Bjoern Meyer
- **Published:** 2014-02-10
- **Modified:** 2026-07-17
- **Description:** The FieldMerged event in the TX Text Control MailMerge component allows injecting formatted HTML content into merge fields during the merge process. HTML stored in XML CDATA sections is loaded through ServerTextControl and converted using the PreserveFormatting property flag.
- **3 min read** (504 words)
- **Tags:**
  - Reporting
  - Tutorial
- **Web URL:** https://www.textcontrol.com/blog/2014/02/10/reporting-merge-formatted-html-into-merge-fields/
- **LLMs URL:** https://www.textcontrol.com/blog/2014/02/10/reporting-merge-formatted-html-into-merge-fields/llms.txt
- **LLMs-Full URL:** https://www.textcontrol.com/blog/2014/02/10/reporting-merge-formatted-html-into-merge-fields/llms-full.txt
- **GitHub Repository:** https://github.com/TextControl/TextControl.MailMerge.MergeFormattedHTML

---

The basic concept of Text Control Reporting is to keep the text formatting in templates and to merge plain text values into the final report. The string format can be adjusted in the field properties to add currency symbols or to define the number of decimal places.

Sometimes, formatted text should be added to parts of a document. Generally, the key concept for this purpose would be an [INCLUDETEXT](https://www.textcontrol.com/technology/reporting/) field where sub-templates can be merged into master templates. But consider applications, where HTML formatted text is stored in database fields and not in physical sub-templates.

TX Text Control's [MailMerge](https://docs.textcontrol.com/textcontrol/windows-forms/ref.txtextcontrol.documentserver.mailmerge.class.htm) component provides a very flexible event handling that enables you to change the field text during the merge process.

This tutorial shows how to create templates with fields that are merged with formatted HTML from an XML data source.

Let's have a look at the XML file that is the data source for this tutorial:

```
<report>
    <company>
        <name>Text Control, LLC</name>
        <company_description><![CDATA[<p style="font-size: 16pt; color: red;">Founded in 1991, Text Control is an <strong>award-winning Visual Studio Industry Partner</strong> and leading vendor of word processing and reporting components for Microsoft development technologies.</p>]]></company_description>
    </company>
    <company>
        <name>Text Control GmbH</name>
        <company_description><![CDATA[<ol><li>List Item 1</li><li>List Item 2</li><li>List Item 3</li><li><strong>List Item 4</strong></li></ol>]]></company_description>
    </company>
</report>
```

In the element *company\_description*, HTML code is included in a *CDATA* section that should not be parsed by the XML parser of *DataSet*.

In the template, a merge field with the name *company\_description* is added. The switch *Preserve formatting during updates* is used to indicate that this field contains HTML text.

![Merge formatted HTML into merge fields](https://s1-www.textcontrol.com/assets/dist/blog/2014/02/10/a/assets/tx_formatted_text.webp "Merge formatted HTML into merge fields")The event [FieldMerged](https://docs.textcontrol.com/textcontrol/windows-forms/ref.txtextcontrol.documentserver.mailmerge.fieldmerged.event.htm) is raised for each successful merge of a single field. In this event, we will check for the *Preserve formatting during updates* switch which is accessible through the [MergeField.PreserveFormatting](https://docs.textcontrol.com/textcontrol/windows-forms/ref.txtextcontrol.documentserver.fields.mailmergefieldadapter.preserveformatting.property.htm) property. If this flag is set to true, the field content is loaded as HTML into a temporary *ServerTextControl* and then returned as formatted text to the event argument [e.MergedField](https://docs.textcontrol.com/textcontrol/windows-forms/ref.txtextcontrol.documentserver.mailmerge.fieldmergedeventargs.mergedfield.property.htm).

```
private void mailMerge1_FieldMerged(object sender,
    TXTextControl.DocumentServer.MailMerge.FieldMergedEventArgs e)
{
    // check whether the field is a MergeField
    if(e.MailMergeFieldAdapter.TypeName != "MERGEFIELD")
        return;

    MergeField field = e.MailMergeFieldAdapter as MergeField;

    // if switch "Preserve formatting during updates" is set
    // load HTML data into temporary ServerTextControl and return
    // formatted text
    if (field.PreserveFormatting == true)
    {
        byte[] data;

        using (ServerTextControl tx = new ServerTextControl())
        {
            tx.Create();

            tx.Load(field.Text, StringStreamType.HTMLFormat);
            tx.Save(out data, BinaryStreamType.InternalUnicodeFormat);
        }

        e.MergedField = data;
    }
}
```

The resulting document contains the HTML formatted text:

![Merge formatted HTML into merge fields](https://s1-www.textcontrol.com/assets/dist/blog/2014/02/10/a/assets/tx_format_results.webp "Merge formatted HTML into merge fields")

---

## 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

- [Creating Your First ASP.NET Reporting Application](https://www.textcontrol.com/blog/2020/01/01/creating-your-first-aspnet-reporting-application/llms.txt)
- [New Online Sample: Build your First Report](https://www.textcontrol.com/blog/2019/07/03/build-your-first-report/llms.txt)
- [Create your First Document with ReportingCloud](https://www.textcontrol.com/blog/2019/02/19/create-your-first-document-with-reportingcloud/llms.txt)
- [MailMerge: Starting Each Merge Block on a New Page](https://www.textcontrol.com/blog/2016/09/09/mailmerge-starting-each-merge-block-on-a-new-page/llms.txt)
- [Using MailMerge with JSON Data](https://www.textcontrol.com/blog/2015/08/04/using-mailmerge-with-json-data/llms.txt)
- [Merging Documents with RESTful Web API's](https://www.textcontrol.com/blog/2015/07/31/merging-documents-with-restful-web-apis/llms.txt)
- [Windows Forms: Printing Multiple Pages Per Sheet](https://www.textcontrol.com/blog/2015/07/24/windows-forms-printing-multiple-pages-per-sheet/llms.txt)
- [Inserting Watermark Images to All Pages Dynamically](https://www.textcontrol.com/blog/2015/07/17/inserting-watermark-images-to-all-pages-dynamically/llms.txt)
- [Reporting: Sorting Merge Block Rows by Column Name](https://www.textcontrol.com/blog/2015/07/15/reporting-sorting-merge-block-rows-by-column-name/llms.txt)
- [MailMerge with the Entity Framework Using Database First](https://www.textcontrol.com/blog/2015/06/24/mailmerge-with-the-entity-framework-using-database-first/llms.txt)
- [Checked and Unchecked Check Boxes with IF Fields](https://www.textcontrol.com/blog/2015/03/25/checked-and-unchecked-check-boxes-with-if-fields/llms.txt)
- [Reporting: Conditional Formatted Text Blocks](https://www.textcontrol.com/blog/2015/03/13/reporting-conditional-formatted-text-blocks/llms.txt)
- [Reporting Best Practices and How-To Guides](https://www.textcontrol.com/blog/2015/02/05/reporting-best-practices-and-how-to-guides/llms.txt)
- [MailMerge: Master Table and Client Tables](https://www.textcontrol.com/blog/2015/01/28/mailmerge-master-table-and-client-tables/llms.txt)
- [MailMerge: Formatting Numeric Strings in Merge Fields](https://www.textcontrol.com/blog/2015/01/22/mailmerge-formatting-numeric-strings-in-merge-fields/llms.txt)
- [MailMerge: Merge CheckBoxes During the Merge Process](https://www.textcontrol.com/blog/2015/01/12/mailmerge-merge-checkboxes-during-the-merge-process/llms.txt)
- [MailMerge: Conditional INCLUDETEXT Fields](https://www.textcontrol.com/blog/2015/01/08/mailmerge-conditional-includetext-fields/llms.txt)
- [HTML5 Technical Considerations - The Concept Explained](https://www.textcontrol.com/blog/2014/10/16/html5-technical-considerations-the-concept-explained/llms.txt)
- [TX Text Control Web: Attaching Events to Ribbon Elements](https://www.textcontrol.com/blog/2014/10/07/tx-text-control-web-attaching-events-to-ribbon-elements/llms.txt)
- [TX Text Control Web: Customize the Ribbon Bar](https://www.textcontrol.com/blog/2014/10/03/tx-text-control-web-customize-the-ribbon-bar/llms.txt)
- [Text Control Web - Fundamental Concepts: The Data Source](https://www.textcontrol.com/blog/2014/10/02/text-control-web-fundamental-concepts-the-data-source/llms.txt)
- [Reporting: Removing Empty Table Rows](https://www.textcontrol.com/blog/2014/09/30/reporting-removing-empty-table-rows/llms.txt)
- [MS Word Content Controls Field Adapter Classes](https://www.textcontrol.com/blog/2014/08/05/ms-word-content-controls-field-adapter-classes/llms.txt)
- [Reporting: Merge Blocks and Structured Numbered Lists](https://www.textcontrol.com/blog/2014/07/29/reporting-merge-blocks-and-structured-numbered-lists/llms.txt)
- [Creating Avery Labels Using Text Control Reporting](https://www.textcontrol.com/blog/2014/07/22/creating-avery-labels-using-text-control-reporting/llms.txt)
