Customizing annotation tools

Customizing tools is little different from customizing header items. In WebViewer UI, they are setup in a special way to be mapped to DocumentViewer's tool modes and grouped into tool group buttons. Thus, WebViewer UI provides specific APIs for customizing tools.

Disabling/enabling tools

By default, WebViewer provides shortcuts for most of the tools. If you want users not to use certain tools, you should disable both the DOM element and the shortcut. You can do that with a single API, using one of the following:

And you can check if a tool is disabled using this API:

You can refer to the full list of tool names for the values to pass to these functions.

For example,

1WebViewer(...)
2 .then(instance => {
3 instance.UI.disableTools([ 'AnnotationCreateSticky', 'AnnotationCreateFreeText' ]); // hides DOM element + disables shortcut
4 console.log(instance.UI.isToolDisabled('AnnotationCreateSticky'));
5
6 // re-enable every tool when no parameter is passed in
7 instance.UI.enableTools();
8 });

Updating tool property

You can also update tool properties to customize the buttons on the header using updateTool:

  • buttonImage
  • buttonName
  • buttonGroup
  • tooltip

Changing button icon

1WebViewer(...)
2 .then(instance => {
3 instance.UI.updateTool('AnnotationCreateSticky', {
4 buttonImage: 'https://www.pdftron.com/favicon-32x32.png'
5 });
6 });

Changing button group

1WebViewer(...)
2 .then(instance => {
3 instance.UI.updateTool('AnnotationCreateRectangle', {
4 buttonGroup: 'miscTools'
5 });
6 });

Event when tool creates an annotation

WebViewer provides the annotationAdded event on most annotation tools which is fired when a new annotation is created with that tool.

1WebViewer(...)
2 .then(instance => {
3 const { documentViewer } = instance.Core;
4 const tool = documentViewer.getTool('AnnotationCreateFreeText');
5
6 // the tool is just a reference to the instance of a tool
7 // it could also be something like:
8 // const tool = new MyCustomTool(documentViewer);
9 tool.addEventListener('annotationAdded', (annotation) => {
10 annotation.FillColor = new Annotations.Color(255, 0, 0);
11 });
12 });

Extending built in tools

WebViewer allows you to extend the behavior of tools by overriding certain functions on them. Common functions you might want to override include mouseLeftDown, mouseMove and mouseLeftUp among others.

As an example let's change the highlight tool so that when the user finishes highlighting, the highlight will turn cyan. To override a particular function on a tool we can set its value to a new function.

1WebViewer(...)
2 .then(instance => {
3 const { Tools } = instance.Core;
4 // get a copy of the default mouse up function
5 const highlightMouseUp = Tools.TextHighlightCreateTool.prototype.mouseLeftUp;
6 // set it to our own function
7 Tools.TextHighlightCreateTool.prototype.mouseLeftUp = function() {
8 // just call the original function for now, passing all the arguments
9 highlightMouseUp.apply(this, arguments);
10 };
11 })

In the example code above we're just calling the original mouse up function that we saved as a variable. Just using that code there should be no visible change in the behavior of the highlight tool.

Let's change the color of the annotation now. All annotation tools have an "annotation" property which is the current annotation being created by the tool. It's created in mouseLeftDown, modified in mouseMove and removed from the tool in mouseLeftUp. This means we need to access it before the original mouseLeftUp function is called.

1WebViewer(...)
2 .then(instance => {
3 const { Tools } = instance.Core;
4 const highlightMouseUp = Tools.TextHighlightCreateTool.prototype.mouseLeftUp;
5 Tools.TextHighlightCreateTool.prototype.mouseLeftUp = function() {
6 if (this.annotation) {
7 this.annotation.StrokeColor = new Annotations.Color(0, 255, 255);
8 }
9 highlightMouseUp.apply(this, arguments);
10 };
11 })

If you try this code you'll see that it almost works, however you need to click the page again for the annotation to be redrawn in the new color. To fix this we'll need to redraw the annotation inside the tool so that it's updated right away.

1WebViewer(...)
2 .then(instance => {
3 const { Tools, annotationManager } = instance.Core;
4 const highlightMouseUp = Tools.TextHighlightCreateTool.prototype.mouseLeftUp;
5 Tools.TextHighlightCreateTool.prototype.mouseLeftUp = function() {
6 if (this.annotation) {
7 this.annotation.StrokeColor = new Annotations.Color(0, 255, 255);
8 annotationManager.redrawAnnotation(this.annotation);
9 }
10 highlightMouseUp.apply(this, arguments);
11 };
12 })

Now the highlight should change colors right after it's created. As mentioned earlier you can override several different functions on tools and it should work similarly to the example above.

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales