Some test text!


Working with Interactive Forms (AcroForms)


Accessing Interactive Fields
Understanding Field Types
Creating Form Fields
Filling Form Fields
Form Flattening

An interactive form (sometimes referred to as an AcroForm) is a collection of fields (such as text boxes, checkboxes, radio buttons, drop-down lists, and pushbuttons) for gathering information interactively from the user. A PDF document may contain any number of Fields appearing on any combination of pages. All these fields make up a single, global interactive form spanning the entire document. While PDF forms are similar to HTML forms, there are some important differences:

  • Unlike HTML pages, a PDF document has a single, global interactive form spanning the entire document.
  • In PDF documents, the field and value appearance can be completely customized. Although field appearances give incredible customization power to PDF forms, developers need to learn to work with forms where field's value and appearance are two different entities.
  • The PDF format supports combo boxes with text editing.
  • In the PDF format, it's possible to associate fields with different kinds of Actions (or Action chains).

PDFTron SDK fully supports reading, writing, and editing PDF forms and provides utility methods to make working with forms simple and efficient. Using the PDFTron SDK forms API, arbitrary subsets of form fields can be imported or exported from the document, new forms can be created from scratch, and the appearance of existing forms can be modified.

linkAccessing Interactive Fields

The form shown in the following figure consists of a number of Fields:

Every field has its name and value, as well as its annotation appearance.

In the PDFTron SDK, Fields are accessed through FieldIterators.

For example, the list of all Fields present in the document can be traversed using the following code snippet:

FieldIterator itr;
for(itr=doc.GetFieldIterator(); itr.HasNext(); itr.Next())
  Field field = itr.Current();
  Console.WriteLine("Field name: {0}",field.GetName());

linkUnderstanding Field Types

PDF offers six different field types. Each type of form field is used for a different purpose, and they have different properties, appearances, options, and actions that can be associated with the fields. In this section, we will explain how to create all the seven field types and some attributes specific to each one.

Common field types are text-box, checkbox, radio-button, combo-box, and push-button. To find out the type of the Field use Field.GetType() method:

Field.FieldType type = field.GetType();
case Field.FieldType.e_button:
case Field.FieldType.e_check:
case Field.FieldType.e_radio:
case Field.FieldType.e_text:
case Field.FieldType.e_choice:
case Field.FieldType.e_signature:

linkCreating Form Fields

Regardless of which field type you create, you must provide a Field name:

Field myfiled
  = doc.FieldCreate(

Under most circumstances, field names must be unique. If you have a field you name as "address" and you create a second field you likewise call "address", you cannot supply different data in the two fields.

Field names can use alphanumeric characters to identify a field. All field names are case-sensitive. For example, you can use names such as empFirstName, empSecondName, empNumber, and so on for a group of fileds that are related to the same concept (in our sample employee entity).

Another technique for naming fields is to use a parent and child name. For example, you could name the above fields as follows:,, employee.number.

This naming convention is not only useful for organizing purposes but is well-suited for automatic operations on Fields. In the PDFTron SDK, Field.GetName() returns a string representing the fully qualified name of the field (e.g. ""). To get the child name ("first") use the Field.GetPartialName() method.

For more information about adding Fields, see the FDF code sample.

linkFilling Form Fields

Form Fields can be populated using the Field.SetValue() method:

field.SetValue("New Value");

// Regenerate appearance stream.

Note that, after modifying the Field's value, we refreshed its appearance stream. In the PDF format, Field's value and appearance are two different entities. Therefore, if you don't call RefreshAppearance(), the initial value on a PDF page will remain unchanged — it may have retain the old value or it may be blank.

One approach used by other PDF libraries is to let the PDF viewer automatically pre-generate appearance streams by setting the 'NeedAppearances' flag in AcroForm dictionary:

doc.GetAcroForm().PutBool("NeedAppearances", true);

This will force viewer applications to auto-generate appearance streams every time the document is opened. This method is unreliable — Acrobat does not always generate appearance streams correctly. Another disadvantage of this approach is that the user will always be prompted to save the document even if the document was never modified.

Field.GetValueAsString() returns the field's value as a string. The value returned varies based on the field type. A text field type varies depending on the field type. A text field will return a string:

if (type == Field.FieldType.e_text
    && field.GetValue())
  Console.WriteLine("Field value: {0}", field.GetValueAsString());
  Console.WriteLine("Field is blank");

Other field types, such as check boxes and radio buttons, can also return text from GetValueAsString(). Similarly, the Field.GetValueAsString() method is available.

linkForm Flattening

Form flattening refers to the operation that changes active form fields into a static area that is part of the PDF document, just like other text and images in the document. A completely flattened PDF form does not have any widget annotations or interactive fields.

Using the Field.Flatten() or Page.FlattenField() methods, it is possible to merge individual field appearances with the page content. PDFTron SDK also allows you to flatten all forms in the document in a single function call, with PDFDoc.FlattenAnnotations(true). (The true argument instructs PDFTron SDK to only flatten fields; were this method passed false, or no argument, it would flatten all annotations as well as all fields.)

Note that it is not possible to undo the Field.Flatten() operation. An alternative approach, one that can be programmatically reversed, would be to set the field as read only using the Field.SetFlag(Field.e_read_only, true) method.

Get the anwsers you need: Support


Accessing Interactive Fields
Understanding Field Types
Creating Form Fields
Filling Form Fields
Form Flattening