About a PDF format (understanding and working with PDF documents)
PDF documents are very different from regular documents. The document format is designed to present documents the same way regardless of the platform and software.
This format basically is a complete description of a fixed-layout flat document, including text, fonts, graphics, and all other information required to display it or print it.
As such, it has several specificities, that need to be taken into account when working with PDF documents.
You can keep the default or you can choose to relocate the origin to any of the corners using the SetOrigin method. All subsequent measurements will be then performed relative to this new origin.
In PDF documents, everything is measured using “Point”. In the PDF world, 1 point = 1/72 inch, it means that a page width of 595 points is actually 595 / 72 = 8.27 inches, which is the standard width for DIN A4 page size.
For convenience, GdPicture.NET provides the SetMeasurementUnit method that will let you choose between Points, Inches, Millimeters and Centimeters (Point being the default unit). After calling this method, all the subsequent location and size values will be returned in the selected unit.
Only raster images have a resolution, in a PDF document, anything that is text or vector graphic will be rendered at the desired resolution of the output device (screen or printer). For display, the resolution depends on the rendering zoom level. The physical size of the page won’t change, but depending on the zoom level, the number of pixels used to represent it, will vary. For instance, let’s take a full A4 page, its height will be 8.27 inches and its width will be 11.69 inches.
When displayed at 100%, the resolution is 96 DPI,so by applying the well-known “inches = pixels / resolution” formula, the actual pixels needed to display the image will be 1123 x 794 pixels. When displayed at 150%, resolution is 144 DPI, so the display size will be 1684 x 1191 pixels and so on.
Note that the current display resolution is accessible through HorizontalResolution and VerticalResolution properties of the GdViewer object.
The same formula applies, when you want to convert a PDF page to a raster image (this process is called rasterization), because it is the exact same thing except that you will be choosing the output resolution instead of working with the screen or printer resolution.
So keep in mind, that the higher the resolution is, the bigger the result image will be and so will be the required memory and system resources.
The MediaBox is mandatory and is the largest page box in a PDF document. The CropBox is optional and defines the region to which the page content is to clipped, it is used for displaying and printing. It can have the same size as the MediaBox, but if it is smaller, what is outside the CropBox boundaries, won’t be visible.
With a 90° or 270° rotation, width and height will be swapped and it can make several processing operations tricky.
That’s why GdPicture.NET provides the NormalizePage method that will set the page rotation to 0 and also set the CropBox equal to the MediaBox, so you can work using a simple regular coordinates system.
Knowing this, it becomes easy to draw something on a PDF page at a given location.
But when the user interactively selects an area or a location in the viewer, that is displaying the page and the goal is to draw something at this exact location, you need to be careful about the coordinates you retrieve. Indeed, the area coordinates in the viewer frame are very different from what you would obtain in the document coordinates system and they are also in a different units (pixels vs inches).
GdPicture.NET provides several methods to retrieve coordinates and sizes either in the viewer coordinate system (in pixels) or in the document coordinates system (in inches).
If, for any reason, you need to convert coordinates from a one system to another, GdPicture.NET also provides two methods that will save you the hassle of the translation both ways (from viewer to document and also from document to viewer).
The first method is CoordViewerPixelToDocumentInch, it will convert pixels coordinates of a point relative to the viewer into inches coordinates relative to the document. The CoordDocumentInchToViewerPixel method does the same thing, but the other way around.