TX Text Control provides a comprehensive set of features for creating and deploying electronic forms, including form fields such as form text fields, check boxes, and drop-down lists. The Document Editor can be used to create these forms, and the Document Viewer can be used to deploy these forms to collect the data.

This article describes how to use TX Text Control to create PDF documents with form fields and how to extract form field values from completed PDF documents.

Creating Forms Programmatically

TX Text Control makes it easy to create, share, and collect PDF documents in business applications. The following tutorial will show you how to programmatically create a simple form and how to export it to a PDF file.

  1. In Visual Studio, create a new C# Console App, enter a name, select your preferred Framework (.NET 6) and create it by clicking Create.

    Creating forms with TX Text Control

Adding the NuGet Package

  1. In the Solution Explorer, select your created project and choose Manage NuGet Packages... from the Project main menu.

    Select Text Control Offline Packages from the Package source drop-down.

    Install the latest versions of the following package:

    • TXTextControl.TextControl.ASP.SDK

    Creating forms with TX Text Control

Creating the PDF

  1. To programmatically create a PDF with form fields from scratch, add the following code to the Main method of the Program class:

    Loading...

When this document is loaded into Adobe Acrobat Reader, it is automatically recognized as a form and users can fill in the fields.

Creating forms with TX Text Control

Flatten Form Fields

If you have deployed the document using the Document Editor or the Document Viewer and the user has filled in the form fields, you will probably want to flatten the form fields. This means the selected form field value is used to replace the form field and the form fields themselves are removed.

The Remove TX Text Control .NET Server for ASP.NET
TXTextControl Namespace
FormFieldCollection Class
Remove Method
Removes the specified form field from a Text Control document.
method of the FormFieldCollection TX Text Control .NET Server for ASP.NET
TXTextControl Namespace
FormFieldCollection Class
An instance of the FormFieldCollection class contains all form fields in a Text Control document represented through objects of the types TextFormField, CheckFormField and SelectionFormField.
can be used to replace the field with the actual content. The following function will iterate through all of the fields in the form in order to remove the field functionality:

private void FlattenFormFields(TextControl textControl) {
int fieldCount = textControl1.FormFields.Count;
for (int i = 0; i < fieldCount; i++) {
TextFieldCollectionBase.TextFieldEnumerator fieldEnum =
textControl1.FormFields.GetEnumerator();
fieldEnum.MoveNext();
FormField curField = (FormField)fieldEnum.Current;
textControl1.FormFields.Remove(curField, true);
}
}
view raw test.cs hosted with ❤ by GitHub

Pre-Selecting Form Fields

The MailMerge TX Text Control .NET Server for ASP.NET
DocumentServer Namespace
MailMerge Class
The MailMerge class is a .NET component that can be used to effortlessly merge template documents with database content in .NET projects, such as ASP.NET web applications, web services or Windows services.
class can be used for merging of objects or JSON data with form fields.

How form fields are handled during the merge process is controlled by the FormFieldMergeType TX Text Control .NET Server for ASP.NET
DocumentServer Namespace
MailMerge Class
FormFieldMergeType Property
Specifies in which manner form fields are treated during the merge process.
property.

Member Description
None Form fields are not merged at all.
Preselect Form field contents are pre-selected when possible. Form fields that are not associated with a data table column are not affected.
Replace Form fields are replaced by database content. Empty form fields are removed according to the RemoveEmptyFields TX Text Control .NET Server for ASP.NET
DocumentServer Namespace
MailMerge Class
RemoveEmptyFields Property
Specifies whether empty fields should be removed from the template or not.
property.

Consider the following sample template. It contains various form fields, including checkboxes, drop-down lists, and form text fields:

Creating forms with TX Text Control

Pre-Select Form Fields

In the first scenario, to help users by minimizing their effort to complete the form, the form should be pre-populated with known data. The following code shows how to load the template into a ServerTextControl instance for merging 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);
}
view raw test.cs hosted with ❤ by GitHub

The following screenshot shows the pre-selected form fields of the PDF document in the Acrobat Reader.

Creating forms with TX Text Control

Replacing Form Fields

In the second scenario, the document should be flattened by the removal of all form fields and their replacement 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);
}
view raw test.cs hosted with ❤ by GitHub

In the resulting document, all of the form fields will be removed and the document will no longer be an editable form document:

Creating forms with TX Text Control

Extract Form Field Values

Interactive forms in the Adobe PDF format - also known as AcroForm - are a de-facto standard for the processing of PDF forms. The forms can be created and exported using the TX Text Control, so that the end user can fill out these form fields in Acrobat Reader or other applications.

To collect results from completed documents, TX Text Control allows you to extract data from form fields.

The following code shows how to get all the AcroForm fields from the above sample PDF document using the GetAcroFormFields TX Text Control .NET Server for ASP.NET
DocumentServer.PDF Namespace
Forms Class
GetAcroFormFields Method
Imports AcroFormFields from an Adobe PDF document.
method, which accepts filenames and byte arrays.

FormField[] acroForms = Forms.GetAcroFormFields("lease_agreement.pdf");
foreach (FormField field in acroForms) {
switch (field) {
case FormTextField textField:
Console.WriteLine("Field \"{0}\" extracted: {1}",
textField.FieldName,
textField.Value);
break;
case FormCheckBox checkBoxField:
Console.WriteLine("Field \"{0}\" extracted: {1}",
checkBoxField.FieldName,
checkBoxField.IsChecked.ToString());
break;
case FormComboBox comboBoxField:
Console.WriteLine("Field \"{0}\" extracted. Selected value: {1}",
comboBoxField.FieldName,
comboBoxField.Value);
foreach (var item in comboBoxField.Options) {
Console.WriteLine(" -> Option: {0}", item);
}
break;
}
}
view raw test.cs hosted with ❤ by GitHub

The output lists all completed form fields including the name and possible drop-down options:

Field "insurance_date" extracted: 1/1/2020
Field "insurance_name" extracted: Health Providers
Field "insurance_check" extracted: True
Field "notes" extracted: Thanks for your business.
Field "insurance_state" extracted. Selected value: North Carolina
 -> Option: Georgia
 -> Option: California
 -> Option: North Carolina