# Merging Templates with MailMerge with Different Merge Field Settings in C#

> This article shows how to merge templates with different merge field settings using the MailMerge class. The various settings allow for the removal of unused fields or the possibility to make changes to fields after the merge process.

- **Author:** Bjoern Meyer
- **Published:** 2023-12-16
- **Modified:** 2025-11-16
- **Description:** This article shows how to merge templates with different merge field settings using the MailMerge class. The various settings allow for the removal of unused fields or the possibility to make changes to fields after the merge process.
- **4 min read** (727 words)
- **Tags:**
  - ASP.NET Core
  - ASP.NET
  - Document Editor
  - MailMerge
- **Web URL:** https://www.textcontrol.com/blog/2023/12/16/merging-templates-with-mailmerge-with-different-merge-field-settings/
- **LLMs URL:** https://www.textcontrol.com/blog/2023/12/16/merging-templates-with-mailmerge-with-different-merge-field-settings/llms.txt
- **LLMs-Full URL:** https://www.textcontrol.com/blog/2023/12/16/merging-templates-with-mailmerge-with-different-merge-field-settings/llms-full.txt

---

When providing users with the ability to modify documents after they have been generated by MailMerge, there are several options you can utilize.

### Merge Data

Consider the following scenario where a particular JSON data source is being used for template creation.

```
[
  {
    "id": 1,
    "customer": {
      "id": 1,
      "name": "John Doe",
      "street": "123 Main St",
      "city": "Anytown",
      "state": "TX",
      "zip": "12345"
    },
    "items": [
      {
        "id": 1,
        "name": "Widget",
        "price": 9.99,
        "quantity": 2
      },
      {
        "id": 2,
        "name": "Thing",
        "price": 5.99,
        "quantity": 5
      },
      {
        "id": 3,
        "name": "Gadget",
        "price": 199.99,
        "quantity": 1
      }
    ],
    "total": 0
  },
{
    "id": 2,
    "customer": {
    ...
```

The controller loads this JSON and passes it to the view, where it is loaded into the editor to populate the dropdowns with the correct merge field names.

```
public IActionResult Index()
{
    string json = System.IO.File.ReadAllText("App_Data/data.json");
    ViewBag.Data = json;

    return View();
}
```

### Sending Data to Endpoint

The view code loads the JSON and makes a jquery AJAX call to send the create template to the *Merge* endpoint.

```
@using TXTextControl.Web.MVC

@Html.TXTextControl().TextControl().LoadDataFromJson(ViewBag.Data).Render()

<input type="button" onclick="mergeDocument()" value="Merge Document" />

@section Scripts {

    <script>

        function mergeDocument() {
            TXTextControl.saveDocument(TXTextControl.StreamType.InternalUnicodeFormat, function (results) {
                $.ajax({
                    url: '@Url.Action("Merge")',
                    type: 'POST',
                    contentType: "application/json",
                    data: JSON.stringify(results),
                    success: function (data) {
                        TXTextControl.loadDocument(TXTextControl.StreamType.InternalUnicodeFormat, data);
                    }
                });
            });
        }

    </script>

}
```

The editor enables users to create the template by adding available merge fields and merge blocks.

![Creating documents with TX Text Control](https://s1-www.textcontrol.com/assets/dist/blog/2023/12/16/a/assets/template.webp "Creating documents with TX Text Control")

After the merge process, the returned document contains merged content and the field functionality is removed.

![Creating documents with TX Text Control](https://s1-www.textcontrol.com/assets/dist/blog/2023/12/16/a/assets/template2.webp "Creating documents with TX Text Control")

### Preserve Empty Fields

Now consider that there are fields in the template that have no data. By default, these fields are removed after the merge.

![Creating documents with TX Text Control](https://s1-www.textcontrol.com/assets/dist/blog/2023/12/16/a/assets/template3.webp "Creating documents with TX Text Control")

The following screenshot shows the merged document with the empty "MERGEFIELD" field removed.

![Creating documents with TX Text Control](https://s1-www.textcontrol.com/assets/dist/blog/2023/12/16/a/assets/template4.webp "Creating documents with TX Text Control")

By setting the RemoveEmptyFields property to false, these fields are retained and the field functionality is preserved.

```
using (MailMerge mm = new MailMerge())
{
    mm.TextComponent = tx;

    mm.RemoveEmptyFields = false;

    // load json data
    string json = System.IO.File.ReadAllText("App_Data/data.json");
    // merge data
    mm.MergeJsonData(json, true);
}
```

> **Learn More**
> 
> The MailMerge class provides several settings to control the merge process and formatting in the resulting document. There are several switches to control how text fields, blocks, images, and blank lines are handled. This article provides an overview of when and how to use each option.
> 
> [MailMerge Class Settings Explained ](https://www.textcontrol.com/blog/2019/12/05/mailmerge-settings-explained/llms-full.txt)

### Preserve Field Functionality

But now consider a scenario in which you want the merged fields to be editable after the merge process is complete. In this case, there is not a property that is available, but there is a typical workflow that allows this to be realized. Each field provides an event that fires when fields are merged. It is usually used to change the contents of the field after the merge.

The advantage of this process is that the functionality of the field is maintained after the field is changed.

The following code attaches the event to the MailMerge object, passing the merged content back to the field.

```
[HttpPost]
public IActionResult Merge([FromBody] Content results)
{
    using (TXTextControl.ServerTextControl tx = new TXTextControl.ServerTextControl())
    {
        tx.Create();
        tx.Load(Convert.FromBase64String(results.Data), TXTextControl.BinaryStreamType.InternalUnicodeFormat);

        using (MailMerge mm = new MailMerge())
        {
            mm.TextComponent = tx;

            mm.FieldMerged += Mm_FieldMerged;

            // load json data
            string json = System.IO.File.ReadAllText("App_Data/data.json");
            // merge data
            mm.MergeJsonData(json, true);
        }

        tx.Save(out byte[] data, TXTextControl.BinaryStreamType.InternalUnicodeFormat);

        return Ok(Convert.ToBase64String(data));
    } 
}

private void Mm_FieldMerged(object sender, MailMerge.FieldMergedEventArgs e)
{
    e.MergedField = e.MergedField;
}
```

As a result, the merge fields are populated, but the field functionality remains intact.

![Creating documents with TX Text Control](https://s1-www.textcontrol.com/assets/dist/blog/2023/12/16/a/assets/template5.webp "Creating documents with TX Text Control")

This allows users to modify these fields and other content and merge the template again Another use case is previewing live data during the template creation process without losing field functionality.

---

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

- [Create SignFabric Envelopes from Mail Merge Templates in .NET C#](https://www.textcontrol.com/blog/2026/06/23/create-signfabric-envelopes-from-mail-merge-templates-using-dotnet-csharp/llms.txt)
- [Beyond WebSockets: A Glimpse into the Future of Document Editing with WebAssembly](https://www.textcontrol.com/blog/2026/06/10/beyond-websockets-glimpse-future-document-editing-webassembly/llms.txt)
- [5 Layout Patterns for Integrating the TX Text Control Document Editor in ASP.NET Core C#](https://www.textcontrol.com/blog/2026/04/09/5-layout-patterns-for-integrating-the-tx-text-control-document-editor-in-aspnet-core-csharp/llms.txt)
- [Introducing Text Control Agent Skills](https://www.textcontrol.com/blog/2026/03/27/introducing-text-control-agent-skills/llms.txt)
- [Deploying the TX Text Control Document Editor from the Private NuGet Feed to Azure App Services (Linux and Windows)](https://www.textcontrol.com/blog/2026/03/25/deploying-the-tx-text-control-document-editor-from-the-private-nuget-feed-to-azure-app-services-linux-and-windows/llms.txt)
- [Build a Custom Backstage View in ASP.NET Core with TX Text Control](https://www.textcontrol.com/blog/2026/02/17/build-a-custom-backstage-view-in-aspnet-core-with-tx-text-control/llms.txt)
- [ASP.NET Core Document Editor with Backend via the Text Control Private NuGet Feed](https://www.textcontrol.com/blog/2026/02/09/aspnet-core-document-editor-private-nuget-feed/llms.txt)
- [Why Document Processing Libraries Require a Document Editor](https://www.textcontrol.com/blog/2025/12/04/why-document-processing-libraries-require-a-document-editor/llms.txt)
- [Getting Started Video Tutorial: Document Editor in ASP.NET Core C# on Linux](https://www.textcontrol.com/blog/2025/07/29/getting-started-video-tutorial-document-editor-aspnet-core-csharp-linux/llms.txt)
- [Use MailMerge in .NET on Linux to Generate Pixel-Perfect PDFs from DOCX Templates](https://www.textcontrol.com/blog/2025/05/27/use-mailmerge-in-dotnet-on-linux-to-generate-pixel-perfect-pdfs-from-docx-templates/llms.txt)
- [Generating Dynamic NDAs Using TX Text Control MailMerge in C# .NET](https://www.textcontrol.com/blog/2025/04/08/generating-dynamic-ndas-using-tx-text-control-mailmerge-in-csharp-dotnet/llms.txt)
- [Designing a Maintainable PDF Generation Web API in ASP.NET Core (Linux) C# with Clean Architecture and TX Text Control](https://www.textcontrol.com/blog/2025/03/27/designing-a-maintainable-pdf-generation-web-api-in-asp-net-core-linux-c-sharp-with-clean-architecture-and-tx-text-control/llms.txt)
- [Deploying the TX Text Control Document Editor in an ASP.NET Core Web App to Azure App Services](https://www.textcontrol.com/blog/2025/03/26/deploying-the-tx-text-control-document-editor-in-an-asp-net-core-web-app-to-azure-app-services/llms.txt)
- [Building an ASP.NET Core Backend (Linux and Windows) for the Document Editor and Viewer](https://www.textcontrol.com/blog/2025/03/26/building-an-asp-net-core-backend-for-the-document-editor-and-viewer/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)
- [TX Text Control Document Editor and Viewer for Blazor Released](https://www.textcontrol.com/blog/2025/03/25/tx-text-control-document-editor-and-viewer-for-blazor-released/llms.txt)
- [Getting Started: Document Editor for Blazor in ASP.NET Core](https://www.textcontrol.com/blog/2025/03/25/getting-started-document-editor-for-blazor-in-asp-net-core/llms.txt)
- [Introducing TXTextControl.Web.Server.Core: A Cross-Platform Backend for TX Text Control Document Editor](https://www.textcontrol.com/blog/2025/03/13/introducing-txtextcontrol-web-server-core-a-cross-platform-backend-for-tx-text-control-document-editor/llms.txt)
- [Getting Started: Document Editor with ASP.NET Core and Docker Support with Linux Containers](https://www.textcontrol.com/blog/2025/03/12/getting-started-document-editor-with-asp-net-core-and-docker-support-with-linux-containers/llms.txt)
- [Announcing Our Work on a Blazor Component for Document Editing and Viewing](https://www.textcontrol.com/blog/2025/01/24/announcing-our-work-on-a-blazor-component-for-document-editing-and-viewing/llms.txt)
- [Manipulating Table Cells During the MailMerge Process in .NET C#](https://www.textcontrol.com/blog/2024/12/03/manipulating-table-cells-during-the-mailmerge-process-in-net-csharp/llms.txt)
- [Preparing Documents for E-Signing for Multiple Signers in .NET C#](https://www.textcontrol.com/blog/2024/11/13/preparing-documents-for-e-signing-for-multiple-signers-in-net-c-sharp/llms.txt)
- [ASP.NET Core: Use the Document Editor and Viewer in the Same Razor View](https://www.textcontrol.com/blog/2024/11/08/asp-net-core-use-the-document-editor-and-viewer-in-the-same-razor-view/llms.txt)
- [When to Generate Documents Server-Side Instead of Client-Side: A Focus on Data Security](https://www.textcontrol.com/blog/2024/10/04/when-to-generate-documents-server-side-instead-of-client-side-a-focus-on-data-security/llms.txt)
- [Mail Merge: Inserting Merge Blocks using the DataSourceManager in C#](https://www.textcontrol.com/blog/2024/10/02/mail-merge-inserting-merge-blocks-using-the-datasourcemanager-in-csharp/llms.txt)
