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 => {
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();
});
});
});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.
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.
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.
Last updated
Was this helpful?