Skip to content

PdfGraphics class

View as Markdown

Namespace: PdfNetStack.Drawing
Assembly: PdfNetStack.dll
Package: PdfNetStack

A drawing surface on a page. Coordinates are in points with the origin at the top left corner of the media box and y pointing down. Dispose it, or save the document, to write the drawing into the page. Not thread safe.

public sealed class PdfGraphics : IDisposable

Inheritance

object ← PdfGraphics

Implements

IDisposable

On an existing page, the page’s own content is wrapped in a save and restore pair so that graphics state it leaves behind cannot affect this drawing. The page’s /Rotate is not applied: coordinates are those of the unrotated page.

How further drawing blends with what is under it. Saved and restored with the graphics state.

public PdfBlendMode BlendMode { get; set; }

PdfBlendMode

The form being drawn into, or null when drawing on a page.

public PdfForm? Form { get; }

PdfForm?

Height of the drawing area in points.

public double Height { get; }

double

The page being drawn on, or null when drawing into a PdfForm.

public PdfPage? Page { get; }

PdfPage?

The current transform from user coordinates to canvas coordinates.

public PdfMatrix Transform { get; }

PdfMatrix

Width of the drawing area in points: the media box, or the form’s width.

public double Width { get; }

double

AddLink(double, double, double, double, Uri)

Section titled “ AddLink(double, double, double, double, Uri)”

Adds a link over a rectangle in current canvas coordinates; under a transform, the link covers the transformed rectangle’s bounds. Only on pages.

public PdfAnnotation AddLink(double x, double y, double width, double height, Uri uri)

x double

y double

width double

height double

uri Uri

PdfAnnotation

AddLink(double, double, double, double, PdfPage, double?)

Section titled “ AddLink(double, double, double, double, PdfPage, double?)”

Adds a link to a page of this document over a rectangle in current canvas coordinates.

public PdfAnnotation AddLink(double x, double y, double width, double height, PdfPage target, double? top = null)

x double

y double

width double

height double

target PdfPage

top double?

PdfAnnotation

Writes the drawing into the page. Further drawing is not possible.

public void Dispose()

DrawArc(PdfPen, double, double, double, double, double, double)

Section titled “ DrawArc(PdfPen, double, double, double, double, double, double)”

Draws an elliptical arc of the ellipse inscribed in the given rectangle. Angles in degrees, clockwise.

public void DrawArc(PdfPen pen, double x, double y, double width, double height, double startAngle, double sweepAngle)

pen PdfPen

x double

y double

width double

height double

startAngle double

sweepAngle double

DrawBezier(PdfPen, PdfPoint, PdfPoint, PdfPoint, PdfPoint)

Section titled “ DrawBezier(PdfPen, PdfPoint, PdfPoint, PdfPoint, PdfPoint)”

Draws a cubic Bezier curve.

public void DrawBezier(PdfPen pen, PdfPoint start, PdfPoint control1, PdfPoint control2, PdfPoint end)

pen PdfPen

start PdfPoint

control1 PdfPoint

control2 PdfPoint

end PdfPoint

DrawEllipse(PdfPen?, PdfBrush?, double, double, double, double)

Section titled “ DrawEllipse(PdfPen?, PdfBrush?, double, double, double, double)”

Draws an ellipse inscribed in the given rectangle.

public void DrawEllipse(PdfPen? pen, PdfBrush? brush, double x, double y, double width, double height)

pen PdfPen?

brush PdfBrush?

x double

y double

width double

height double

Draws a form at its own size, its top left corner at (x, y).

public void DrawForm(PdfForm form, double x, double y)

form PdfForm

x double

y double

DrawForm(PdfForm, double, double, double, double)

Section titled “ DrawForm(PdfForm, double, double, double, double)”

Draws a form scaled into the rectangle: a stamp, a template, a page shown in miniature.

public void DrawForm(PdfForm form, double x, double y, double width, double height)

form PdfForm

x double

y double

width double

height double

ArgumentException

The form belongs to another document.

Draws an image at its natural size: its pixels at the resolution it records, else 96 per inch.

public void DrawImage(PdfImage image, double x, double y)

image PdfImage

x double

y double

DrawImage(PdfImage, double, double, double, double)

Section titled “ DrawImage(PdfImage, double, double, double, double)”

Draws an image stretched to the rectangle, upright as its EXIF orientation says. Painting follows the current transform and clip; transparency comes from the image’s alpha channel.

public void DrawImage(PdfImage image, double x, double y, double width, double height)

image PdfImage

x double

y double

width double

height double

DrawImage(PdfImage, double, double, double, double, PdfImageFit, PdfImageAlignment, double)

Section titled “ DrawImage(PdfImage, double, double, double, double, PdfImageFit, PdfImageAlignment, double)”

Draws an image into a box, fitted as fit says and placed by alignment where it does not fill the box. Whatever falls outside the box is clipped. opacity fades the whole image, on top of its own alpha.

public void DrawImage(PdfImage image, double x, double y, double width, double height, PdfImageFit fit, PdfImageAlignment alignment = PdfImageAlignment.Center, double opacity = 1)

image PdfImage

The image.

x double

Left edge of the box.

y double

Top edge of the box.

width double

Width of the box.

height double

Height of the box.

fit PdfImageFit

How the image fills the box.

alignment PdfImageAlignment

Where the image sits when it is smaller or larger than the box.

opacity double

0 (invisible) to 1 (opaque).

ArgumentOutOfRangeException

opacity is not between 0 and 1.

DrawInvisibleText(string, double, double, double, double)

Section titled “ DrawInvisibleText(string, double, double, double, double)”

Draws text that is invisible but selectable and searchable, stretched to fill a box: the text layer of a scanned page. The box’s top left corner is at (x, y); the text’s baseline runs along its bottom edge and its height is the font size. Any script works, unshaped, since nothing is drawn.

public void DrawInvisibleText(string text, double x, double y, double width, double height)

text string

The text, in logical order.

x double

Left edge of the box.

y double

Top edge of the box.

width double

Width the text is stretched or squeezed to.

height double

Height of the box: the font size.

// A word an OCR engine found at 100,200 to 180,214 points on the page.
graphics.DrawInvisibleText("Invoice", 100, 200, 80, 14);

DrawInvisibleText(string, double, double, double, double, double)

Section titled “ DrawInvisibleText(string, double, double, double, double, double)”

Draws invisible, searchable text in a box rotated clockwise about its top left corner.

public void DrawInvisibleText(string text, double x, double y, double width, double height, double angle)

text string

The text, in logical order.

x double

Left edge of the box, before rotation.

y double

Top edge of the box, before rotation.

width double

Width the text is stretched or squeezed to.

height double

Height of the box: the font size.

angle double

Rotation in degrees, clockwise, about (x, y).

DrawLine(PdfPen, double, double, double, double)

Section titled “ DrawLine(PdfPen, double, double, double, double)”

Draws a straight line.

public void DrawLine(PdfPen pen, double x1, double y1, double x2, double y2)

pen PdfPen

x1 double

y1 double

x2 double

y2 double

Draws connected straight lines through the given points.

public void DrawLines(PdfPen pen, IEnumerable<PdfPoint> points)

pen PdfPen

points IEnumerable<PdfPoint>

Strokes path with pen and fills it with brush; either may be null.

public void DrawPath(PdfPen? pen, PdfBrush? brush, PdfPath path)

pen PdfPen?

brush PdfBrush?

path PdfPath

DrawPie(PdfPen?, PdfBrush?, double, double, double, double, double, double)

Section titled “ DrawPie(PdfPen?, PdfBrush?, double, double, double, double, double, double)”

Draws a pie slice of the ellipse inscribed in the given rectangle. Angles in degrees, clockwise.

public void DrawPie(PdfPen? pen, PdfBrush? brush, double x, double y, double width, double height, double startAngle, double sweepAngle)

pen PdfPen?

brush PdfBrush?

x double

y double

width double

height double

startAngle double

sweepAngle double

DrawPolygon(PdfPen?, PdfBrush?, IEnumerable<PdfPoint>, PdfFillRule)

Section titled “ DrawPolygon(PdfPen?, PdfBrush?, IEnumerable<PdfPoint>, PdfFillRule)”

Draws a closed polygon.

public void DrawPolygon(PdfPen? pen, PdfBrush? brush, IEnumerable<PdfPoint> points, PdfFillRule fillRule = PdfFillRule.NonZero)

pen PdfPen?

brush PdfBrush?

points IEnumerable<PdfPoint>

fillRule PdfFillRule

DrawRectangle(PdfPen?, PdfBrush?, double, double, double, double)

Section titled “ DrawRectangle(PdfPen?, PdfBrush?, double, double, double, double)”

Draws a rectangle, stroked with pen and filled with brush; either may be null.

public void DrawRectangle(PdfPen? pen, PdfBrush? brush, double x, double y, double width, double height)

pen PdfPen?

brush PdfBrush?

x double

y double

width double

height double

DrawRoundedRectangle(PdfPen?, PdfBrush?, double, double, double, double, double)

Section titled “ DrawRoundedRectangle(PdfPen?, PdfBrush?, double, double, double, double, double)”

Draws a rectangle with circular corners.

public void DrawRoundedRectangle(PdfPen? pen, PdfBrush? brush, double x, double y, double width, double height, double radius)

pen PdfPen?

brush PdfBrush?

x double

y double

width double

height double

radius double

DrawRoundedRectangle(PdfPen?, PdfBrush?, double, double, double, double, PdfCornerRadii)

Section titled “ DrawRoundedRectangle(PdfPen?, PdfBrush?, double, double, double, double, PdfCornerRadii)”

Draws a rectangle with elliptical corners, sized as CSS border-radius does.

public void DrawRoundedRectangle(PdfPen? pen, PdfBrush? brush, double x, double y, double width, double height, PdfCornerRadii radii)

pen PdfPen?

brush PdfBrush?

x double

y double

width double

height double

radii PdfCornerRadii

DrawString(string, PdfFont, double, PdfBrush, double, double)

Section titled “ DrawString(string, PdfFont, double, PdfBrush, double, double)”

Draws one line of text filled with brush. (x, y) is the start of the baseline; add Ascent times the size to y to place the top of the line instead.

public void DrawString(string text, PdfFont font, double size, PdfBrush brush, double x, double y)

text string

font PdfFont

size double

brush PdfBrush

x double

y double

ArgumentException

The font has no glyph for a character of the text.

DrawString(string, PdfFont, double, PdfPen?, PdfBrush?, double, double)

Section titled “ DrawString(string, PdfFont, double, PdfPen?, PdfBrush?, double, double)”

Draws one line of text, its glyph outlines stroked with pen and filled with brush; either may be null. (x, y) is the start of the baseline. Control and format characters, such as tabs and zero width joiners, are skipped.

public void DrawString(string text, PdfFont font, double size, PdfPen? pen, PdfBrush? brush, double x, double y)

text string

font PdfFont

size double

pen PdfPen?

brush PdfBrush?

x double

y double

ArgumentException

The font has no glyph for a character of the text.

Draws an SVG drawing at its own size, its top left corner at (x, y).

public void DrawSvg(PdfSvg svg, double x, double y)

svg PdfSvg

x double

y double

DrawSvg(PdfSvg, double, double, double, double)

Section titled “ DrawSvg(PdfSvg, double, double, double, double)”

Draws an SVG drawing scaled to fill width by height points; its viewBox and preserveAspectRatio decide how it fits inside.

public void DrawSvg(PdfSvg svg, double x, double y, double width, double height)

svg PdfSvg

x double

y double

width double

height double

DrawText(PdfParagraph, double, double, double)

Section titled “ DrawText(PdfParagraph, double, double, double)”

Lays out a paragraph at width points, wrapping and aligning its lines, and draws it with its top left corner at (x, y). Returns the layout, whose Height tells how much room it took.

public PdfTextLayout DrawText(PdfParagraph paragraph, double x, double y, double width)

paragraph PdfParagraph

x double

y double

width double

PdfTextLayout

DrawText(string, PdfTextStyle, double, double, double, PdfTextAlignment)

Section titled “ DrawText(string, PdfTextStyle, double, double, double, PdfTextAlignment)”

Draws text in one style, wrapped to width points; returns the layout.

public PdfTextLayout DrawText(string text, PdfTextStyle style, double x, double y, double width, PdfTextAlignment alignment = PdfTextAlignment.Start)

text string

style PdfTextStyle

x double

y double

width double

alignment PdfTextAlignment

PdfTextLayout

Draws a laid out paragraph with its top left corner at (x, y).

public void DrawText(PdfTextLayout layout, double x, double y)

layout PdfTextLayout

x double

y double

DrawText(PdfTextLayout, double, double, int, int)

Section titled “ DrawText(PdfTextLayout, double, double, int, int)”

Draws count lines of a layout from line first, the first of them at (x, y): for text that continues on the next page or column.

public void DrawText(PdfTextLayout layout, double x, double y, int first, int count)

layout PdfTextLayout

x double

y double

first int

count int

Restricts all further drawing to the inside of path, until the next restore.

public void IntersectClip(PdfPath path)

path PdfPath

IntersectClip(double, double, double, double)

Section titled “ IntersectClip(double, double, double, double)”

Restricts all further drawing to a rectangle, until the next restore.

public void IntersectClip(double x, double y, double width, double height)

x double

y double

width double

height double

Applies matrix before the current transform.

public void MultiplyTransform(PdfMatrix matrix)

matrix PdfMatrix

Restores the most recently saved state.

public void Restore()

InvalidOperationException

Nothing is saved.

RotateAtTransform(double, double, double)

Section titled “ RotateAtTransform(double, double, double)”

Rotates about (x, y), clockwise for positive angles.

public void RotateAtTransform(double degrees, double x, double y)

degrees double

x double

y double

Rotates about the origin, clockwise for positive angles.

public void RotateTransform(double degrees)

degrees double

Saves the graphics state: transform, clip, colors and pen settings. Dispose the result, or call Restore, to return to it.

public PdfGraphicsState Save()

PdfGraphicsState

Scales about the origin.

public void ScaleTransform(double sx, double sy)

sx double

sy double

Moves the origin.

public void TranslateTransform(double dx, double dy)

dx double

dy double