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.

The Mail
Merging Form Fields
The Form
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 Remove |
Consider the following sample template that contains different form fields including checkboxes, drop-downs and form text fields:
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:
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:
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.
Also See
This post references the following in the documentation:
- TXText
Control. Document Server. Mail Merge. Form Field Merge Type Property - TXText
Control. Document Server. Mail Merge. Merge Object Method
Related Posts
MailMerge: Rendering Conditional Table Rows
The MailMerge class supports repeating merge blocks that are repeated based on the given data rows. Sub-blocks can be rendered conditionally based on value comparisons of the parent data table.…
TX Text Control 33.0 SP3 is Now Available: What's New in the Latest Version
TX Text Control 33.0 Service Pack 3 is now available, offering important updates and bug fixes for all platforms. If you use TX Text Control in your document processing applications, this service…
TX Text Control 33.0 SP2 is Now Available: What's New in the Latest Version
TX Text Control 33.0 Service Pack 2 is now available, offering important updates and bug fixes for all platforms. If you use TX Text Control in your document processing applications, this service…
Document Lifecycle Optimization: Leveraging TX Text Control's Internal Format
Maintaining the integrity and functionality of documents throughout their lifecycle is paramount. TX Text Control provides a robust ecosystem that focuses on preserving documents in their internal…
Expert Implementation Services for Legacy System Modernization
We are happy to officially announce our partnership with Quality Bytes, a specialized integration company with extensive experience in modernizing legacy systems with TX Text Control technologies.