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.

Overview  
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 (3 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>\LEADTOOLS23\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:

Note: 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 = async (viewer, interactiveSelect) => { 
    // Load a PDF document 
    try { 
        const url = "https://demo.leadtools.com/images/pdf/leadtools.pdf"; 
        let doc = await lt.Document.DocumentFactory.loadFromUri(url, null); 
 
        doc.isReadOnly = false; 
        let annContainers = await doc.annotations.getAnnotations(true); 
 
        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"; 
        annContainer.children.add(rectangleObj); 
 
        await doc.annotations.setAnnotations(annContainers); 
        viewer.setDocument(doc); 
 
        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 = lt.Annotations.Engine.AnnUserMode.run; 
 
        if (doc.isStructureSupported && !doc.structure.isParse) { 
            await doc.structure.parse(); 
        } 
 
        const panZoom = lt.Document.Viewer.DocumentViewerCommands.interactivePanZoom; 
        interactiveSelect.value = panZoom; 
        viewer.commands.run(panZoom, null); 
        if (this.callback) this.callback(viewer); 
    } catch (error) { 
        console.error(error); 
    } 
}; 

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>\LEADTOOLS23\Examples\Document\JS\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.

Wrap-up

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

See Also

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

Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.