Create new PDF form fields using JavaScript

Form fields, also known as AcroForms, are a collection of fields such as text boxes, checkboxes, radio buttons, drop-down lists, push buttons, and more that will gather information interactively from the user.

Widget Annotation

One of the most important ideas to understand is the appearance (how it is displayed) of a form field is independent of the field itself and exists as a widget annotation. In fact, there can be multiple widget annotations for a single field. This gives the freedom to present a field appearance over multiple pages or even multiple times on the same page of a document.

Annotations and fields should be added to the document only after the document has finished loading. This can be done by listening for the DocumentViewer.documentLoaded event:

1WebViewer(...)
2 .then(instance => {
3 const { documentViewer } = instance.Core;
4 documentViewer.addEventListener('documentLoaded', () => {
5 // create field and widget annotations here
6 });
7 });

Types of Fields to Add

Text fields and widget annotations can be added programmatically. There are several properties and widget flags as highlighted below:

Text widget flags

Widget Flag

Description

Unique to text?

READ_ONLY

The field value cannot be changed.

No

REQUIRED

The field must have a value when exported.

No

MULTILINE

The field may contain multiple lines of text.

Yes

COMB

The field shall be automatically divided into as many equally spaced positions as the value of the max length.

Yes

DO_NOT_SCROLL

The field shall not scroll (horizontally for single-line fields, vertically for multiple-line fields) to accommodate more text than fits within its annotation rectangle.

Yes

DO_NOT_SPELL_CHECK

The field text shall not be spell-checked.

Yes

Text widget sample code

1WebViewer(...)
2.then(instance => {
3 const { annotationManager, Annotations, documentViewer } = instance.Core;
4 const { WidgetFlags } = Annotations;
5
6 documentViewer.addEventListener('documentLoaded', () => {
7 // Sets flags for the text widget.
8 const flags = new WidgetFlags();
9 flags.set(WidgetFlags.REQUIRED, true);
10 flags.set(WidgetFlags.MULTILINE, true);
11
12 // Creates a text form field.
13 const field = new Annotations.Forms.Field('TextFormField 1', {
14 type: 'Tx',
15 defaultValue: 'Default Value',
16 flags,
17 });
18
19 // Creates a text widget annotation.
20 const widgetAnnot = new Annotations.TextWidgetAnnotation(field);
21 widgetAnnot.PageNumber = 1;
22 widgetAnnot.X = 100;
23 widgetAnnot.Y = 100;
24 widgetAnnot.Width = 200;
25 widgetAnnot.Height = 50;
26
27 // Add form field to field manager and widget annotation to annotation manager.
28 annotationManager.getFieldManager().addField(field);
29 annotationManager.addAnnotation(widgetAnnot);
30 annotationManager.drawAnnotationsFromList([widgetAnnot]);
31 });
32});

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales