使用 ViewerJS是否可以获取正在显示的PDF文档中的页数?
我可以从 pdf.js
中看到ViewerJS 附带的文件中有 data.numPages
但我根本不知道如何访问它。
一个解决方案(我不喜欢,但有效)是获取 <span id="numPages" class="toolbarLabel">of 12</span>
并解析内部文本...
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>
谁能告诉我如何从 ViewerJS 数据中获取值(value),而不是上面可怕的解决方案?
另请注意,ViewerJS 显示在 <iframe>
中,但这对答案没有任何影响,因为它是同域等。
最佳答案
您似乎可以通过计算 .page
元素来获取总页数,如下所示:
iframe.contentWindow.document.querySelectorAll('.page').length
关于javascript - 通过 ViewerJS 获取 PDF 中的页数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54256742/