Class PdfCanvas
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
IsTagged
True if this is a page canvas of a document with tagging enabled (see EnableTagging()).
public bool IsTagged { get; }
Property Value
StructureParent
The structure element that layout elements attach their tags to (set by LayoutDocument).
public PdfStructureElement? StructureParent { get; set; }
Property Value
Target
The page or XObject this canvas draws into.
public IResourceContainer Target { get; }
Property Value
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
drawAction<PdfCanvas>typeArtifactType
Returns
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
typeArtifactType
Returns
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
elementPdfStructureElement
Returns
BeginText()
Begins a text object (BT). Text operators are only valid between this and EndText().
public PdfCanvas BeginText()
Returns
Circle(double, double, double)
Appends a closed circle around (centerX, centerY) to the path.
public PdfCanvas Circle(double centerX, double centerY, double radius)
Parameters
Returns
Clip(bool)
Intersects the clipping path with the current path and ends the path.
public PdfCanvas Clip(bool evenOdd = false)
Parameters
evenOddbool
Returns
ClosePath()
Closes the current subpath with a straight line to its start point (h).
public PdfCanvas ClosePath()
Returns
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
Returns
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
imagePdfImageboxPdfRectangle
Returns
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
Returns
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
Returns
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
Returns
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
textstringfontPdfFontfontSizedoublexdoubleydoublecolorPdfColor?alignmentTextAlignmentkerningbool
Returns
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
textstringfontPdfFontfontSizedoubleboxPdfRectanglealignmentTextAlignmentverticalAlignmentVerticalAlignmentcolorPdfColor?lineSpacingdouble
Returns
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
xobjectPdfFormXObjectxdoubleydoublewidthdouble?heightdouble?
Returns
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
Returns
EndArtifact()
Ends the artifact started with BeginArtifact(ArtifactType) (EMC).
public PdfCanvas EndArtifact()
Returns
EndPath()
Ends the current path without painting it (n).
public PdfCanvas EndPath()
Returns
EndTag()
Ends the marked-content sequence started with BeginTag(PdfStructureElement) (EMC).
public PdfCanvas EndTag()
Returns
EndText()
Ends the text object started with BeginText() (ET).
public PdfCanvas EndText()
Returns
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
evenOddbool
Returns
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
evenOddbool
Returns
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
Returns
MoveText(double, double)
Moves to the start of the next line offset by (tx, ty) (Td).
public PdfCanvas MoveText(double tx, double ty)
Parameters
Returns
MoveTo(double, double)
Begins a new subpath at (x, y) (m).
public PdfCanvas MoveTo(double x, double y)
Parameters
Returns
NewLine()
Moves to the start of the next line, using the current leading (T*).
public PdfCanvas NewLine()
Returns
Rectangle(PdfRectangle)
Appends the rectangle r to the path (re).
public PdfCanvas Rectangle(PdfRectangle r)
Parameters
Returns
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
Returns
RestoreState()
Restores the graphics state saved by the matching SaveState() (Q), including the current font.
public PdfCanvas RestoreState()
Returns
Rotate(double, double, double)
Rotates counter-clockwise by degrees around (cx, cy).
public PdfCanvas Rotate(double degrees, double cx = 0, double cy = 0)
Parameters
Returns
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
Returns
SaveState()
Saves the current graphics state (q). Pair every call with RestoreState().
public PdfCanvas SaveState()
Returns
Scale(double, double)
Scales the coordinate system by sx horizontally and sy vertically (cm).
public PdfCanvas Scale(double sx, double sy)
Parameters
Returns
SetCharacterSpacing(double)
Sets extra spacing added after every character (Tc), in unscaled text space units.
public PdfCanvas SetCharacterSpacing(double spacing)
Parameters
spacingdouble
Returns
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
patterndouble[]
Returns
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
Returns
SetFillColor(PdfColor)
Sets the color for filling paths and text (g, rg or k).
public PdfCanvas SetFillColor(PdfColor color)
Parameters
colorPdfColor
Returns
SetFont(PdfFont, double)
Selects the font (Tf). Fonts are registered as page resources automatically.
public PdfCanvas SetFont(PdfFont font, double size)
Parameters
Returns
SetHorizontalScaling(double)
Sets the horizontal scaling of glyphs in percent (Tz); 100 is normal width.
public PdfCanvas SetHorizontalScaling(double percent)
Parameters
percentdouble
Returns
SetLeading(double)
Sets the leading (TL): the distance between baselines used by NewLine(), in unscaled text space units.
public PdfCanvas SetLeading(double leading)
Parameters
leadingdouble
Returns
SetLineCap(LineCap)
Sets the line cap style (J).
public PdfCanvas SetLineCap(LineCap cap)
Parameters
capLineCap
Returns
SetLineJoin(LineJoin)
Sets the line join style (j).
public PdfCanvas SetLineJoin(LineJoin join)
Parameters
joinLineJoin
Returns
SetLineWidth(double)
Sets the line width in points for stroking (w).
public PdfCanvas SetLineWidth(double width)
Parameters
widthdouble
Returns
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
limitdouble
Returns
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
Returns
SetSolidLine()
Resets the dash pattern to a solid line.
public PdfCanvas SetSolidLine()
Returns
SetStrokeColor(PdfColor)
Sets the color for stroking paths (G, RG or K).
public PdfCanvas SetStrokeColor(PdfColor color)
Parameters
colorPdfColor
Returns
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
Returns
SetTextPosition(double, double)
Sets the text matrix so that the next text starts at (x, y).
public PdfCanvas SetTextPosition(double x, double y)
Parameters
Returns
SetTextRenderingMode(TextRenderingMode)
Sets how glyphs are painted (Tr), e.g. Invisible for an OCR text layer.
public PdfCanvas SetTextRenderingMode(TextRenderingMode mode)
Parameters
modeTextRenderingMode
Returns
SetTextRise(double)
Sets the text rise (Ts): a vertical baseline shift, positive upwards (e.g. for superscripts).
public PdfCanvas SetTextRise(double rise)
Parameters
risedouble
Returns
SetWordSpacing(double)
Word spacing (Tw). Note: only effective for single-byte fonts (standard fonts).
public PdfCanvas SetWordSpacing(double spacing)
Parameters
spacingdouble
Returns
ShowText(string)
Shows text with the current font (Tj).
public PdfCanvas ShowText(string text)
Parameters
textstring
Returns
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
Returns
Stroke()
Strokes the current path with the stroke color and ends it (S).
public PdfCanvas Stroke()
Returns
Tag(PdfStructureElement, Action<PdfCanvas>)
Draws the content produced by draw as content of element.
public PdfCanvas Tag(PdfStructureElement element, Action<PdfCanvas> draw)
Parameters
elementPdfStructureElementdrawAction<PdfCanvas>
Returns
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
Returns
Translate(double, double)
Moves the origin of the coordinate system by (x, y) points (cm).
public PdfCanvas Translate(double x, double y)
Parameters
Returns
WriteRaw(string)
Writes raw content stream operators.
public PdfCanvas WriteRaw(string operators)
Parameters
operatorsstring