Get Page numbers which contains text

19 views
Skip to first unread message

Devanshi Bhagat

unread,
Oct 13, 2016, 7:54:03 PM10/13/16
to PDFTron WebViewer

Hi,

Once document is loaded in PDFtron webviewer, I want to check in uploaded document which pages contains text.

I used below code on documentLoaded event. 

var doc = readerControl.docViewer.getDocument();
var pageContainsNoText = new Array();
var counter = 1;
for(var i = 0; i < doc.getPageCount(); i++){
doc.loadPageText(i, function(text) {
        if(text == null || text == "")){
        pageContainsNoText .push(counter);
        }
                counter++;
       });
}

But it does not return correct page numbers.

When I write the same code on any button click event once the whole document is loaded, then it works properly.
So is there any event which gets called even after documentLoaded event?
Or any other way around to find the page numbers?

Thanks,
Devanshi Bhagat

Kristian Hein

unread,
Oct 14, 2016, 4:00:05 PM10/14/16
to PDFTron WebViewer
Hello,

The problem you might be experiencing is that you are checking the pageContainsNoText variable to early. 
doc.lodPageText function is asynchronous and you need to wait until all the callbacks for that loadPageText have been called before pageContainsNoText is set properly.

I was able to get the correct value from pageContainsNoText by firing an event when all callbacks have been called and checking it then. Here is the code:

$(document).on('documentLoaded', function() {

 
var doc = readerControl.docViewer.getDocument();
 
var pageContainsNoText = new Array();
 
var counter = 1;
 
for(var i = 0; i < doc.getPageCount(); i++){
    doc
.loadPageText(i, function(text) {

     
if(text == null || text === ""){
        pageContainsNoText
.push(counter);
     
}
     
if (counter === doc.getPageCount())  {
        $
(window).trigger('done');
     
}
      counter
++;
   
});
 
}
  $
(window).on('done', function() {
    console
.log(pageContainsNoText);
 
});
});

-Kristian
Reply all
Reply to author
Forward
0 new messages