Some test text!

keyboard_arrow_down

Getting started

Quick startDownload and run samplesIntegrate into your projectDeployment optionsMigrating to v4
keyboard_arrow_down

Deployment options

keyboard_arrow_down

Fundamentals

keyboard_arrow_down

Basics

keyboard_arrow_down

Annotations

keyboard_arrow_down

UI

keyboard_arrow_down

Legacy UI

keyboard_arrow_down

Advanced

keyboard_arrow_down

PDFNetJS Full

keyboard_arrow_down

PDF Processing API

What is PDFNet?Opening a documentSerializing (saving) a documentWorking with pagesWorking with Page ContentWorking with BookmarksWorking with Interactive Forms (AcroForms)PDF SecurityLow-level PDF APIError handling
Working with Interactive Forms (AcroForms)keyboard_arrow_down

Working with Interactive Forms (AcroForms)

In this document
chevron_rightAccessing Interactive Fields
chevron_rightUnderstanding Field Types
chevron_rightCreating Form Fields
chevron_rightFilling Form Fields
chevron_rightForm 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).

PDFNet fully supports reading, writing, and editing PDF forms and provides utility methods to make working with forms simple and efficient. Using the PDFNet 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 PDFNet 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();
switch(type)
{
case Field.FieldType.e_button: 
  Console.WriteLine("Button");
  break;
case Field.FieldType.e_check: 
  Console.WriteLine("Check");
  break;
case Field.FieldType.e_radio: 
  Console.WriteLine("Radio");
  break;
case Field.FieldType.e_text:
  Console.WriteLine("Text");
  break;
case Field.FieldType.e_choice:
  Console.WriteLine("Choice");
  break;
case Field.FieldType.e_signature:
  Console.WriteLine("Signature");
  break;
}

linkCreating Form Fields

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

Field myfiled 
  = doc.FieldCreate(
      "address", 
      Field.FieldType.e_text);

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.name.first, employee.name.second, employee.number.

This naming convention is not only useful for organizing purposes but is well-suited for automatic operations on Fields. In the PDFNet SDK, Field.GetName() returns a string representing the fully qualified name of the field (e.g. "employee.name.first"). 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.
field.RefreshAppearance();

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());
}
else
{
  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. PDFNet also allows you to flatten all forms in the document in a single function call, with PDFDoc.FlattenAnnotations(true). (The true argument instructs PDFNet 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.