> 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/form-field-styling.md).

# Styling PDF form fields

Discover how to enhance the appearance of PDF form fields with styling changes using WidgetAnnotation's functions. Customize widget annotations' styles and container elements effortlessly, and create

## Styling widgets programmatically

Starting in WebViewer 11 you can style widgets by modifying their annotation properties directly. These styles are serialized and therefore saved to the PDF document.

In earlier versions, styles could only be modified by using the [`Widget.getCustomStyles`](https://sdk.apryse.com/api/web/Core.Annotations.WidgetAnnotation.html#WidgetAnnotation__anchor) or [`Widget.getCustomContainerStyles`](https://sdk.apryse.com/api/web/Core.Annotations.WidgetAnnotation.html#WidgetAnnotation__anchor) functions and were not serialized or saved to the document.

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

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

  documentViewer.addEventListener('annotationsLoaded', () => {
    const widgets = annotationManager.getAnnotationsList().filter(annotation => annotation instanceof Annotations.WidgetAnnotation);
    widgets.forEach(widget => {
      // Changes widget border color and thickness.
      widget.StrokeColor = new Color(255, 255, 50, 1);
      widget.StrokeThickness = 5;

      // Changes fill color within widget container.
      widget.FillColor = new Color(100, 0, 0, 1);

      // Changes widget position.
      widget.X = Math.floor(Math.random() * 500) + 1;
      widget.Y = Math.floor(Math.random() * 500) + 1;

      // Changes widget dimensions.
      widget.Width = Math.floor(Math.random() * (300 - 100 + 1)) + 100;
      widget.Height = Math.floor(Math.random() * (300 - 100 + 1)) + 100;

      // Redraws widget with updated styling.
      widget.refresh();
    });

    const widgetsWithText = widgets.filter(widget => {
      return (
        widget instanceof Annotations.TextWidgetAnnotation ||
        widget instanceof Annotations.ListWidgetAnnotation ||
        widget instanceof Annotations.ChoiceWidgetAnnotation
      );
    });

    // Change font family, size, and color of any widgets with text.
    widgetsWithText.forEach(widget => {
      widget.font = new Annotations.Font({ name: 'Arial', size: 15 });
      widget.TextColor = new Color(255, 255, 255, 1);
      widget.refresh();
    });
  });
});
```

{% endcode %}

[Annotations.Color](https://sdk.apryse.com/api/web/Core.Annotations.Color.html) [Annotations.Font](https://sdk.apryse.com/api/web/Core.Annotations.Font.html) [Annotations.WidgetAnnotation](https://sdk.apryse.com/api/web/Core.Annotations.WidgetAnnotation.html) [AnnotationManager](https://sdk.apryse.com/api/web/Core.AnnotationManager.html)
{% endtab %}

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

```js
WebViewer(...)
.then(instance => {
  // This method of styling widgets is not available in WebViewer 8.
  // See sections below for usage on getCustomStyles()
  // and getCustomContainerStyles().
});
```

{% endcode %}
{% endtab %}

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

```js
WebViewer(...)
.then(instance => {
  // This method of styling widgets is not available in WebViewer 6.
  // See sections below for usage on getCustomStyles()
  // and getCustomContainerStyles().
});
```

{% endcode %}
{% endtab %}
{% endtabs %}

## Styling the widget inner element

Implementing the [WidgetAnnotation's getCustomStyles function](https://sdk.apryse.com/api/web/Core.Annotations.WidgetAnnotation.html#WidgetAnnotation__anchor) allows you to add styling changes to particular types of widget annotations. This function gets called for every annotation but in the sample below we show how to filter for all widgets, and style specific widgets based on their type.

{% hint style="warning" %}
These style changes will not be serialized and therefore won't be saved to the PDF document.
{% endhint %}

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

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

  Annotations.WidgetAnnotation.getCustomStyles = widget => {
    if (widget instanceof Annotations.TextWidgetAnnotation) {
      // can check widget properties
      if (widget.fieldName === 'f1-1') {
        return {
          'background-color': 'lightgreen'
        };
      }
      return {
        'background-color': 'lightblue',
        color: 'brown'
      };
    } else if (widget instanceof Annotations.PushButtonWidgetAnnotation) {
      return {
        'background-color': 'black',
        color: 'white'
      };
    } else if (widget instanceof Annotations.CheckButtonWidgetAnnotation) {
      return {
        'background-color': 'lightgray',
        opacity: 0.8
      };
    }
  };
});
```

{% endcode %}

[Annotations.WidgetAnnotation](https://sdk.apryse.com/api/web/Core.Annotations.WidgetAnnotation.html)
{% endtab %}

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

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

  Annotations.WidgetAnnotation.getCustomStyles = widget => {
    if (widget instanceof Annotations.TextWidgetAnnotation) {
      return {
        'background-color': 'lightblue',
        color: 'brown'
      };
    } else if (widget instanceof Annotations.PushButtonWidgetAnnotation) {
      return {
        'background-color': 'black',
        color: 'white'
      };
    } else if (widget instanceof Annotations.CheckButtonWidgetAnnotation) {
      return {
        'background-color': 'lightgray',
        opacity: 0.8
      };
    }
  };
});
```

{% endcode %}

[Annotations.WidgetAnnotation](https://sdk.apryse.com/api/web/Core.Annotations.WidgetAnnotation.html)
{% endtab %}

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

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

  Annotations.WidgetAnnotation.getCustomStyles = widget => {
    if (widget instanceof Annotations.TextWidgetAnnotation) {
      return {
        'background-color': 'lightblue',
        color: 'brown'
      };
    } else if (widget instanceof Annotations.PushButtonWidgetAnnotation) {
      return {
        'background-color': 'black',
        color: 'white'
      };
    } else if (widget instanceof Annotations.CheckButtonWidgetAnnotation) {
      return {
        'background-color': 'lightgray',
        opacity: 0.8
      };
    }
  };
});
```

{% endcode %}

[Annotations.WidgetAnnotation](https://sdk.apryse.com/api/web/Core.Annotations.WidgetAnnotation.html)
{% endtab %}
{% endtabs %}

## Styling the widget container element

Implementing the [WidgetAnnotation's getContainerCustomStyles function](https://sdk.apryse.com/api/web/Core.Annotations.WidgetAnnotation.html#WidgetAnnotation__anchor) allows you to add styling changes to the ***container element*** of the widget annotation.

{% hint style="warning" %}
These style changes will not be serialized and therefore won't be saved to the PDF document.
{% endhint %}

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

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

  Annotations.WidgetAnnotation.getContainerCustomStyles = widget => {
    if (widget instanceof Annotations.TextWidgetAnnotation) {
      return {
        'background-color': 'lightblue',
        color: 'brown'
      };
    } else if (widget instanceof Annotations.PushButtonWidgetAnnotation) {
      return {
        'background-color': 'black',
        color: 'white'
      };
    } else if (widget instanceof Annotations.CheckButtonWidgetAnnotation) {
      return {
        'background-color': 'lightgray',
        opacity: 0.8
      };
    }
  };
});
```

{% endcode %}

[Annotations.WidgetAnnotation](https://sdk.apryse.com/api/web/Core.Annotations.WidgetAnnotation.html)
{% endtab %}

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

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

  Annotations.WidgetAnnotation.getContainerCustomStyles = widget => {
    if (widget instanceof Annotations.TextWidgetAnnotation) {
      return {
        'background-color': 'lightblue',
        color: 'brown'
      };
    } else if (widget instanceof Annotations.PushButtonWidgetAnnotation) {
      return {
        'background-color': 'black',
        color: 'white'
      };
    } else if (widget instanceof Annotations.CheckButtonWidgetAnnotation) {
      return {
        'background-color': 'lightgray',
        opacity: 0.8
      };
    }
  };
});
```

{% endcode %}

[Annotations.WidgetAnnotation](https://sdk.apryse.com/api/web/Core.Annotations.WidgetAnnotation.html)
{% endtab %}

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

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

  Annotations.WidgetAnnotation.getContainerCustomStyles = widget => {
    if (widget instanceof Annotations.TextWidgetAnnotation) {
      // can check widget properties
      if (widget.fieldName === 'f1-1') {
        return {
          'background-color': 'lightgreen'
        };
      }
      return {
        'background-color': 'lightblue',
        color: 'brown'
      };
    } else if (widget instanceof Annotations.PushButtonWidgetAnnotation) {
      return {
        'background-color': 'black',
        color: 'white'
      };
    } else if (widget instanceof Annotations.CheckButtonWidgetAnnotation) {
      return {
        'background-color': 'lightgray',
        opacity: 0.8
      };
    }
  };
});
```

{% endcode %}

[Annotations.WidgetAnnotation](https://sdk.apryse.com/api/web/Core.Annotations.WidgetAnnotation.html)
{% endtab %}
{% endtabs %}

## Customizing the widget DOM element

You can also extend the createInnerElement function on widget annotations. This allows you to use your own DOM elements for the display or just to add your own event handlers.

{% hint style="warning" %}
These style changes will not be serialized and therefore won't be saved to the PDF document.
{% endhint %}

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

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

  const createInnerElement = Annotations.CheckButtonWidgetAnnotation.prototype.createInnerElement;
  Annotations.CheckButtonWidgetAnnotation.prototype.createInnerElement = function() {
    const button = this;

    const el = createInnerElement.apply(this, arguments);
    el.addEventListener('click', () => {
      console.log('check button clicked', button.fieldName);
    });

    return el;
  };
});
```

{% endcode %}

[Annotations.CheckButtonWidgetAnnotation](https://sdk.apryse.com/api/web/Core.Annotations.CheckButtonWidgetAnnotation.html)
{% endtab %}

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

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

  const createInnerElement = Annotations.CheckButtonWidgetAnnotation.prototype.createInnerElement;
  Annotations.CheckButtonWidgetAnnotation.prototype.createInnerElement = function() {
    const button = this;

    const el = createInnerElement.apply(this, arguments);
    el.addEventListener('click', () => {
      console.log('check button clicked', button.fieldName);
    });

    return el;
  };
});
```

{% endcode %}

[Annotations.CheckButtonWidgetAnnotation](https://sdk.apryse.com/api/web/Core.Annotations.CheckButtonWidgetAnnotation.html)
{% endtab %}

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

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

  const createInnerElement = Annotations.CheckButtonWidgetAnnotation.prototype.createInnerElement;
  Annotations.CheckButtonWidgetAnnotation.prototype.createInnerElement = function() {
    const button = this;

    const el = createInnerElement.apply(this, arguments);
    el.addEventListener('click', () => {
      console.log('check button clicked', button.fieldName);
    });

    return el;
  };
});
```

{% endcode %}

[Annotations.CheckButtonWidgetAnnotation](https://sdk.apryse.com/api/web/Core.Annotations.CheckButtonWidgetAnnotation.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 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/form-field-styling.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.
