Section:

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.

1// config.js
2const { annotationManager, Annotations } = instance.Core;
3
4annotationManager.addEventListener('annotationChanged', (annotations, action, { imported }) => {
5 // if the annotation change occurs because of an import then
6 // these are fields from inside the document
7 if (imported && action === 'add') {
8 annotations.forEach(function(annot) {
9 if (annot instanceof Annotations.WidgetAnnotation) {
10 annot.fieldFlags.set('ReadOnly', true);
11 }
12 });
13 }
14});

Iterate 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.

1// config.js
2const { documentViewer, annotationManager } = instance.Core;
3
4const getFieldNameAndValue = (field) => {
5 // Do something with data
6 const { name, value } = field;
7 console.log(`Name: ${name}, Value: ${value}`);
8
9 // Check children fields
10 field.children.forEach(getFieldNameAndValue);
11}
12
13documentViewer.addEventListener('annotationsLoaded', () => {
14 const fieldManager = annotationManager.getFieldManager();
15 fieldManager.forEachField(getFieldNameAndValue);
16});

Accessing fields by name

You can access any field by its field name:

1// config.js
2const { documentViewer, annotationManager } = instance.Core;
3
4documentViewer.addEventListener('documentLoaded', () => {
5 documentViewer.getAnnotationsLoadedPromise().then(function() {
6 const fieldManager = annotationManager.getFieldManager();
7 const field = fieldManager.getField('fieldName');
8 field.widgets.map(annot => {
9 annot.fieldFlags.set('ReadOnly', true);
10 });
11 });
12});

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales