Products Technologies Demo Docs Blog Support Company

Create, Pre-Select, Flatten and Extract PDF Form Fields using C#

TX Text Control can be used for the creation and processing of PDF form fields. This article shows how to create, select, flatten, and extract form fields in PDF documents.

Create, Pre-Select, Flatten and Extract PDF Form Fields using C#

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:

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 method of the FormFieldCollection 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);
  }
}

Pre-Selecting Form Fields

The MailMerge 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 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 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);
}

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);
}

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 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;
    }
}

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

Stay in the loop!

Subscribe to the newsletter to receive the latest updates.

Also See

This post references the following in the documentation:

  • TXTextControl.FormFieldCollection.Remove Method
  • TXTextControl.FormFieldCollection Class
  • TXTextControl.DocumentServer.MailMerge Class
  • TXTextControl.DocumentServer.MailMerge.FormFieldMergeType Property
  • TXTextControl.DocumentServer.MailMerge.RemoveEmptyFields Property
  • TXTextControl.DocumentServer.PDF.Forms.GetAcroFormFields Method

Related Posts

ASP.NETASP.NET CoreForm Fields

Create Fillable PDF Forms in .NET C#

This article shows how to create fillable PDF forms in .NET C# using the TX Text Control .NET Server component. The created forms can be filled out using Adobe Reader or any other PDF viewer that…


ASP.NETASP.NET CoreForm Fields

Extension Method: Flatten Forms Fields in PDF Documents using .NET C#

This article shows how to flatten form fields in TX Text Control before exporting the document to PDF. This is a common requirement when documents should be shared with others and the form fields…


ASP.NETForm FieldsPDF

Document Viewer: Save the Values of Form Fields in Documents

The TX Text Control Document Viewer is used to allow users to fill in form fields in documents. This article explains how to save a document with the values of the filled in form fields.


ASP.NETForm FieldsPDF

Document Viewer: Save the Values of Form Fields in Documents

The TX Text Control Document Viewer is used to allow users to fill in form fields in documents. This article explains how to save a document with the values of the filled in form fields.


ASP.NETAcroFormsForm Fields

Healthcare Use Case: Digital Forms Workflow with Electronic Signatures

Forms play an important role in healthcare processes and offer a huge potential for automation and digital forms processing. This article and sample show how to use TX Text Control to streamline…