Subscribe to the Run Event in the Document Viewer Annotations - HTML5 JavaScript

This tutorial shows how to subscribe to the Annotations Run event inside the Document Viewer in an HTML5 JS application using the LEADTOOLS SDK.

Summary This tutorial covers how to subscribe to the Annotations Run event inside the HTML5/JS DocumentViewer.
Completion Time 15 minutes
Visual Studio Project Download tutorial project (4 KB)
Platform JS Web Application
IDE Visual Studio Code
Development License Download LEADTOOLS

Required Knowledge

Get familiar with the basic steps of creating a project and working with the HTML5/JS Document Viewer by reviewing the Add References and Set a License - HTML5 JavaScript and Display Files in the Document Viewer tutorials, before working on the Subscribe to the Run Event in the Document Viewer Annotations - HTML5 JavaScript tutorial.

This tutorial makes use of http-server, a console command for running a static file server. To install http-server first install Node.js and then install http-server.

Create the Project and Add LEADTOOLS References

Start with a copy of the project created in the Add Annotations Programmatically to the Document Viewer tutorial. If that project is unavailable, follow the steps in that tutorial to create it.

The references needed depend upon the purpose of the project. References can be added by .js files located at <INSTALL_DIR>\LEADTOOLS21\Bin\JS.

For this project, the following references are needed:

Make sure to copy these files to the project's lib folder.

For a complete list of which JS files are required for your application, refer to Files to be Included with your Application

Set the License File

The License unlocks the features needed for the project. It must be set before any toolkit function is called. For details including tutorials for different platforms, refer to Setting a Runtime License.

There are two types of runtime licenses:


Adding LEADTOOLS references and setting a license are covered in more detail in the Add References and Set a License - HTML5 JavaScript tutorial.

Subscribe to the Annotations Run Event

With the project created, dependencies added, the license set, and the Add Annotations Programmatically to the Document Viewer tutorial code added, coding can begin.

Open the app.js file located in the project folder. Inside the loadDefaultDoc function, modify the code as shown below to subscribe to the annotation's run event in the document viewer object.

loadDefaultDoc = (documentViewer, interactiveSelect) => { 
   // Load a PDF document 
   const url = ""; 
   lt.Document.DocumentFactory.loadFromUri(url, null) 
   .done((doc) => { 
         const ready = () => { 
            const panZoom = lt.Document.Viewer.DocumentViewerCommands.interactivePanZoom; 
            interactiveSelect.value = panZoom; 
  , null) 
         doc.isReadOnly = false; 
            .done(function (annContainers){ 
               let annContainer = annContainers[0]; 
               let rectangleObj = new lt.Annotations.Engine.AnnRectangleObject(); 
               rectangleObj.rect = lt.LeadRectD.create(100, 100, 600, 600); 
               rectangleObj.fill = lt.Annotations.Engine.AnnSolidColorBrush.create("transparent"); 
               rectangleObj.tag = "specialtag"; 
               doc.annotations.setAnnotations(annContainers).done(function (){ 
         viewer.operation.add((_sender, e) => { 
            if (e.operation == lt.Document.Viewer.DocumentViewerOperation.createAutomation && e.isPostOperation) { 
               viewer.annotations.automation.add_run((_sender, e) => { 
                     if (e.get_operationStatus() === lt.Annotations.Engine.AnnDesignerOperationStatus.end) 
                     // Add code here to do something when the object you wanted is clicked 
                        alert("Hello World"); 
         viewer.annotations.automationManager.userMode =; 
         if (doc.isStructureSupported && !doc.structure.isParse) 

Run the Project

Before running the front end HTML5/JS Document Viewer, run the Document Service. The LEADTOOLS SDK installation provides three examples of the Document Service for the following platforms:

For instructions on how to set up and configure the Document Service, in the three platforms previously listed, refer to the steps in the Get Started with the Document Viewer Demo - HTML5 JavaScript tutorial.

For the purposes of this tutorial, the .NET Framework Document Service is used and it can be found here: <INSTALL_DIR>\LEADTOOLS21\Examples\JS\Services\DocumentServiceDotNet\fx.

Once you have the back-end Document Service running, open the Command Line and cd into the project folder. Use the following command to run a static file server: http-server

The server should start and run on http://localhost:8080. A message should appear in the console indicating all the ports that the server is available on.

Screenshot of Http Server running.

Select the programmatically added AnnRectangleObject object in the Document Viewer to trigger the Run event.


This tutorial showed how to subscribe to the HTML5/JS Document Viewer's Annotation Run event.

See Also

Help Version 21.0.2021.6.17
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2021 LEAD Technologies, Inc. All Rights Reserved.