officefileapi-116197-pdf-document-api-examples-pdf-graphics-and-additional-content-how-to-create-graphics-in-a-document-with-landscape-and-portrait-page-orientations.md
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;
}
Imports DevExpress.Drawing
Imports System.Collections.Generic
Imports DevExpress.Pdf
Private Const DrawingDpi As Single = 72F
Shared Sub Main(ByVal args() As String)
Using processor As New PdfDocumentProcessor()
processor.LoadDocument("..\..\RotatedDocument.pdf")
Using textBrush As New SolidBrush(Color.FromArgb(100, Color.Blue))
AddGraphics(processor, "text", textBrush)
End Using
processor.SaveDocument("..\..\RotatedDocumentWithGraphics.pdf")
End Using
End Sub
Shared Sub AddGraphics(ByVal processor As PdfDocumentProcessor, ByVal text As String, ByVal textBrush As DXSolidBrush)
Dim pages As IList(Of PdfPage) = processor.Document.Pages
For i As Integer = 0 To pages.Count - 1
Dim page As PdfPage = pages(i)
Using graphics As PdfGraphics = processor.CreateGraphicsPageSystem()
Dim actualPageSize As SizeF = PrepareGraphics(page, graphics, DrawingDpi, DrawingDpi)
Using font As New DXFont("Segoe UI", 20, DXFontStyle.Regular)
Dim textSize As SizeF = graphics.MeasureString(text, font, PdfStringFormat.GenericDefault)
Dim topLeft As New PointF(0, 0)
Dim bottomRight As 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)
End Using
End Using
Next i
End Sub
Shared Function PrepareGraphics(ByVal page As PdfPage, ByVal graphics As PdfGraphics, ByVal dpiX As Single, ByVal dpiY As Single) As SizeF
Dim cropBox As PdfRectangle = page.CropBox
Dim cropBoxWidth As Single = ConvertFromPdfUnits(CSng(cropBox.Width), dpiX)
Dim cropBoxHeight As Single = ConvertFromPdfUnits(CSng(cropBox.Height), dpiY)
Select Case 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)
End Select
Return New SizeF(cropBoxWidth, cropBoxHeight)
End Function
Shared Function ConvertFromPdfUnits(ByVal pdfValue As Single, ByVal targetDpi As Single) As Single
Return pdfValue / 72F * targetDpi
End Function
See Also