简体   繁体   中英

Get number of pages in PDF via ViewerJS

Using ViewerJS is it possible to get the number of pages in the PDF document being displayed?

I can see from the pdf.js file that comes with ViewerJS that there is data.numPages but I simply cannot figure out how access it.

One solution (which I don't like, but works) is to get the <span id="numPages" class="toolbarLabel">of 12</span> and parse the inner text...

 var text = document.getElementById("numPages").innerText; var numPages = parseInt(text.match(/^of (\\d+)/i)[1], 10); console.log(numPages); 
 <span id="numPages" class="toolbarLabel">of 12</span> 

Can anybody point me to how to get the value from the ViewerJS data, rather than the horrible solution above?

Please also be aware that the ViewerJS is being displayed within an <iframe> , but that should't make any difference to the answer as it's same-domain, etc.

看来您可以计算.page元素来获取总页数,如下所示:

iframe.contentWindow.document.querySelectorAll('.page').length

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM