2

Is there a possible way to do a PDF page count in javascript (with titanium)? I'm working on an application, and i need the amount of pages to know on which page a user is. Right now i'm doing the amount of pages hard coded, but I want to retrieve the pages via javascript.

  var pageheight;
        var numPages = 180;
        wv.addEventListener('load', function(e){

            var documentHeight = wv.evalJS('window.outerHeight');
            var innerHeight = wv.evalJS('window.innerHeight');
            var ratio = innerHeight / wv.height;
            pageHeight = parseInt(ratio * (documentHeight / numPages), 10);
            Ti.API.info(title);
            wv.evalJS("var currentPage = 1;");
            wv.evalJS("window.onscroll = function() { currentPage = parseInt(window.pageYOffset/" + pageHeight + ") + 1;}");
            wv.evalJS("window.onclick = function() { currentPage = parseInt(window.pageYOffset/" + pageHeight + ") + 1;}");
            setInterval(getCurrentPage, 100);
        });


        function jumpToPage(page){
            wv.evalJS('window.scrollTo(0, ' + pageHeight * (page - 1) + ');');
        }

        function getCurrentPage(){
            var currentPage = parseInt(wv.evalJS("currentPage"), 10);
            Ti.API.info('currentpage: ' + currentPage);
            Ti.API.info(title)
        }

I hope someone can help me, i really needs this to work.

thanks in advance

klaasdm
  • 79
  • 1
  • 10

1 Answers1

1

You could set the pageHeight manually, since this is most likely static. Then you would just divide the document height with the page height.

wv.evalJS( "Math.ceil( document.body.scrollHeight / " + pageHeight + " )" );

If not, then you could get page count server side and then just get it with XHR from the app.

Community
  • 1
  • 1
Sindre Sorhus
  • 62,972
  • 39
  • 168
  • 232
  • the problem is, I'm using numPages to get the pageHeight. So i need to get the page numbers before i can use pageHeight. Or is it possible to get the page height via javascript – klaasdm Apr 20 '11 at 10:27