Back to Devexpress

PDF Graphics API

officefileapi-119009-pdf-document-api-pdf-graphics.md

latest32.1 KB
Original Source

PDF Graphics API

  • Nov 07, 2025
  • 15 minutes to read

The PDF Document API allows you to draw graphics content on a new or existing PDF page. Use the PdfGraphics class to draw graphics content.

To create a PdfGraphics object, call one of the PdfDocumentProcessor.CreateGraphicsPageSystem methods. Each method creates a graphics context based on a specific coordinate system (page, user, or world).

Note

Add a reference to the DevExpress.Pdf.Drawing.v25.2 assembly to use the PdfGraphics class.

The following graphics content is available:

  • Page content
  • Text
  • Image
  • Shape (rectangle, ellipse, polygon, line, Bezier curve, path)
  • Form Field
  • Link

Draw Graphics Content on a Page

Call the following methods to draw graphics content to a page:

PdfGraphics.AddToPageForeground, PdfGraphics.AddToPageBackgroundThese methods allow you to draw content on an existing page.PdfDocumentProcessor.RenderNewPageDraws content on a new page.

Draw Page Content

Call the DrawPageContent method to draw content.

The following code snippet scales source content of two document pages and draws these pages in a landscape page.

csharp
using DevExpress.Pdf;
using System.Drawing;
//...

using (PdfDocumentProcessor processor = new PdfDocumentProcessor()) {
    processor.CreateEmptyDocument();
    PdfPage page = processor.AddNewPage(PdfPaperSize.A4Rotated);
    using (PdfDocumentProcessor processor2 = new PdfDocumentProcessor()) {
        processor2.LoadDocument(@"FirstLook.pdf");

        using (PdfGraphics graphics = processor.CreateGraphicsWorldSystem()) {
            graphics.SaveGraphicsState();

            // Obtain the first document page's boundaries.
            PdfRectangle clip = processor2.Document.Pages[0].CropBox;

            // Resize the source page content to fit half of the target page.
            graphics.ScaleTransform((float)((page.CropBox.Width / 2) / clip.Width), (float)((page.CropBox.Width / 2) / clip.Width));

            // Draw the content of the first document page.
            graphics.DrawPageContent(processor2.Document.Pages[0]);
            graphics.RestoreGraphicsState();

            // Define the position to insert the second page content to the target page.
            graphics.SaveGraphicsState();
            graphics.TranslateTransform((float)(page.CropBox.Width / 2), 0);

            // Obtain the second document page's boundaries.
            clip = processor2.Document.Pages[1].CropBox;

            // Resize the source page content to fit half of the target page.
            graphics.ScaleTransform((float)(page.CropBox.Width / clip.Width / 2), (float)(page.CropBox.Width / clip.Width / 2));

            // Draw the content of the second document page.
            graphics.DrawPageContent(processor2.Document.Pages[1]);
            graphics.RestoreGraphicsState();

            // Add graphics content to the target page.
            graphics.AddToPageForeground(page);
        }
        processor.SaveDocument("out2.pdf");
    }
}
vb
Imports DevExpress.Pdf
Imports System.Drawing
'...

Using processor As New PdfDocumentProcessor()
    processor.CreateEmptyDocument()
    Dim page As PdfPage = processor.AddNewPage(PdfPaperSize.A4Rotated)
    Using processor2 As New PdfDocumentProcessor()
        processor2.LoadDocument("FirstLook.pdf")

        Using graphics As PdfGraphics = processor.CreateGraphicsWorldSystem()
            graphics.SaveGraphicsState()

            ' Obtain the first document page's boundaries.
            Dim clip As PdfRectangle = processor2.Document.Pages(0).CropBox

            ' Resize the source page content to fit half of the target page.
            graphics.ScaleTransform(CSng((page.CropBox.Width \ 2) \ clip.Width), CSng((page.CropBox.Width \ 2) \ clip.Width))

            ' Draw the content of the first document page.
            graphics.DrawPageContent(processor2.Document.Pages(0))
            graphics.RestoreGraphicsState()

            ' Define the position to insert the second page content to the target page.
            graphics.SaveGraphicsState()
            graphics.TranslateTransform(CSng(page.CropBox.Width \ 2), 0)

            ' Obtain the second document page's boundaries.
            clip = processor2.Document.Pages(1).CropBox

            ' Resize the source page content to fit half of the target page.
            graphics.ScaleTransform(CSng(page.CropBox.Width \ clip.Width \ 2), CSng(page.CropBox.Width \ clip.Width \ 2))

            ' Draw the content of the second document page.
            graphics.DrawPageContent(processor2.Document.Pages(1))
            graphics.RestoreGraphicsState()

            ' Add graphics content to the target page.
            graphics.AddToPageForeground(page)
        End Using
        processor.SaveDocument("out2.pdf")
    End Using
End Using

Draw Text

Call the DrawString method to add text at the specified page area or point. You can specify the text font, brush, and format parameters.

Use the MeasureString method to calculate the size of the drawn text and a point where you can draw text.

The following code snippet draws text in the center of an empty page:

csharp
using DevExpress.Pdf;
using DevExpress.Drawing;
//...

using (var processor = new PdfDocumentProcessor()) {
  processor.CreateEmptyDocument();
  using (PdfGraphics graphics = processor.CreateGraphicsWorldSystem()) {

    // Obtain the first document page
    PdfPage page = processor.AddNewPage(PdfPaperSize.A4);
    PdfRectangle pageSize = page.CropBox;

    // Specify text to draw
    string text = "PDF Document API";
    using (var textBrush = new DXSolidBrush(Color.FromArgb(255, Color.DarkOrange))) {

        DXFont font = new DXFont("Segoe UI", 20, DXFontStyle.Regular);
        // Calculate text size
        SizeF textSize = graphics.MeasureString(text, font, new PdfStringFormat());

        // Calculate a point where to draw text
        PointF textPoint =
            new PointF((float)((pageSize.Width - textSize.Width) / 2), (float)((pageSize.Height - textSize.Height) / 2));

        // Draw text at the calculated point
        graphics.DrawString(text, font, textBrush, textPoint);

        // Add graphics content to the page foreground
        graphics.AddToPageForeground(page);
      }
  }
  processor.SaveDocument("result.pdf");
}
vb
Imports DevExpress.Pdf
Imports DevExpress.Drawing
'...

Using processor = New PdfDocumentProcessor()
  processor.CreateEmptyDocument()

  Using graphics As PdfGraphics = processor.CreateGraphicsWorldSystem()
    ' Obtain the first document page
    Dim page As PdfPage = processor.AddNewPage(PdfPaperSize.A4)
    Dim pageSize As PdfRectangle = page.CropBox

    ' Specify text to draw
    Dim text As String = "PDF Document API"
    Using textBrush As New DXSolidBrush(Color.FromArgb(255, Color.DarkOrange))
      Dim font As DXFont = New DXFont("Segoe UI", 20, DXFontStyle.Regular)
        ' Calculate text size
        Dim textSize As SizeF = graphics.MeasureString(text, font, New PdfStringFormat(), 72, 72)

        ' Calculate a point where to draw text
        Dim textPoint As New PointF(CSng((pageSize.Width - textSize.Width) \ 2), CSng((pageSize.Height - textSize.Height) \ 2))

        ' Draw text at the calculated point
        graphics.DrawString(text, font, textBrush, textPoint)

        ' Add graphics content to the page foreground
        graphics.AddToPageForeground(page)
    End Using
  End Using
  processor.SaveDocument("result.pdf")
End Using

Draw Image

Call the DrawImage method to add an image to a page.

This method renders an image with its original resolution (DPI). The following formats are available:

  • BMP
  • JPEG
  • PNG
  • EMF
  • EMF+
  • TIFF
  • GIF
  • SVG

If you embed a multi-page TIFF image into the document, the PdfGraphics instance draws the active frame only (the default active frame is the first frame). Use the Image.SelectActiveFrame method to select a frame to use. The compression retains for TIFF images with CCITT T.4 or CCITT T.6 compression.

You can use the following properties to reduce the image size and the size of the resulting PDF:

  • ConvertImagesToJpeg - Specifies whether to convert bitmap images into JPEG to reduce the size of the resulting PDF file.
  • JpegImageQuality - Gets or sets the quality of JPEG images in the resulting PDF file.

You can also call the PdfDocumentProcessor.OptimizeDocument method to optimize PDF document images. The method parameter specifies image compression settings.

Tip

The PdfDocumentProcessor caches image data used as the DrawImage method parameter. If you need to draw the same image on multiple pages, you can reuse image data to improve the application performance and reduce the resulting file size.

To draw an image on the PDF page, use one of the following methods:

PdfGraphics.AddToPageForeground, PdfGraphics.AddToPageBackgroundThese methods allow you to draw content on an existing page.PdfDocumentProcessor.RenderNewPageDraws content on a new page.

The following code snippet draws an image in the page center:

csharp
using DevExpress.Pdf;
using System.Drawing;

using (var processor = new PdfDocumentProcessor()) {
    processor.LoadDocument("Documents//Document.pdf");

    using (PdfGraphics graphics = processor.CreateGraphicsWorldSystem()) {
        // Obtain the first document page
        PdfPage page = processor.Document.Pages[0];

        // Specify an image to draw
        string sourceFile = "Documents//DevExpress.png";
        DXImage image = DXImage.FromStream(new FileStream(sourceFile, FileMode.Open));

        // Calculate the image position
        PdfRectangle rect = page.CropBox;
        var point = new PointF((float)rect.Center.X-image.Width/2, (float)rect.Center.Y);

        // Draw an image into the calculated area
        graphics.DrawImage(image, point);

        // Add graphics content to the page foreground
        graphics.AddToPageForeground(page);
    }
    processor.SaveDocument("result.pdf");
}
vb
Imports DevExpress.Pdf
Imports System.Drawing

Using processor = New PdfDocumentProcessor()
  processor.LoadDocument("Documents//Document.pdf")

  Using graphics As PdfGraphics = processor.CreateGraphicsWorldSystem()
    ' Obtain the first document page
    Dim page As PdfPage = processor.Document.Pages(0)

      ' Specify an image to draw
      Dim sourceFile As String = "Documents//DevExpress.png"
      Dim image As DXImage = DXImage.FromStream(New FileStream(sourceFile, FileMode.Open))
      ' Calculate the image position
      Dim rect As PdfRectangle = page.CropBox
      Dim point = New PointF(CSng(rect.Center.X)-image.Width\2, CSng(rect.Center.Y))

      ' Draw an image into the calculated area
      graphics.DrawImage(image, point)

      ' Add graphics content to the page foreground
      graphics.AddToPageForeground(page)
  End Using
  processor.SaveDocument("result.pdf")
End Using

Draw Shapes

The table below lists supported shape types and API used to draw and fill each type.

Shape TypeDraw ShapeFill Shape
RectangleDrawRectangleFillRectangle
EllipseDrawEllipseFillEllipse
PolygonDrawPolygonFillPolygon
LineDrawLine
DrawLinesNo method
Bezier CurveDrawBezier
DrawBeziersNo method
PathDrawPathFillPath

The following code snippet draws a rectangle in the specified area of the document page.

csharp
using DevExpress.Pdf;
using DevExpress.Drawing;
//...

using (PdfDocumentProcessor processor = new PdfDocumentProcessor()) {
    processor.CreateEmptyDocument();
    PdfPage page = processor.AddNewPage(PdfPaperSize.A4);
    using (PdfGraphics graphics = processor.CreateGraphicsPageSystem()) {

        // Draw a rectangle.
        using (var pen = new DXPen(Color.Red, 5))
            graphics.DrawRectangle(pen, new RectangleF(50, 50, 500, 300));

        // Add graphics content to the document page.
        graphics.AddToPageForeground(page);
    }
    processor.SaveDocument("out2.pdf");
}
Process.Start("out.pdf");
vb
Imports DevExpress.Pdf
Imports System.Drawing
'...

Using processor As New PdfDocumentProcessor()
    processor.CreateEmptyDocument()
    Dim page As PdfPage = processor.AddNewPage(PdfPaperSize.A4)
    Using graphics As PdfGraphics = processor.CreateGraphicsPageSystem()
        ' Draw a rectangle.
        Using pen As New DXPen(Color.Red, 5)
            graphics.DrawRectangle(pen, New RectangleF(50, 50, 500, 300))
        End Using

        ' Add graphics content to the document page.
        graphics.AddToPageForeground(page)
    End Using
    processor.SaveDocument("out2.pdf")
End Using
Process.Start("out.pdf")

Draw a Form Field

Call the AddFormField to draw a form field. The table below lists available form fields and API used to create each type. Pass the required class instance as the AddFormField method parameter.

Form FieldClassMethod
Check BoxPdfGraphicsAcroFormCheckBoxFieldNo method
Combo BoxPdfGraphicsAcroFormComboBoxFieldPdfGraphicsAcroFormField.CreateComboBox
List BoxPdfGraphicsAcroFormListBoxFieldPdfGraphicsAcroFormField.CreateListBox
Radio GroupPdfGraphicsAcroFormRadioGroupFieldPdfGraphicsAcroFormField.CreateRadioGroup
SignaturePdfGraphicsAcroFormSignatureFieldPdfGraphicsAcroFormField.CreateSignature
Text BoxPdfGraphicsAcroFormTextBoxFieldPdfGraphicsAcroFormField.CreateTextBox

The following code snippet uses PDF Graphics API to add interactive form fields – text box and radio button group.

View Example

csharp
using DevExpress.Pdf;
using System.Drawing;

using (var processor = new PdfDocumentProcessor()) {
    // Create an empty document.
    processor.CreateEmptyDocument("..\\..\\Result.pdf");

    // Create graphics and draw form fields.
    using (PdfGraphics graphics = processor.CreateGraphicsPageSystem()) {
        DrawFormFields(graphics);

        // Render a page with graphics.
        processor.RenderNewPage(PdfPaperSize.Letter, graphics);
    }
}

static void DrawFormFields(PdfGraphics graphics){
    // Create a text box field and specify its location on the page.
    var textBox =
         new PdfGraphicsAcroFormTextBoxField("text box", new RectangleF(30, 10, 200, 30));

    // Specify text and appearance parameters.
    textBox.Text = "Text Box";
    textBox.Appearance.FontSize = 12;
    textBox.Appearance.BackgroundColor = Color.AliceBlue;

    // Add the text box field to graphics.
    graphics.AddFormField(textBox);

    // Create a radio group field.
    var radioGroup =
         new PdfGraphicsAcroFormRadioGroupField("First Group");

    // Add the first radio button to the group and specify its location.
    radioGroup.AddButton("button1", new RectangleF(30, 60, 20, 20));

    // Add the second radio button to the group.
    radioGroup.AddButton("button2", new RectangleF(30, 90, 20, 20));

    // Specify radio group selected index
    radioGroup.SelectedIndex = 0;

    // Specify appearance options:
    radioGroup.Appearance.BorderAppearance =
         new PdfGraphicsAcroFormBorderAppearance()
        {
            Color = Color.Red,
            Width = 3
        };

    // Add the radio group field to graphics.
    graphics.AddFormField(radioGroup);
}
vb
Imports DevExpress.Pdf
Imports System.Drawing

Shared Sub Main(ByVal args() As String)
    Using processor As New PdfDocumentProcessor()

        ' Create an empty document.
        processor.CreateEmptyDocument("..\..\Result.pdf")

        ' Create graphics and draw form fields.
        Using graphics As PdfGraphics = processor.CreateGraphicsPageSystem()
            DrawFormFields(graphics)

            ' Render a page with graphics.
            processor.RenderNewPage(PdfPaperSize.Letter, graphics)
        End Using
    End Using
End Sub

Private Shared Sub DrawFormFields(ByVal graphics As PdfGraphics)

    ' Create a text box field and specify its location on the page.
    Dim textBox As New PdfGraphicsAcroFormTextBoxField("text box", New RectangleF(30, 10, 200, 30))

    ' Specify text and appearance parameters.
    textBox.Text = "Text Box"
    textBox.Appearance.FontSize = 12
    textBox.Appearance.BackgroundColor = Color.AliceBlue

    ' Add the text box field to graphics.
    graphics.AddFormField(textBox)

    ' Create a radio group field.
    Dim radioGroup As New PdfGraphicsAcroFormRadioGroupField("First Group")

    ' Add the first radio button to the group and specify its location.
    radioGroup.AddButton("button1", New RectangleF(30, 60, 20, 20))

    ' Add the second radio button to the group.
    radioGroup.AddButton("button2", New RectangleF(30, 90, 20, 20))

    ' Specify radio group selected index,
    radioGroup.SelectedIndex = 0

    ' Specify appearance parameters
    radioGroup.Appearance.BorderAppearance = New PdfGraphicsAcroFormBorderAppearance() With {.Color = Color.Red, .Width = 3}

    ' Add the radio group field to graphics.
    graphics.AddFormField(radioGroup)
End Sub

Use the AddLinkToPage or AddLinkToUri method to create a link to a URI or document page, respectively.

This example creates a document with graphics, add a hyperlink and URI to a page.

csharp
using DevExpress.Pdf;
using System;
using System.Drawing;
//...
static void Main(string[] args) {
    using (PdfDocumentProcessor processor = new PdfDocumentProcessor()) {
        // Create an empty document.
        processor.CreateEmptyDocument("..\\..\\Result.pdf");

        // Create and draw PDF graphics.
        using (PdfGraphics graph = processor.CreateGraphicsPageSystem()) {

        DrawGraphics(graph);

        // Create a link to a page
        graph.AddLinkToPage(new Rectangle(180, 160, 480, 30), 1, 168, 230);

        // Create a link to URI
        graph.AddLinkToUri(new Rectangle(110, 350, 180, 15), new Uri("https://www.devexpress.com"));

        // Render a page with graphics.
        processor.RenderNewPage(PdfPaperSize.Letter, graph);
        }
    }
}

static void DrawGraphics(PdfGraphics graph) {

    // Draw text lines on the page.
    SolidBrush black = (SolidBrush)Brushes.Black;

    DXFont font1 = new DXFont("Times New Roman", 32, DXFontStyle.Bold));
    graph.DrawString("PDF Document Processor", font1, black, 180, 150);

    DXFont font2 = new DXFont("Arial", 20));
    graph.DrawString("Display, Print and Export PDF Documents", font2, black, 168, 230);

    DXFont font3 = new DXFont("Arial", 10));
    graph.DrawString("The PDF Document Processor is a non-visual component " +
                        "that provides the application programming interface of the PDF Viewer.", font3, black, 16, 300);
    graph.DrawString("Learn more at", font3, black, 20, 350);
        SolidBrush blue = (SolidBrush)Brushes.Blue;
        graph.DrawString("https://www.devexpress.com", font3, blue, 110, 350);
}
vb
Imports DevExpress.Pdf
Imports System.Drawing
'...

Private Shared Sub Main(args As String())

    Using processor As New PdfDocumentProcessor()

        ' Create an empty document.
        processor.CreateEmptyDocument("..\..\Result.pdf")

        ' Create and draw PDF graphics.
        Using graph As PdfGraphics = processor.CreateGraphicsPageSystem()
            DrawGraphics(graph)

            ' Create a link to a page.
            graph.AddLinkToPage(New Rectangle(180, 160, 480, 30), 1, 168, 230)

            ' Create a link to URI.
            graph.AddLinkToUri(New Rectangle(110, 350, 180, 15), New Uri("https://www.devexpress.com"))

            ' Render a page with graphics.
            processor.RenderNewPage(PdfPaperSize.Letter, graph)
        End Using
    End Using
End Sub

Private Shared Sub DrawGraphics(graph As PdfGraphics)

    ' Draw text lines on the page.
    Dim black As SolidBrush = DirectCast(Brushes.Black, SolidBrush)
    Using font1 As New Font("Times New Roman", 32, FontStyle.Bold)
        graph.DrawString("PDF Document Processor", font1, black, 180, 150)
    End Using
    Using font2 As New Font("Arial", 20)
        graph.DrawString("Display, Print and Export PDF Documents", font2, black, 168, 230)
    End Using
    Using font3 As New Font("Arial", 10)
        graph.DrawString("The PDF Document Processor is a non-visual component " + "that provides the application programming interface of the PDF Viewer.", font3, black, 16, 300)
        graph.DrawString("Learn more at", font3, black, 20, 350)
        Dim blue As SolidBrush = DirectCast(Brushes.Blue, SolidBrush)
        graph.DrawString("https://www.devexpress.com", font3, blue, 110, 350)
    End Using
End Sub

Transform Graphics Content

The table below lists available actions with graphics content and API used to perform these actions:

ActionMethod
Scale graphics contentScaleTransform
Rotate graphics contentRotateTransform
Translate the coordinate system originTranslateTransform
Crop the clip regionIntersectClip

The following scales the coordinate system and draws shapes in the initial and scaled coordinate systems.

csharp
using DevExpress.Pdf;
using DevExpress.Drawing;
//...

using (PdfDocumentProcessor processor = new PdfDocumentProcessor()) {
    processor.CreateEmptyDocument();
    PdfPage page = processor.AddNewPage(PdfPaperSize.A4);
        using (PdfGraphics graphics = processor.CreateGraphicsWorldSystem()) {
            var rectangle = new RectangleF(0, 0, 300, 300);

            // Draw a blue square.
            using (var brush = new DXSolidBrush(Color.Blue))
                graphics.FillRectangle(brush, rectangle);

            // Scale the coordinate system.
            graphics.ScaleTransform(0.5f, 0.5f);

            // Draw a red square in the scaled coordinate system.
            using (var brush = new DXSolidBrush(Color.Red))
                graphics.FillRectangle(brush, rectangle);

            // Add graphics content to the document page.
            graphics.AddToPageForeground(page);
        }
    processor.SaveDocument("out2.pdf");
}
Process.Start("out.pdf");
vb
Imports DevExpress.Pdf
Imports DevExpress.Drawing
'...

Using processor As New PdfDocumentProcessor()
    processor.CreateEmptyDocument()
    Dim page As PdfPage = processor.AddNewPage(PdfPaperSize.A4)
        Using graphics As PdfGraphics = processor.CreateGraphicsWorldSystem()
            Dim rectangle = New RectangleF(0, 0, 300, 300)

            ' Draw a blue square.
            Using brush = New DXSolidBrush(Color.Blue)
                graphics.FillRectangle(brush, rectangle)
            End Using

            ' Scale the coordinate system.
            graphics.ScaleTransform(0.5F, 0.5F)

            ' Draw a red square in the scaled coordinate system.
            Using brush = New DXSolidBrush(Color.Red)
                graphics.FillRectangle(brush, rectangle)
            End Using

            ' Add graphics content to the document page.
            graphics.AddToPageForeground(page)
        End Using
    processor.SaveDocument("out2.pdf")
End Using
Process.Start("out.pdf")

Save and Restore Graphics State

The graphics state is a data structure that contains the drawing parameters of the PdfGraphics object (the clip region in which graphics are drawn and the transformation matrix). Call the SaveGraphicsState() method to save the current graphics state to the stack.

After you modify the PdfGraphics object’s drawing parameters, call the RestoreGraphicsState paired method to return the parameters to the most recently saved graphics state. This graphics state is removed from the stack when you call the RestoreGraphicsState method.

You can nest calls to the SaveGraphicsState method to save multiple graphics states. Pair each call to the SaveGraphicsState method with the RestoreGraphicsState method.

The following example saves the current graphics state, draws a shape in the translated coordinate system, restores the saved graphics state, and draws another shape in the restored coordinate system.

csharp
using DevExpress.Pdf;
using System.Drawing;
//...

using (PdfDocumentProcessor processor = new PdfDocumentProcessor()) {
    processor.CreateEmptyDocument();
    PdfPage page = processor.AddNewPage(PdfPaperSize.A4);
        using (PdfGraphics graphics = processor.CreateGraphicsWorldSystem()) {

            // Save the current graphics state (the coordinate system origin is (0, 0)).
            graphics.SaveGraphicsState();

            // Translate the origin of the coordinate system to the point (300, 300).
            graphics.TranslateTransform(300, 300);

            // Draw a blue square in the translated coordinate system.
            using (var brush = new SolidBrush(Color.Blue))
                graphics.FillRectangle(brush, new RectangleF(0, 0, 200, 200));

            // Restore the saved graphics state (the coordinate system origin is (0, 0)).
            graphics.RestoreGraphicsState();

            // Draw a red square in the restored coordinate system.
            using (var brush = new SolidBrush(Color.Red))
                graphics.FillRectangle(brush, new RectangleF(0, 0, 200, 200));

            // Add graphics content to the document page.
            graphics.AddToPageForeground(page);
        }
    processor.SaveDocument("out2.pdf");
}
Process.Start("out.pdf");
vb
Imports DevExpress.Pdf
Imports System.Drawing
'...

Using processor As New PdfDocumentProcessor()
    processor.CreateEmptyDocument()
    Dim page As PdfPage = processor.AddNewPage(PdfPaperSize.A4)
        Using graphics As PdfGraphics = processor.CreateGraphicsWorldSystem()
            ' Save the current graphics state (the coordinate system origin is (0, 0)).
            graphics.SaveGraphicsState()

            ' Translate the origin of the coordinate system to the point (300, 300).
            graphics.TranslateTransform(300, 300)

            ' Draw a blue square in the translated coordinate system.
            Using brush = New SolidBrush(Color.Blue)
                graphics.FillRectangle(brush, New RectangleF(0, 0, 200, 200))
            End Using

            ' Restore the saved graphics state (the coordinate system origin is (0, 0)).
            graphics.RestoreGraphicsState()

            ' Draw a red square in the restored coordinate system.
            Using brush = New SolidBrush(Color.Red)
                graphics.FillRectangle(brush, New RectangleF(0, 0, 200, 200))
            End Using

            ' Add graphics content to the document page.
            graphics.AddToPageForeground(page)
        End Using
    processor.SaveDocument("out2.pdf")
End Using
Process.Start("out.pdf")