Filling PDF form fields on iOS

To fill and set values for existing form fields.

1PTPDFDoc *doc = [[PTPDFDoc alloc] initWithFilepath: filename];
2
3// Search for a specific field
4PTField *fld = [doc GetField: @"employee.name.first"];
5[fld SetValue: @"John"];

PDF Interactive Forms (AcroForms)
Full code sample which illustrates some basic PDFNet capabilities related to interactive forms (also known as AcroForms).

About filling form fields

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

1[field SetValue: @"New Value"];
2
3// Regenerate appearance stream.
4[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:

1[doc GetAcroForm] PutBool: @"NeedAppearances" value: 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:

1if (type == e_pttext && [field GetValue])
2{
3 printf("Field value: %s", [field GetValueAsString];
4}
5else
6{
7 printf("Field is blank");
8}

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

Access interactive fields

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

Apryse Docs Image

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

In the Apryse 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:

1PTFieldIterator * itr = [doc GetFieldIterator];
2for(; [itr HasNext]; [itr Next])
3{
4 PTField *field = [[itr Current] GetName];
5 printf("Field name: %s", [field UTF8String]);
6}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales