[Expand]General Information
[Expand]WinForms Controls
[Expand]ASP.NET Controls and MVC Extensions
[Expand]ASP.NET Bootstrap Controls
[Expand]ASP.NET Core Bootstrap Controls
[Expand]WPF Controls
[Expand]Xamarin Controls
[Expand]Windows 10 App Controls
[Collapse]Office File API
  Demo Application
  Redistribution and Deployment
 [Expand]Spreadsheet Document API
 [Expand]Word Processing Document API
 [Collapse]PDF Document API
   Getting Started
   Coordinate Systems
   Document Generation
  [Collapse]PDF Graphics
    Overview
    Creating PDF Graphics Context
    Drawing into Graphics Context
    Adding Graphics Content to a Page
    Adding Interactive Form Fields
  [Expand]Document Manipulation
  [Expand]Additional Content
  [Expand]Interactive Forms
  [Expand]Text Markup Annotations
  [Expand]Document Security
  [Expand]Examples
 [Expand]Excel Export Library
 [Expand]Snap Report API
 [Expand]Zip Compression and Archive API
 [Expand]Barcode Generation API
 [Expand]Unit Conversion API
 [Expand]API Reference
[Expand]Reporting
[Expand]Report and Dashboard Server
[Expand]Dashboard
[Expand]eXpressApp Framework
[Expand]CodeRush
[Expand]CodeRush Classic
[Expand]Cross-Platform Core Libraries
[Expand]Tools and Utilities
 End-User Documentation

Overview

The PDF graphics context that allow an application to draw on a page is represented by an instance of the PdfGraphics class. This class provides an API similar to the standard System.Drawing.Graphics class's API - you can draw on graphics context the same way as you do using the GDI+ API.

In addition to methods used to create an image (PdfGraphics.DrawImage), text (PdfGraphics.DrawString) and graphic elements in a document (e.g, PdfGraphics.DrawBezier), the PdfGraphics class also provides the following properties:

Member Description
PdfGraphics.ConvertImagesToJpeg Specifies whether to convert bitmap images to the Jpeg format reducing the size of the resulting PDF document.
PdfGraphics.JpegImageQuality Gets or sets the quality of Jpeg images in the resulting PDF file.
PdfGraphics.TextOrigin Specifies how to interpret a point passed to one of the DrawString overload methods that take a PointF object.
PdfGraphics.UseKerning Gets or sets a value which indicates whether kerning is used when drawing characters.

The PdfGraphics class uses a world coordinate system that models a particular graphics world. See the Coordinate Systems topic to learn more.

If you have an existing document and want to draw graphics content at the required position of the page, you need to convert page coordinates to world coordinates.

To transform page space coordinates to world coordinates:

  • Obtain a world Y coordinate by subtracting the page space Y value from the page crop box height, the X coordinate remains the same.
  • Draw graphics content (e.g. a text line) on the page by calling Draw methods of the PdfGraphics class using the converted point.

To add graphics content to a page, transform world coordinates to page space coordinates by calling one of the PdfGraphics.AddToPageForeground, PdfGraphics.AddToPageBackground or PdfDocumentProcessor.RenderNewPage overload methods. Since page unit of measurement is a point (1/72 of an inch), pass 72 as dpiX and dpiY values to one of these methods. See Adding Graphics Content to a Page for more details.

Example

This example shows how to draw graphics content on an existing page using the transformation from the page to world coordinates described above (see Coordinate Systems for more details).

Before drawing shapes, lines and other graphic content on a page, you need to create a PdfGraphics object. See Creating PDF Graphics Context for more information on how to create PdfGraphics.

Use PdfGraphics class methods (for example, PdfGraphics.DrawImage, PdfGraphics.DrawString, PdfGraphics.DrawPolygon, PdfGraphics.DrawRectangle, etc.) to draw into a PDF graphics context. See Drawing into Graphics Context for more information.

After creating graphics context and drawing on it, you need to add graphics content to a page. See Adding Graphics Content to a Page.

Is this topic helpful?​​​​​​​