We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Get page rotation from pdf

I try to get the rotation from every page in a pdf document.
My example pdf is attached. It contains one page with rotation of 270 degree.
But the following code returns none and not on270 as expected.

Document document = new Document("testfile_270.pdf");
Console.WriteLine(document.Pages[1].Rotate); // -> none

testfile_270.pdf (56.5 KB)

 We analysed attached document, it contains page which has Rotate value  = 0 (that's why Rotate returns None)
The page contents is rotated because page contains  form X-Object rotated applying transformation matrix: 
-0.00000000000000018 1 -1 -0.00000000000000018 792. 0.00000000000014548 cm
/Fm0 Do
You can programmatically read operators from the page contents in the following way: 
    Document doc = new Document("testfile_270.pdf");
    foreach (Operator op in doc.Pages[1].Contents)
      if (op is Aspose.Pdf.Operators.ConcatenateMatrix)
          Matrix matrix = (op as Aspose.Pdf.Operators.ConcatenateMatrix).Matrix;
          // "-" because we suppose  clockwise rotation
          double angle = Math.Atan2(-matrix.B, matrix.A);
          Console.WriteLine(angle * 180 / Math.PI);

Thanks for your reply, but I’m not sure if that’s what I was looking for. :thinking:

I used the PdfPageEditor to rotate a page in a pdf:

var editor = new PdfPageEditor();
editor.PageRotations = new System.Collections.Generic.Dictionary<int, int> { { 1, 270 } };

After saving the file I want to check the rotation of the first page.
Is the PdfPageEditor then the best way to rotate a whole page?
When does the page.rotate property return a value?

In your code: Can there be more than one ConcatenateMatrix object per page?

PdfPageEditor uses this approach because it can be used not only for rotation, but 
also for moving, zooming etc. of the page contents. This uses matrix to make these 
transformations. And Rotation key of the page is not changed in this case. 
 If you want just rotate page, you can try to set Rotation of the page directly: 
                Document doc = new Document(:input.pdf");
                doc.Pages[1].Rotate = Rotation.on270;
                doc = new Document("result.pdf");