Customize annotation rendering
The PDF specification defines a standard set of annotations, but not how they appear on screen, as that is left to the document viewers. WebViewer lets you change how annotations are rendered when viewing them using the setCustomDrawHandler API. You can apply this to both standard and custom annotations in WebViewer.
If you're using custom annotations, they already have support for custom rendering. However, implementing one may be too much if you only want to change how annotations look, especially the standard set. This API will allow you to change how the annotations render without having to implement one.
Change annotation rendering
To change how annotations are rendered, you can use the setCustomDrawHandler API.
Annotations.setCustomDrawHandler(Annotations.RectangleAnnotation, function(ctx, pageMatrix, rotation, options) {
options.originalDraw(ctx, pageMatrix, rotation);
ctx.save();
ctx.fillStyle = '#000000';
ctx.fillText('Hello', this.X, this.Y + 12);
ctx.restore();
});Related links

Alongside the annotation class you're targeting, you must provide a new custom draw function to the API to perform the rendering. It takes the regular parameters you would expect from an annotation draw function with an additional options parameter. If you are using an arrow function (bound function), then you can find the annotation through the options parameter.
Related links
Note that you get the annotation being drawn as well as the original annotation's draw function as part of the options parameter. This lets you reference the annotation and run the original draw function if needed.
Control appearances in other viewers
Although this works with annotations in WebViewer, other viewers will render annotations according to their own rendering. By default, setCustomDrawHandler will generate an appearance for the annotation so it will look similar in other viewers. If you want to turn this off, perhaps for changing rendering at certain times or if it's unnecessary, you can set the generateAppearance option in the third parameter of the API.
Restore custom annotation rendering
When customizing annotation rendering, the original draw function is preserved. Calling the setCustomDrawHandler API again will not overwrite the original draw function. This lets you restore the annotation's original behavior with the restoreDraw API.
Related links
Add custom line and fill styles
In addition to custom annotation renderings, APIs allow customized line styling on existing line annotation tools and customized fill styling on existing shape annotation tools. These styles will appear in the style panel on the selected tools as icons (if available) or text if icons are omitted.
Any custom line styles will appear in the dropdowns under the style heading on line tools. Custom fill styles will appear under the fill section in a new dropdown under the style heading.

Register line styles
To register custom line styles, you can use the registerCustomLineStyle API.

This API takes in a key that is tied to the custom line style and a custom draw handler responsible for drawing the custom line styling.
Alongside these two required parameters, the API also takes options to set which sections of the line the style applies to (section), which line tools this style can appear on (appliesTo), the title that will be shown in the style dropdown (title), an SVG string to display in the dropdown (svg), and extra bounds padding (padding). Re-registering a new custom style with a previously used and active key will override the key's previously associated handler and options and replace them with the new draw handler and options.

Register fill styles
To register custom fill styles, you can use the registerCustomFillStyle API.

This API takes a key tied to the custom fill style and a custom draw handler that draws the custom fill styling within the shape annotation.
Alongside these two required parameters, the API also takes options to set which type of shape annotation tool the style applies to, the title that will be shown in the style dropdown, an SVG string that will be displayed in the dropdown, a flag to use a custom function for clipping the fill to shape, and the actual custom function to be used in place of the default fill clipping function. Re-registering a new custom style with a previously used and active key will override the key's previously associated handler and options and replace them with the new draw handler and options.

Remove custom styles
To unregister currently registered custom line and fill styles, you can use the unregisterCustomLineStyle and unregisterCustomFillStyle APIs, respectively, which will clear the registered custom styles and their associated draw handlers.
Next steps
Check out how to customize the serialization/deserialization behavior of annotations to save your custom properties.
Last updated
Was this helpful?