Integrate WebViewer into Tomcat JavaServer Pages (JSP)

Using an Apache Tomcat 9.0 Servlet/JSP container, WebViewer is defined as a web application to be hosted and displayed when navigated to. WebViewer is able to view, annotate, and manipulate PDFs and other documents.

The sample also demonstrates how to programmatically add a rectangle annotation with user-defined dimensions and includes a number of example files that can be loaded (PDF, XOD, and an annotation XFDF sample).

WebViewer provides a slick out-of-the-box responsive UI that enables you to view, annotate and manipulate PDFs and other document types inside any web project.

Click the button below to view the full project in GitHub.

1WebViewer({
2 path: 'lib',
3 initialDoc: 'files/webviewer-demo-annotated.pdf'
4 }, document.getElementById('viewer'))
5 .then(function(instance) {
6
7 // you can access documentViewer object for low-level APIs
8 const { documentViewer } = instance.Core;
9
10 // now you can access APIs through this.webviewer.getInstance()
11 instance.UI.openElements(['notesPanel']);
12 // see https://www.pdftron.com/documentation/web/guides/ui/apis for the full list of APIs
13
14 // or listen to events from the viewer element
15 document.getElementById('viewer').addEventListener('pageChanged', (e) => {
16 const [ pageNumber ] = e.detail;
17 console.log(`Current page is ${pageNumber}`);
18 });
19
20 // or from the documentViewer instance
21 documentViewer.addEventListener('annotationsLoaded', () => {
22 console.log('annotations loaded');
23 });
24
25 documentViewer.addEventListener('documentLoaded', function() {
26 // and access classes defined in the WebViewer iframe
27 const { Annotations, annotationManager } = instance.Core;
28 const rectangle = new Annotations.RectangleAnnotation();
29 rectangle.PageNumber = 1;
30 rectangle.X = 100;
31 rectangle.Y = 100;
32 rectangle.Width = 250;
33 rectangle.Height = 250;
34 rectangle.StrokeThickness = 5;
35 rectangle.Author = annotationManager.getCurrentUser();
36 annotationManager.addAnnotation(rectangle);
37 annotationManager.drawAnnotations(rectangle.PageNumber);
38 // see https://www.pdftron.com/api/web/WebViewer.html for the full list of low-level APIs
39 });
40
41 });
42

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales