Some test text!

menu
search

Modifying form fields

You can iterate over all fields using the forEachField function of FieldManager.

Note that you'll need to wait for the annotationsLoadedPromise to resolve to ensure that the fields are accessible in the FieldManager.
WebViewer(...)
.then(instance => {
  const { docViewer, annotManager } = instance;

  docViewer.on('documentLoaded', () => {
    docViewer.getAnnotationsLoadedPromise().then(() => {
      // iterate over fields
      var fieldManager = annotManager.getFieldManager();
      fieldManager.forEachField(field => {
        console.log(field.getValue());
      });
    });
  });
});

linkAccessing fields by id

You can access any field by its field id:

WebViewer(...)
.then(instance => {
  const { docViewer, annotManager } = instance;

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

linkListening for field changes

The fieldChanged event is fired any time the value of a form field changes.

WebViewer(...)
.then(instance => {
  const { annotManager } = instance;

  annotManager.on('fieldChanged', (e, field, value) => {
    console.log('Field changed: ' + field.name + ', ' + value);
  });
});

linkModify fields to readonly

The following code sets all fields to readonly when the document is loaded. The annotationChanged event is triggered when the annotations are added to the document and checking the action and e.imported ensures that they changed because of an import into the document and not because of a user modification.

WebViewer(...)
.then(instance => {
  const { docViewer, annotManager, Annotations } = instance;

  annotManager.on('annotationChanged', (e, annotations, action) => {
    // if the annotation change occurs because of an import then
    // these are fields from inside the document
    if (action === 'add' && e.imported) {
      annotations.forEach(function(annot) {
        if (annot instanceof Annotations.WidgetAnnotation) {
          annot.fieldFlags.set('ReadOnly', true);
        }
      });
    }
  });
});

Get the answers you need: Support

close

Free Trial

Get unlimited trial usage of PDFTron SDK to bring accurate, reliable, and fast document processing capabilities to any application or workflow.

Select a platform to get started with your free trial.

Unlimited usage. No email address required.

PDFTron Receives USD$71 Million Growth Investment Led By Silversmith Capital Partners

Learn more
close