![]() |
Products | Support | Email a link to this topic. | Send comments on this topic. | Back to Introduction - All Topics | Help Version 19.0.3.23
|
PDF files have the following properties:
The origin of a document is at the lower left corner. The x-axis extends to the right and the y-axis extends upward
All locations and sizes are stored in a logical value called a PDF unit. 1 PDF unit is equivalent to 1/72 of an inch
PDF documents do not have a resolution. To convert a PDF unit to a physical value such as pixels, you must provide an external value for the resolution
For example, a typical PDF document may have a page width and height of 612 by 792 units. Hence:
The size in inches is 8.5" by 11" - Obtained by dividing the width and height by 72
An object located at point 288, 432 in the PDF has a distance of 4" by 6" from the bottom left corner of the page - Also obtained by dividing the location point by 72
The following image shows this typical PDF document and the object location:
Note that PDF documents contain a media box and a crop box values. The media box is the total size of the page while the crop box is its viewable areas. PDF viewers typically do not show any object that is located outside the crop box and report the size of the page to be the width and height of the crop box. So when a PDF viewer such as Adobe Acrobat Reader opens the document shown above; it will report the page size to be 8.5" by 11" even though the media box size of 10.5" by 13". Object location are always calculated as the distance from the media box location (always 0, 0) regardless of the value of the crop box. However, most PDF documents contain a media box and crop box of the same values so the two values are the same.
Physical values such as pixels are typically needed when a PDF document is rendered to the screen. To convert from logical to physical units, a resolution must be provided. Resolution is the value of dots (pixels) per inch to use when converting logical to physical values. In the example above, if a resolution value of 150 is used, then the size of the page in pixels is 1275 by 1650 - Obtained by multiplying 8.5" and 11" by 150. It is also desirable to obtain the converted value in top-left coordinates when converting from PDF units (logical) to pixels (physical) since most rendering systems such as System.Drawing and System.Windows.Media has the origin at the upper left corner of the window.
PDF documents do not contain a resolution; it is up to the user to specify the value to use when rendering the document on screen. High resolution values will render the document with greater details at the expense of using more system resources. Typical resolution values are 72, 96, 150, 200, 300 and 600.
If the document shown in the image above is loaded at a resolution of 150, then the object will be located at the following pixel value:
Pixel X = 600px (288 by 72 multiplied by 150)
Pixel Y = 750px (Page Height - 432 by 72 multiplied by 150)
Notice when the Y value is calculated, we subtracted the total value from the height of the page and thus, converted the result from bottom-left to top-left coordinates.
When using the Leadtools.Pdf.PDFFile or Leadtools.Pdf.PDFDocument classes of Leadtools.Pdf, LEADTOOLS will read the values from the PDF document as is without any conversion. The following values are read in PDF units (1/72 of an inch):
PDFDocumentPage.Width, PDFDocumentPage.Height, PDFDocumentPage.MediaBox and PDFDocumentPage.CropBox
PDFInternalLink.SourceBounds, PDFInternalLink.BorderWidth, PDFInternalLink.BorderDashLength and PDFInternalLink.TargetPosition
PDFTextProperties.FontWidth and PDFTextProperties.FontHeight
The PDFDocument.Resolution property contains the resolution value to use when converting PDF units (logical) to pixels or inches and back. This resolution can be changed at any time as desired.
The helper methods PDFDocumentPage.ConvertPoint and PDFDocumentPage.ConvertRect can be used to convert from any unit to another. These methods accept a Leadtools.Pdf.PDFCoordinateType enumeration member for the source and destination units.
The following converts a point (sourcePoint) from PDF units to inches and pixels:
PDFPoint destPointInches = pdfDocumentPage.ConvertPoint(PDFCoordinateType.Pdf, PDFCoordinateType.Inch, sourcePoint);
PDFPoint destPointPixels = pdfDocumentPage.ConvertPoint(PDFCoordinateType.Pdf, PDFCoordinateType.Pixel, sourcePoint);
To convert a rectangle (sourceRect) from PDF units to pixels and inches, you use the following:
PDFRect destRectInches = pdfDocumentPage.ConvertRect(PDFCoordinateType.Pdf, PDFCoordinateType.Inch, sourceRect);
PDFRect destRectPixels = pdfDocumentPage.ConvertRect(PDFCoordinateType.Pdf, PDFCoordinateType.Pixel, sourceRect);
To convert the values back to PDF units, you use the following:
sourcePoint = pdfDocumentPage.ConvertPoint(PDFCoordinateType.Inch, PDFCoordinateType.Pdf, destPointInches);
sourcePoint = pdfDocumentPage.ConvertPoint(PDFCoordinateType.Pixel, PDFCoordinateType.Pdf, destPointPixels);
sourceRect = pdfDocumentPage.ConvertRect(PDFCoordinateType.Inch, PDFCoordinateType.Pdf, destRectInches);
sourceRect = pdfDocumentPage.ConvertRect(PDFCoordinateType.Pixel, PDFCoordinateType.Pdf, destRectPixels);
Note that the Leadtools.Pdf.PDFDocumentPage object will always use the value of the owner PDFDocument.Resolution when converting from and to pixels.