Modifying PDF form fields in JavaScript
This guide will walk you through modifying widget dimensions and position, modifying field properties, and iterating over all fields in a PDF document.
Changing widget dimensions and position
Changing widget dimensions is the same process as changing any annotation dimensions. You can use the Annotation APIs to change the dimensions and position of a widget.
The sample code below code listens to the annotationsLoaded event and changes the first widget annotation size to be a square and places it in the top-left corner of the page.
WebViewer(...)
.then(instance => {
const { annotationManager, Annotations, documentViewer } = instance.Core;
documentViewer.addEventListener('annotationsLoaded', () => {
const widgets = annotationManager.getAnnotationsList().filter(annotation => annotation instanceof Annotations.WidgetAnnotation);
if (!widgets.length) {
return;
}
const widget = widgets[0];
widget.setWidth(50);
widget.setHeight(50);
widget.setX(10);
widget.setY(10);
});
});WebViewer(...)
.then(instance => {
const { annotationManager, Annotations, documentViewer } = instance.Core;
documentViewer.addEventListener('annotationsLoaded', () => {
const widgets = annotationManager.getAnnotationsList().filter(annotation => annotation instanceof Annotations.WidgetAnnotation);
if (!widgets.length) {
return;
}
const widget = widgets[0];
widget.setWidth(50);
widget.setHeight(50);
widget.setX(10);
widget.setY(10);
});
});Changing field properties
In WebViewer 11 you can modify the properties of a field by using the WidgetAnnotation.setFieldFlag API. The sample code below listens to the annotationChanged event and updates the required property for newly added fields to ‘true’. Imported fields are ignored.
Accessing fields by name
You can access any field by its name with the FieldManager.getField API. The sample code below fetches a field with the name fieldName and sets all associated widgets to read-only.
Iterate over all fields
Starting in WebViewer 11, you can iterate over all fields using the getFields API from FieldManager. The sample code below iterates through all form fields and prints out their name and values.
Last updated
Was this helpful?