PDF.js Express Plusplay_arrow

Professional PDF.js Viewing & Annotations - Try for free

side menu

Get Started

play_arrow

Learn more

play_arrow

Common use cases

play_arrow

Open a document

play_arrow

Save a document

play_arrow

Viewer

play_arrow

UI Customization

play_arrow

Annotations

play_arrow

Collaboration

play_arrow

Forms

play_arrow

Signature

play_arrow

Searching

play_arrow

Measurement

play_arrow

Compare

play_arrow

Advanced Capabilities

play_arrow

PDF.js Express REST API

play_arrow

Migration Guides

play_arrow

Add annotation events

The following features are available in:

check

PDF.js Express Viewer

help_outline

PDF.js Express Viewer is a free viewer with limited capabilities compared to PDF.js Express Plus

check

PDF.js Express Plus

help_outline

PDF.js Express Plus is a commercial PDF SDK for viewing, annotating, signing, form filling and more

There are a number of events related to annotations that can be useful to hook into. To do this you'll add a listener to the AnnotationManager.

Do not add the listener in the documentLoaded event
This will cause a new listener to be attached every time a new document is loaded
WebViewer(...)
  .then(instance => {
    const { documentViewer, annotationManager } = instance.Core;

    annotationManager.addEventListener('annotationChanged', () => {
      // ...
    });
  });

annotationChanged (add/modify/delete)

The annotationChanged event is fired every time an annotation is added, modified or deleted. The handler takes three parameters; the event object, an array of annotations that have changed and a string for the action (add, modify, delete).

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);
      });
    });
  })
The annotationChanged event will also be fired whenever annotations are imported from your server or inside the document, that is, they weren't created directly by a user.

If you want to do something different in that case, maybe ignore those events, you can use the imported property of the event object. For example:

WebViewer(...)
  .then(instance => {
    const { annotationManager } = instance.Core;
    annotationManager.addEventListener('annotationChanged', (annotations, action, { imported }) => {
      if (imported) {
        return;
      }
      // do event handling
    });
  })

annotationSelected

The annotationSelected event is fired any time an annotation is selected or deselected in the UI. The parameters are similar to annotationChanged; there is an event object, array of annotations and a string for the action (selected or deselected). If all annotations have been deselected then the annotations array will be null.

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 (annotations === null && action === 'deselected') {
        console.log('all annotations deselected');
      }
    });
  })

Annotations can be selected from the UI by clicking on them and once selected there will be a dashed border drawn around them. To programmatically select/deselect annotations you can use the selectAnnotation(s) and deselectAnnotation(s) functions. The getSelectedAnnotations will tell you which annotations are currently selected. For example, the following code deselects one of the currently selected annotations:

WebViewer(...)
  .then(instance => {
    const { annotationManager } = instance.Core;
    const selectedAnnots = annotationManager.getSelectedAnnotations();

    if (selectedAnnots.length > 0) {
      const firstSelectedAnnot = selectedAnnots[0];
      annotationManager.deselectAnnotation(firstSelectedAnnot);
    }
  });

There are several more events on AnnotationManager that may be useful to you.

annotationsLoaded

The annotationsLoaded event is fired when all the annotations internal to the document have been loaded. Since DocumentViewer is managing this process the event is fired on DocumentViewer.

WebViewer(...)
  .then(instance => {
    const { documentViewer } = instance.Core;
    documentViewer.addEventListener('annotationsLoaded', () => {
      // all annotations are available
      const annotations = annotationManager.getAnnotationsList();
    });
  })