我正尝试使用pdf.js生成pdf,现在我想在单击特定画布时获得画布“ id”。
这是我的小提琴演示 https://jsfiddle.net/14953eak/
代码:
<div class="container">
<div class="col-xl-12 col-lg-12 col-md-12 col-sm-12">
<div id='pdf-viewer'></div>
</div>
</div>
脚本:
url = 'https://raw.githubusercontent.com/mozilla/pdf.js/ba2edeae/web/compressed.tracemonkey-pldi-09.pdf';
var thePdf = null;
var scale = 1;
var canvas;
var inst = this;
var ctx;
pdfjsLib.GlobalWorkerOptions.workerSrc = 'https://mozilla.github.io/pdf.js/build/pdf.worker.js';
pdfjsLib.getDocument(url).promise.then(function (pdf) {
thePdf = pdf;
viewer = document.getElementById('pdf-viewer');
for (page = 1; page <= pdf.numPages; page++) {
canvas = document.createElement("canvas");
canvas.id = "Page-" + page;
canvas.className = 'pdf-page-canvas';
viewer.appendChild(canvas);
renderPage(page, canvas);
}
});
function renderPage(pageNumber, canvas) {
thePdf.getPage(pageNumber).then(function (page) {
viewport = page.getViewport(scale);
canvas.height = viewport.height;
canvas.width = viewport.width;
newcanvas = document.getElementById("Page-" + pageNumber);
ctx = canvas.getContext('2d');
var renderContext = {
canvasContext: ctx,
viewport: viewport
};
var renderTask = page.render(renderContext);
renderTask.promise.then(function () {
console.log('Page rendered');
});
});
}
答案 0 :(得分:1)
要实现此目的,您需要在创建click
元素的for
循环内添加canvas
事件处理程序。然后,您可以在事件处理函数中使用id
来检索元素的this.id
。试试这个:
canvas.addEventListener('click', function() {
console.log(this.id);
// work with the id here...
});