我现在有一个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
的预览中获取拖放图像actual width, height
,然后计算width, height
和预览的高度,从而在x, y
的PDF左上角生成拖放图像编辑我也尝试使用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());
```
困扰了将近一个星期,终于解决了问题,算法本身没有问题,但是第三方系统会缩放目标图像,用这种缩放计算位置是准确的。