Welcome GuestLogin

PDFsharp and MigraDoc Wiki

RSS RSS

Navigation




Quick Search
»

PoweredBy

Visit the new Website for PDFsharp & MigraDoc Foundation 6.0 for .NET 6 and find information about the new version for Windows, Linux, and other platforms.

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:

  • All floating point values are of type double to prevent the tedious casting to float necessary in GDI+/System.Drawing
  • X graphics classes and functions supports only functionality available in PDFsharp


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:

  • DrawLine
  • DrawLines
  • DrawBezier
  • DrawBeziers
  • DrawCurve
  • DrawArc

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

Shapes

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

  • DrawRectangle
  • DrawRectangles
  • DrawRoundedRectangle
  • DrawEllipse
  • DrawPolygon
  • DrawPie
  • DrawClosedCurve

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:

  • DrawPath

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:
  • Use ANSI or Unicode encoding
  • Embed the font, or a subset of the font, in the PDF document

Text

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

  • MeasureString
  • DrawString

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:

  • DrawImage

PDFsharp supports the following image formats:

  • BMP
  • GIF (with optional transparency)
  • PNG (with optional transparency)
  • JPEG
  • TIFF (with optional transparency)

Graphics State

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

  • Save
  • Restore
  • BeginContainer
  • EndContainer

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:

  • TranslateTransform
  • ScaleTransform
  • RotateTransform
  • MultiplyTransform
  • AddTransform

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:

  • IntersectClip

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:

  • WriteComment
  • Internals

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


Visit the new Website for PDFsharp & MigraDoc Foundation 6.0 for .NET 6 and find information about the new version for Windows, Linux, and other platforms.



Miscellaneous
Home
PDFsharp
FAQ
Samples
Articles
MigraDoc
FAQ
Samples
Articles

ScrewTurn Wiki version 3.0.5.600. Some of the icons created by FamFamFam.

Impressum - Privacy Policy, Data Protection Declaration, Legal Notice