Graphics System in PDF

From
Revision as of 16:25, 13 February 2018 by Adminko (talk | contribs)
Jump to: navigation, search

Overview

Graphics system in PDF is built around a few concepts:

  • Coordinate space, which defines the canvas on which all painting occurs. It determines the position, orientation, and scale of the text, graphics, and images that appear on a page. Paths and positions shall be defined in terms of pairs of coordinates on the Cartesian plane. A coordinate pair is a pair of real numbers x and y that locate a point horizontally and vertically within a two-dimensional coordinate space. A coordinate space is determined by the following properties with respect to the current page: the location of the origin, the orientation of the x and y axes, the lengths of the units along each axis.
  • Graphics state, which defines the current state of the drawing system: current stroking and non-stroking colors, masks, transformations, clipping, alpha etc. See section 8.4 ”Graphics State” of the PDF specification.
  • Drawing commands, which use current coordinate space and graphics state and may affect them if designed to do so. They often use graphics paths as an input, which in turn consist of atomic operations for combining lines and curves into a single figure.
  • ColorSpace, which defines the current color representation and transformation

Implementation in Fixed layout API

Coordinate space

ClippedContent class instances are being used as command containers and can be nested into each other providing a way to create combined clippings and other visual effects. Page class is inherited from ClippedContent thus providing the same functionality. Page maps directly to the document's page it represents and its coordinate space corresponds by default to the doc's page coordinate space.

Drawing commands

One may draw complex and simple figures by using the Path class that represents the PDF path object. It's possible to draw lines, curves, standard primitives, clipped shapes and more using this object. Please check the samples below demonstrating its functionality. The approach for using it is as follows: build the path, set container properties, add path to the container invoking desired operation(stroke, fill, stroke and fill).

Drawing a line

We create a Path object and add line to it by calling the self-explaining method Path.AppendLine. After that we set line width and color and stroke this path object by calling ClippedContent.StrokePath method.

// create output PDF file
using (FileStream outputStream = new FileStream("outfile.pdf", FileMode.Create, FileAccess.Write))
{
    // create new document
    using(FixedDocument document = new FixedDocument())
    {
        // create new page
        Page page = new Page(Boundaries.A4);

        // create new path representing a line
        Path path = new Path(10, 820);
        path.AppendLine(300,750);

        // set current non-stroking color             
        page.Content.SetDeviceStrokingColor(new double[] { 1, 0, 1 });                
        // set line width                
        page.Content.SetLineWidth(2.0);
        // stroke path
        page.Content.StrokePath(path);

        // add page to the document
        document.Pages.Add(page);

        // save to output stream
        document.Save(outputStream);
    }
}
 

The results produced by this code are below:

Drawing a line

Graphics state

Implementation in Flow layout API

There is no direct access to the graphics system, however existing or created using Fixed layout API FormXObjects can be included into the final document using ContentReference element.