Create new form fields and widget annotations on iOS

To create new form fields and widget annotations within a new document.

1// Create a blank page
2PTPDFDoc *doc = [[PTPDFDoc alloc] init];
3PTPage *blank_page = [doc PageCreate: [[PTPDFRect alloc] initWithX1: 0 y1: 0 x2: 612 y2: 792]];
4
5// Create a new text field and text widget/annotation (aka AcroForms).
6PTField *text_field = [doc FieldCreateWithString: @"employee.name" type: e_pttext field_value: @"" def_field_value: @""];
7PTTextWidget *text = [PTTextWidget CreateWithField: doc pos: [[PTPDFRect alloc] initWithX1: 110 y1: 620 x2: 380 y2: 650] field: text_field];
8[text SetFont: [PTFont Create: [doc GetSDFDoc] type: e_pttimes_bold embed: NO]];
9[text RefreshAppearance];
10[blank_page AnnotPushBack: text];
11
12// Create a new signature field and signature annotation (aka AcroForms)
13PTDigitalSignatureField* sig_field = [doc CreateDigitalSignatureField: @"employee.signature"];
14PTSignatureWidget* signature = [PTSignatureWidget CreateWithDigitalSignatureField: doc pos: [[PTPDFRect alloc] initWithX1: 0 y1: 100 x2: 200 y2: 150] field: sig_field];
15[signature RefreshAppearance];
16[blank_page AnnotPushBack: signature];
17
18// Create a new checkbox field and checkbox widget/annotation (aka AcroForms)
19PTCheckBoxWidget *checkbox = [PTCheckBoxWidget Create: doc pos: [[PTPDFRect alloc] initWithX1: 190 y1: 490 x2: 250 y2: 540] field_name: @"employee.checkbox"];
20[checkbox SetBackgroundColor: [[PTColorPt alloc] initWithX: 1 y: 1 z: 1 w: 0] compnum: 3];
21[checkbox SetBorderColor: [[PTColorPt alloc] initWithX: 0 y: 0 z: 0 w: 0] compnum: 3];
22[checkbox SetChecked: true]; // Check the widget (by default it is unchecked).
23[checkbox RefreshAppearance];
24[blank_page AnnotPushBack: checkbox];
25
26// Create a radio button group and add three radio button widget/annotation (aka AcroForms) in it.
27PTRadioButtonGroup *radio_group = [PTRadioButtonGroup Create: doc field_name: @"employee.radiogroup"];
28PTRadioButtonWidget *radiobutton1 = [radio_group Add: [[PTPDFRect alloc] initWithX1: 140 y1: 410 x2: 190 y2: 460] onstate: @""];
29[radiobutton1 SetBackgroundColor: [[PTColorPt alloc] initWithX: 1 y: 1 z: 0 w: 0] compnum: 3];
30[radiobutton1 RefreshAppearance];
31PTRadioButtonWidget *radiobutton2 = [radio_group Add: [[PTPDFRect alloc] initWithX1: 310 y1: 410 x2: 360 y2: 460] onstate: @""];
32[radiobutton2 SetBackgroundColor: [[PTColorPt alloc] initWithX: 0 y: 1 z: 0 w: 0] compnum: 3];
33[radiobutton2 RefreshAppearance];
34PTRadioButtonWidget *radiobutton3 = [radio_group Add: [[PTPDFRect alloc] initWithX1: 480 y1: 410 x2: 530 y2: 460] onstate: @""];
35[radiobutton3 EnableButton]; // Enable the third radio button. By default the first one is selected
36[radiobutton3 SetBackgroundColor: [[PTColorPt alloc] initWithX: 0 y: 1 z: 1 w: 0] compnum: 3];
37[radiobutton3 RefreshAppearance];
38[radio_group AddGroupButtonsToPage: blank_page];
39
40// Add the page as the last page in the document.
41[doc PagePushBack: blank_page];

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

About creating form fields

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

1PTField *my_field = [doc FieldCreate: @"address" type: e_pttext];

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 Apryse 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 .

Understand 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:

1PTFieldType type = [field GetType];
2switch(type)
3{
4 case e_ptbutton:
5 printf("Button");
6 break;
7 case e_ptcheck:
8 printf("Check");
9 break;
10 case e_ptradio:
11 printf("Radio");
12 break;
13 case e_pttext:
14 printf("Text")
15 break;
16 case e_ptchoice:
17 printf("Choice");
18 break;
19 case e_ptsignature:
20 printf("Signature");
21 break;
22 default:
23 break;
24}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales