Skip to content

PDF Generation Template Editor

Open-source visual PDF generation engine with customizable templates and developer-friendly APIs.

View on GitHub

Events ​

There are two kinds of events: Core events, fired by documentViewer, annotationManager and other Core objects, and UI events, fired by instance.UI.

Core events ​

Core objects have addEventListener(name, handler, options?) and removeEventListener(name, handler):

javascript
const { documentViewer, annotationManager } = instance.Core;
documentViewer.addEventListener('pageNumberUpdated', (pageNumber) => console.log(pageNumber));
annotationManager.addEventListener('annotationChanged', (annotations, action) => console.log(action));

Common documentViewer events ​

EventArgumentsFires when
documentLoaded—The document finishes loading.
documentUnloaded—The document is closed.
annotationsLoaded—The annotations of the document are loaded.
pageNumberUpdatedpageNumberThe current page changes.
pagesUpdatedchangesPages are added, removed, moved or rotated.
zoomUpdatedzoomThe zoom level changes.
rotationUpdatedrotationThe rotation changes.
fitModeUpdatedfitModeThe fit mode changes.
toolModeUpdatednewTool, oldToolThe active tool changes.
textSelectedquads, text, pageNumberText is selected.
searchResultsChangedresultsThe search results change.
finishedRendering—Rendering of the visible pages finishes.
clickeventThe document area is clicked.

Common annotationManager events ​

EventArgumentsFires when
annotationChangedannotations, action, infoAnnotations are added / modified / deleted (action is 'add', 'modify' or 'delete').
annotationSelectedannotations, actionAnnotations are selected ('selected') or deselected ('deselected').
annotationDeselectedannotationsAnnotations are deselected.

History managers fire historyChanged (documentViewer.getAnnotationHistoryManager()) and undoRedoStatusChanged (documentViewer.getContentEditHistoryManager()).

UI events ​

instance.UI.addEventListener(name, handler, options?) and removeEventListener(name, handler) work with the names in instance.UI.Events:

EventArgumentsFires when
VIEWER_LOADED—The UI is mounted. Listeners added later are still called once.
LOAD_ERRORerrorThe UI or a document fails to load.
VISIBILITY_CHANGEDdataElement, isVisibleAn element is opened or closed.
TOOLBAR_GROUP_CHANGEDtoolbarGroupThe active toolbar group changes.
THEME_CHANGEDthemeThe theme changes.
LANGUAGE_CHANGEDpreviousLanguage, newLanguageThe language changes.
PANEL_RESIZEDdataElement, widthA panel width changes.
FULLSCREEN_MODE_TOGGLEDisFullscreenFull screen mode changes.
FILE_DOWNLOADED—The document is downloaded.
MODULAR_UI_IMPORTED—importModularComponents finishes.
ACTIVE_DOCUMENT_VIEWER_CHANGEDnewKey, oldKeyThe active document viewer changes.
PERMISSION_DENIED{ permission, action, documentViewerKey }An action is not allowed by the document permissions.
PERMISSION_VERIFIED{ documentViewerKey }The permission password is verified.
TRUSTED_CERTIFICATE_ADDED{ fieldName, certificate }The user trusts a signer certificate in the certificate viewer. certificate is a copy of the DER bytes. Not fired by UI.VerificationOptions.addTrustedCertificates. The trust list is kept in memory only: save the certificate and pass it to addTrustedCertificates next time to trust it again.

How the UI listens ​

When an instance is created, the UI binds its handlers to the Core events of each document viewer (event-listeners/). The handlers update the stores — for example documentLoaded updates the page count and the outline, annotationSelected updates the selection, toolModeUpdated updates the active tool button. All bindings are removed when the instance is disposed.

Lifecycle note: remove the listeners you add with removeEventListener when you no longer need them, or pass { once: true } for a listener that runs once.

Examples ​

Listen for document load ​

javascript
instance.Core.documentViewer.addEventListener('documentLoaded', () => {
  console.log('Document loaded');
});

Listen for annotation changes ​

javascript
const handler = (annotations, action) => {
  console.log('annotations', action, annotations);
};
instance.Core.annotationManager.addEventListener('annotationChanged', handler);
// later
instance.Core.annotationManager.removeEventListener('annotationChanged', handler);

Listen for tool changes ​

javascript
instance.Core.documentViewer.addEventListener('toolModeUpdated', (newTool) => {
  console.log('Active tool:', newTool.name);
});

Listen for UI events ​

javascript
const { UI } = instance;
UI.addEventListener(UI.Events.THEME_CHANGED, (theme) => console.log('theme', theme));
UI.addEventListener(UI.Events.TOOLBAR_GROUP_CHANGED, (group) => console.log('group', group));

The viewer-loaded event ​

ComPDFKitViewer() resolves with instance when the UI is mounted, so code in .then() can use instance directly. Inside a config script (the config option), wait for UI.Events.VIEWER_LOADED:

javascript
instance.UI.addEventListener(instance.UI.Events.VIEWER_LOADED, () => {
  console.log('Viewer Loaded');
});