> 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/fill-fields.md).

# Fill PDF form fields in JavaScript

Learn how to fill PDF form fields in JavaScript with this comprehensive guide. Access and fill any field by its ID, iterate over all fields, and listen for field changes easily. The Apryse Web SDK str

This guide will walk you through how to programmatically fill out PDF form fields. You can also try it now with our [form fill demo](https://showcase.apryse.com/pdf-forms).

## Filling widget values

You can fill widget values by using the [`WidgetAnnotation.setValue`](https://sdk.apryse.com/api/web/Core.Annotations.WidgetAnnotation.html#setValue__anchor) API. This also internally updates the associated field value.The following sample code shows how to clear the value of all widgets in a document.

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

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

  documentViewer.addEventListener('annotationsLoaded', () => {
    const widgets = annotationManager.getAnnotationsList().filter(annotation => annotation instanceof Annotations.WidgetAnnotation);
    widgets.forEach((widget) => {
      widget.setValue('');
    });
  });
});
```

{% endcode %}

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

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

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

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

{% endcode %}

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

## Listening for widget events

Interacting with widget annotations causes a number of [events](https://sdk.apryse.com/api/web/Core.Annotations.WidgetAnnotation.html#toc111__anchor) to be fired. The following sample demonstrates how to respond to the ‘focus’ event.

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

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

  const DEFAULT_VALUE = 'N/A';

  documentViewer.addEventListener('annotationsLoaded', () => {
    const textWidgets = annotationManager.getAnnotationsList().filter(annotation => annotation instanceof Annotations.TextWidgetAnnotation);
    textWidgets.forEach((widget) => {
      widget.addEventListener('focus', () => {
        if (!widget.getValue()) {
          widget.setValue(DEFAULT_VALUE);
          annotationManager.redrawAnnotation(widget);
        }
      });
    });
  });
});
```

{% endcode %}

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

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

```js
WebViewer(...)
.then(instance => {
  const { docViewer, annotManager } = instance;
  const DEFAULT_VALUE = 'N/A';

  docViewer.on('documentLoaded', () => {
    docViewer.getAnnotationsLoadedPromise().then(() => {
      const textWidgets = annotManager.getAnnotationsList().filter(annotation => annotation instanceof Annotations.TextWidgetAnnotation);
      textWidgets.forEach((widget) => {
        widget.addEventListener('focus', () => {
          if (!widget.getValue()) {
            widget.setValue(DEFAULT_VALUE);
            annotManager.redrawAnnotation(widget);
          }
        });
      });
    });
  });
});
```

{% endcode %}

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

## Filling field values

You can access and fill any form field by its field name. The following sample demonstrates how to get a field by its name and set a new value.

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

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

  documentViewer.addEventListener('annotationsLoaded', () => {
    const fieldManager = annotationManager.getFieldManager();
    const fieldName = ''; // Add field name here.
    const field = fieldManager.getField(fieldName);
    if (field) {
      field.setValue('new value');
    }
  });
});
```

{% endcode %}

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

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

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

  docViewer.on('documentLoaded', () => {
    docViewer.getAnnotationsLoadedPromise().then(() => {
      const fieldManager = annotManager.getFieldManager();
      const fieldName = ''; // Add field name here.
      const field = fieldManager.getField(fieldName);
      if (field) {
        field.setValue('new value');
      }
    });
  });
});
```

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

## Listening for field changes

The [fieldChanged](https://sdk.apryse.com/api/web/Core.AnnotationManager.html#event:fieldChanged__anchor) event is fired any time the value of a form field changes. The following sample shows how to listen to the event and log the changed field's name and value.

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

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

  annotationManager.addEventListener('fieldChanged', (field, value) => {
    console.log(`Field changed: ${field.name}, ${value}`);
  });
});
```

{% endcode %}

[AnnotationManager#fieldChanged](https://sdk.apryse.com/api/web/Core.AnnotationManager.html#event:fieldChanged__anchor)
{% endtab %}

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

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

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

{% endcode %}

[AnnotationManager#fieldChanged](https://sdk.apryse.com/api/web/Core.AnnotationManager.html#event:fieldChanged__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 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/web/forms/fill-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.
