Skip to main content

How to: Create Graphics in a PDF File with Landscape and Portrait Page Orientations

  • 3 minutes to read


You need a license for the DevExpress Office File API Subscription or DevExpress Universal Subscription to use these examples in production code.

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.

PDF graphics are represented by an instance of the PdfGraphics class. To create graphics, call the PdfDocumentProcessor.CreateGraphics method. To access PdfGraphics, you need to reference the DevExpress.Pdf.Drawing assembly.

To rotate text that should be drawn in a document with landscape and portrait pages, call the PdfGraphics.RotateTransform method.

To move rotated text to desired position (bottom right) on a page, call the PdfGraphics.TranslateTransform method.

To draw text on a page, call the PdfGraphics.DrawString method with the specified text, font, brush and location.

To add graphics to a page foreground, call the PdfGraphics.AddToPageForeground method.

View Example

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

const float DrawingDpi = 72f;

static void Main(string[] args)
    using (PdfDocumentProcessor processor = new PdfDocumentProcessor())
        using (SolidBrush textBrush = new SolidBrush(Color.FromArgb(100, Color.Blue)))
            AddGraphics(processor, "text", textBrush);

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, DrawingDpi, DrawingDpi);
            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, float dpiX, float dpiY) {
    PdfRectangle cropBox = page.CropBox;
    float cropBoxWidth = ConvertFromPdfUnits((float)cropBox.Width, dpiX);
    float cropBoxHeight = ConvertFromPdfUnits((float)cropBox.Height, dpiY);
    switch(page.Rotate) {
        case 90:
            graphics.TranslateTransform(-cropBoxHeight, 0);
            return new SizeF(cropBoxHeight, cropBoxWidth);
        case 180:
            graphics.TranslateTransform(-cropBoxWidth, -cropBoxHeight);
            return new SizeF(cropBoxWidth, cropBoxHeight);
        case 270:
            graphics.TranslateTransform(0, -cropBoxWidth);
            return new SizeF(cropBoxHeight, cropBoxWidth);
    return new SizeF(cropBoxWidth, cropBoxHeight);

static float ConvertFromPdfUnits(float pdfValue, float targetDpi) {
    return pdfValue / 72f * targetDpi;
See Also