PdfGraphics class
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 : IDisposableInheritance
Implements
Remarks
Section titled “Remarks”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.
Properties
Section titled “Properties”BlendMode
Section titled “ BlendMode”How further drawing blends with what is under it. Saved and restored with the graphics state.
public PdfBlendMode BlendMode { get; set; }Property Value
Section titled “Property Value”The form being drawn into, or null when drawing on a page.
public PdfForm? Form { get; }Property Value
Section titled “Property Value”Height
Section titled “ Height”Height of the drawing area in points.
public double Height { get; }Property Value
Section titled “Property Value”The page being drawn on, or null when drawing into a PdfForm.
public PdfPage? Page { get; }Property Value
Section titled “Property Value”Transform
Section titled “ Transform”The current transform from user coordinates to canvas coordinates.
public PdfMatrix Transform { get; }Property Value
Section titled “Property Value”Width of the drawing area in points: the media box, or the form’s width.
public double Width { get; }Property Value
Section titled “Property Value”Methods
Section titled “Methods”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)Parameters
Section titled “Parameters”x double
y double
width double
height double
uri Uri
Returns
Section titled “Returns”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)Parameters
Section titled “Parameters”x double
y double
width double
height double
target PdfPage
top double?
Returns
Section titled “Returns”Dispose()
Section titled “ Dispose()”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)Parameters
Section titled “Parameters”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)Parameters
Section titled “Parameters”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)Parameters
Section titled “Parameters”pen PdfPen?
brush PdfBrush?
x double
y double
width double
height double
DrawForm(PdfForm, double, double)
Section titled “ DrawForm(PdfForm, double, double)”Draws a form at its own size, its top left corner at (x, y).
public void DrawForm(PdfForm form, double x, double y)Parameters
Section titled “Parameters”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)Parameters
Section titled “Parameters”form PdfForm
x double
y double
width double
height double
Exceptions
Section titled “Exceptions”The form belongs to another document.
DrawImage(PdfImage, double, double)
Section titled “ DrawImage(PdfImage, double, double)”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)Parameters
Section titled “Parameters”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)Parameters
Section titled “Parameters”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)Parameters
Section titled “Parameters”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).
Exceptions
Section titled “Exceptions”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)Parameters
Section titled “Parameters”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.
Examples
Section titled “Examples”// 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)Parameters
Section titled “Parameters”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)Parameters
Section titled “Parameters”pen PdfPen
x1 double
y1 double
x2 double
y2 double
DrawLines(PdfPen, IEnumerable<PdfPoint>)
Section titled “ DrawLines(PdfPen, IEnumerable<PdfPoint>)”Draws connected straight lines through the given points.
public void DrawLines(PdfPen pen, IEnumerable<PdfPoint> points)Parameters
Section titled “Parameters”pen PdfPen
points IEnumerable<PdfPoint>
DrawPath(PdfPen?, PdfBrush?, PdfPath)
Section titled “ DrawPath(PdfPen?, PdfBrush?, PdfPath)”Strokes path with pen and fills it with brush; either may be null.
public void DrawPath(PdfPen? pen, PdfBrush? brush, PdfPath path)Parameters
Section titled “Parameters”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)Parameters
Section titled “Parameters”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)Parameters
Section titled “Parameters”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)Parameters
Section titled “Parameters”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)Parameters
Section titled “Parameters”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)Parameters
Section titled “Parameters”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)Parameters
Section titled “Parameters”text string
font PdfFont
size double
brush PdfBrush
x double
y double
Exceptions
Section titled “Exceptions”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)Parameters
Section titled “Parameters”text string
font PdfFont
size double
pen PdfPen?
brush PdfBrush?
x double
y double
Exceptions
Section titled “Exceptions”The font has no glyph for a character of the text.
DrawSvg(PdfSvg, double, double)
Section titled “ DrawSvg(PdfSvg, double, double)”Draws an SVG drawing at its own size, its top left corner at (x, y).
public void DrawSvg(PdfSvg svg, double x, double y)Parameters
Section titled “Parameters”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)Parameters
Section titled “Parameters”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)Parameters
Section titled “Parameters”paragraph PdfParagraph
x double
y double
width double
Returns
Section titled “Returns”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)Parameters
Section titled “Parameters”text string
style PdfTextStyle
x double
y double
width double
alignment PdfTextAlignment
Returns
Section titled “Returns”DrawText(PdfTextLayout, double, double)
Section titled “ DrawText(PdfTextLayout, double, double)”Draws a laid out paragraph with its top left corner at (x, y).
public void DrawText(PdfTextLayout layout, double x, double y)Parameters
Section titled “Parameters”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)Parameters
Section titled “Parameters”layout PdfTextLayout
x double
y double
first int
count int
IntersectClip(PdfPath)
Section titled “ IntersectClip(PdfPath)”Restricts all further drawing to the inside of path, until the next restore.
public void IntersectClip(PdfPath path)Parameters
Section titled “Parameters”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)Parameters
Section titled “Parameters”x double
y double
width double
height double
MultiplyTransform(PdfMatrix)
Section titled “ MultiplyTransform(PdfMatrix)”Applies matrix before the current transform.
public void MultiplyTransform(PdfMatrix matrix)Parameters
Section titled “Parameters”matrix PdfMatrix
Restore()
Section titled “ Restore()”Restores the most recently saved state.
public void Restore()Exceptions
Section titled “Exceptions”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)Parameters
Section titled “Parameters”degrees double
x double
y double
RotateTransform(double)
Section titled “ RotateTransform(double)”Rotates about the origin, clockwise for positive angles.
public void RotateTransform(double degrees)Parameters
Section titled “Parameters”degrees double
Save()
Section titled “ Save()”Saves the graphics state: transform, clip, colors and pen settings. Dispose the result, or call Restore, to return to it.
public PdfGraphicsState Save()Returns
Section titled “Returns”ScaleTransform(double, double)
Section titled “ ScaleTransform(double, double)”Scales about the origin.
public void ScaleTransform(double sx, double sy)Parameters
Section titled “Parameters”sx double
sy double
TranslateTransform(double, double)
Section titled “ TranslateTransform(double, double)”Moves the origin.
public void TranslateTransform(double dx, double dy)Parameters
Section titled “Parameters”dx double
dy double
Used in
Section titled “Used in”- PDF coordinates and units in PdfNetStack
- Get started with PdfNetStack
- PDF links and annotations in C# and .NET
- Compress PDF files in C# and .NET
- Draw on PDF pages in C# and .NET
- Extract text from PDF files in C#
- Fonts and text in PDF with C# and .NET
- Fill and create PDF forms in C# and .NET
- CSS support in PdfNetStack.Html
- Add images to PDF in C# and .NET
- Create PDF/A files in C# and .NET
- Add page numbers to a PDF in C#
- Add a watermark to a PDF in C#
- Write Arabic text in a PDF in C#
- Downsample images in a PDF in C#
- Create a Factur-X invoice PDF in C#
- Reduce PDF size for email in C#
- Convert a TIFF to PDF in C#