简体   繁体   English

从pdf文档获取X和Y坐标

[英]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文档获取x和y坐标。 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. Pdf文档没有DOM,这就是为什么我将div覆盖在pdf文档的顶部以从div获取坐标的原因。 I am using itextsharp library and trying to stamp a value you the pdf document. 我正在使用itextsharp库,并尝试在pdf文档中标记一个值。 Unfortunately the x and y coordinates of the div are in pixels and I need to relate them to the values in the stamper. 不幸的是,div的x和y坐标以像素为单位,我需要将它们与压模中的值相关联。 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. 我从div单击事件获得的X坐标是:80、300、600、880,我需要在压模中输入的X坐标是10、205,405,600。 What ratio do I need to apply to convert the div coordinates to the stamper coordinates. 我需要将div坐标转换为压模坐标的比例是多少。

 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 尝试获取userUnit-在我的情况下,它们为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. 在iText官方网站上有一个FAQ,您可以在其中找到有关Stack Overflow之前已回答的问题的信息。

Measurement unit 计量单位

The first FAQ entry you have to read is: How to get the UserUnit from a PDF file? 您必须阅读的第一个FAQ条目是: 如何从PDF文件获取UserUnit?

I quote: 我引用:

FAQ What is the measurement unit in PDF documents? 常问问题 PDF文件中的度量单位是什么? Most of the measurements in PDFs are expressed in user space units. PDF中的大多数度量都以用户空间单位表示。 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. ISO-32000-1(第8.3.2.3节)告诉我们“默认用户空间(1/72英寸)中单位尺寸的默认值与打印中广泛使用的单位(pt)大致相同。行业。 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). 简而言之,1英寸= 25.4毫米= 72个用户单位(大约相当于72磅)。

By default 72 user units are 1 inch, but this default can be changed by defining a UserUnit. 默认情况下,72个用户单位为1英寸,但是可以通过定义UserUnit来更改此默认值。

Coordinate system: orientation 坐标系:方向

The second FAQ entry you have to read is: How should I interpret the coordinates of a rectangle in PDF? 您必须阅读的第二个FAQ条目是: 如何解释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? 最后,您必须阅读: PDF页面的原点(x,y)在哪里?

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. 阅读上一个问题后,您可能会认为左下角与坐标(0, 0) ,但这并不总是正确的。

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. 您声称您将PDF呈现为图像,并且可以将坐标检索为像素坐标,例如,所有像素行的第300行中的第80个像素。 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. 但是,如果在将PDF(矢量数据)转换为光栅图像时,如果没人知道PDF呈现的分辨率,则没人能告诉您用户单位与像素的关系。 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. 因此,请仔细阅读上面列出的问题的答案,您将能够进行必要的数学运算。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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