> 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/web/forms/modify-fields.md).

# Modifying PDF form fields in JavaScript

Learn how to modify widget dimensions, field properties, and iterate over all fields in a PDF document with this comprehensive guide. Enhance your PDF editing skills now! The Apryse Web SDK streamline

This guide will walk you through modifying widget dimensions and position, modifying field properties, and iterating over all fields in a PDF document.

## Changing widget dimensions and position

Changing widget dimensions is the same process as changing any annotation dimensions. You can use the [`Annotation`](https://sdk.apryse.com/api/web/Core.Annotations.Annotation.html) APIs to change the dimensions and position of a widget.

The sample code below code listens to the [`annotationsLoaded`](https://sdk.apryse.com/api/web/Core.DocumentViewer.html#event:annotationsLoaded__anchor) event and changes the first widget annotation size to be a square and places it in the top-left corner of the page.

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

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

  documentViewer.addEventListener('annotationsLoaded', () => {
    const widgets = annotationManager.getAnnotationsList().filter(annotation => annotation instanceof Annotations.WidgetAnnotation);
    if (!widgets.length) {
      return;
    }

    const widget = widgets[0];
    widget.setWidth(50);
    widget.setHeight(50);
    widget.setX(10);
    widget.setY(10);
  });
});
```

{% endcode %}

[Annotations](https://sdk.apryse.com/api/web/Core.Annotations.html) [WebViewerInstance](https://sdk.apryse.com/api/web/WebViewerInstance.html) [DocumentViewer#annotationsLoaded](https://sdk.apryse.com/api/web/Core.DocumentViewer.html#event:annotationsLoaded__anchor)
{% endtab %}

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

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

  documentViewer.addEventListener('annotationsLoaded', () => {
    const widgets = annotationManager.getAnnotationsList().filter(annotation => annotation instanceof Annotations.WidgetAnnotation);
    if (!widgets.length) {
      return;
    }

    const widget = widgets[0];
    widget.setWidth(50);
    widget.setHeight(50);
    widget.setX(10);
    widget.setY(10);
  });
});
```

{% endcode %}

[Annotations](https://sdk.apryse.com/api/web/Core.Annotations.html) [WebViewerInstance](https://sdk.apryse.com/api/web/WebViewerInstance.html) [DocumentViewer#annotationsLoaded](https://sdk.apryse.com/api/web/Core.DocumentViewer.html#event:annotationsLoaded__anchor)
{% endtab %}
{% endtabs %}

## Changing field properties

In WebViewer 11 you can modify the properties of a field by using the [`WidgetAnnotation.setFieldFlag`](https://sdk.apryse.com/api/web/Core.Annotations.WidgetAnnotation.html#setFieldFlag__anchor) API. The sample code below listens to the [`annotationChanged`](https://sdk.apryse.com/api/web/Core.AnnotationManager.html#event:annotationChanged__anchor) event and updates the required property for newly added fields to ‘true’. Imported fields are ignored.

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

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

  annotationManager.addEventListener('annotationChanged', (annotations, action, { imported }) => {
    if (imported) {
      return;
    }

    annotations.forEach((annot) => {
      if (action === 'add' && annot instanceof Annotations.WidgetAnnotation) {
        annot.setFieldFlag(WidgetFlags.REQUIRED, true);
      }
    });
  });
});
```

{% endcode %}

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

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

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

  annotationManager.addEventListener('annotationChanged', (annotations, action, { imported }) => {
    if (imported) {
      return;
    }

    annotations.forEach(function(annot) {
      if (action === 'add' && annot instanceof Annotations.WidgetAnnotation) {
        annot.fieldFlags.set(WidgetFlags.REQUIRED, true);
      }
    });
  });
});
```

{% endcode %}

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

## Accessing fields by name

You can access any field by its name with the [`FieldManager.getField`](https://sdk.apryse.com/api/web/Core.Annotations.Forms.FieldManager.html#getField__anchor) API. The sample code below fetches a field with the name `fieldName` and sets all associated widgets to read-only.

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

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

  documentViewer.addEventListener('annotationsLoaded', () => {
    const fieldManager = annotationManager.getFieldManager();
    const field = fieldManager.getField(''); // Add the field name here.
    if (field) {
      field.widgets.map(annot => {
        annot.setFieldFlag(WidgetFlags.READ_ONLY, true);
      });
    }
  });
});
```

{% 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.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) [WidgetAnnotation.setFieldFlag](https://sdk.apryse.com/api/web/Core.Annotations.WidgetAnnotation.html#setFieldFlag)
{% endtab %}

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

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

  documentViewer.addEventListener('annotationsLoaded', () => {
    const fieldManager = annotationManager.getFieldManager();
    const field = fieldManager.getField(''); // Add the field name here.
    if (field) {
      field.widgets.map(annot => {
        annot.fieldFlags.set(WidgetFlags.READ_ONLY, true);
      });
    }
  });
});
```

{% 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.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) [WidgetAnnotation.setFieldFlag](https://sdk.apryse.com/api/web/Core.Annotations.WidgetAnnotation.html#setFieldFlag)
{% endtab %}
{% endtabs %}

## Iterate over all fields

Starting in WebViewer 11, you can iterate over all fields using the [getFields](https://sdk.apryse.com/api/web/Core.Annotations.Forms.FieldManager.html#getFields__anchor) API from FieldManager. The sample code below iterates through all form fields and prints out their name and values.

{% 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 v11.0+)" %}
{% code lineNumbers="true" %}

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

  // After the annotations are loaded, print out all field name and values.
  documentViewer.addEventListener('annotationsLoaded', () => {
    const fieldManager = annotationManager.getFieldManager();
    const fields = fieldManager.getFields();
    fields.forEach((field) => {
      const { name, value } = field;
      console.log(`Name: ${name}, Value: ${value}`);
    });
  });
});
```

{% 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 %}

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

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

  // After the annotations are loaded, print out all field name and values.
  documentViewer.addEventListener('annotationsLoaded', () => {
    const fieldManager = annotationManager.getFieldManager();
    fieldManager.forEachField(printFieldNameAndValue);
  });
});

function printFieldNameAndValue(field) {
  const { name, value } = field;
  console.log(`Name: ${name}, Value: ${value}`);

  // Recursively iterate through any child fields.
  field.children.forEach(printFieldNameAndValue);
}
```

{% 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 %}


---

# 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 dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

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

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
