Html 以PDF格式绘制框并捕获坐标
我想用画布在pdf中画一个方框,并找出方框的坐标。我这里有一个代码,在选择pdf之后,它将转换为画布,在画布上我们创建了一个方框,但方框没有显示。 为此,请使用javascript pdf.js和pdf.worker.js 要使用此代码,请使用附加的javascript文件Html 以PDF格式绘制框并捕获坐标,html,Html,我想用画布在pdf中画一个方框,并找出方框的坐标。我这里有一个代码,在选择pdf之后,它将转换为画布,在画布上我们创建了一个方框,但方框没有显示。 为此,请使用javascript pdf.js和pdf.worker.js 要使用此代码,请使用附加的javascript文件 我也有同样的要求 检查此链接。我在这里为你做了演示 检查页面源代码中的JavaScript代码 如果您需要zip中的所有文件,请告诉我。 这是 下面是绘制矩形的代码。Rest是PDF.js,它已经在那里了 "use str
我也有同样的要求 检查此链接。我在这里为你做了演示 检查页面源代码中的JavaScript代码 如果您需要zip中的所有文件,请告诉我。 这是 下面是绘制矩形的代码。Rest是PDF.js,它已经在那里了
"use strict";
requestAnimationFrame(mainLoop);
const canvas = document.getElementById("pdf-canvas");
const ctx = canvas.getContext("2d");
const storedRects = [];
const baseImage = loadImage("");
var refresh = true;
const rect = (() => {
var x1, y1, x2, y2;
var show = false;
function fix() {
rect.x = Math.min(x1, x2);
rect.y = Math.min(y1, y2);
rect.w = Math.max(x1, x2) - Math.min(x1, x2);
rect.h = Math.max(y1, y2) - Math.min(y1, y2);
jQuery("body").click(function(e) {
console.log(rect.x+'='+rect.y+'='+rect.w+'='+rect.h);
$('#output').html('current x : '+rect.x+', current y : '+rect.y+', width : '+rect.w+', height : '+rect.h);
if (e.target.id == "pdf-canvas") {
ddrag();
jQuery(".position_x").val(rect.x);
jQuery(".position_y").val(rect.y);
jQuery(".position_w").val(rect.w);
jQuery(".position_h").val(rect.h);
}
});
}
function draw(ctx) {
ctx.clearRect(this.x, this.y, this.w, this.h);
ctx.strokeRect(this.x, this.y, this.w, this.h)
}
const rect = {x : 0, y : 0, w : 0, h : 0, draw};
const API = {
restart(point) {
x2 = x1 = point.x;
y2 = y1 = point.y;
fix();
show = true;
},
update(point) {
x2 = point.x;
y2 = point.y;
fix();
show = true;
},
toRect() {
show = false;
return Object.assign({}, rect);
},
draw(ctx) {
if (show) { rect.draw(ctx) }
},
show : false,
}
return API;
})();
function loadImage(url) {
const image = new Image();
image.src = url;
image.onload = () => refresh = true;
return image;
}
const mouse = {
button : false,
x : 0,
y : 0,
down : false,
up : false,
element : null,
event(e) {
const m = mouse;
m.bounds = m.element.getBoundingClientRect();
m.x = e.pageX - m.bounds.left - scrollX;
m.y = e.pageY - m.bounds.top - scrollY;
const prevButton = m.button;
m.button = e.type === "mousedown" ? true : e.type === "mouseup" ? false : mouse.button;
if (!prevButton && m.button) { m.down = true;
/*
var cv=document.createElement('canvas');
cv.width=canvas.width;
cv.height=canvas.height;
*/
}
if (prevButton && !m.button) { m.up = true }
},
start(element) {
mouse.element = element;
"down,up,move".split(",").forEach(name => document.addEventListener("mouse" + name, mouse.event));
}
}
mouse.start(canvas);
function draw() {
const storedRects = [];
ctx.restore();
ctx.drawImage(baseImage, 0, 0, ctx.canvas.width, ctx.canvas.width);
ctx.lineWidth = 1;
ctx.strokeStyle = "yellow";
storedRects.forEach(rect => rect.draw(ctx));
ctx.strokeStyle = "red";
rect.draw(ctx);
}
function mainLoop() {
if (refresh || mouse.down || mouse.up || mouse.button) {
refresh = false;
if (mouse.down) {
mouse.down = false;
rect.restart(mouse);
} else if (mouse.button) {
rect.update(mouse);
} else if (mouse.up) {
mouse.up = false;
rect.update(mouse);
//storedRects.push(rect.toRect());
}
draw();
}
requestAnimationFrame(mainLoop)
}
function clearCanvas (canvas)
{
const storedRects = [];
//alert(jQuery(".position_x").val());
jQuery('#pdf-contents').removeClass('test');
// ctx.fillRect(jQuery(".position_x").val(), jQuery(".position_y").val(), jQuery(".position_w").val(),jQuery(".position_h").val());
//ctx.clearRect(0,0,jQuery(".position_w").val(),jQuery(".position_h").val());
//ctx.drawImage(baseImage, 0, 0, ctx.canvas.width, ctx.canvas.width);
// URL.createObjectURL($("#file-input").get(0).files[0]); /*reload mate*/
//alert(URL.createObjectURL($("#file-input").get(0).files[0]))
showPDF(URL.createObjectURL($("#file-input").get(0).files[0]));
//setTimeout(function(){ showPDF(URL.createObjectURL($("#file-input").get(0).files[0])); }, 3000);
}
function ddrag(){
//const storedRects = [];
jQuery('.test #pdf-canvas').mousedown(function(){
console.log(jQuery(this).parent().parent().attr('class'));
if(jQuery(this).parent().parent().attr('class')=="test"){
return false;
}else{
return true;
}
});
jQuery('#pdf-contents').addClass('test');
//return false;
}
我也有同样的要求 检查此链接。我在这里为你做了演示 检查页面源代码中的JavaScript代码 如果您需要zip中的所有文件,请告诉我。 这是 下面是绘制矩形的代码。Rest是PDF.js,它已经在那里了
"use strict";
requestAnimationFrame(mainLoop);
const canvas = document.getElementById("pdf-canvas");
const ctx = canvas.getContext("2d");
const storedRects = [];
const baseImage = loadImage("");
var refresh = true;
const rect = (() => {
var x1, y1, x2, y2;
var show = false;
function fix() {
rect.x = Math.min(x1, x2);
rect.y = Math.min(y1, y2);
rect.w = Math.max(x1, x2) - Math.min(x1, x2);
rect.h = Math.max(y1, y2) - Math.min(y1, y2);
jQuery("body").click(function(e) {
console.log(rect.x+'='+rect.y+'='+rect.w+'='+rect.h);
$('#output').html('current x : '+rect.x+', current y : '+rect.y+', width : '+rect.w+', height : '+rect.h);
if (e.target.id == "pdf-canvas") {
ddrag();
jQuery(".position_x").val(rect.x);
jQuery(".position_y").val(rect.y);
jQuery(".position_w").val(rect.w);
jQuery(".position_h").val(rect.h);
}
});
}
function draw(ctx) {
ctx.clearRect(this.x, this.y, this.w, this.h);
ctx.strokeRect(this.x, this.y, this.w, this.h)
}
const rect = {x : 0, y : 0, w : 0, h : 0, draw};
const API = {
restart(point) {
x2 = x1 = point.x;
y2 = y1 = point.y;
fix();
show = true;
},
update(point) {
x2 = point.x;
y2 = point.y;
fix();
show = true;
},
toRect() {
show = false;
return Object.assign({}, rect);
},
draw(ctx) {
if (show) { rect.draw(ctx) }
},
show : false,
}
return API;
})();
function loadImage(url) {
const image = new Image();
image.src = url;
image.onload = () => refresh = true;
return image;
}
const mouse = {
button : false,
x : 0,
y : 0,
down : false,
up : false,
element : null,
event(e) {
const m = mouse;
m.bounds = m.element.getBoundingClientRect();
m.x = e.pageX - m.bounds.left - scrollX;
m.y = e.pageY - m.bounds.top - scrollY;
const prevButton = m.button;
m.button = e.type === "mousedown" ? true : e.type === "mouseup" ? false : mouse.button;
if (!prevButton && m.button) { m.down = true;
/*
var cv=document.createElement('canvas');
cv.width=canvas.width;
cv.height=canvas.height;
*/
}
if (prevButton && !m.button) { m.up = true }
},
start(element) {
mouse.element = element;
"down,up,move".split(",").forEach(name => document.addEventListener("mouse" + name, mouse.event));
}
}
mouse.start(canvas);
function draw() {
const storedRects = [];
ctx.restore();
ctx.drawImage(baseImage, 0, 0, ctx.canvas.width, ctx.canvas.width);
ctx.lineWidth = 1;
ctx.strokeStyle = "yellow";
storedRects.forEach(rect => rect.draw(ctx));
ctx.strokeStyle = "red";
rect.draw(ctx);
}
function mainLoop() {
if (refresh || mouse.down || mouse.up || mouse.button) {
refresh = false;
if (mouse.down) {
mouse.down = false;
rect.restart(mouse);
} else if (mouse.button) {
rect.update(mouse);
} else if (mouse.up) {
mouse.up = false;
rect.update(mouse);
//storedRects.push(rect.toRect());
}
draw();
}
requestAnimationFrame(mainLoop)
}
function clearCanvas (canvas)
{
const storedRects = [];
//alert(jQuery(".position_x").val());
jQuery('#pdf-contents').removeClass('test');
// ctx.fillRect(jQuery(".position_x").val(), jQuery(".position_y").val(), jQuery(".position_w").val(),jQuery(".position_h").val());
//ctx.clearRect(0,0,jQuery(".position_w").val(),jQuery(".position_h").val());
//ctx.drawImage(baseImage, 0, 0, ctx.canvas.width, ctx.canvas.width);
// URL.createObjectURL($("#file-input").get(0).files[0]); /*reload mate*/
//alert(URL.createObjectURL($("#file-input").get(0).files[0]))
showPDF(URL.createObjectURL($("#file-input").get(0).files[0]));
//setTimeout(function(){ showPDF(URL.createObjectURL($("#file-input").get(0).files[0])); }, 3000);
}
function ddrag(){
//const storedRects = [];
jQuery('.test #pdf-canvas').mousedown(function(){
console.log(jQuery(this).parent().parent().attr('class'));
if(jQuery(this).parent().parent().attr('class')=="test"){
return false;
}else{
return true;
}
});
jQuery('#pdf-contents').addClass('test');
//return false;
}
您好@Viral,您答案中的链接不再有效。你能帮我提供你的解决方案的示例文件吗?@你能提供新的链接吗?在处理相同的要求时,您的答案中的链接不再有效。你能帮我提供你的解决方案的示例文件吗?@你能提供新的链接吗?处理相同的要求