Some test text!

Search
Hamburger Icon

Web / Guides / Style form fields

Styling PDF form fields

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 or Widget.getCustomContainerStyles functions and were not serialized or saved to the document.

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

Styling the widget inner element

Implementing the WidgetAnnotation's getCustomStyles function 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.

These style changes will not be serialized and therefore won't be saved to the PDF document.
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
      };
    }
  };
});

Styling the widget container element

Implementing the WidgetAnnotation's getContainerCustomStyles function allows you to add styling changes to the container element of the widget annotation.

These style changes will not be serialized and therefore won't be saved to the PDF document.
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
      };
    }
  };
});

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.

These style changes will not be serialized and therefore won't be saved to the PDF document.
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;
  };
});

Trial setup questions? Ask experts on Discord
Need other help? Contact Support
Pricing or product questions? Contact Sales