The requested page is not available for the requested platform. You are viewing the content for .NET Framework 4.5.2+ platform.

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

  • 3 min to read
Important

You require a license to the DevExpress Office File API or DevExpress Universal Subscription to use these examples in production code. Refer to the DevExpress Subscription page for pricing information.

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.

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