Table of Contents

Class PdfCanvas

Namespace
MiPdf.Graphics
Assembly
MiPdf.dll

Writes page or XObject content: paths, colors, text, images. Coordinates are in PDF user space (points, origin at the bottom-left of the page). The content is stored when the canvas is disposed (or Flush() is called).

public sealed class PdfCanvas : IDisposable
Inheritance
PdfCanvas
Implements
Inherited Members
Extension Methods

Examples

using (var canvas = page.GetCanvas())
{
    canvas.DrawText("Hello", PdfStandardFont.Helvetica, 12, 72, 770);
    canvas.SetStrokeColor(PdfColor.Red).Rectangle(72, 700, 200, 50).Stroke();
}

Properties

Document

The document of Target.

public PdfDocument Document { get; }

Property Value

PdfDocument

IsTagged

True if this is a page canvas of a document with tagging enabled (see EnableTagging()).

public bool IsTagged { get; }

Property Value

bool

StructureParent

The structure element that layout elements attach their tags to (set by LayoutDocument).

public PdfStructureElement? StructureParent { get; set; }

Property Value

PdfStructureElement

Target

The page or XObject this canvas draws into.

public IResourceContainer Target { get; }

Property Value

IResourceContainer

Methods

Artifact(Action<PdfCanvas>, ArtifactType)

Draws the content produced by draw as an artifact.

public PdfCanvas Artifact(Action<PdfCanvas> draw, ArtifactType type = ArtifactType.None)

Parameters

draw Action<PdfCanvas>
type ArtifactType

Returns

PdfCanvas

BeginArtifact(ArtifactType)

Starts an artifact: content that is not part of the document's logical content (page numbers, running heads, decorative lines, backgrounds). Screen readers skip artifacts.

public PdfCanvas BeginArtifact(ArtifactType type = ArtifactType.None)

Parameters

type ArtifactType

Returns

PdfCanvas

BeginTag(PdfStructureElement)

Starts a marked-content sequence that belongs to element (BDC with MCID). Everything drawn until EndTag() becomes the content of that element.

public PdfCanvas BeginTag(PdfStructureElement element)

Parameters

element PdfStructureElement

Returns

PdfCanvas

BeginText()

Begins a text object (BT). Text operators are only valid between this and EndText().

public PdfCanvas BeginText()

Returns

PdfCanvas

Circle(double, double, double)

Appends a closed circle around (centerX, centerY) to the path.

public PdfCanvas Circle(double centerX, double centerY, double radius)

Parameters

centerX double
centerY double
radius double

Returns

PdfCanvas

Clip(bool)

Intersects the clipping path with the current path and ends the path.

public PdfCanvas Clip(bool evenOdd = false)

Parameters

evenOdd bool

Returns

PdfCanvas

ClosePath()

Closes the current subpath with a straight line to its start point (h).

public PdfCanvas ClosePath()

Returns

PdfCanvas

CurveTo(double, double, double, double, double, double)

Appends a cubic Bezier curve from the current point to (x3, y3) with the control points (x1, y1) and (x2, y2) (c).

public PdfCanvas CurveTo(double x1, double y1, double x2, double y2, double x3, double y3)

Parameters

x1 double
y1 double
x2 double
y2 double
x3 double
y3 double

Returns

PdfCanvas

Dispose()

Stores the content by calling Flush().

public void Dispose()

DrawImage(PdfImage, PdfRectangle)

Draws an image scaled to fit into box, keeping its aspect ratio and centering it.

public PdfCanvas DrawImage(PdfImage image, PdfRectangle box)

Parameters

image PdfImage
box PdfRectangle

Returns

PdfCanvas

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

Draws an image into the rectangle (x, y, width, height).

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

Parameters

image PdfImage
x double
y double
width double
height double

Returns

PdfCanvas

DrawLine(double, double, double, double, PdfColor?, double)

Draws a straight line from (x1, y1) to (x2, y2) with the given color (default: the current stroke color) and width in points. The graphics state is restored afterwards.

public PdfCanvas DrawLine(double x1, double y1, double x2, double y2, PdfColor? color = null, double lineWidth = 1)

Parameters

x1 double
y1 double
x2 double
y2 double
color PdfColor?
lineWidth double

Returns

PdfCanvas

DrawRectangle(double, double, double, double, PdfColor?, PdfColor?, double)

Draws a rectangle with an optional border and/or fill.

public PdfCanvas DrawRectangle(double x, double y, double width, double height, PdfColor? stroke = null, PdfColor? fill = null, double lineWidth = 1)

Parameters

x double
y double
width double
height double
stroke PdfColor?
fill PdfColor?
lineWidth double

Returns

PdfCanvas

DrawText(string, PdfFont, double, double, double, PdfColor?, TextAlignment, bool)

Draws a single line of text. (x, y) is the baseline point; the alignment determines whether x is the left edge, center or right edge of the text.

public PdfCanvas DrawText(string text, PdfFont font, double fontSize, double x, double y, PdfColor? color = null, TextAlignment alignment = TextAlignment.Left, bool kerning = false)

Parameters

text string
font PdfFont
fontSize double
x double
y double
color PdfColor?
alignment TextAlignment
kerning bool

Returns

PdfCanvas

DrawTextBox(string, PdfFont, double, PdfRectangle, TextAlignment, VerticalAlignment, PdfColor?, double)

Draws text wrapped into box with horizontal and vertical alignment. Line breaks ("\n") are honored; text that does not fit is clipped at the box. Justify stretches every line except the last one of a paragraph to the width of the box. Returns the canvas.

public PdfCanvas DrawTextBox(string text, PdfFont font, double fontSize, PdfRectangle box, TextAlignment alignment = TextAlignment.Left, VerticalAlignment verticalAlignment = VerticalAlignment.Top, PdfColor? color = null, double lineSpacing = 1.15)

Parameters

text string
font PdfFont
fontSize double
box PdfRectangle
alignment TextAlignment
verticalAlignment VerticalAlignment
color PdfColor?
lineSpacing double

Returns

PdfCanvas

DrawXObject(PdfFormXObject, double, double, double?, double?)

Draws a form XObject at (x, y), optionally scaled to the given size.

public PdfCanvas DrawXObject(PdfFormXObject xobject, double x, double y, double? width = null, double? height = null)

Parameters

xobject PdfFormXObject
x double
y double
width double?
height double?

Returns

PdfCanvas

Ellipse(double, double, double, double)

Adds an ellipse inscribed in the given rectangle.

public PdfCanvas Ellipse(double x, double y, double width, double height)

Parameters

x double
y double
width double
height double

Returns

PdfCanvas

EndArtifact()

Ends the artifact started with BeginArtifact(ArtifactType) (EMC).

public PdfCanvas EndArtifact()

Returns

PdfCanvas

EndPath()

Ends the current path without painting it (n).

public PdfCanvas EndPath()

Returns

PdfCanvas

EndTag()

Ends the marked-content sequence started with BeginTag(PdfStructureElement) (EMC).

public PdfCanvas EndTag()

Returns

PdfCanvas

EndText()

Ends the text object started with BeginText() (ET).

public PdfCanvas EndText()

Returns

PdfCanvas

Fill(bool)

Fills the current path with the fill color and ends it (f, or f* with the even-odd rule).

public PdfCanvas Fill(bool evenOdd = false)

Parameters

evenOdd bool

Returns

PdfCanvas

FillAndStroke(bool)

Fills, then strokes the current path and ends it (B, or B* with the even-odd rule).

public PdfCanvas FillAndStroke(bool evenOdd = false)

Parameters

evenOdd bool

Returns

PdfCanvas

Flush()

Stores the content in the page/XObject. Further drawing is not possible afterwards.

public void Flush()

GetContentBytes()

Returns the content written so far.

public byte[] GetContentBytes()

Returns

byte[]

LineTo(double, double)

Appends a straight line from the current point to (x, y) (l).

public PdfCanvas LineTo(double x, double y)

Parameters

x double
y double

Returns

PdfCanvas

MoveText(double, double)

Moves to the start of the next line offset by (tx, ty) (Td).

public PdfCanvas MoveText(double tx, double ty)

Parameters

tx double
ty double

Returns

PdfCanvas

MoveTo(double, double)

Begins a new subpath at (x, y) (m).

public PdfCanvas MoveTo(double x, double y)

Parameters

x double
y double

Returns

PdfCanvas

NewLine()

Moves to the start of the next line, using the current leading (T*).

public PdfCanvas NewLine()

Returns

PdfCanvas

Rectangle(PdfRectangle)

Appends the rectangle r to the path (re).

public PdfCanvas Rectangle(PdfRectangle r)

Parameters

r PdfRectangle

Returns

PdfCanvas

Rectangle(double, double, double, double)

Appends a rectangle with lower-left corner (x, y) to the path (re).

public PdfCanvas Rectangle(double x, double y, double width, double height)

Parameters

x double
y double
width double
height double

Returns

PdfCanvas

RestoreState()

Restores the graphics state saved by the matching SaveState() (Q), including the current font.

public PdfCanvas RestoreState()

Returns

PdfCanvas

Rotate(double, double, double)

Rotates counter-clockwise by degrees around (cx, cy).

public PdfCanvas Rotate(double degrees, double cx = 0, double cy = 0)

Parameters

degrees double
cx double
cy double

Returns

PdfCanvas

RoundedRectangle(double, double, double, double, double)

Appends a closed rectangle with rounded corners to the path. The radius is limited to half the smaller side; a radius of 0 or less gives a plain rectangle.

public PdfCanvas RoundedRectangle(double x, double y, double width, double height, double radius)

Parameters

x double
y double
width double
height double
radius double

Returns

PdfCanvas

SaveState()

Saves the current graphics state (q). Pair every call with RestoreState().

public PdfCanvas SaveState()

Returns

PdfCanvas

Scale(double, double)

Scales the coordinate system by sx horizontally and sy vertically (cm).

public PdfCanvas Scale(double sx, double sy)

Parameters

sx double
sy double

Returns

PdfCanvas

SetCharacterSpacing(double)

Sets extra spacing added after every character (Tc), in unscaled text space units.

public PdfCanvas SetCharacterSpacing(double spacing)

Parameters

spacing double

Returns

PdfCanvas

SetDashPattern(params double[])

Sets a dash pattern (d) starting at its beginning: every argument is a length in points, alternating dash and gap. SetDashPattern(3, 2) draws 3 on / 2 off. To start elsewhere in the pattern use SetDashPattern(double[], double).

public PdfCanvas SetDashPattern(params double[] pattern)

Parameters

pattern double[]

Returns

PdfCanvas

SetDashPattern(double[], double)

Sets a dash pattern (d) with a phase: pattern holds the alternating dash and gap lengths in points; phase is the distance into the pattern at which the dashing starts. Unlike SetDashPattern(3, 2), SetDashPattern(new[] { 3.0 }, 2) is the pattern [3] (3 on / 3 off) started 2 points into it.

public PdfCanvas SetDashPattern(double[] pattern, double phase)

Parameters

pattern double[]
phase double

Returns

PdfCanvas

SetFillColor(PdfColor)

Sets the color for filling paths and text (g, rg or k).

public PdfCanvas SetFillColor(PdfColor color)

Parameters

color PdfColor

Returns

PdfCanvas

SetFont(PdfFont, double)

Selects the font (Tf). Fonts are registered as page resources automatically.

public PdfCanvas SetFont(PdfFont font, double size)

Parameters

font PdfFont
size double

Returns

PdfCanvas

SetHorizontalScaling(double)

Sets the horizontal scaling of glyphs in percent (Tz); 100 is normal width.

public PdfCanvas SetHorizontalScaling(double percent)

Parameters

percent double

Returns

PdfCanvas

SetLeading(double)

Sets the leading (TL): the distance between baselines used by NewLine(), in unscaled text space units.

public PdfCanvas SetLeading(double leading)

Parameters

leading double

Returns

PdfCanvas

SetLineCap(LineCap)

Sets the line cap style (J).

public PdfCanvas SetLineCap(LineCap cap)

Parameters

cap LineCap

Returns

PdfCanvas

SetLineJoin(LineJoin)

Sets the line join style (j).

public PdfCanvas SetLineJoin(LineJoin join)

Parameters

join LineJoin

Returns

PdfCanvas

SetLineWidth(double)

Sets the line width in points for stroking (w).

public PdfCanvas SetLineWidth(double width)

Parameters

width double

Returns

PdfCanvas

SetMiterLimit(double)

Sets the miter limit (M): the maximum ratio of miter length to line width before a miter join is beveled.

public PdfCanvas SetMiterLimit(double limit)

Parameters

limit double

Returns

PdfCanvas

SetOpacity(double, double?, string?)

Sets constant opacity (0 = transparent, 1 = opaque) for fill and stroke operations.

public PdfCanvas SetOpacity(double fillOpacity, double? strokeOpacity = null, string? blendMode = null)

Parameters

fillOpacity double
strokeOpacity double?
blendMode string

Returns

PdfCanvas

SetSolidLine()

Resets the dash pattern to a solid line.

public PdfCanvas SetSolidLine()

Returns

PdfCanvas

SetStrokeColor(PdfColor)

Sets the color for stroking paths (G, RG or K).

public PdfCanvas SetStrokeColor(PdfColor color)

Parameters

color PdfColor

Returns

PdfCanvas

SetTextMatrix(double, double, double, double, double, double)

Sets the text matrix and the text line matrix to [a b c d e f] (Tm).

public PdfCanvas SetTextMatrix(double a, double b, double c, double d, double e, double f)

Parameters

a double
b double
c double
d double
e double
f double

Returns

PdfCanvas

SetTextPosition(double, double)

Sets the text matrix so that the next text starts at (x, y).

public PdfCanvas SetTextPosition(double x, double y)

Parameters

x double
y double

Returns

PdfCanvas

SetTextRenderingMode(TextRenderingMode)

Sets how glyphs are painted (Tr), e.g. Invisible for an OCR text layer.

public PdfCanvas SetTextRenderingMode(TextRenderingMode mode)

Parameters

mode TextRenderingMode

Returns

PdfCanvas

SetTextRise(double)

Sets the text rise (Ts): a vertical baseline shift, positive upwards (e.g. for superscripts).

public PdfCanvas SetTextRise(double rise)

Parameters

rise double

Returns

PdfCanvas

SetWordSpacing(double)

Word spacing (Tw). Note: only effective for single-byte fonts (standard fonts).

public PdfCanvas SetWordSpacing(double spacing)

Parameters

spacing double

Returns

PdfCanvas

ShowText(string)

Shows text with the current font (Tj).

public PdfCanvas ShowText(string text)

Parameters

text string

Returns

PdfCanvas

ShowTextWithWordSpacing(string, double, bool)

Shows text where each space is widened by extraWordSpace points (TJ). Works with all fonts. With kerning the font's pair kerning is applied.

public PdfCanvas ShowTextWithWordSpacing(string text, double extraWordSpace, bool kerning = false)

Parameters

text string
extraWordSpace double
kerning bool

Returns

PdfCanvas

Stroke()

Strokes the current path with the stroke color and ends it (S).

public PdfCanvas Stroke()

Returns

PdfCanvas

Tag(PdfStructureElement, Action<PdfCanvas>)

Draws the content produced by draw as content of element.

public PdfCanvas Tag(PdfStructureElement element, Action<PdfCanvas> draw)

Parameters

element PdfStructureElement
draw Action<PdfCanvas>

Returns

PdfCanvas

Transform(double, double, double, double, double, double)

Concatenates a transformation matrix (cm).

public PdfCanvas Transform(double a, double b, double c, double d, double e, double f)

Parameters

a double
b double
c double
d double
e double
f double

Returns

PdfCanvas

Translate(double, double)

Moves the origin of the coordinate system by (x, y) points (cm).

public PdfCanvas Translate(double x, double y)

Parameters

x double
y double

Returns

PdfCanvas

WriteRaw(string)

Writes raw content stream operators.

public PdfCanvas WriteRaw(string operators)

Parameters

operators string

Returns

PdfCanvas