Rotate a PDF page

Rotate a PDF page

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.

using (FileStream inFile = new FileStream(@"..\..\..\inputDocuments\PackingLightBrochure.pdf", FileMode.Open, FileAccess.Read))
{
    // open the source document
    Document documentIn = new Document(inFile);

    // create the target document
    Document documentOut = new Document();

    // enumerate the pages in the source document
    foreach (Page inputPage in documentIn.Pages)
    {
        // append a rotated version of the original page to the target document

        // create a new page that has the width and height swapped
        Page page = new Page(inputPage.Height, inputPage.Width);


        // because the pageShape rotates clockwise with the rotation point at the lower left
        // corner, we must apply a translation in order to get the page in view again
        PageShape pageShape = new PageShape(inputPage, 0, inputPage.Width, inputPage.Width, inputPage.Height, true, 90, PageBoundary.Default);
        page.VisualOverlay.Add(pageShape);

        documentOut.Pages.Add(page);
    }

    // write the target document to disk
    using (FileStream outFile = new FileStream(@"..\..\rotatepage.pdf", FileMode.Create, FileAccess.Write))
    {
        documentOut.Write(outFile);
    }
        Using inFile As New FileStream("..\..\..\inputDocuments\PackingLightBrochure.pdf", FileMode.Open, FileAccess.Read)
            ' open the source document
            Dim documentIn As New Document(New BinaryReader(inFile))

            ' create the target document
            Dim documentOut As New Document()
            For i As Integer = 0 To documentIn.Pages.Count - 1

                ' enumerate the pages in the source document
                ' append a rotated version of thew original page to the target document

                ' create a new page that has the width and height swapped
                Dim page As New Page(documentIn.Pages(i).Height, documentIn.Pages(i).Width)


                ' because the pageShape rotates clockwise with the rotation point at the lower left
                ' corner, we must apply a translation in order to get the page in view again
                Dim pageShape As New PageShape(documentIn.Pages(i), 0, page.Height, page.Height, page.Width, True, _
                 90, PageBoundary.[Default])
                page.VisualOverlay.Add(pageShape)

                documentOut.Pages.Add(page)
            Next

            ' write the target document to disk
         Using outFile As New FileStream("..\..\rotatepage.pdf", FileMode.Create, FileAccess.Write)
            documentOut.Write(New BinaryWriter(outFile))
         End Using
        End Using