PDF documents use a subset of the PostScript command language to create text and images in a document. The commands allow text to be laid out on a page as well as lines, shapes and graphics. Since the PDF format is a page layout language, everything that is placed on the page is positioned using a coordinate system. Page element my overlap. Each element obscures any previously placed elements that share the same coordinate space on the page.
There are several different coordinate spaces that apply to various pieces of a PDF document. The main coordinate space for a PDF document is called user space. User space is the coordinate space used to locate objects on a page. Traditionally user space is set to be 72 units per inch on a 8.5 by 11 inch piece of paper. This works out to a user space that is 612 units wide and 792 units high. The origin is in the top left corner of the page and the coordinate values increase as you move down and to the right. The exact dimensions of the user space page are defined by the values of the mediaBox for each page. These dimensions can actually be set to just about anything. The reason for this is that the PDF specification is designed to be device independent. This means that you should be able to print the same PDF document on a printer, plotter, computer screen or anything else that that a PDF program has been written to display it on. When it comes time to actually render the PDF, the user space coordinates are scaled to the resolution of the output device. If the width to height ratio of the output device differs from the ratio of the page's user space, the rendered document may be cropped or warped. That is why the user space is traditionaly designed to be 8.5x11.
The PDF specification provides a set of graphics commands for drawing arcs, circles, rectangles and other shapes. These commands may also be used to specify clipping regions to mask other elements on the page.
Every time a set of commands is used, it creates what is called a path. A path is a set of lines and curves that define a shape or region. Once a path is defined, it can be outlined, filled or used as a clipping path. Outlining a path is called stroking. If a path does not create a closed shape, one of several fill algorythms can be used to dermine what is inside and outside the path and then the region is filled appropriately. Clipping a path creates a mask that designates which portions of the page can be drawn on.
The first example shows a page containing an empty rectangle.
<pdf>
<!-- Page --> <contents>
<!-- Empty Square -->
<moveto>
<int>100</int>
<int>100</int>
</moveto>
<strokepath></strokepath>
<!-- Filled Square -->
<moveto>
<int>300</int>
<int>300</int>
</moveto>
<fillpath></fillpath>
</contents>
</pdf> |
The first set of commands draws an empty box. The moveto command takes a pair of X and Y coordinates and moves the cursor to the specified location. Each of the following lineto commands move the cursor to the new point, drawing a line from the previous point. Once the path has been specified, the strokepath command draw lines along the path. The second set of path commands is used to draw a filled box using the fillpath command instead of the strokepath command.