C# Class PDFExport.PDFGraphics

Implements the IGraphics interface for the PDF-Sharp output.
Every point, path, rectangle and so on is measured in pixels. PDF doesn't know about pixels, but dots. One dot is the 72th of an inch. So one has to apply some scaling for alle drawn elements. This can be done with a call to ScaleTransform for example to get from pixel coordinates to dot coordinates. This solution has one disadvantage: There are a few points where PDF-Sharp scales from pixels to dots itself: fonts and images. If we don't care about this, all fonts and images would appear to small in the resulting PDF. The solution is to "unscale" those elements first. To do this, there are the two properties ScaleFont and ScaleImage. //We take the DPI of the screen from a Control Graphics gdiGraphics = (new Control()).CreateGraphics(); //Apply a scaling to get from pixels to dots graphics.ScaleTransform(72.0f / gdiGraphics.DpiX, 72.0f / gdiGraphics.DpiY); //Unscale the font graphics.ScaleFont = gdiGraphics.DpiY / 72.0f; //Unscale the images graphics.ScaleImage = gdiGraphics.DpiX / 72.0f; graphics.TakeInitialTransform(); Word wrap is slightly different to the GDI word wrapping. If a single word is longer than a line, GDI breaks the word and prints the rest in a new line. Here the rest isn't printed but an ellipsis is appendet to the not fitting word. The following points are on the to do list: - Clipping isn't very good right now. We should unerstand the regions. There are a few things which are not perfect and won't be changed now: - Only horizontal, vertical and diagonal gradients are drawn correctly. Thats ok since NClass dosn't use any other gradients. - Images get a gray border in pdf if they have colored (even white) pixels next to transparent pixels. A workaround is to draw them by GDI on a surface. This surface is white. This looks good until the backgound of the entity is also white. If not, images are inside a white box. - If one draws with a gradient brush and the next drawing operation uses a black brush, this drawing is done with the gradient again. That's because the brush isn't changed in pdf. This is a bug in PDFSharp.
Inheritance: IGraphics
Afficher le fichier Open project: gbaychev/NClass Class Usage Examples

Méthodes publiques

Méthode Description
DrawEllipse ( Pen pen, int x, int y, int width, int height ) : void
DrawImage ( Image image, Point point ) : void
DrawImage ( Image image, int x, int y ) : void
DrawLine ( Pen pen, Point pt1, Point pt2 ) : void
DrawLine ( Pen pen, int x1, int y1, int x2, int y2 ) : void
DrawLines ( Pen pen, Point points ) : void
DrawPath ( Pen pen, GraphicsPath path ) : void
DrawPolygon ( Pen pen, Point points ) : void
DrawRectangle ( Pen pen, Rectangle rect ) : void
DrawString ( string s, Font font, Brush brush, PointF point ) : void
DrawString ( string s, Font font, Brush brush, PointF point, StringFormat format ) : void
DrawString ( string s, Font font, Brush brush, RectangleF layoutRectangle ) : void
DrawString ( string s, Font font, Brush brush, RectangleF layoutRectangle, StringFormat format ) : void
FillEllipse ( Brush brush, Rectangle rect ) : void
FillEllipse ( Brush brush, int x, int y, int width, int height ) : void
FillPath ( Brush brush, GraphicsPath path ) : void
FillPolygon ( Brush brush, Point points ) : void
FillRectangle ( Brush brush, Rectangle rect ) : void
PDFGraphics ( XGraphics graphics ) : System

Initializes a new instnace of PDFGraphics.

ResetTransform ( ) : void
RotateTransform ( float angle ) : void
ScaleTransform ( float sx, float sy ) : void
SetClip ( GraphicsPath path, CombineMode combineMode ) : void
SetClip ( Rectangle rect, CombineMode combineMode ) : void
SetClip ( RectangleF rect, CombineMode combineMode ) : void
SetClip ( Region region, CombineMode combineMode ) : void
TakeInitialTransform ( ) : void

Takes the inital state.

TranslateTransform ( float dx, float dy ) : void

Private Methods

Méthode Description
ApplyClip ( Region region ) : void

Applies the clipping stored in region.

BrushToXBrush ( Brush brush ) : PdfSharp.Drawing.XBrush

Converts a GDI-Brush to a PDF-XBrush.

Only Solid- and LinearGradientBrushes are supported.

CombineClippingRegion ( CombineMode combineMode, Region region ) : void

Combines the local clippingRegion with the given region.

DashStyleToXDashStyle ( DashStyle dashStyle ) : XDashStyle

Converts a GDI-DashStyle to a PDF-XDashStyle.

FillModeToXFillMode ( FillMode fillMode ) : XFillMode

Converts a GDI-FillMode to a PDF-XFillMode.

FloatArrayToDoubleArray ( float floats ) : double[]

Converts an array of floats to an array of doubles.

FontStyleToXFontStyle ( FontStyle fontStyle ) : XFontStyle

Converts a GDI-FontStyle to a PDF-XFontStyle.

FontToXFont ( Font font ) : PdfSharp.Drawing.XFont

Converts a GDI-Font to a PDF-XFont.

GraphicsPathToXGraphicsPath ( GraphicsPath path ) : PdfSharp.Drawing.XGraphicsPath

Converts a GDI-GraphicsPath to a PDF-XGraphicsPath.

ImageToXImage ( Image image ) : PdfSharp.Drawing.XImage

Converts a GDI-Image to a PDF-XImage.

The GDI-Image gets drawn on a white background.

LineCapToXLineCap ( LineCap lineCap ) : XLineCap

Converts a GDI-LineCap to a PDF-XLineCap.

PDF only supports square, round and flat line caps. So all the other GDI line caps gets mapped to the best PDF line caps as follows. XLineCap.Square LineCap.Square LineCap.Triangle LineCap.SquareAnchor LineCap.DiamondAnchor LineCap.ArrowAnchor XLineCap.Round LineCap.Round LineCap.RoundAnchor XLineCap.Flat LineCap.Flat LineCap.NoAnchor LineCap.Custom LineCap.AnchorMask

LineJoinToXLineJoin ( LineJoin lineJoin ) : XLineJoin

Converts a GDI-LineJoin to a PDF-XLineJoin.

PDF doesn't support LineJoin.MiterClipped so this is mapped to XLineJoin.Miter, too.

PenToXPen ( Pen pen ) : PdfSharp.Drawing.XPen

Converts a GDI-Pen to a PDF-XPen.

RestoreInitialClip ( ) : void

Resets the clipping.

RestoreInitialTransform ( ) : void

Restores the state to the initial state.

SetText ( String text, float width, int maxLines, PdfSharp.Drawing.XFont xFont ) : List

This method will take a text and do a word wrap so it fits into the given width. If a line is wider than width, a new line is started. If the resulting line count is greater than maxLines, an ellipsis is added. The result is a list of strings where one item per line.

StringAlignmentToXLineAlignment ( StringAlignment stringAlignment ) : XLineAlignment

Converts a GDI-StringAlignment to a PDF-XLineAlignment.

StringAlignmentToXStringAlignment ( StringAlignment stringAlignment ) : XStringAlignment

Converts a GDI-StringAlignment to a PDF-XStringAlignment.

StringFormatFlagsToXStringFormatFlags ( StringFormatFlags stringFormatFlags ) : XStringFormatFlags

Converts a GDI-StringFormatFlags to a PDF-XStringFormatFlags.

StringFormatToXStringFormat ( StringFormat format ) : PdfSharp.Drawing.XStringFormat

Converts a GDI-StringFormat to a PDF-XStringFormat.

TokenizeString ( string text, string separator ) : List

This method will devide string by a separator and return the tokens as a list. The list will contain the text between the separators as well as the separators itself.

TrimString ( string s, float width, PdfSharp.Drawing.XFont font, StringTrimming stringTrimming ) : string

Trims a string so it fits into the rectangle. Only one line is trimed.

The StringTrimming.EllipsisPath is handeled as StringTrimming.EllipsisCharacter.

Method Details

DrawEllipse() public méthode

public DrawEllipse ( Pen pen, int x, int y, int width, int height ) : void
pen System.Drawing.Pen
x int
y int
width int
height int
Résultat void

DrawImage() public méthode

public DrawImage ( Image image, Point point ) : void
image Image
point Point
Résultat void

DrawImage() public méthode

public DrawImage ( Image image, int x, int y ) : void
image Image
x int
y int
Résultat void

DrawLine() public méthode

public DrawLine ( Pen pen, Point pt1, Point pt2 ) : void
pen System.Drawing.Pen
pt1 Point
pt2 Point
Résultat void

DrawLine() public méthode

public DrawLine ( Pen pen, int x1, int y1, int x2, int y2 ) : void
pen System.Drawing.Pen
x1 int
y1 int
x2 int
y2 int
Résultat void

DrawLines() public méthode

public DrawLines ( Pen pen, Point points ) : void
pen System.Drawing.Pen
points Point
Résultat void

DrawPath() public méthode

public DrawPath ( Pen pen, GraphicsPath path ) : void
pen System.Drawing.Pen
path System.Drawing.Drawing2D.GraphicsPath
Résultat void

DrawPolygon() public méthode

public DrawPolygon ( Pen pen, Point points ) : void
pen System.Drawing.Pen
points Point
Résultat void

DrawRectangle() public méthode

public DrawRectangle ( Pen pen, Rectangle rect ) : void
pen System.Drawing.Pen
rect System.Drawing.Rectangle
Résultat void

DrawString() public méthode

public DrawString ( string s, Font font, Brush brush, PointF point ) : void
s string
font System.Drawing.Font
brush System.Drawing.Brush
point System.Drawing.PointF
Résultat void

DrawString() public méthode

public DrawString ( string s, Font font, Brush brush, PointF point, StringFormat format ) : void
s string
font System.Drawing.Font
brush System.Drawing.Brush
point System.Drawing.PointF
format System.Drawing.StringFormat
Résultat void

DrawString() public méthode

public DrawString ( string s, Font font, Brush brush, RectangleF layoutRectangle ) : void
s string
font System.Drawing.Font
brush System.Drawing.Brush
layoutRectangle System.Drawing.RectangleF
Résultat void

DrawString() public méthode

public DrawString ( string s, Font font, Brush brush, RectangleF layoutRectangle, StringFormat format ) : void
s string
font System.Drawing.Font
brush System.Drawing.Brush
layoutRectangle System.Drawing.RectangleF
format System.Drawing.StringFormat
Résultat void

FillEllipse() public méthode

public FillEllipse ( Brush brush, Rectangle rect ) : void
brush System.Drawing.Brush
rect System.Drawing.Rectangle
Résultat void

FillEllipse() public méthode

public FillEllipse ( Brush brush, int x, int y, int width, int height ) : void
brush System.Drawing.Brush
x int
y int
width int
height int
Résultat void

FillPath() public méthode

public FillPath ( Brush brush, GraphicsPath path ) : void
brush System.Drawing.Brush
path System.Drawing.Drawing2D.GraphicsPath
Résultat void

FillPolygon() public méthode

public FillPolygon ( Brush brush, Point points ) : void
brush System.Drawing.Brush
points Point
Résultat void

FillRectangle() public méthode

public FillRectangle ( Brush brush, Rectangle rect ) : void
brush System.Drawing.Brush
rect System.Drawing.Rectangle
Résultat void

PDFGraphics() public méthode

Initializes a new instnace of PDFGraphics.
public PDFGraphics ( XGraphics graphics ) : System
graphics PdfSharp.Drawing.XGraphics An instance of which is used to /// create the PDF.
Résultat System

ResetTransform() public méthode

public ResetTransform ( ) : void
Résultat void

RotateTransform() public méthode

public RotateTransform ( float angle ) : void
angle float
Résultat void

ScaleTransform() public méthode

public ScaleTransform ( float sx, float sy ) : void
sx float
sy float
Résultat void

SetClip() public méthode

public SetClip ( GraphicsPath path, CombineMode combineMode ) : void
path System.Drawing.Drawing2D.GraphicsPath
combineMode CombineMode
Résultat void

SetClip() public méthode

public SetClip ( Rectangle rect, CombineMode combineMode ) : void
rect System.Drawing.Rectangle
combineMode CombineMode
Résultat void

SetClip() public méthode

public SetClip ( RectangleF rect, CombineMode combineMode ) : void
rect System.Drawing.RectangleF
combineMode CombineMode
Résultat void

SetClip() public méthode

public SetClip ( Region region, CombineMode combineMode ) : void
region System.Drawing.Region
combineMode CombineMode
Résultat void

TakeInitialTransform() public méthode

Takes the inital state.
public TakeInitialTransform ( ) : void
Résultat void

TranslateTransform() public méthode

public TranslateTransform ( float dx, float dy ) : void
dx float
dy float
Résultat void