Chapter 7. AcroForms

This chapter will go into detail on a special type of annotation: the widget annotation that is the building block for PDF forms.

PDF 1.2 introduced the concept of an interactive form (a collection of fields that can be used to gather information interactively from a user) to the PDF format. There will be at most one single, global form in the PDF; it can contain any number of fields, which can appear on any combination of pages.

Note

Interactive forms should not be confused with form XObjects (discussed in Vector Images). Despite the similarity of their names, the two are different, unrelated types of objects.

The Interactive Form Dictionary

The document’s interactive form is described using an interactive form dictionary, which is the value of the AcroForm key found in the document catalog dictionary.

This dictionary has only one required key, Fields, which specifies an array of field dictionaries that represent the fields in the form. However, there are a few common optional fields that you may need to specify for some of your forms:

The Field Dictionary

The field dictionary specifies the details of each field in a document’s interactive form. Fields can be organized hierarchically and can inherit attributes from their ancestors in the hierarchy. A field that has children that are fields is called a nonterminal field. A field that does not have children, such as a simple button or text field, is called a terminal field. Child fields are specified in an array of field dictionaries that is the value of a Kids key, while a child’s parent is specified via the Parent key.

Every field has to have a class associated with it via an FT key in the field dictionary (or inherited from a parent field). There are four possible values for the class of a field:

Since the purpose of a field is to gather information from a user, it needs a way to store that value. That storage location is the value of the V key in the field dictionary. The type of value and how it relates to the field differs based on the type of field.

While not required, most fields have names associated with them. In fact, a field can have up to three different names, each used in a different context.

The T key in the field dictionary consists of a text string value that defines the field’s partial field name. A field’s fully qualified name is never explicitly defined in the PDF but instead is dynamically constructed from the partial field names of the field and all of its ancestors. For a field with no parent, the partial and fully qualified names are the same. For a field that is the child of another field, the fully qualified name is constructed by appending the child field’s partial name to the parent’s fully qualified name, separated by a period (2Eh). Because the period is used as a separator for fully qualified names, a partial name cannot contain a period character.

When preparing a PDF that may be processed by a screen reader or other assistive technology, it is important to provide a TU key for each field. The value of this key is a text string that is used instead of the field’s partial or fully qualified name to identify it to the user.

The third name that a field might have is its export name—a name used when exporting the data to a file. The TM key’s string value will be exported as the name that is associated with the field’s value when it is exported to various formats. If not present, then the field’s partial name will be used.

As mentioned earlier, every field must have a class associated with it. The field types include button, text, choice, and signature fields.

A button field represents an interactive control or element on the page that the user can “press” with a mouse or other pointing device. There are three types of button fields (see Figure 7-1):

For button fields, bits 15, 16, 17, and 26 of the field flags indicate the intended behavior, as described in Table 7-2.

% Document catalog
12 0 obj
<<
    /Metadata 2 0 R
    /Type /Catalog
    /Pages 9 0 R
    /AcroForm 22 0 R
    /Outlines 6 0 R
>>
endobj

% AcroForm dictionary
22 0 obj
<<
    /Fields [16 0 R 17 0 R 24 0 R]
>>
endobj

% Pushbutton field
16 0 obj
<<
    /F 4
    /Type /Annot
    /Rect [27.014 749.644 99.014 769.644]
    /FT /Btn
    /Ff 65536
    /Subtype /Widget
    /T (PushButton)
    /AP << /N 42 0 R >>
>>
endobj

% Checkbox field
17 0 obj
<<
    /Type /Annot
    /Rect [27.014 715.314 45.014 733.314]
    /FT /Btn
    /AS /Off         % default state is Off (unchecked)
    /Subtype /Widget
    /F 4
    /T (CheckBox)
    /AP <<
        /N << /Yes 38 0 R /Off 37 0 R >>
        /D << /Yes 41 0 R /Off 40 0 R >>
    >>
>>
endobj

% Radio button #3
18 0 obj
<<
    /F 4
    /Rect [27.014 682.711 45.014 700.711]
    /Parent 24 0 R    % this is the radio button group...
    /AS /Off          % default state is Off (unchecked)
    /Subtype /Widget
    /Type /Annot
    /AP <<
        /N << /2 33 0 R /Off 34 0 R >>
        /D << /2 35 0 R /Off 36 0 R >>
    >>
>>
endobj

% Radio button #2
20 0 obj
<<
    /F 4
    /Rect [48.7541 682.711 66.7541 700.711]
    /Parent 24 0 R    % this is the radio button group...
    /AS /Off          % default state is Off (unchecked)
    /Subtype /Widget
    /Type /Annot
    /AP <<
        /N << /1 29 0 R /Off 30 0 R >>
        /D << /1 31 0 R /Off 32 0 R >>
    >>
>>
endobj

% Radio button #1
21 0 obj
<<
    /F 4
    /Type /Annot
    /Rect [73.1377 682.711 91.1377 700.711]
    /Parent 24 0 R    % this is the radio button group...
    /AS /0            % default state is 0, which is On
    /Subtype /Widget
    /AP <<
        /N << /0 25 0 R /Off 26 0 R >>
        /D << /0 27 0 R /Off 28 0 R >>
    >>
>>
endobj

% this is the radio button grouping field
24 0 obj
<<
    /Kids [21 0 R 20 0 R 18 0 R]
    /FT /Btn
    /Opt [(RadioButton1) (RadioButton1) (RadioButton1)]
    /Ff 49152
    /T (RadioButton)
>>
endobj

Text fields are areas on the page where arbitrary text/strings can be entered either by a user or programmatically by software. The text is normally only drawn in a single font, size, and color along a single line; however, various flags and additional keys can be supplied in the field dictionary to enable the text to span multiple lines (wrapping accordingly), or use rich text formatting, or both. Figure 7-2 shows a few examples.

If the text value of the field should be displayed in multiple styles, then a rich text string is provided as the value of the RV key in the field dictionary and bit 26 of the text field flags should be set. A rich text string is a fully formed XML document that conforms to the grammar defined for rich text in the XFA specification, which is based on specific subsets of the XHTML 1.0 and CSS2 standards.

The <body> element is the root of the document, which is then divided up into paragraphs (<p>) and spans (<span>) that are styled using standard CSS attributes such as font-family and color. Example 7-3 shows an example of a rich text field.

Example 7-3. A rich text field
20 0 obj
<<
    /F 4
    /FT Tx
    /Ff 33554432
    /Rect [ 312.427002 680.749023 600.427002 702.749023 ]
    /Subtype /Widget
    /Type /Annot
    /T (RichText)
    /V (A single line of text in multiple styles)
    /RV (<?xml version="1.0"?> \
        <body xfa:APIVersion="Acroform:2.7.0.0" xfa:spec="2.1" \
        xmlns="http://www.w3.org/1999/xhtml" \
        xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/"><p \
        style="margin-top:0pt;margin-bottom:0pt;text-valign:top;font- \
        family:Helvetica;font-size:13pt"><span>A </span><span \
        style="font-weight:bold">single</span><span \
        style="font-weight:normal"> </span><span \
        style="font-weight:normal;text-decoration:underline">line</span>< \
        span style="font-weight:normal;text-decoration:none"> \
        </span><span \
        style="color:#0000ff;font-weight:normal;text-decoration:none">of< \
        /span><span \
        style="color:#000000;font-weight:normal;text-decoration:none"> \
        </span><span \
        style="color:#000000;font-weight:normal;font-style:italic;text- \
        decoration:none">text</span><span \
        style="color:#000000;font-weight:normal;font-style:normal;text- \
        decoration:none"> </span><span \
        style="vertical-align:4.03pt;font-size:8.58pt;color:#000000;font- \
        weight:normal;font-style:normal;text-decoration:none">in</span>< \
        span \
        style="vertical-align:baseline;color:#000000;font-weight:normal; \
        font-style:normal;text-decoration:none"> </span><span \
        style="vertical-align:baseline;color:#00ff00;font-weight:normal; \
        font-style:normal;text-decoration:none">multiple</span><span \
        style="vertical-align:baseline;color:#000000;font-weight:normal; \
        font-style:normal;text-decoration:none"> styles</span></p></body>)
>>
endobj


21 0 obj<<
    /F 4
    /FT Tx
    /Ff 33558528
    /Rect [ 311.756012 550.416992 599.755981 653.090027 ]
    /Subtype /Widget
    /Type /Annot
    /T (MultilineRichText)
    /V (Multiple lines of text, in multiple styles,\
        automatically wrap around from line to line as more text is added. \
        A second, centered, paragraph)
    /RV (<?xml version="1.0"?>
    <body xfa:APIVersion="Acroform:2.7.0.0" xfa:spec="2.1" \
    xmlns="http://www.w3.org/1999/xhtml" \
    xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/"><p \
    style="margin-top:0pt;margin-bottom:0pt;font-family:Helvetica;font- \
    size:13pt"><span>Multiple </span><span \
    style="text-decoration:underline">lines</span><span \
    style="text-decoration:none"> of </span><span \
    style="font-style:italic;text-decoration:none">text,</span><span \
    style="font-style:normal;text-decoration:none"> in </span><span \
    style="color:#00ff00;font-style:normal;text-decoration:none">multiple \
    styles</span><span \
    style="color:#000000;font-style:normal;text-decoration:none">, that \
    will automatically </span><span \
    style="color:#000000;font-weight:bold;font-style:normal;text- \
    decoration:none">wrap</span><span \
    style="color:#000000;font-weight:normal;font-style:normal;text- \
    decoration:none"> around from </span><span \
    style="color:#000000;font-weight:normal;font-style:normal;text- \
    decoration:line-through">line to line</span><span \
    style="color:#000000;font-weight:normal;font-style:normal;text- \
    decoration:none"> as </span><span \
    style="color:#ff0000;font-weight:normal;font-style:normal;text- \
    decoration:none">more text</span><span \
    style="color:#000000;font-weight:normal;font-style:normal;text- \
    decoration:none"> is added.</span></p><p \
    style="margin-top:0pt;margin-bottom:0pt;font-family:Helvetica;font- \
    size:13pt"> </p><p \
    style="margin-top:0pt;margin-bottom:0pt;text-align:center;font-family \
    :Helvetica;font-size:13pt"><span>A second, centered, \
    paragraph</span></p></body>)
>>
endobj

A choice field is used to present a user with a choice of multiple options from which she can select one or more to become the value of the field.

In addition to the navigational actions, there are three types of actions that are specific to form fields: SubmitForm, ResetForm, and ImportData. Just as the navigational actions can be associated with bookmarks or links, these form-related actions can be as well. However, it is more common to associate them with a button field on the form.

The SubmitForm action informs the PDF viewer to transmit the names and values of the specified fields to the specified URL in the specified format. This information is provided in the action dictionary, whose S key will have a value of SubmitForm.

The URL where the form data will be submitted is specified as a URL file specification, which is the value of the F key in the action dictionary.

The fields that are to be included in the submitted data are specified in the array value of the Fields key. Each element of the array can be either an indirect reference to a field dictionary or a text string representing the fully qualified name of a field. Elements of both kinds may be mixed in the same array.

PDF provides the specification of four different formats in which the form data can be submitted to the specified URL. The choice of format is determined by a series of flags that are specified as the value of the Flags key of the action dictionary:

Example 7-8 shows an example SubmitForm action.

In this chapter, you learned about a special type of annotation, the widget annotation, which is the building block for PDF forms. Next you will learn about how to embed files in a PDF.