首页 文章

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

提问于
浏览
0

我现在有一个 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,但始终存在一些错误 . 这是我映射的方式:

  • 在浏览器的 width, height 中获取预览,在上面的预览中拖放图像 left corner of the x, y

  • 后端获取PDF的 actual width, height ,然后计算 width, height 和预览的高度,从而在 x, y 中的PDF左上角生成拖放图像

  • 因为PDF中的坐标原点是文档的左下角,所以x和y的最终公式为:

  • x:float targetX =(previewX 1.0F / previewWidth)pdfPageWidth;

  • y:float targetY = pdfPageHeight - (previewY 1.0F / previewHeight)pdfPageHeight - dragImageHeight

  • 根据之前的x计算,在这个页面中用PDF绘制这个图,但是有错误,且错误很明显, how can I do?

参考文件

iText

Edit 我也尝试使用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());

1 回答

  • 0

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

相关问题