Search code examples
cocoapdfrotationquartz-graphicspdfkit

Cocoa PDF Getting Page Rotation value issue


It is the first time I ask a question on this forum. Sorry if my English is not right, this not my mother tongue langage. Usually I always find my answers on this forum or by Googling. But this time, I didn't find.

Here is my question (Mac OS X Lion):

I have few PDFs. For testing, I rotate some pages with "Preview" and/or rotate some pages of the PDF programmatically with PDFKit ([PDFPage setRotation:90]). I save this new PDF. The output is a PDF with some rotated pages. I re-open in Preview or other, to make sure everything ok. This is fine, the display is right. so far so good.

Now, I open this new created PDF and I'm trying to get the PDF pages rotation values (page by page). I tried with the PDFKIT methods [PDFPage rotation] or with Quartz methods with the CGPDF... (CGPDFPageGetRotationAngle), I always get a value 0 for the rotation of the page. NSLog(@"value :%d", angle)

How to reproduce, with the PDFKIT methods, simply use the "PDF Annotation Editor" code from Apple. Open a PDF with some page in Landscape and in Portrait. The display will be as expected. But now Trying to read the value rotation at the line 'rotation = [page rotation];' in the transformContextForPage:(PDFPage*)page method, I always get 0. When the expected values should be 0 or 90 depending on the page. Same problem when trying to use the Quartz Method with CGPDFPageGetRotationAngle.

Am I missing something ? Is it a bug ? or my understanding of the Rotation of a PDF page is wrong ?

Thanks


Solution

  • In PDF you can create rotated pages in 2 ways: set page size to 8.5*11in and set the Rotate page attribute to 90, or simply set the page size to 11*8.5in. Visually there is no difference.
    The problem with Preview (and I assume also PDFKit behind it) is that when you set the rotation to 90, it does not simply set the Rotate attribute to 90, but it switches the page width and height and applies a transformation on the page content stream so that the page content appears rotated. Because the [PDFPage rotation] and CGPDFPageGetRotationAngle return the value of the Rotate attribute, they will always return 0 on pages rotated with Preview and PDFKit.