Re: Accessing the webviewer after it has loaded the first time

217 views
Skip to first unread message

Matt Parizeau

unread,
May 30, 2013, 6:38:24 PM5/30/13
to pdfnet-w...@googlegroups.com
The id of the DOM element associated with the WebViewer is viewer which you have access to from your viewerElement variable or $("#viewer"), however that isn't the actual WebViewer object that you want to call loadDocument on.  In your click function you set the myWebViewer variable to be document.getElementById('myWebViewer') however there isn't actually anything with the id myWebViewer so this will always return null.

The object that you actually want to call loadDocument on is the one that is returned from calling the constructor at the beginning of your code (var myWebViewer = new PDFTron.WebViewer({...).  Now because of closures in JavaScript you actually have access to this variable inside of the click function.  So to get this to work I believe you should be able to just remove the line var myWebViewer = document.getElementById('myWebViewer'); in your click function as that is changing the value of myWebViewer to null and then when myWebViewer.loadDocument is called it will use the variable defined above.

Matt Parizeau
Software Developer
PDFTron Systems Inc.

On Thursday, May 30, 2013 3:15:31 PM UTC-7, Marcus Lyon wrote:
I am in the process of testing out the WebViewer. The scenario is simple, a listbox with a list of file names in it. When the page opens, the first file loads succesfully in the webviewer component. My goal is to be able to select a different file in the list, then call the webviewer loaddocument method. However, have not been able to figure out how to reference the webviewer once it has loaded. What am I missing?

Variables object 1,2,3 do not have a loadDocument method. The variable myWebViewer is always null. How do a load a new document? NOTE: I will be passing in the file name as a param in the ConvertAndStream query string.

        $(document).ready(function ()
        {

            var viewerElement = document.getElementById('viewer');
            var myWebViewer = new PDFTron.WebViewer({
                path: "WebViewer",
                type: "Silverlight,html5",
                initialDoc: "ConvertAndStream.aspx",
                streaming: false
            }, viewerElement);

            $("#lbFileList").click(function () {
                var object1 = $("#viewer");
                var object2 = $("#viewerElement");
                var object3 = $("#myWebViewer");
                var myWebViewer = document.getElementById('myWebViewer');

                myWebViewer.loadDocument("ConvertAndStream.aspx?filename=");
            });

        });
Reply all
Reply to author
Forward
0 new messages