> 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 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/fill-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.
