Create new form fields and widget annotations on Server/Desktop

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

1// Create a blank page
2PDFDoc doc = new PDFDoc();
3Page blank_page = doc.PageCreate();
4
5// Create a new text field and text widget/annotation (aka AcroForms).
6Field text_field = doc.FieldCreate("employee.name", Field.Type.e_text);
7TextWidget text = TextWidget.Create(doc, new Rect(110, 660, 380, 690), text_field);
8text.SetFont(Font.Create(doc, Font.StandardType1Font.e_times_bold));
9text.RefreshAppearance();
10blank_page.AnnotPushBack(text);
11
12// Create a new signature field and signature annotation (aka AcroForms)
13DigitalSignatureField sig_field = doc.CreateDigitalSignatureField("employee.signature");
14SignatureWidget signature = SignatureWidget.Create(doc, new Rect(0, 100, 200, 150), sig_field);
15signature.RefreshAppearance();
16blank_page.AnnotPushBack(signature);
17
18// Create a new checkbox field and checkbox widget/annotation (aka AcroForms)
19Field checkbox_field = doc.FieldCreate("employee.checkbox", Field.Type.e_check);
20CheckBoxWidget checkbox = CheckBoxWidget.Create(doc, new Rect(190, 490, 250, 540), checkbox_field);
21checkbox.SetBackgroundColor(new ColorPt(1, 1, 1), 3);
22checkbox.SetBorderColor(new ColorPt(0, 0, 0), 3);
23checkbox.SetChecked(true); // Check the widget (by default it is unchecked).
24checkbox.RefreshAppearance();
25blank_page.AnnotPushBack(checkbox);
26
27// Create a radio button group and add three radio button widget/annotation (aka AcroForms) in it.
28RadioButtonGroup radio_group = RadioButtonGroup.Create(doc, "employee.radiogroup");
29RadioButtonWidget radiobutton1 = radio_group.Add(new Rect(140, 410, 190, 460));
30radiobutton1.SetBackgroundColor(new ColorPt(1, 1, 0), 3);
31radiobutton1.RefreshAppearance();
32RadioButtonWidget radiobutton2 = radio_group.Add(new Rect(310, 410, 360, 460));
33radiobutton2.SetBackgroundColor(new ColorPt(0, 1, 0), 3);
34radiobutton2.RefreshAppearance();
35RadioButtonWidget radiobutton3 = radio_group.Add(new Rect(480, 410, 530, 460));
36radiobutton3.EnableButton(); // Enable the third radio button. By default the first one is selected
37radiobutton3.SetBackgroundColor(new ColorPt(0, 1, 1), 3);
38radiobutton3.RefreshAppearance();
39radio_group.AddGroupButtonsToPage(blank_page);
40
41// Add the page as the last page in the document.
42doc.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:

1Field myField = doc.FieldCreate("address", Field.Type.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 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:

1FieldType type = field.GetType();
2switch(type)
3{
4case Field.FieldType.e_button:
5 Console.WriteLine("Button");
6 break;
7case Field.FieldType.e_check:
8 Console.WriteLine("Check");
9 break;
10case Field.FieldType.e_radio:
11 Console.WriteLine("Radio");
12 break;
13case Field.FieldType.e_text:
14 Console.WriteLine("Text");
15 break;
16case Field.FieldType.e_choice:
17 Console.WriteLine("Choice");
18 break;
19case Field.FieldType.e_signature:
20 Console.WriteLine("Signature");
21 break;
22}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales