Some test text!

menu
Modify form fieldskeyboard_arrow_down

Modifying PDF form fields

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', (annotations, action, { imported }) => {
    // if the annotation change occurs because of an import then
    // these are fields from inside the document
    if (imported && action === 'add') {
      annotations.forEach(function(annot) {
        if (annot instanceof Annotations.WidgetAnnotation) {
          annot.fieldFlags.set('ReadOnly', true);
        }
      });
    }
  });
});

linkIterate over all 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;

  const getFieldNameAndValue = (field) => {
    // Do something with data
    const { name, value } = field;
    console.log(`Name: ${name}, Value: ${value}`);
      
    // Check children fields
    field.children.forEach(getFieldNameAndValue);
  }

  docViewer.on('annotationsLoaded', () => {
    const fieldManager = annotManager.getFieldManager();
    fieldManager.forEachField(getFieldNameAndValue);
  });
});

linkAccessing fields by name

You can access any field by its field name:

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

  docViewer.on('documentLoaded', () => {
    docViewer.getAnnotationsLoadedPromise().then(function() {
      var fieldManager = annotManager.getFieldManager();
      var field = fieldManager.getField('fieldName');
      console.log(field.getValue());
      field.widgets.map(annot => {
        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.

Join our live demo to learn about use cases & capabilities for WebViewer

Learn more
close