How to rotate a page 90 degrees?

Tags: page rotationPdfStamperiText 5

I am trying to use a PDF for stamping and need to rotate it 90 degrees to lay it on correctly? Anyone know how to do this?

Posted on StackOverflow on Nov 19, 2014 by Casey

Please take a look at the Rotate90Degrees example.

In this example, we use PdfReader to get an instance of the document, and we change the /Rotate value in every page dictionary (if there is no such entry, we add one with value 90):

PdfReader reader = new PdfReader(src);
int n = reader.getNumberOfPages();
PdfDictionary page;
PdfNumber rotate;
for (int p = 1; p <= n; p++) {
    page = reader.getPageN(p);
    rotate = page.getAsNumber(PdfName.ROTATE);
    if (rotate == null) {
        page.put(PdfName.ROTATE, new PdfNumber(90));
    }
    else {
        page.put(PdfName.ROTATE, new PdfNumber((rotate.intValue() + 90) % 360));
    }
}

Once this is done, we use a PdfStamper to persist the change:

PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
stamper.close();
reader.close();

This is an iText example. If you want an iTextSharp example, you'll discover that it is very easy to port the Java to C# as the terminology is identical. You just need to change some lower cases into upper cases like this:

PdfDictionary page = reader.GetPageN(1);
page.Put(PdfName.ROTATE, new PdfNumber(90));