Skip to main content
.NET Framework 4.5.2+

PdfGraphics.AddToPageBackground(PdfPage, Single, Single) Method

Adds graphics to a PDF page background with the specified horizontal and vertical resolutions.

Namespace: DevExpress.Pdf

Assembly: DevExpress.Pdf.v22.2.Drawing.dll

NuGet Package: DevExpress.Pdf.Drawing

Declaration

public void AddToPageBackground(
    PdfPage page,
    float dpiX,
    float dpiY
)

Parameters

Name Type Description
page PdfPage

A PdfPage object that is the page where graphics are drawn (measured in points - 1/72 of an inch).

dpiX Single

A Single object that represents the value, in dots per inch, for the horizontal resolution.

dpiY Single

A Single object that represents the value, in dots per inch, for the vertical resolution.

Remarks

Call the AddToPageBackground method to draw graphics as the background of the page. To draw graphics as the foreground of the page content, call the PdfGraphics.AddToPageForeground method.

The overloaded AddToPageBackground method automatically converts world coordinates to page coordinates. See Coordinate Systems to learn more.

The world coordinates (units) are converted to page coordinates (points) using the following formula:

x = (unitX / dpiX) * 72;

y = cropBoxHeight - (unitY / dpiY) * 72.

To render graphics on a PDF document page, pass 72 as a DPI value to this method. The DPI value you pass into this method does not affect graphics quality and used for coordinates transformation only (world coordinates to page coordinates). For example, 72 DPI value allows transforming coordinates without any scaling.

Example

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())
    {
        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);
}
See Also