简体   繁体   中英

Getting X and Y coordinates from pdf document

I am trying to get the x and y coordinates from a pdf document on click event. Pdf documents do not have DOM that's why I am overlaying a div on the top of the pdf document to get the coordinates from the div. I am using itextsharp library and trying to stamp a value you the pdf document. Unfortunately the x and y coordinates of the div are in pixels and I need to relate them to the values in the stamper. The X coordinates that I am getting from the div click event are: 80, 300, 600, 880 and the X coordinates that I need to enter in the stamper are 10, 205,405,600. What ratio do I need to apply to convert the div coordinates to the stamper coordinates.

 private static void InsertTextToPdf(string sourceFileName, string newFileName)
 {
     using (Stream pdfStream = new FileStream(sourceFileName, FileMode.Open))
     {
         using (Stream newpdfStream = new FileStream(newFileName, FileMode.Create, FileAccess.ReadWrite))
         {
                PdfReader pdfReader = new PdfReader(pdfStream);
                iTextSharp.text.Rectangle pageSize = pdfReader.GetPageSize(1);
                Console.Write(pageSize);

                PdfStamper pdfStamper = new PdfStamper(pdfReader, newpdfStream);
                PdfContentByte pdfContentByte = pdfStamper.GetOverContent(1);
                BaseFont baseFont = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1250, BaseFont.NOT_EMBEDDED);
                pdfContentByte.SetColorFill(BaseColor.RED);
                pdfContentByte.SetFontAndSize(baseFont, 12);
                pdfContentByte.BeginText();
                pdfContentByte.ShowTextAligned(PdfContentByte.ALIGN_CENTER, "Test", 10, 190, 0);
                pdfContentByte.ShowTextAligned(PdfContentByte.ALIGN_CENTER, "Test", 205, 190, 0);
                pdfContentByte.ShowTextAligned(PdfContentByte.ALIGN_CENTER, "Test", 405, 190, 0);
                pdfContentByte.ShowTextAligned(PdfContentByte.ALIGN_CENTER, "Test", 600, 190, 0);
                pdfContentByte.EndText();
                pdfStamper.Close();
         }
     }
 }

Trying to get the userUnit - in my case they are null

                PdfReader pdfReader = new PdfReader(pdfStream);
                iTextSharp.text.Rectangle pageSize = pdfReader.GetPageSize(1);
                Console.Write(pageSize);
                iTextSharp.text.pdf.PdfDictionary pageDict = pdfReader.GetPageN(1);
                iTextSharp.text.pdf.PdfNumber userUnit = pageDict.GetAsNumber(iTextSharp.text.pdf.PdfName.USERUNIT);
                Console.Write("\nPageDict " + pageDict);
                Console.Write("\nUser Unit " + userUnit);

在此处输入图片说明

You are making different assumptions that are wrong.

There is an FAQ on the official iText web site, where you can find questions that were answered before on Stack Overflow.

Measurement unit

The first FAQ entry you have to read is: How to get the UserUnit from a PDF file?

I quote:

FAQ What is the measurement unit in PDF documents? Most of the measurements in PDFs are expressed in user space units. ISO-32000-1 (section 8.3.2.3) tells us “the default for the size of the unit in default user space (1/72 inch) is approximately the same as a point (pt), a unit widely used in the printing industry. It is not exactly the same; there is no universal definition of a point.” In short, 1 in. = 25.4 mm = 72 user units (which roughly corresponds to 72 pt).

By default 72 user units are 1 inch, but this default can be changed by defining a UserUnit.

Coordinate system: orientation

The second FAQ entry you have to read is: How should I interpret the coordinates of a rectangle in PDF?

Let me just copy/paste the image:

在此处输入图片说明

Coordinate system: origin

Finally, you have to read: Where is the origin (x,y) of a PDF page?

After reading the previous question, you might assume that the lower-left corner corresponds with the coordinate (0, 0) , but that's not always true.

Why your question can't be answered

You claim that you are presenting the PDF as an image, and that you can retrieve coordinates as pixel coordinates, eg the 80th pixel in row 300 of all the pixel rows. However, if no one knows at which resolution the PDF is rendered when you converted the PDF (vector data) to a raster image, no one can tell you how the user units relate to pixels. That's something only you can know.

So please read the answers to the questions listed above carefully, and you'll be able to do the necessary Math.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM