Some test text!
Web / Guides / Annotation events
There are a number of events related to annotations that can be useful to hook into. This is especially important for knowing when annotations are created in order to export them or immediately change a property. To do this you'll add a listener to the AnnotationManager
.
WebViewer(...)
.then(instance => {
const { annotationManager } = instance.Core;
annotationManager.addEventListener('annotationChanged', () => {
// ...
});
});
The annotationChanged event is fired every time an annotation is added, modified, or deleted. The handler takes three parameters: an array of annotations that have changed, a string for the action (add, modify, delete), and an info object describing the event.
WebViewer(...)
.then(instance => {
const { annotationManager } = instance.Core;
annotationManager.addEventListener('annotationChanged', (annotations, action) => {
if (action === 'add') {
console.log('this is a change that added annotations');
} else if (action === 'modify') {
console.log('this change modified annotations');
} else if (action === 'delete') {
console.log('there were annotations deleted');
}
annotations.forEach((annot) => {
console.log('annotation page number', annot.PageNumber);
});
});
})
If you want to do something different in that scenario, perhaps ignore those types of events, you can use the imported
property of the info object. For example:
WebViewer(...)
.then(instance => {
const { annotationManager } = instance.Core;
annotationManager.addEventListener('annotationChanged', (annotations, action, { imported }) => {
// imported indicates if the annotations were imported via a process, mainly XFDF
if (imported) {
return;
}
// do event handling
});
})
One other property you might want to utilize is isUndoRedo
, which will return true if the annotation has changed as a result of an undo or redo action.
The annotationSelected event is fired any time an annotation is selected or deselected in the UI. The parameters are similar to annotationChanged
: an array of annotations and a string for the action (selected or deselected). The preferred way to detect deselection is using the annotationDeselected
event.
WebViewer(...)
.then(instance => {
const { annotationManager } = instance.Core;
annotationManager.addEventListener('annotationSelected', (annotations, action) => {
if (action === 'selected') {
console.log('annotation selection');
} else if (action === 'deselected') {
console.log('annotation deselection');
}
console.log('annotation list', annotations);
if (annotationManager.getSelectedAnnotations().length === 0 && action === 'deselected') {
console.log('all annotations deselected');
}
});
})
Using the annotationDeselected event is the recommended way to detect annotation deselection and getting the deselected annotations. You will get only the deselected annotations as a parameter to the listener.
WebViewer(...)
.then(instance => {
const { annotationManager, Annotations } = instance.Core;
annotationManager.addEventListener('annotationDeselected', (annotations) => {
annotations.forEach(annot => {
if (annot.FillColor) {
annot.FillColor = new Annotations.Color(255, 0, 0, 1);
}
});
});
})
The annotationsLoaded event is fired when all the annotations internal to the document have been loaded. It is one of the lifecycle events in WebViewer. Since DocumentViewer is managing this process the event is fired on DocumentViewer. When working with annotations, it would be best to use this event over documentLoaded
to ensure all annotations are available.
WebViewer(...)
.then(instance => {
const { documentViewer, annotationManager } = instance.Core;
documentViewer.addEventListener('annotationsLoaded', () => {
// all annotations are available
const annotations = annotationManager.getAnnotationsList();
});
})
Look into how you can create your own annotations!
Trial setup questions? Ask experts on Discord
Need other help? Contact Support
Pricing or product questions? Contact Sales