How do the coordinates of the browser preview map map to the coordinates in the PDF file

  java, question

I have one now.PdfDocuments, adoptedPDFBoxRendered as one picture per page

// 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

The saved picture in this step will be previewed in the browser. Users can drag and drop the picture into this preview picture, and then I will map this coordinate to the real one.PdfHowever, there are always some errors. The following is my mapping method:

  1. Gets the preview map in the browser’swidth,heightGets the in the upper-left corner of the preview of the drag-and-drop picture.x,y
  2. Back-end acquisitionPdfActualwidth,height, and then calculate and preview theWide ratioIn the endDrag and drop picturesIn PDFThe upper left cornerThex,y
  3. Because the origin of coordinates in the PDF is the lower left corner of the document, the finalx,yThe calculation formula is:

    • x: float targetX = (previewX1.0F / previewWidth)pdfPageWidth;
    • y: float targetY = pdfPageHeight – (previewY1.0F / previewHeight)pdfPageHeight – dragImageHeight
  4. According to the calculation in the previous stepx,yThis figure is drawn in the PDF on this page, but there are errors, and the errors are obvious.What should I do?

Reference document

  1. PDF coordinate system
  2. PDF development and electronic signature

The problem has puzzled week. The problem has finally been solved. The algorithm itself has no problem. Only the third-party system will zoom the target image. The calculation will be accurate when the zoom ratio is added.