Interacting with Viewer events

WebViewer triggers numerous events in response to user interactions or processes. They might be helpful in adding additional behavior to how users use WebViewer or performing updates to annotations as users scroll to another page.

Page events

Other than the lifecycle events mentioned in the loading guide, you may also be interested when a user changes pages. The pageNumberUpdated event can be triggered by scrolling through the document or jumping to a certain page.

Subscribe to this event via DocumentViewer.

1WebViewer({
2 initialDoc: 'https://myserver.com/myfile.pdf'
3}, document.getElementById('viewer'))
4 .then(instance => {
5 const { documentViewer, annotationManager } = instance.Core;
6
7 documentViewer.addEventListener('pageNumberUpdated', pageNumber => {
8 const annotsOnPage = annotationManager.getAnnotationList().filter(annot => annot.PageNumber === pageNumber);
9
10 // Do something with annotations e.g. Focus on certain form fields
11 });
12 });

Display events

When users are viewing the document, they may change zoom levels or how pages are viewed (in ways that don't permanently affect the document). These will also trigger events that you could listen to.

1WebViewer({
2 initialDoc: 'https://myserver.com/myfile.pdf'
3}, document.getElementById('viewer'))
4 .then(instance => {
5 const { documentViewer, Tools } = instance.Core;
6
7 documentViewer.addEventListener('zoomUpdated', zoom => {
8 if (zoom > 3) {
9 // Change tools when zoom level is above 300%
10 documentViewer.setToolMode(documentViewer.getToolMode(Tools.ToolNames.RECTANGLE));
11 }
12 });
13 });

Search events

When searches are conducted with the DocumentViewer, whether this is done through the UI or directly through an API, you can get the results by listening to these events:

1WebViewer({
2 initialDoc: 'https://myserver.com/myfile.pdf'
3}, document.getElementById('viewer'))
4 .then(instance => {
5 const { documentViewer, Tools } = instance.Core;
6
7 const prevSearchResults = []; // Rendered in a custom panel
8
9 documentViewer.addEventListener('searchResultsChanged', results => {
10 prevSearchResults.push(results);
11 });
12 });

Input events

These input events are fired when the mouse and keyboard are interacting with the document viewer.

Here is an example of an onHover for annotations using the mouseMove event.

1WebViewer({
2 initialDoc: 'https://myserver.com/myfile.pdf'
3}, document.getElementById('viewer'))
4 .then(instance => {
5 const { documentViewer, annotationManager } = instance.Core;
6
7 documentViewer.addEventListener('mouseMove', evt => {
8 const annot = annotationManager.getAnnotationByMouseEvent(evt);
9 if (annot) {
10 console.log("onHover: " + annot.Id);
11 }
12 });
13 });

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales