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.

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

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.

1WebViewer(...)
2.then(instance => {
3 const { Annotations } = instance.Core;
4
5 Annotations.WidgetAnnotation.getCustomStyles = widget => {
6 if (widget instanceof Annotations.TextWidgetAnnotation) {
7 return {
8 'background-color': 'lightblue',
9 color: 'brown'
10 };
11 } else if (widget instanceof Annotations.PushButtonWidgetAnnotation) {
12 return {
13 'background-color': 'black',
14 color: 'white'
15 };
16 } else if (widget instanceof Annotations.CheckButtonWidgetAnnotation) {
17 return {
18 'background-color': 'lightgray',
19 opacity: 0.8
20 };
21 }
22 };
23});

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.

1WebViewer(...)
2.then(instance => {
3 const { Annotations } = instance.Core;
4
5 Annotations.WidgetAnnotation.getContainerCustomStyles = widget => {
6 if (widget instanceof Annotations.TextWidgetAnnotation) {
7 return {
8 'background-color': 'lightblue',
9 color: 'brown'
10 };
11 } else if (widget instanceof Annotations.PushButtonWidgetAnnotation) {
12 return {
13 'background-color': 'black',
14 color: 'white'
15 };
16 } else if (widget instanceof Annotations.CheckButtonWidgetAnnotation) {
17 return {
18 'background-color': 'lightgray',
19 opacity: 0.8
20 };
21 }
22 };
23});

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.

1WebViewer(...)
2.then(instance => {
3 const { Annotations } = instance.Core;
4
5 const createInnerElement = Annotations.CheckButtonWidgetAnnotation.prototype.createInnerElement;
6 Annotations.CheckButtonWidgetAnnotation.prototype.createInnerElement = function() {
7 const button = this;
8
9 const el = createInnerElement.apply(this, arguments);
10 el.addEventListener('click', () => {
11 console.log('check button clicked', button.fieldName);
12 });
13
14 return el;
15 };
16});

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales