Skip to main content
A newer version of this page is available. .

Overview

  • 4 minutes to read

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 PdfGraphics.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.

using (PdfDocumentProcessor processor = new PdfDocumentProcessor()) { 
    // Load a document.  
    processor.LoadDocument("..\\..\\Document.pdf"); 
    // Create graphics context.  
    using (PdfGraphics graphics = processor.CreateGraphics()) { 
        // Create a point in the page coordinate system.      
        PdfPoint sourcePoint = new PdfPoint(10, 100); 
        // Convert a source point from the page coordinate system  
        // to world coordinate system.  
        PointF convertedPoint = new PointF((float)sourcePoint.X, 
            (float)(processor.Document.Pages[0].CropBox.Height  
            - sourcePoint.Y)); 
        // Draw a text line on the page using the converted point. 
        SolidBrush black = (SolidBrush)Brushes.Black; 
        using (Font font1 =  
            new Font("Times New Roman", 32, FontStyle.Bold)) { 
            graphics.DrawString("PDF Document Processor", font1, 
                black, convertedPoint); 
        } 
    // Add graphics content to the page foreground. 
    graphics.AddToPageForeground(processor.Document.Pages[0], 
            72, 72); 
    } 
    // Save the result document. 
    processor.SaveDocument("..\\..\\Result.pdf"); 
}