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

Add Graphics Content to a Page

  • 5 minutes to read

When adding graphics content to an entire page or a part of it, the world coordinates are converted to page coordinates (see Coordinate Systems to learn more).

The sections below describe methods used to add graphics content to a page.

Add graphics content to an existing page

To add graphics content to a foreground/background of a page, call the overloaded PdfGraphics.AddToPageForeground or PdfGraphics.AddToPageBackground method and pass a PdfPage object containing the page where graphics should be drawn, and dpiX and dpiY values as arguments. Use the PdfDocument.Pages property to access a collection of pages within a document, and the PdfDocumentProcessor.Document property to get a document.

This example shows how to add text to the top left and bottom right of a page in a document with landscape and portrait pages.

using System.Drawing;
using System.Collections.Generic;
using DevExpress.Pdf;

namespace CreateGraphics {
    class Program {
        const float DrawingDpi = 72f;

        static void Main(string[] args) {
            using (PdfDocumentProcessor processor = new PdfDocumentProcessor()) {
                processor.LoadDocument("..\\..\\RotatedDocument.pdf");
                using (SolidBrush textBrush = new SolidBrush(Color.FromArgb(100, Color.Blue)))
                    AddGraphics(processor, "text", textBrush);
                processor.SaveDocument("..\\..\\RotatedDocumentWithGraphics.pdf");
            }
        }

        static void AddGraphics(PdfDocumentProcessor processor, string text, SolidBrush textBrush) {
            IList<PdfPage> pages = processor.Document.Pages;
            for (int i = 0; i < pages.Count; i++) {
                PdfPage page = pages[i];
                using (PdfGraphics graphics = processor.CreateGraphics()) {
                    SizeF actualPageSize = PrepareGraphics(page, graphics);
                    using (Font font = new Font("Segoe UI", 20, FontStyle.Regular)) {
                        SizeF textSize = graphics.MeasureString(text, font, PdfStringFormat.GenericDefault);
                        PointF topLeft = new PointF(0, 0);
                        PointF bottomRight = new PointF(actualPageSize.Width - textSize.Width, actualPageSize.Height - textSize.Height);
                        graphics.DrawString(text, font, textBrush, topLeft);
                        graphics.DrawString(text, font, textBrush, bottomRight);
                        graphics.AddToPageForeground(page, DrawingDpi, DrawingDpi);
                    }
                }
            }
        }

        static SizeF PrepareGraphics(PdfPage page, PdfGraphics graphics) {
            PdfRectangle cropBox = page.CropBox;
            float cropBoxWidth = (float)cropBox.Width;
            float cropBoxHeight = (float)cropBox.Height;

            switch (page.Rotate) {
                case 90:
                    graphics.RotateTransform(-90);
                    graphics.TranslateTransform(-cropBoxHeight, 0);
                    return new SizeF(cropBoxHeight, cropBoxWidth);
                case 180:
                    graphics.RotateTransform(-180);
                    graphics.TranslateTransform(-cropBoxWidth, -cropBoxHeight);
                    return new SizeF(cropBoxWidth, cropBoxHeight);
                case 270:
                    graphics.RotateTransform(-270);
                    graphics.TranslateTransform(0, -cropBoxWidth);
                    return new SizeF(cropBoxHeight, cropBoxWidth);
            }
            return new SizeF(cropBoxWidth, cropBoxHeight);
        }
    }
}

Add graphics content to a new page

To render a new page with graphics content, call the overloaded PdfDocumentProcessor.RenderNewPage method and pass graphics content (represented by a PdfGraphics object), a PdfRectangle object containing the page size, and dpiX and dpiY values as arguments.

Note

The dpiX and dpiY values are equal to 96 in all overloaded PdfDocumentProcessor.RenderNewPage, PdfGraphics.AddToPageForeground, PdfGraphics.AddToPageBackground methods. To render graphics content on a page at another dpi, pass dpiX and dpiY values to one of these overloaded methods.

A page’s foreground content (text, an image) can overlap a watermark added to a page’s background using the PdfGraphics.AddToPageBackground method. We recommend placing a semi-transparent watermark on the page’s foreground (instead of the page’s background) by calling the PdfGraphics.AddToPageForeground method.

This example shows how to draw a business card in the document foreground.

using DevExpress.Pdf;
using System;
using System.Drawing;

namespace DocumentCreationAPI
{
    class Program {

        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.CreateGraphics())
                {
                    DrawGraphics(graph);

                    // 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;
            using (Font font1 = new Font("Times New Roman", 32, FontStyle.Bold))
            {
                graph.DrawString("PDF Document Processor", font1, black, 180, 150);
            }
            using (Font font2 = new Font("Arial", 20)) {
                graph.DrawString("Display, Print and Export PDF Documents", font2, black, 168, 230);
            }
            using (Font font3 = 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);
            }
        }
    }
}
See Also