# Reading Form Fields and Values

> Show the values of a filled PDF form in a WPF MessageBox with GetAcroFormFields, keyed by field name, and transfer them into the TextControl's form fields.

- **Product:** TX Text Control .NET for WPF
- **Section:** Documentation
- **Kind:** Topic
- **Path:** TX Text Control .NET for WPF > PDF Processing > PDF Forms > Reading Form Fields and Values
- **3470 words**
- **Web URL:** https://docs.textcontrol.com/tx-text-control-dotnet-wpf/pdf/forms/reading-fields/
- **LLMs URL:** https://docs.textcontrol.com/tx-text-control-dotnet-wpf/pdf/forms/reading-fields/llms.txt
- **LLMs-Full URL:** https://docs.textcontrol.com/tx-text-control-dotnet-wpf/pdf/forms/reading-fields/llms-full.txt

---

Once a customer has filled in a PDF form and sent it back, your WPF application needs the values, not the pages. `Forms.GetAcroFormFields` reads them straight from the PDF file or a byte array, without loading the document into the `TextControl` of a window. The values can be shown, stored or transferred back into the form in the editor, where a user checks and corrects them.

The examples read *form-filled.pdf*, the form of [Create a PDF Form](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/getting-started/pdf/form/llms-full.txt) with the fields `name`, `country` and `newsletter`, filled in and saved in a PDF reader. [Creating AcroForms](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/pdf/forms/acroforms/llms-full.txt) describes how such forms are made.

### How GetAcroFormFields Reads a Form

[Forms.GetAcroFormFields](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-documentserver-pdf/forms/getacroformfields-method/llms-full.txt), introduced in version 29.0, is a static method of the `TXTextControl.DocumentServer.PDF` namespace. It takes the path of a PDF file or the PDF as a byte array and returns an array of [FormField](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-documentserver-pdf/acroforms-formfield/llms-full.txt) objects of the `TXTextControl.DocumentServer.PDF.AcroForms` namespace. Each object is of the class of its field:

| Class | Field in the PDF | Value |
|---|---|---|
| [FormTextField](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-documentserver-pdf/acroforms-formtextfield/llms-full.txt) | Text field; also the date form fields of TX Text Control | [Value](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-documentserver-pdf/acroforms-formtextfield/value-property/llms-full.txt): the text entered |
| [FormCheckBox](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-documentserver-pdf/acroforms-formcheckbox/llms-full.txt) | Check box | [IsChecked](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-documentserver-pdf/acroforms-formcheckbox/ischecked-property/llms-full.txt) |
| [FormRadioButton](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-documentserver-pdf/acroforms-formradiobutton/llms-full.txt) | Radio button, derived from `FormCheckBox` | `IsChecked` |
| [FormChoiceField](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-documentserver-pdf/acroforms-formchoicefield/llms-full.txt) | The base class of combo boxes and list boxes | [Value](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-documentserver-pdf/acroforms-formchoicefield/value-property/llms-full.txt): the selected item; [Options](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-documentserver-pdf/acroforms-formchoicefield/options-property/llms-full.txt): all items |
| [FormComboBox](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-documentserver-pdf/acroforms-formcombobox/llms-full.txt) | Combo box or drop-down list, derived from `FormChoiceField` | `Value` |
| [FormListBox](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-documentserver-pdf/acroforms-formlistbox/llms-full.txt) | List box, derived from `FormComboBox` | `Value` |
| [FormButton](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-documentserver-pdf/acroforms-formbutton/llms-full.txt) | Push button | None |

The base class gives every field these members:

- **[FieldName](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-documentserver-pdf/acroforms-formfield/fieldname-property/llms-full.txt)**: the name of the field, for forms of TX Text Control the `Name` of the form field.
- **[AlternateFieldName](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-documentserver-pdf/acroforms-formfield/alternatefieldname-property/llms-full.txt)**: the alternate name, shown by PDF readers as the tooltip.
- **[FieldType](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-documentserver-pdf/acroforms-formfield/fieldtype-property/llms-full.txt)**: `Tx` for text, `Btn` for buttons, `Ch` for choice fields and `Sig` for signature fields.
- **[Bounds](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-documentserver-pdf/acroforms-formfield/bounds-property/llms-full.txt)**: the rectangle of the field in the document.

Forms filled in with any of the readers in [Where Users Fill In the Form](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/pdf/forms/acroforms/#where-users-fill-in-the-form) can be read. In Google Chrome, users save the filled form with the **Download** button and choose the version with their changes.

### Show the Values of a Filled Form

Add a button with the handler `ReadFormButton_Click` to *MainWindow.xaml*. The following code in *MainWindow.xaml.cs* collects the values of a PDF form by field name and shows them in a message box:

```csharp
private Dictionary<string, string> ReadFormValues(string path)
{
    Dictionary<string, string> values = new Dictionary<string, string>();

    foreach (AcroForms.FormField field in Forms.GetAcroFormFields(path))
    {
        switch (field)
        {
            case AcroForms.FormTextField textField:
                values[field.FieldName] = textField.Value;
                break;

            case AcroForms.FormCheckBox checkBox:          // check boxes and radio buttons
                values[field.FieldName] = checkBox.IsChecked ? "true" : "false";
                break;

            case AcroForms.FormChoiceField choiceField:    // combo boxes and list boxes
                values[field.FieldName] = choiceField.Value;
                break;
        }
    }

    return values;
}

private void ReadFormButton_Click(object sender, RoutedEventArgs e)
{
    OpenFileDialog dialog = new OpenFileDialog() { Filter = "PDF forms|*.pdf" };

    if (dialog.ShowDialog(this) != true)
    {
        return;
    }

    Dictionary<string, string> values;

    try
    {
        values = ReadFormValues(dialog.FileName);
    }
    catch (FilterException exception)
    {
        MessageBox.Show(this, "The form could not be read: " + exception.Reason, "PDF Form");
        return;
    }

    if (values.Count == 0)
    {
        MessageBox.Show(this, "The PDF document has no form fields.", "PDF Form");
        return;
    }

    MessageBox.Show(this, string.Join("\n", values.Select(value => value.Key + ": " + value.Value)), "PDF Form");
}
```

The code needs these `using` directives:

```csharp
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using Microsoft.Win32;
using TXTextControl;
using TXTextControl.DocumentServer.PDF;
using AcroForms = TXTextControl.DocumentServer.PDF.AcroForms;
```

The code works as follows:

- The alias `AcroForms` keeps the PDF classes apart from the form fields of the document: both namespaces contain a class named `FormField`.
- The `switch` statement handles check boxes and radio buttons through `FormCheckBox`, and combo boxes and list boxes through the base class `FormChoiceField`, because only their values are needed. To tell the derived classes apart, test `FormRadioButton` before `FormCheckBox` and `FormListBox` before `FormComboBox`.
- A name can occur more than once in the array. The indexer of the dictionary keeps the last value, where `ToDictionary` would throw.
- A file that can't be read throws a [FilterException](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/filterexception/llms-full.txt). `GetAcroFormFields` has no password parameter, so PDF forms that require a password to open can't be read.

For the form of Getting Started, the message box shows lines such as `name: Tim Typer`, `country: Germany` and `newsletter: true`.

### Put the Values Back Into the Form

TX Text Control doesn't write values into an existing PDF. Load your document of the form, the one the PDF was created from, into `textControl1`, and transfer the values into its form fields:

```csharp
private void FillEditorFormButton_Click(object sender, RoutedEventArgs e)
{
    Dictionary<string, string> values = ReadFormValues("form-filled.pdf");

    // The main text, headers, footers and text frames
    foreach (IFormattedText textPart in textControl1.TextParts)
    {
        foreach (FormField formField in textPart.FormFields)
        {
            if (string.IsNullOrEmpty(formField.Name) ||
                !values.TryGetValue(formField.Name, out string value) || value == null)
            {
                continue;
            }

            switch (formField)
            {
                case CheckFormField checkFormField:
                    checkFormField.Checked = value == "true";
                    break;

                case SelectionFormField selectionFormField:
                    // -1 selects nothing if the value isn't one of the items
                    selectionFormField.SelectedIndex = Array.IndexOf(selectionFormField.Items, value);
                    break;

                case TextFormField textFormField:
                    textFormField.Text = value;
                    break;
            }
        }
    }
}
```

The code works as follows:

- [TextParts](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-wpf/textcontrol/textparts-property/llms-full.txt) of the WPF `TextControl` contains the main text, the headers and footers and the text frames, each with its own [FormFields](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/iformattedtext/formfields-property/llms-full.txt) collection.
- Each form field type receives the value through its own property: [Checked](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/checkformfield/checked-property/llms-full.txt), [SelectedIndex](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/selectionformfield/selectedindex-property/llms-full.txt) of the [Items](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/selectionformfield/items-property/llms-full.txt) or [Text](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/textfield/text-property/llms-full.txt).
- Date form fields are skipped, because their value arrives as the displayed text. To set one, parse the text with the culture of the form and assign [DateFormField.Date](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/dateformfield/date-property/llms-full.txt).

The user sees the values in the form at once. Saved again as PDF, the document is a new fillable form; flattened first, it has fixed values, as [Exporting Interactive and Flattened Forms](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/pdf/forms/exporting/llms-full.txt) shows. A `MailMerge` with `FormFieldMergeType.Preselect` fills form fields from data as well, as [Merge Fields](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/mail-merge/fields/llms-full.txt) describes.

### Next Steps

- [Exporting Interactive and Flattened Forms](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/pdf/forms/exporting/llms-full.txt): save the form with fixed values.
- [Creating AcroForms](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/pdf/forms/acroforms/llms-full.txt): create the PDF forms you read.
- The blog article [Export and Import Adobe PDF XFDF XML Files in .NET C#](https://www.textcontrol.com/blog/2025/01/22/export-and-import-adobe-pdf-xfdf-xml-files-in-net-c-sharp/): store the values of a form as XFDF.

---

## Nearby

- **Previous:** [Creating AcroForms](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/pdf/forms/acroforms/llms-full.txt)
- **Next:** [Exporting Interactive and Flattened Forms](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/pdf/forms/exporting/llms-full.txt)
