Welcome
Guest
•
Login
PDFsharp and MigraDoc Wiki
Navigation
¶
Main Page
Random Page
Create a new Page
All Pages
Categories
Navigation Paths
File Management
Create Account
Quick Search
»
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.
Back
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 {s:className|System.Drawing} namespace. The classes have names like {s:className|XColor}, {s:className|XPen}, {s:className|XBrush}, {s:className|XGraphics}, {s:className|XRect}, {s:className|XMatrix}, {s:className|XImage} etc. and their purpose is the same as in GDI+. All names have an {s:className|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+/{s:className|System.Drawing} * {s:className|X} graphics classes and functions supports only functionality available in PDFsharp {toc} In the WPF build of PDFsharp the {s:className|X} classes have nearly the same interface as in the GDI+ build, but are based internally on Windows Presentation Foundation. ==Graphics Context== The {s:className|XGraphics} class represents an abstract drawing surface. With the following code you can draw on a PDF page: {s:beginCsharp} XGraphics gfx = XGraphics.FromPdfPage(page); {s:endCsharp} If the page already has content (e. g. it comes from an existing PDF file), you can choose with the {s:className|XGraphicsPdfPageOptions} whether new objects are placed above or below the existing content. An {s:className|XGraphics} object can also draw on a {s:className|System.Drawing.Graphics} object in the GDI+ build: {s:beginCsharp} XGraphics gfx = XGraphics.FromGraphics(graphics, size); {s:endCsharp} In the WPF build an {s:className|XGraphics} object can also draw on a {s:className|System.Windows.Media.DrawingContext} object: {s:beginCsharp} XGraphics gfx = XGraphics.FromDrawingContext(drawingContext, size); {s:endCsharp} 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_9|Transformation]. With the {s:className|XUnit} class you can convert between point, inch, centimeter and millimeter. ==Lines and Curves== The {s:className|XGraphics} class provides the following functions for drawing lines and curves: * DrawLine * DrawLines * DrawBezier * DrawBeziers * DrawCurve * DrawArc Each function requires an {s:className|XPen} that defines how to draw the line. ==Shapes== The {s:className|XGraphics} class provides the following functions to draw predefined shapes: * DrawRectangle * DrawRectangles * DrawRoundedRectangle * DrawEllipse * DrawPolygon * DrawPie * DrawClosedCurve Each function can be invoked with an {s:className|XPen} (stroke only), or an {s:className|XBrush} (fill only), or both (stroke and fill). ==Graphical Paths== The {s:className|XGraphicalPath} represents a graphical path. The {s:className|XGraphics} class provides the following function for drawing a path: * DrawPath The function can be invoked with an {s:className|XPen} (stroke only), or an {s:className|XBrush} (fill only), or both (stroke and fill). ==Fonts== The current implementation of {s:className|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 {s:className|XFont} class: {s:beginCsharp} XFont font = new XFont("Verdana", 12, XFontStyle.Italic); {s:endCsharp} The {s:className|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 {s:className|XGraphics} class provides the following functions in connection with text: * MeasureString * DrawString The {s:funcName|DrawString} function has an {s:className|XStringFormat} parameter that specifies the alignment of the text. The {s:funcName|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 [MixMigraDocAndPdfSharp-sample|Mix MigraDoc and PDFsharp] ==Images== The {s:className|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 {s:className|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 {s:funcName|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 {s:className|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 {s:className|XMatrix} object. Use {s:className|XMatrixOrder} to choose whether the transformation is appended or prepended to the current transformation. Use {s:funcName|Save} / {s:funcName|Restore} to preserve the current transformation. ==Clipping== The {s:className|XGraphics} class provides the following function to restrict the current clip region: * IntersectClip Text and graphics outside the clip area are not displayed. Use {s:funcName|Save} / {s:funcName|Restore} to preserve the current transformation. ==Miscellaneous== The {s:className|XGraphics} class provides the following miscellaneous functions: * WriteComment * Internals ---- {s:seeAlso|Graphics-sample|Graphics sample} | [Build_Variations|Supported Platforms and Technologies, Available Builds ]
Meta Keywords:
Meta Description:
Change Comment:
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