How to: Create Graphics in a PDF File with Landscape and Portrait Page Orientations
- 4 minutes to read
Important
You need a license for the DevExpress Office File API Subscription or DevExpress Universal Subscription to use these examples in production code.
This example describes 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. The PdfDocumentProcessor class contains several CreateGraphics methods that create a graphics object in different coordinate systems. 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 DevExpress.Drawing;
using System.Collections.Generic;
using DevExpress.Pdf;
const float DrawingDpi = 72f;
// Create a PDF processor and load the source document
using (var processor = new PdfDocumentProcessor()) {
processor.LoadDocument("..\\..\\RotatedDocument.pdf");
// Create a semi-transparent blue brush and add text to each page
using (DXSolidBrush textBrush = new DXSolidBrush(Color.FromArgb(100, Color.Blue)))
AddGraphics(processor, "text", textBrush);
// Save the modified document
processor.SaveDocument("..\\..\\RotatedDocumentWithGraphics.pdf");
}
// Add text graphics to each page of the PDF document
static void AddGraphics(PdfDocumentProcessor processor, string text, DXSolidBrush textBrush)
{
IList<PdfPage> pages = processor.Document.Pages;
for (int i = 0; i < pages.Count; i++) {
PdfPage page = pages[i];
// Create a graphics context for the current page
using (PdfGraphics graphics = processor.CreateGraphicsPageSystem()) {
// Prepare graphics according to page rotation and get actual page size
SizeF actualPageSize = PrepareGraphics(page, graphics, DrawingDpi, DrawingDpi);
using (DXFont font = new DXFont("Segoe UI", 20, DXFontStyle.Regular)) {
// Measure the size of the text to be drawn
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);
// Draw the text in the top-left and bottom-right corners
graphics.DrawString(text, font, textBrush, topLeft);
graphics.DrawString(text, font, textBrush, bottomRight);
// Add the graphics to the page foreground
graphics.AddToPageForeground(page, DrawingDpi, DrawingDpi);
}
}
}
}
// Adjust the graphics context for the page's rotation and returns the actual page size
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.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);
}
// Convert PDF units (points) to the specified DPI
static float ConvertFromPdfUnits(float pdfValue, float targetDpi) {
return pdfValue / 72f * targetDpi;
}