# MailMerge: Using IncludeText Fields to Inject Formatted Text

> IncludeText fields can embed dynamically generated content using the IncludeTextMerging event rather than referencing physical files. The Filename property of each field acts as an identifier that controls which formatted text block a temporary ServerTextControl renders and injects.

- **Author:** Bjoern Meyer
- **Published:** 2009-10-09
- **Modified:** 2026-07-17
- **Description:** IncludeText fields can embed dynamically generated content using the IncludeTextMerging event rather than referencing physical files. The Filename property of each field acts as an identifier that controls which formatted text block a temporary ServerTextControl renders and injects.
- **4 min read** (615 words)
- **Tags:**
  - Mail Merge
- **Web URL:** https://www.textcontrol.com/blog/2009/10/09/mailmerge-using-includetext-fields-to-inject-formatted-text/
- **LLMs URL:** https://www.textcontrol.com/blog/2009/10/09/mailmerge-using-includetext-fields-to-inject-formatted-text/llms.txt
- **LLMs-Full URL:** https://www.textcontrol.com/blog/2009/10/09/mailmerge-using-includetext-fields-to-inject-formatted-text/llms-full.txt

---

Generally, [IncludeText](https://docs.textcontrol.com/textcontrol/windows-forms/ref.txtextcontrol.documentserver.fields.includetext.class.htm) fields contain a reference to external documents that will be included during the mail merge process. That allows you to store a specific sub-template separately in order to reuse it in other templates.

But these fields can do much more. This article shows how to use the [IncludeTextMerging](https://docs.textcontrol.com/textcontrol/windows-forms/ref.txtextcontrol.documentserver.mailmerge.includetextmerging.event.htm) event to inject dynamic formatted content. In this case, the *Filename* parameter of the *IncludeText* field can be used to save a specific identifier like an ID or a field name. Based on this value, we can decide which document or text should be inserted. This approach keeps the sub-templates more flexible as we do not need to store the actual name of each sub-template nor do we need to save the sub-templates physically.

Our sample template just contains two *IncludeText* fields and some text:

![Template](https://s1-www.textcontrol.com/assets/dist/blog/2009/10/09/a/assets/blog_template_mailmerge.webp "Template")

In the following code, this template is loaded, the *IncludeTextMerging* event is attached and merged. As we do not have any normal merge fields in the template, we can call the [Merge](https://docs.textcontrol.com/textcontrol/windows-forms/ref.txtextcontrol.documentserver.mailmerge.merge.method.htm) method without a *DataTable*. Instead of passing data, *null* is passed.

```
mailMerge1.LoadTemplate("template.docx", TXTextControl.DocumentServer.FileFormat.WordprocessingML);
mailMerge1.IncludeTextMerging += new TXTextControl.DocumentServer.MailMerge.IncludeTextMergingHandler(
                                 mailMerge1_IncludeTextMerging);
mailMerge1.Merge(null, false);
mailMerge1.SaveDocument("results.pdf", TXTextControl.StreamType.AdobePDF,
                        new TXTextControl.SaveSettings());
```

Now the interesting part. In the *IncludeTextMerging* event, a new [ServerTextControl](https://docs.textcontrol.com/textcontrol/windows-forms/ref.txtextcontrol.servertextcontrol.class.htm) instance is created to provide the formatted text. At this point, it is also possible to load a sub-template from a database or a physical file.

The event arguments return the *Filename* property of the *IncludeText* field which is used as an identifier in our scenario. A simple *switch* statement shows how to handle this argument. Based on the field’s [Filename](https://docs.textcontrol.com/textcontrol/windows-forms/ref.txtextcontrol.documentserver.mailmerge.includetextmergingeventargs.filename.property.htm) value, different text is added to the temporary *ServerTextContro*l. After some formatting operations, the content is saved in a *byte* array in the internal TX Text Control format. This *byte* array can be passed to the [IncludeTextDocument](https://docs.textcontrol.com/textcontrol/windows-forms/ref.txtextcontrol.documentserver.mailmerge.includetextmergingeventargs.includetextdocument.property.htm) property of the [IncludeTextEvent](https://docs.textcontrol.com/textcontrol/windows-forms/ref.txtextcontrol.documentserver.mailmerge.includetextmergingeventargs.class.htm) arguments. The complete document will be inserted at the current field location.

```
void mailMerge1_IncludeTextMerging(object sender,
                                   TXTextControl.DocumentServer.MailMerge.IncludeTextMergingEventArgs e)
{
    TXTextControl.ServerTextControl tempTX = new TXTextControl.ServerTextControl();
    tempTX.Create();

    switch (e.Filename)
    {
        case "\field1":
            tempTX.Text = "Field 1 inserted at " + DateTime.Now.ToString();
            tempTX.Select(0, 7);
            tempTX.Selection.Bold = true;
            break;

        case "\field2":
            tempTX.Text = "Field 2 inserted at " + DateTime.Now.ToString();
            tempTX.Select(0, 7);
            tempTX.Selection.Italic = true;
            break;
    }

  byte[] data;

    tempTX.Save(out data, TXTextControl.BinaryStreamType.InternalUnicodeFormat);
    tempTX.Dispose();

    e.IncludeTextDocument = data;
}
```

Using this approach, you can insert complete documents or just dynamic formatted field text which is not possible using the normal merge fields. The idea of the merge field is that the formatting is part of the template and the merge process just replaces the text. The following screenshot shows the resultsing PDF document with the injected formatted text.

![Document](https://s1-www.textcontrol.com/assets/dist/blog/2009/10/09/a/assets/blog_template_mailmerge_pdf.webp "Document")

[Download sample project](https://s1-www.textcontrol.com/assets/dist/blog/2009/10/09/a/assets/tx_mailmerge_includetext.zip)

Feel free to post your questions or comments on this.

---

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

- [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)
- [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)
- [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)
- [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)
- [Angular: Loading Excerpt JSON Data on Initializing TX Text Control](https://www.textcontrol.com/blog/2020/09/02/angular-loading-excerpt-json-data-on-initializing-txtextcontrol/llms.txt)
- [ASP.NET Core: Server-Side MailMerge](https://www.textcontrol.com/blog/2020/04/28/aspnet-core-server-side-mailmerge/llms.txt)
- [Windows Forms Ribbon: Displaying User-Friendly Merge Field Names](https://www.textcontrol.com/blog/2020/01/14/displaying-user-friendly-merge-field-names-in-the-ribbon/llms.txt)
- [MailMerge Class Settings Explained](https://www.textcontrol.com/blog/2019/12/05/mailmerge-settings-explained/llms.txt)
- [Merge Excel Documents into MailMerge Templates using IncludeText Fields](https://www.textcontrol.com/blog/2019/08/13/merge-excel-documents-into-mailmerge-templates/llms.txt)
- [Using MailMerge with Chart Objects and JSON Data](https://www.textcontrol.com/blog/2019/07/18/using-mailmerge-with-chart-objects-and-json-data/llms.txt)
- [MailMerge: Conditional Table Cell Colors using Filter Instructions](https://www.textcontrol.com/blog/2019/06/06/mailmerge-conditional-table-cell-colors-using-filter-instructions/llms.txt)
- [MailMerge: Using Filters to Remove Unwanted Rows](https://www.textcontrol.com/blog/2019/06/04/mailmerge-using-filters-to-remove-unwanted-rows/llms.txt)
- [MailMerge: Conditional Rendering of Merge Blocks](https://www.textcontrol.com/blog/2018/11/28/mailmerge-conditional-rendering-of-merge-blocks/llms.txt)
- [DataSourceManager: Using the Ready-to-Use Reporting Dialog Boxes](https://www.textcontrol.com/blog/2018/11/27/datasourcemanager-using-the-ready-to-use-reporting-dialog-boxes/llms.txt)
- [Merge Blocks in X16: Filtering, Sorting and Conditional Rendering](https://www.textcontrol.com/blog/2018/11/22/merge-blocks-in-x16-filtering-sorting-and-conditional-rendering/llms.txt)
