For the complete documentation index, see llms.txt. This page is also available as Markdown.

Fill PDF form fields in JavaScript

This guide will walk you through how to programmatically fill out PDF form fields. You can also try it now with our form fill demo.

Filling widget values

You can fill widget values by using the WidgetAnnotation.setValue API. This also internally updates the associated field value.The following sample code shows how to clear the value of all widgets in a document.

WebViewer(...)
.then(instance => {
  const { documentViewer, annotationManager, Annotations } = instance.Core;

  documentViewer.addEventListener('annotationsLoaded', () => {
    const widgets = annotationManager.getAnnotationsList().filter(annotation => annotation instanceof Annotations.WidgetAnnotation);
    widgets.forEach((widget) => {
      widget.setValue('');
    });
  });
});
WebViewer(...)
.then(instance => {
  const { docViewer, annotManager } = instance;

  docViewer.on('documentLoaded', () => {
    docViewer.getAnnotationsLoadedPromise().then(() => {
      const fieldManager = annotManager.getFieldManager();
      const field = fieldManager.getField(fieldId);
      field.setValue('new value');
    });
  });
});

Listening for widget events

Interacting with widget annotations causes a number of events to be fired. The following sample demonstrates how to respond to the ‘focus’ event.

Filling field values

You can access and fill any form field by its field name. The following sample demonstrates how to get a field by its name and set a new value.

Listening for field changes

The fieldChanged event is fired any time the value of a form field changes. The following sample shows how to listen to the event and log the changed field's name and value.

Last updated

Was this helpful?