TX Text Control .NET for Windows Forms

Creating AcroForms

Topic

The fields that a customer fills in with Adobe Acrobat Reader or a browser are AcroForm fields, the interactive form fields of the PDF format. TX Text Control creates them from the form fields of a document: whatever form a user designs in your Windows Forms application, or a Word form it loads, becomes a fillable PDF with one Save call. Knowing how the fields end up in the PDF helps you design forms whose values you can read back reliably.

A first PDF form with a text box, a drop-down list and a check box is built in Create a PDF Form in Getting Started. How users insert and edit form fields in the editor is described in Form Fields and Conditional Instructions, and the classes of form fields in Forms and Conditional Instructions. Form fields are part of the Enterprise edition.

What AcroForms Are

AcroForms are the common standard for interactive forms in PDF documents. An AcroForm field is a text field, a check box, a radio button, a push button or a choice field such as a drop-down list, and each field has a name that identifies its value. TX Text Control works with AcroForms in both directions:

  • Creating. Since version 28.0, the form fields of a document are converted into PDF form fields when the document is saved as PDF. No setting is needed.
  • Reading. Since version 29.0, Forms.GetAcroFormFields returns the fields of a PDF document with their values, as Reading Form Fields and Values describes.

How Form Fields Become PDF Fields

Each form field type of TX Text Control becomes the PDF field that fits it, and its Name becomes the name of the PDF field:

Form field in the documentField in the PDF
TextFormFieldText field
CheckFormFieldCheck box
SelectionFormFieldChoice field: a drop-down list, or a combo box when users may type their own value
DateFormFieldText field with the date as the form displays it

TX Text Control has no radio buttons or push buttons, so it doesn't create these PDF fields, but it reads them from PDF forms created with other tools. The values your code or the user set before saving are filled in and can be changed in the PDF reader.

Design the Form in the Editor

Users design forms in the TextControl with the Form Fields tab of the ribbon, as Design a Form With the Ribbon describes. Saved with StreamType.AdobePDF, the document is a fillable PDF form. Form Fields and Conditional Instructions also covers the rules that change fields depending on the values of others.

Turn a Word Form Into a Fillable PDF

Forms created in Microsoft Word contain legacy form fields or content controls. Loaded into the TextControl, they become form fields, and saved as PDF, they become AcroForm fields. The following handler in Form1.cs loads registration.docx, gives every field without a description one and saves the fillable PDF:

private void convertWordFormButton_Click(object sender, EventArgs e)
{
    LoadSettings loadSettings = new LoadSettings()
    {
        // Content controls and legacy form fields become form fields
        ApplicationFieldFormat = ApplicationFieldFormat.MSWordTXFormFields
    };

    textControl1.Load("registration.docx", StreamType.WordprocessingML, loadSettings);

    // A tooltip in the PDF reader for every field without a description
    foreach (FormField formField in textControl1.FormFields)
    {
        if (string.IsNullOrEmpty(formField.DescriptiveText))
        {
            formField.DescriptiveText = "Enter " + formField.Name;
        }
    }

    textControl1.Save("registration.pdf", StreamType.AdobePDF);
}

The code works as follows:

  • LoadSettings.ApplicationFieldFormat MSWordTXFormFields imports the form fields of the Word document as form fields of TX Text Control and all other Word fields, such as merge fields, as application fields. With the default value None, form fields are imported as form fields as well, but the other Word fields aren't imported as fields.
  • TextControl.FormFields contains all form fields of the document.
  • FormField.DescriptiveText describes a field for screen readers in an accessible PDF, and PDF readers such as Adobe Acrobat Reader show it as the tooltip of the field.

The tooltip appears when the user points at the field:

A PDF form in Adobe Acrobat Reader with an empty text field for the authorized individual name and the tooltip "Please add the authorized individual name" below it.

The blog article Designing the Perfect PDF Form with TX Text Control in .NET C# places the form fields in table cells, so that they keep their size and position in any PDF reader, and gives them names that describe their content. Names such as account_owner_name let your code map the values of returned forms to its data.

Check the Fields of the Saved Form

Forms.GetAcroFormFields reads the PDF you saved and shows what a PDF reader sees. The following handler lists each field with its type and tooltip in the Output window:

private void listAcroFormFieldsButton_Click(object sender, EventArgs e)
{
    foreach (AcroForms.FormField field in Forms.GetAcroFormFields("registration.pdf"))
    {
        // Derived classes first: a radio button is a check box, a list box is a combo box
        string kind = field switch
        {
            AcroForms.FormTextField => "text field",
            AcroForms.FormRadioButton => "radio button",
            AcroForms.FormCheckBox => "check box",
            AcroForms.FormListBox => "list box",
            AcroForms.FormComboBox => "combo box",
            AcroForms.FormButton => "push button",
            _ => field.FieldType.ToString()
        };

        Debug.WriteLine($"{field.FieldName}: {kind}, tooltip \"{field.AlternateFieldName}\"");
    }
}

Form1.cs needs these using directives for it:

using System.Diagnostics;
using TXTextControl.DocumentServer.PDF;
using AcroForms = TXTextControl.DocumentServer.PDF.AcroForms;

The alias AcroForms is needed because the TXTextControl namespace has a FormField class, too. FieldName is the name of the field and AlternateFieldName the tooltip, which comes from DescriptiveText. Reading Form Fields and Values describes all classes.

Where Users Fill In the Form

  • Adobe Acrobat Reader and other PDF readers that support form fields.
  • Google Chrome fills in AcroForm fields since version 85 and downloads the PDF with the values entered.
  • Your own application. The same document can be filled in inside the TextControl, with the rest of the text protected, as Let Users Fill Out the Form shows.

A sample form with a company name and a drop-down list of countries, saved as PDF and opened in Chrome:

A PDF form in Google Chrome with the company name filled in and the drop-down list of countries opened.

Forms, Permissions and PDF/A

Next Steps