> For the complete documentation index, see [llms.txt](https://docs.apryse.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.apryse.com/salesforce/forms/modify-fields.md).

# Modifying PDF form fields

Learn how to set fields to readonly, trigger events, iterate over fields, and access fields by name in your document with this informative guide. Salesforce WebViewer adds accurate, reliable document

The following code sets all fields to readonly when the document is loaded. The [`annotationChanged`](https://sdk.apryse.com/api/web/Core.AnnotationManager.html#event:annotationChanged__anchor) 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.

{% tabs %}
{% tab title="JavaScript (SDK v8.0+)" %}
{% code lineNumbers="true" %}

```js
// config.js
const { annotationManager, Annotations } = instance.Core;

annotationManager.addEventListener('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);
      }
    });
  }
});
```

{% endcode %}

[WebViewerInstance](https://sdk.apryse.com/api/web/WebViewerInstance.html) [Core](https://sdk.apryse.com/api/web/Core.html) [AnnotationManager#annotationChanged](https://sdk.apryse.com/api/web/Core.AnnotationManager.html#event:annotationChanged__anchor) [Annotations](https://sdk.apryse.com/api/web/Core.Annotations.html)
{% endtab %}

{% tab title="JavaScript (SDK v6.0+)" %}
{% code lineNumbers="true" %}

```js
// config.js
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);
      }
    });
  }
});
```

{% endcode %}

[WebViewerInstance](https://sdk.apryse.com/api/web/WebViewerInstance.html#toc0__anchor) [AnnotationManager#annotationChanged](https://sdk.apryse.com/api/web/Core.AnnotationManager.html#event:annotationChanged__anchor) [Annotations](https://sdk.apryse.com/api/web/Core.Annotations.html)
{% endtab %}
{% endtabs %}

## Iterate over all fields

You can iterate over all fields using the [forEachField](https://sdk.apryse.com/api/web/Core.Annotations.Forms.FieldManager.html#forEachField__anchor) function of FieldManager.

{% hint style="info" %}
Note that you'll need to wait for the [annotationsLoadedPromise](https://sdk.apryse.com/api/web/Core.DocumentViewer.html#getAnnotationsLoadedPromise__anchor) to resolve to ensure that the fields are accessible in the FieldManager.
{% endhint %}

{% tabs %}
{% tab title="JavaScript (SDK v8.0+)" %}
{% code lineNumbers="true" %}

```js
// config.js
const { documentViewer, annotationManager } = instance.Core;
const getFieldNameAndValue = (field) => {
  // Do something with data
  const { name, value } = field;
  console.log(`Name: ${name}, Value: ${value}`);
  
  // Check children fields
  field.children.forEach(getFieldNameAndValue);
}
documentViewer.addEventListener('annotationsLoaded', () => {
  const fieldManager = annotationManager.getFieldManager();
  const fields = fieldManager.getFields();
  fields.forEach(getFieldNameAndValue)
});
```

{% endcode %}

[DocumentViewer#annotationsLoaded](https://sdk.apryse.com/api/web/Core.DocumentViewer.html#event:annotationsLoaded__anchor) [AnnotationManager.getFieldManager](https://sdk.apryse.com/api/web/Core.AnnotationManager.html#getFieldManager__anchor) [FieldManager.getFields()](https://sdk.apryse.com/api/web/Core.Annotations.Forms.FieldManager.html#getFields__anchor)
{% endtab %}

{% tab title="JavaScript (SDK v6.0+)" %}
{% code lineNumbers="true" %}

```js
// config.js
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);
});
```

{% endcode %}

[DocumentViewer#annotationsLoaded](https://sdk.apryse.com/api/web/Core.DocumentViewer.html#event:annotationsLoaded__anchor) [AnnotationManager.getFieldManager](https://sdk.apryse.com/api/web/Core.AnnotationManager.html#getFieldManager__anchor) [FieldManager.forEachField](https://sdk.apryse.com/api/web/Core.Annotations.Forms.FieldManager.html#forEachField__anchor)
{% endtab %}
{% endtabs %}

## Accessing fields by name

You can access any field by its field name:

{% tabs %}
{% tab title="JavaScript (SDK v8.0+)" %}
{% code lineNumbers="true" %}

```js
// config.js
const { documentViewer, annotationManager } = instance.Core;

documentViewer.addEventListener('documentLoaded', () => {
  documentViewer.getAnnotationsLoadedPromise().then(function() {
    const fieldManager = annotationManager.getFieldManager();
    const field = fieldManager.getField('fieldName');
    field.widgets.map(annot => {
      annot.fieldFlags.set('ReadOnly', true);
    });
  });
});
```

{% endcode %}

[DocumentViewer.getAnnotationsLoadedPromise](https://sdk.apryse.com/api/web/Core.DocumentViewer.html#getAnnotationsLoadedPromise__anchor) [DocumentViewer#documentLoaded](https://sdk.apryse.com/api/web/Core.DocumentViewer.html#event:documentLoaded__anchor) [AnnotationManager.getFieldManager](https://sdk.apryse.com/api/web/Core.AnnotationManager.html#getFieldManager__anchor) [FieldManager.getField](https://sdk.apryse.com/api/web/Core.Annotations.Forms.FieldManager.html#getField__anchor) [Annotations.WidgetFlags](https://sdk.apryse.com/api/web/Core.Annotations.WidgetFlags.html)
{% endtab %}

{% tab title="JavaScript (SDK v6.0+)" %}
{% code lineNumbers="true" %}

```js
// config.js
const { docViewer, annotManager } = instance;

docViewer.on('documentLoaded', () => {
  docViewer.getAnnotationsLoadedPromise().then(function() {
    const fieldManager = annotManager.getFieldManager();
    const field = fieldManager.getField('fieldName');
    field.widgets.map(annot => {
      annot.fieldFlags.set('ReadOnly', true);
    });
  });
});
```

{% endcode %}

[DocumentViewer.getAnnotationsLoadedPromise](https://sdk.apryse.com/api/web/Core.DocumentViewer.html#getAnnotationsLoadedPromise__anchor) [DocumentViewer#documentLoaded](https://sdk.apryse.com/api/web/Core.DocumentViewer.html#event:documentLoaded__anchor) [AnnotationManager.getFieldManager](https://sdk.apryse.com/api/web/Core.AnnotationManager.html#getFieldManager__anchor) [FieldManager.getField](https://sdk.apryse.com/api/web/Core.Annotations.Forms.FieldManager.html#getField__anchor) [Annotations.WidgetFlags](https://sdk.apryse.com/api/web/Core.Annotations.WidgetFlags.html)
{% endtab %}
{% endtabs %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.apryse.com/salesforce/forms/modify-fields.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
