Graphics

Modified on 2014/12/17 14:18 by Thomas Hövel — Categorized as: Articles, PDFsharp Articles

All graphics classes are designed similar to GDI+ classes from the System.Drawing namespace. The classes have names like XColor, XPen, XBrush, XGraphics, XRect, XMatrix, XImage etc. and their purpose is the same as in GDI+. All names have an X as prefix to make them unique when used in the same code with the GDI+ or WPF classes.

There are the following general differences between the PDFsharp X classes and the classes from System.Drawing:



In the WPF build of PDFsharp the X classes have nearly the same interface as in the GDI+ build, but are based internally on Windows Presentation Foundation.

Graphics Context

The XGraphics class represents an abstract drawing surface. With the following code you can draw on a PDF page:
XGraphics gfx = XGraphics.FromPdfPage(page);
If the page already has content (e. g. it comes from an existing PDF file), you can choose with the XGraphicsPdfPageOptions whether new objects are placed above or below the existing content.

An XGraphics object can also draw on a System.Drawing.Graphics object in the GDI+ build:
XGraphics gfx = XGraphics.FromGraphics(graphics, size);
In the WPF build an XGraphics object can also draw on a System.Windows.Media.DrawingContext object:
XGraphics gfx = XGraphics.FromDrawingContext(drawingContext, size);
This makes it possible to draw with the same functions in a window, on a printer, on a bitmap, or on a PDF page.

Coordinates

The current implementation of PDFsharp has only one layout of the graphics context. The origin (0, 0) is top left and coordinates grow right and down. The unit of measure is always point (1/72 inch).

This is not a real limitation, because a transformation matrix can translate the origin or scale the unit as you like see Transformation. With the XUnit class you can convert between point, inch, centimeter and millimeter.

Lines and Curves

The XGraphics class provides the following functions for drawing lines and curves:


Each function requires an XPen that defines how to draw the line.

Shapes

The XGraphics class provides the following functions to draw predefined shapes:


Each function can be invoked with an XPen (stroke only), or an XBrush (fill only), or both (stroke and fill).

Graphical Paths

The XGraphicalPath represents a graphical path. The XGraphics class provides the following function for drawing a path:


The function can be invoked with an XPen (stroke only), or an XBrush (fill only), or both (stroke and fill).

Fonts

The current implementation of PDFsharp supports TrueType fonts and OpenType fonts with TrueType outline, in other words the typical fonts used with Microsoft Windows.

Create a font with the XFont class:
XFont font = new XFont("Verdana", 12, XFontStyle.Italic);
The XPdfFontOptions class specifies PDF specific options:

Text

The XGraphics class provides the following functions in connection with text:


The DrawString function has an XStringFormat parameter that specifies the alignment of the text. The DrawString function is a primitive function and has no functionality for wrapping. Use MigraDoc foundation for formatting text with different fonts, sizes, etc. in a paragraph. See sample Mix MigraDoc and PDFsharp

Images

The XGraphics class provides the following function for drawing an image:


PDFsharp supports the following image formats:


Graphics State

The XGraphics class provides the following function for saving and restoring the current graphics state:


Use these functions to save the graphics state before you issue transformation and/or clipping functions and restore the state to return to the previous transformation and clipping state. With the BeginContainer function you can specify two additional rectangles and the function calculates an affine transformation for you such that the first rectangle is mapped to the second. This makes it easy to define a new coordinate system.

Transformation

The XGraphics class provides the following function for transforming the coordinate system:


The transformation is applied immediately to the current drawing context by multiplying it with an XMatrix object. Use XMatrixOrder to choose whether the transformation is appended or prepended to the current transformation.

Use Save / Restore to preserve the current transformation.

Clipping

The XGraphics class provides the following function to restrict the current clip region:


Text and graphics outside the clip area are not displayed.

Use Save / Restore to preserve the current transformation.

Miscellaneous

The XGraphics class provides the following miscellaneous functions:


See also: Graphics sample | Supported Platforms and Technologies, Available Builds