For the complete documentation index, see llms.txt. This page is also available as Markdown.

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.

Info: The setCustomDrawHandler and restoreDraw APIs are available in WebViewer 7.1+. The custom line and fill style APIs require WebViewer 12.2.0+.

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

Rectangle annotation with the text "Hello" drawn in its top-left corner by a custom draw handler
Rectangle annotation drawn with the custom draw handler

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.

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.

Add custom line and fill styles

Info: The APIs in this section are available in WebViewer 12.2.0+.

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.

Style panels for the Rectangle and Line tools, with custom fill styles in the Fill Style dropdown and custom line styles in the Line Middle dropdown
Custom fill and line styles in the style panel

Register line styles

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

Two line annotations with a repeating letter H drawn along each line
Line annotations with the H pattern line style

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.

Two line annotations ending in a filled diamond
Line annotations with the diamond end style

Register fill styles

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

Rectangle, ellipse, and triangle annotations filled with a grid of dots
Shape annotations with custom dotted fill style

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.

Rectangle, ellipse, and triangle annotations filled with a blue and orange diagonal crosshatch
Shape annotations with the crosshatch fill style

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?