浏览器预览的坐标如何映射到PDF文件中的坐标

问题描述 投票:0回答:1

我现在有一个PDF文件,在PDFBox中呈现为每页一个图像

// load pdf and save image
try (PDDocument document = PDDocument.load("some file")) {
  PDFRenderer render = new PDFRenderer(document);
  BufferedImage scaledImage = render.renderImageWithDPI(pageIndex, 326);
  // save image
}

此步骤中保存的图像将在浏览器中预览。用户可以将图像拖放到此预览中,然后将此坐标映射到真实PDF,但始终存在一些错误。这是我映射的方式:

  1. 在浏览器的width, height中获取预览,在上部left corner of the x, y的预览中获取拖放图像
  2. 后端获取PDF的actual width, height,然后计算width, height和预览的高度,从而在x, y的PDF左上角生成拖放图像
  3. 由于PDF中坐标的原点是文档的左下角,因此x和y的最终公式为: x:float targetX =(previewX 1.0F / previewWidth)pdfPageWidth; y:float targetY = pdfPageHeight - (previewY 1.0F / previewHeight)pdfPageHeight - dragImageHeight
  4. 根据之前计算的x,y在这个页面上用PDF绘制这个数字,但是有错误,且错误很明显,我该怎么办?

Reference document

iText

编辑我也尝试使用iText:````Rectangle cropBox = reader.getCropBox(firstPageIndex);

float widthRatio = renderRandomX * 1.0F / renderWidth;
float heightRatio = renderRandomY * 1.0F / renderHeight;

float offsetLLX = cropBox.getWidth() * widthRatio;
float offsetLLY = cropBox.getHeight() - cropBox.getHeight() * heightRatio;

Rectangle drawSignRect = new Rectangle(cropBox.getLeft() + cropBox.getWidth() * widthRatio,
    cropBox.getBottom() + offsetLLY,
    cropBox.getLeft() + offsetLLX + signImage.getWidth(),
    cropBox.getBottom() + offsetLLY + signImage.getHeight());

```

java itext coordinates digital-signature pdfbox
1个回答
0
投票

困扰了将近一个星期,终于解决了问题,算法本身没有问题,但是第三方系统会缩放目标图像,用这种缩放计算位置是准确的。

© www.soinside.com 2019 - 2024. All rights reserved.