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

- **Author:** Bjoern Meyer
- **Published:** 2023-08-14
- **Modified:** 2026-11-07
- **Description:** 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.
- **6 min read** (1199 words)
- **Tags:**
  - ASP.NET
  - Form Fields
  - PDF
- **Web URL:** https://www.textcontrol.com/blog/2023/08/14/create-preselect-flatten-and-extract-pdf-form-fields-using-csharp/
- **LLMs URL:** https://www.textcontrol.com/blog/2023/08/14/create-preselect-flatten-and-extract-pdf-form-fields-using-csharp/llms.txt
- **LLMs-Full URL:** https://www.textcontrol.com/blog/2023/08/14/create-preselect-flatten-and-extract-pdf-form-fields-using-csharp/llms-full.txt

---

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](https://s1-www.textcontrol.com/assets/dist/blog/2023/08/14/a/assets/step1.webp "Creating forms with TX Text Control")

#### Adding the NuGet Package

2. 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](https://s1-www.textcontrol.com/assets/dist/blog/2023/08/14/a/assets/step2.webp "Creating forms with TX Text Control")

#### Creating the PDF

3. To programmatically create a PDF with form fields from scratch, add the following code to the *Main* method of the *Program* class:
    
    ```
    using (TXTextControl.ServerTextControl tx = new TXTextControl.ServerTextControl()) {
    	  tx.Create();
    	
    	  tx.Selection.FontSize = 800;
    	  tx.Selection.Text = "Sample Form\r\n\r\n";
    	
    	  tx.Selection.FontSize = 400;
    	  tx.Selection.Text = "Company name:\r\n";
    	
    	  // add a text form field
    	  TXTextControl.TextFormField textFormField = new TXTextControl.TextFormField(1000);
    	  textFormField.Name = "company_name";
    	  textFormField.Text = "Text Control, LLC";
    	  tx.FormFields.Add(textFormField);
    	
    	  tx.Selection.Text = "\r\n\r\nCountry:\r\n";
    	
    	  // add a selection form field
    	  TXTextControl.SelectionFormField selectionFormField = new TXTextControl.SelectionFormField(1000);
    	  selectionFormField.Name = "company_country";
    	  selectionFormField.Items = new string[] { "United States", "Germany", "Italy" };
    	  selectionFormField.SelectedIndex = 0;
    	  tx.FormFields.Add(selectionFormField);
    	
    	  // export the document as PDF
    	  tx.Save("results.pdf", TXTextControl.StreamType.AdobePDF);
    	}
    ```

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](https://s1-www.textcontrol.com/assets/dist/blog/2023/08/14/a/assets/step3.webp "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](https://s1-www.textcontrol.com/assets/dist/blog/2023/08/14/a/assets/form.webp "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](https://s1-www.textcontrol.com/assets/dist/blog/2023/08/14/a/assets/form2.webp "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](https://s1-www.textcontrol.com/assets/dist/blog/2023/08/14/a/assets/form3.webp "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
```

---

## 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 Fillable PDF Forms in .NET C#](https://www.textcontrol.com/blog/2024/11/12/create-fillable-pdf-forms-in-net-c-sharp/llms.txt)
- [Extension Method: Flatten Forms Fields in PDF Documents using .NET C#](https://www.textcontrol.com/blog/2024/07/17/flatten-forms-fields-in-pdf-documents-using-net-c-sharp/llms.txt)
- [Document Viewer: Save the Values of Form Fields in Documents](https://www.textcontrol.com/blog/2023/12/19/document-viewer-save-the-values-of-form-fields-in-documents/llms.txt)
- [Document Viewer: Save the Values of Form Fields in Documents](https://www.textcontrol.com/blog/2023/10/19/document-viewer-save-the-values-of-form-fields-in-documents/llms.txt)
- [Healthcare Use Case: Digital Forms Workflow with Electronic Signatures](https://www.textcontrol.com/blog/2023/03/15/healthcare-use-case-digital-forms-workflow-with-electronic-signatures/llms.txt)
- [Convert DOCX to PDF, PDF/A and PDF/UA in C# Using TX Text Control](https://www.textcontrol.com/blog/2026/10/22/convert-docx-to-pdf-pdfa-and-pdfua-in-csharp-using-tx-text-control/llms.txt)
- [Reliable Document AI in .NET: LLM Tool Calling vs. LLM-Generated Files](https://www.textcontrol.com/blog/2026/10/14/reliable-document-ai-in-dotnet-llm-tool-calling-vs-llm-generated-files/llms.txt)
- [Building a Production-Ready PDF Generation Pipeline in C# with ASP.NET Core](https://www.textcontrol.com/blog/2026/10/12/reliable-pdf-generation-pipeline-csharp-aspnet-core/llms.txt)
- [Native PDF Annotation Support Is Coming to the TX Text Control Document Viewer](https://www.textcontrol.com/blog/2026/08/28/native-pdf-annotation-support-is-coming-to-the-tx-text-control-document-viewer/llms.txt)
- [Building a Browser-Based PDF Workflow for Medical Billing using .NET C#](https://www.textcontrol.com/blog/2026/08/18/building-browser-based-pdf-workflow-medical-billing-dotnet-csharp/llms.txt)
- [AI Natural Language Document Generation with MCP and TX Text Control .NET](https://www.textcontrol.com/blog/2026/07/16/ai-natural-language-document-generation-with-mcp-server-and-tx-text-control-dotnet/llms.txt)
- [C# Document Generation: A Developer's Guide for .NET](https://www.textcontrol.com/blog/2026/07/08/csharp-document-generation-developer-guide-for-dotnet/llms.txt)
- [Validating PDF/UA Documents in .NET C#: A Practical Guide](https://www.textcontrol.com/blog/2026/07/06/validating-pdf-ua-documents-in-dotnet-csharp/llms.txt)
- [Automatically Mapping TX Text Control Form Fields to JSON Data in .NET C#](https://www.textcontrol.com/blog/2026/06/03/automatically-mapping-tx-text-control-form-fields-to-json-data-in-dotnet-csharp/llms.txt)
- [Using QR Codes in PDF Documents in C# .NET](https://www.textcontrol.com/blog/2026/04/21/using-qr-codes-in-pdf-documents-in-csharp-dotnet/llms.txt)
- [Programmatically Fill, Flatten, and Export DOCX Form Templates to PDF in C# .NET](https://www.textcontrol.com/blog/2026/04/10/programmatically-fill-flatten-and-export-docx-form-templates-to-pdf-in-csharp-dotnet/llms.txt)
- [Why Structured E-Invoices Still Need Tamper Protection using C# and .NET](https://www.textcontrol.com/blog/2026/03/24/why-structured-e-invoices-still-need-tamper-protection-using-csharp-and-dotnet/llms.txt)
- [Create Fillable PDFs from HTML Forms in C# ASP.NET Core Using a WYSIWYG Template](https://www.textcontrol.com/blog/2026/03/17/create-fillable-pdfs-from-html-forms-in-csharp-aspnet-core-using-a-wysiwyg-template/llms.txt)
- [Why HTML to PDF Conversion is Often the Wrong Choice for Business Documents in C# .NET](https://www.textcontrol.com/blog/2026/03/13/why-html-to-pdf-conversion-is-often-the-wrong-choice-for-business-documents-in-csharp-dot-net/llms.txt)
- [A Complete Guide to Converting Markdown to PDF in .NET C#](https://www.textcontrol.com/blog/2026/01/07/a-complete-guide-to-converting-markdown-to-pdf-in-dotnet-csharp/llms.txt)
- [Why PDF Creation Belongs at the End of the Business Process](https://www.textcontrol.com/blog/2026/01/02/why-pdf-creation-belongs-at-the-end-of-the-business-process/llms.txt)
- [Designing the Perfect PDF Form with TX Text Control in .NET C#](https://www.textcontrol.com/blog/2025/12/16/designing-the-perfect-pdf-form-with-tx-text-control-in-dotnet-csharp/llms.txt)
- [Why Defining MIME Types for PDF/A Attachments Is Essential](https://www.textcontrol.com/blog/2025/12/10/why-defining-mime-types-for-pdfa-attachments-is-essential/llms.txt)
- [Validate Digital Signatures and the Integrity of PDF Documents in C# .NET](https://www.textcontrol.com/blog/2025/11/14/validate-digital-signatures-and-the-integrity-of-pdf-documents-in-csharp-dotnet/llms.txt)
- [Validate PDF/UA Documents and Verify Electronic Signatures in C# .NET](https://www.textcontrol.com/blog/2025/11/13/validate-pdf-ua-documents-and-verify-electronic-signatures-in-csharp-dotnet/llms.txt)
