# Merging Form Fields using the MailMerge Class

> Usually, the MailMerge class is used to merge data into document templates to replace merge fields. Since version 30.0, it is possible to merge data into form fields.

- **Author:** Bjoern Meyer
- **Published:** 2022-02-21
- **Modified:** 2025-11-16
- **Description:** Usually, the MailMerge class is used to merge data into document templates to replace merge fields. Since version 30.0, it is possible to merge data into form fields.
- **3 min read** (493 words)
- **Tags:**
  - ASP.NET
  - ASP.NET Core
  - Mail Merge
  - Windows Forms
  - WPF
- **Web URL:** https://www.textcontrol.com/blog/2022/02/21/merging-form-fields-using-the-mailmerge-class/
- **LLMs URL:** https://www.textcontrol.com/blog/2022/02/21/merging-form-fields-using-the-mailmerge-class/llms.txt
- **LLMs-Full URL:** https://www.textcontrol.com/blog/2022/02/21/merging-form-fields-using-the-mailmerge-class/llms-full.txt

---

The MailMerge class is usually used to merge objects or JSON data into document templates to populate merge fields. Since version 30.0, the *MailMerge* class can be used to merge data into form fields as well.

### Merging Form Fields

The FormFieldMergeType defines how form fields are treated during the merge process.

| Member | Description |
|---|---|
| None | Form fields are not merged at all. |
| Preselect | Form field content is preselected when possible. Form fields not associated with a data table column are unaffected. |
| Replace | Form fields are replaced with database content. Empty form fields are removed according to property RemoveEmptyFields property. |

Consider the following sample template that contains different form fields including checkboxes, drop-downs and form text fields:

![Form Template](https://s1-www.textcontrol.com/assets/dist/blog/2022/02/21/a/assets/form.webp "Form Template")

### Pre-Populate Form Fields

The *MailMerge* class can now be used for 2 different scenarios. In the first scenario, the form should be pre-populated with known data to help users by minimizing their efforts to complete the form. The following code shows how to load the template into a *ServerTextControl* instance to merge it using the *MailMerge* engine:

```
// create the pre-populate data object
HealthcareForm data = new HealthcareForm() {
  insurance_date = DateTime.Now,
  insurance_name = "Global Health",
  insurance_check = true,
  insurance_state = "North Carolina",
  notes = "Thanks for your business."
};

using (TXTextControl.ServerTextControl tx = new TXTextControl.ServerTextControl()) {
  tx.Create();
  // load the template
  tx.Load("App_Data/health_form.tx", TXTextControl.StreamType.InternalUnicodeFormat);

  using (MailMerge mm = new MailMerge()) {
    mm.TextComponent = tx;
    
    // pre-populate the form fields
    mm.FormFieldMergeType = FormFieldMergeType.Preselect;
    mm.MergeObject(data);
  }

  // save document as PDF
  byte[] document;
  tx.Save(out document, TXTextControl.BinaryStreamType.AdobePDF);
}
```

The following screenshot shows the PDF document in Acrobat Reader. The form fields are pre-populated with the merged data:

![Form Template](https://s1-www.textcontrol.com/assets/dist/blog/2022/02/21/a/assets/results1.webp "Form Template")

### Flatten Form Fields

In the second scenario, the document should be flattened by removing all form fields and replacing them with the actual merge data:

```
HealthcareForm data = new HealthcareForm() {
  insurance_date = new DateTime(2020,1,1),
  insurance_name = "Health Providers",
  insurance_check = true,
  insurance_state = "Georgia",
  notes = "Thanks for your business."
};

using (TXTextControl.ServerTextControl tx = new TXTextControl.ServerTextControl()) {
  tx.Create();
  tx.Load("App_Data/health_form.tx", TXTextControl.StreamType.InternalUnicodeFormat);

  using (MailMerge mm = new MailMerge()) {
    mm.TextComponent = tx;
    mm.FormFieldMergeType = FormFieldMergeType.Replace;
    mm.MergeObject(data);
  }

  // save document as PDF
  byte[] document;
  tx.Save(out document, TXTextControl.BinaryStreamType.AdobePDF);
}
```

In the resulting document, all form fields are removed and the document is not an editable form document anymore:

![Form Template](https://s1-www.textcontrol.com/assets/dist/blog/2022/02/21/a/assets/results2.webp "Form Template")

The *MailMerge* class can be used to merge both merge fields and form fields in the same merge process. This helps to generate forms with dynamic, non-editable data (merge fields) and pre-populated form fields that can be edited by end-users. Pre-populating fields in a form eliminates errors, increases the user acceptance and provides a better user experience.

---

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

- [MailMerge: Rendering Conditional Table Rows](https://www.textcontrol.com/blog/2022/02/17/mailmerge-rendering-conditional-table-rows/llms.txt)
- [TXTextControl.Markdown.Core 34.1.0-beta: Work with Full Documents, Selection, and SubTextParts](https://www.textcontrol.com/blog/2026/04/14/txtextcontrol-markdown-core-34-1-0-beta-work-with-full-documents-selection-and-subtextparts/llms.txt)
- [TX Text Control 34.0 SP2 is Now Available: What's New in the Latest Version](https://www.textcontrol.com/blog/2026/02/18/tx-text-control-34-0-sp2-is-now-available/llms.txt)
- [TX Text Control 34.0 SP1 is Now Available: What's New in the Latest Version](https://www.textcontrol.com/blog/2025/12/03/tx-text-control-34-0-sp1-is-now-available/llms.txt)
- [Introducing TX Text Control 34.0: Your Next Leap in Document Processing](https://www.textcontrol.com/blog/2025/11/10/introducing-tx-text-control-34-0-your-next-leap-in-document-processing/llms.txt)
- [Sneak Peek: TX Text Control 34.0 Coming November 2025](https://www.textcontrol.com/blog/2025/10/02/sneak-peek-tx-text-control-34-0-coming-november-2025/llms.txt)
- [TX Text Control 33.0 SP3 is Now Available: What's New in the Latest Version](https://www.textcontrol.com/blog/2025/08/14/tx-text-control-33-0-sp3-is-now-available/llms.txt)
- [TX Text Control 33.0 SP2 is Now Available: What's New in the Latest Version](https://www.textcontrol.com/blog/2025/06/18/tx-text-control-33-0-sp2-is-now-available/llms.txt)
- [Document Lifecycle Optimization: Leveraging TX Text Control's Internal Format](https://www.textcontrol.com/blog/2025/05/16/document-lifecycle-optimization-leveraging-tx-text-controls-internal-format/llms.txt)
- [Expert Implementation Services for Legacy System Modernization](https://www.textcontrol.com/blog/2025/05/07/expert-implementation-services-for-legacy-system-modernization/llms.txt)
- [Service Pack Releases: What's New in TX Text Control 33.0 SP1 and 32.0 SP5](https://www.textcontrol.com/blog/2025/05/07/service-pack-releases-whats-new-in-tx-text-control-33-0-sp1-and-32-0-sp5/llms.txt)
- [Top 5 Real-World Applications for TX Text Control Document Processing Libraries](https://www.textcontrol.com/blog/2025/04/01/top-5-real-world-applications-for-tx-text-control-document-processing-libraries/llms.txt)
- [DWX Developer Week Moves to Mannheim - And Text Control Is on Board!](https://www.textcontrol.com/blog/2025/03/19/dwx-developer-week-moves-to-mannheim-and-tx-text-control-is-on-board/llms.txt)
- [The Wait is Over: TX Text Control for Linux is Officially Here](https://www.textcontrol.com/blog/2025/03/12/the-wait-is-over-tx-text-control-for-linux-is-officially-here/llms.txt)
- [Full .NET 9 Support in Text Control .NET Components for ASP.NET Core, Windows Forms, and WPF](https://www.textcontrol.com/blog/2024/11/11/full-net-9-support-in-text-control-net-components-for-asp-net-core-windows-forms-and-wpf/llms.txt)
- [Toggle Field Codes in TX Text Control](https://www.textcontrol.com/blog/2024/11/07/toggle-field-codes-in-tx-text-control/llms.txt)
- [Inserting MergeBlocks with the DataSourceManager and Applying Table Styles in C#](https://www.textcontrol.com/blog/2024/04/09/inserting-mergeblocks-with-the-datasourcemanager-and-applying-table-styles-in-csharp/llms.txt)
- [Sneak Peek 32.0: Modifying the Normal Stylesheet](https://www.textcontrol.com/blog/2023/07/04/sneak-peek-320-modifying-the-normal-stylesheet/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)
- [Version 30.0: Changes in Licenses.licx](https://www.textcontrol.com/blog/2021/12/02/version-30-changes-in-licenses-licx/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)
- [Different Ways to Create Documents using Text Control Products](https://www.textcontrol.com/blog/2017/10/17/ways-to-create-documents-using-text-control-products/llms.txt)
