我正在使用Javascript作为前端制作Web应用程序,这是它的工作方式:
我启动该应用程序,它通过我的浏览器打开一个网页。
它显示从我的目录获得的PDF页面。
我可以选择单击图章,然后在pdf上拖动和移动并放置在我想要的任何地方。
完成后,我可以单击“保存”,它会自动将pdf文件保存在我的目录中。
我可以在文件夹中打开pdf文件,以查看更新的PDF以及添加的图章。
问题是当我打开PDF文件进行查看时,其位置与Web浏览器中图章的位置不同。
以下是输入示例(在网络浏览器中):
这是Adobe Reader中的输出示例(这不是我想要的):
我知道这主要是因为与图像中的像素相比,PDF是使用用户单位测量的。我必须更改矩形中的某些内容,使其与网页的位置匹配。 找到正确转换的最佳方法是什么,以使位置与我保存图章的位置完全一样?
Javascript:
function divMove(e){
var div = document.getElementById('stamp');
div.style.position = 'absolute';
//div.style.top = e.clientY + 'px';
//div.style.left = e.clientX + 'px';
var box = div.getBoundingClientRect();
mouse_top = e.clientY;
mouse_left = e.clientX;
var diff_x = mouse_left - box.left;
var diff_y = mouse_top - box.top;
div.style.top = ((Number(div.style.top.replace("px", "")) - 1) + diff_y) +"px";
div.style.left = ((Number(div.style.left.replace("px", "")) - 1) + diff_x) +"px";
document.getElementById("data").innerHTML =
"mouse_top:" + mouse_top + "<br>mouse_left:" + mouse_left
}
function save(){
$.ajax({
type: "GET",
url: "Save",
data: {
mouseTop : mouse_top,
mouseLeft : mouse_left
},
dataType: "text",
success: function (response) {
//use response to set stamp
if (response != null) {
var pdfAsArray = convertDataURIToBinary("data:application/pdf;base64, " + response);
loadPDFToViewer(pdfAsArray);
//document.getElementById("page-1").getElementsByClassName("pdfCanvas")[0].removeEventListener("click", addStamp);
} else {
alert("Response is null");
}
}
});
}
Java(后端):
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
//String src = "C:\\Apache Tomcat\\apache-tomcat-7.0.78\\myWork\\prototype\\haito.pdf";
//String dest = "C:\\Apache Tomcat\\apache-tomcat-7.0.78\\myWork\\prototype\\test.pdf";
Properties p = new Properties();
p.load(new FileInputStream("config.properties"));
String src = p.getProperty("src");
String dest = p.getProperty("dest");
String imgSrc = p.getProperty("stamp");
PdfDocument doc = new PdfDocument(new PdfReader(src), new PdfWriter(dest));
ImageData image = ImageDataFactory.create(imgSrc);
float w = image.getWidth();
float h = image.getHeight();
System.out.println("w: " + w + ", h: " + h);
float mouseX = Float.valueOf(request.getParameter("mouseTop"));
float mouseY = Float.valueOf(request.getParameter("mouseLeft"));
System.out.println("top: " + mouseX + ", left: " + mouseY);
// adjusted mouse X and mouse Y position
Rectangle rect = new Rectangle(mouseX ,mouseY,w,h);
PdfStampAnnotation stamp = new PdfStampAnnotation(rect).setStampName(new PdfName("Approved"));
PdfFormXObject xObj = new PdfFormXObject(new Rectangle(w,h));
PdfCanvas canvas = new PdfCanvas(xObj,doc);
canvas.addImage(image,0,0,false);
//canvas.getGraphicsState();
stamp.setNormalAppearance(xObj.getPdfObject());
stamp.setFlags(PdfAnnotation.PRINT);
stamp.setFlags(PdfAnnotation.LOCKED);
for(int i=1;i<=doc.getNumberOfPages();i++)
{
doc.getPage(i).addAnnotation(stamp);
}
//doc.getFirstPage().addAnnotation(stamp);
FileOutputStream out = new FileOutputStream("config.properties");
p.setProperty("src", dest);
p.setProperty("dest", src);
p.store(out, null);
out.close();
doc.close();
....................
}