Setup options when customizing WebViewer

There are two ways of adding customization to WebViewer, the first way is by accessing the WebViewer instance within your app and the second is by using a config file, which is a stand-alone JavaScript file executed inside WebViewer's iframe.

Config file

You can organize your custom code in a separate JavaScript file that is executed in the context of the iframe. In this way, you have direct access to the iframe window and document, and you can use WebViewer's global methods directly. See more details about config files.

Config files are recommended when you are hosting the WebViewer lib folder on a separate server from your application. Since the config file is executed directly in the iframe there are no cross origin issues.

JavaScript

1// app.js
2// Instantiate WebViewer
3WebViewer({
4 path: 'https://myotherserver.com/webviewer/lib',
5 licenseKey: 'Insert commercial license key here after purchase'
6 initialDoc: '/files/webviewer-demo-annotated.pdf',
7 config: 'config.js' // path/to/your/config.js
8}, document.getElementById('viewer'));
1// config.js executed inside the iframe
2instance.UI.addEventListener(instance.UI.Events.VIEWER_LOADED, () => {
3 const { documentViewer, annotationManager } = instance.Core;
4
5 // Add customization here
6 documentViewer.setMargin(20);
7 documentViewer.addEventListener('fitModeUpdated', fitMode => {
8 console.log('fit mode changed');
9 });
10
11 instance.UI.disableElement('searchButton');
12 instance.UI.setTheme('dark');
13});
14
15instance.UI.addEventListener(instance.UI.Events.DOCUMENT_LOADED, () => {
16 const { documentViewer, annotationManager } = instance.Core;
17
18 // Add customization here
19 // Draw rectangle annotation on first page
20 // "Annotations" can be directly accessed since we're inside the iframe
21 const rectangle = new Annotations.RectangleAnnotation();
22 rectangle.PageNumber = 1;
23 rectangle.X = 100;
24 rectangle.Y = 100;
25 rectangle.Width = 250;
26 rectangle.Height = 250;
27 rectangle.Author = annotationManager.getCurrentUser();
28 annotationManager.addAnnotation(rectangle);
29 annotationManager.drawAnnotations(rectangle.PageNumber);
30})

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales