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
ファイルを表示 Open project: gbaychev/NClass Class Usage Examples

Public Methods

Method 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

Method 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 method

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
return void

DrawImage() public method

public DrawImage ( Image image, Point point ) : void
image Image
point Point
return void

DrawImage() public method

public DrawImage ( Image image, int x, int y ) : void
image Image
x int
y int
return void

DrawLine() public method

public DrawLine ( Pen pen, Point pt1, Point pt2 ) : void
pen System.Drawing.Pen
pt1 Point
pt2 Point
return void

DrawLine() public method

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
return void

DrawLines() public method

public DrawLines ( Pen pen, Point points ) : void
pen System.Drawing.Pen
points Point
return void

DrawPath() public method

public DrawPath ( Pen pen, GraphicsPath path ) : void
pen System.Drawing.Pen
path System.Drawing.Drawing2D.GraphicsPath
return void

DrawPolygon() public method

public DrawPolygon ( Pen pen, Point points ) : void
pen System.Drawing.Pen
points Point
return void

DrawRectangle() public method

public DrawRectangle ( Pen pen, Rectangle rect ) : void
pen System.Drawing.Pen
rect System.Drawing.Rectangle
return void

DrawString() public method

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
return void

DrawString() public method

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
return void

DrawString() public method

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
return void

DrawString() public method

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
return void

FillEllipse() public method

public FillEllipse ( Brush brush, Rectangle rect ) : void
brush System.Drawing.Brush
rect System.Drawing.Rectangle
return void

FillEllipse() public method

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
return void

FillPath() public method

public FillPath ( Brush brush, GraphicsPath path ) : void
brush System.Drawing.Brush
path System.Drawing.Drawing2D.GraphicsPath
return void

FillPolygon() public method

public FillPolygon ( Brush brush, Point points ) : void
brush System.Drawing.Brush
points Point
return void

FillRectangle() public method

public FillRectangle ( Brush brush, Rectangle rect ) : void
brush System.Drawing.Brush
rect System.Drawing.Rectangle
return void

PDFGraphics() public method

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.
return System

ResetTransform() public method

public ResetTransform ( ) : void
return void

RotateTransform() public method

public RotateTransform ( float angle ) : void
angle float
return void

ScaleTransform() public method

public ScaleTransform ( float sx, float sy ) : void
sx float
sy float
return void

SetClip() public method

public SetClip ( GraphicsPath path, CombineMode combineMode ) : void
path System.Drawing.Drawing2D.GraphicsPath
combineMode CombineMode
return void

SetClip() public method

public SetClip ( Rectangle rect, CombineMode combineMode ) : void
rect System.Drawing.Rectangle
combineMode CombineMode
return void

SetClip() public method

public SetClip ( RectangleF rect, CombineMode combineMode ) : void
rect System.Drawing.RectangleF
combineMode CombineMode
return void

SetClip() public method

public SetClip ( Region region, CombineMode combineMode ) : void
region System.Drawing.Region
combineMode CombineMode
return void

TakeInitialTransform() public method

Takes the inital state.
public TakeInitialTransform ( ) : void
return void

TranslateTransform() public method

public TranslateTransform ( float dx, float dy ) : void
dx float
dy float
return void