Rotate a PDF page

Manipulate PDF
6/3/2015

This code sample creates a copy of a PDF document with each page rotated 90 degrees. Note that you can rotate each page to a custom angle.

C# code sample

1 using (FileStream inFile = new FileStream(@"..\..\..\inputDocuments\PackingLightBrochure.pdf", FileMode.Open, FileAccess.Read)) 2 { 3 // open the source document 4 Document documentIn = new Document(inFile); 5 6 // create the target document 7 Document documentOut = new Document(); 8 9 // enumerate the pages in the source document 10 foreach (Page inputPage in documentIn.Pages) 11 { 12 // append a rotated version of the original page to the target document 13 14 // create a new page that has the width and height swapped 15 Page page = new Page(inputPage.Height, inputPage.Width); 16 17 18 // because the pageShape rotates clockwise with the rotation point at the lower left 19 // corner, we must apply a translation in order to get the page in view again 20 PageShape pageShape = new PageShape(inputPage, 0, inputPage.Width, inputPage.Width, inputPage.Height, true, 90, PageBoundary.Default); 21 page.VisualOverlay.Add(pageShape); 22 23 documentOut.Pages.Add(page); 24 } 25 26 // write the target document to disk 27 using (FileStream outFile = new FileStream(@"..\..\rotatepage.pdf", FileMode.Create, FileAccess.Write)) 28 { 29 documentOut.Write(outFile); 30 } 31 }

VB.NET code sample

1 Using inFile As New FileStream("..\..\..\inputDocuments\PackingLightBrochure.pdf", FileMode.Open, FileAccess.Read) 2 ' open the source document 3 Dim documentIn As New Document(New BinaryReader(inFile)) 4 5 ' create the target document 6 Dim documentOut As New Document() 7 For i As Integer = 0 To documentIn.Pages.Count - 1 8 9 ' enumerate the pages in the source document 10 ' append a rotated version of thew original page to the target document 11 12 ' create a new page that has the width and height swapped 13 Dim page As New Page(documentIn.Pages(i).Height, documentIn.Pages(i).Width) 14 15 16 ' because the pageShape rotates clockwise with the rotation point at the lower left 17 ' corner, we must apply a translation in order to get the page in view again 18 Dim pageShape As New PageShape(documentIn.Pages(i), 0, page.Height, page.Height, page.Width, True, _ 19 90, PageBoundary.[Default]) 20 page.VisualOverlay.Add(pageShape) 21 22 documentOut.Pages.Add(page) 23 Next 24 25 ' write the target document to disk 26 Using outFile As New FileStream("..\..\rotatepage.pdf", FileMode.Create, FileAccess.Write) 27 documentOut.Write(New BinaryWriter(outFile)) 28 End Using 29 End Using