Reputation: 490
The code below is intended to display the pages of a PDF while properly handling any non-zero rotation angles that might be specified for each page. My test PDF file has multiple pages and one them has a 180 degree rotation angle, which the code properly detects, but the call to CGPDFPageGetDrawingTransform (followed by CGContextContactCTM) has no effect. The page is being displayed unrotated. What am I doing wrong?
CGContextSaveGState(context);
CGRect drawRect=self.bounds;
CGRect cropBox = CGPDFPageGetBoxRect(pdfPage, kCGPDFCropBox);
int rotationAngle=CGPDFPageGetRotationAngle(pdfPage);
if (kDebug>1) {
NSLog(@"***** page rotation angle is %d",rotationAngle);
}
CGContextTranslateCTM(context, drawRect.origin.x, drawRect.origin.y);
CGAffineTransform transform = CGPDFPageGetDrawingTransform(pdfPage, kCGPDFCropBox, cropBox, rotationAngle, true);
CGContextConcatCTM (context, transform);
float xScaleFactor=drawRect.size.width / cropBox.size.width;
float yScaleFactor=drawRect.size.height / cropBox.size.height;
CGContextTranslateCTM(context, -CGRectGetMinX(cropBox), CGRectGetMaxY(cropBox)*yScaleFactor);
CGContextScaleCTM(context, xScaleFactor, -yScaleFactor);
CGContextClipToRect(context, cropBox);
CGContextDrawPDFPage(context, pdfPage);
CGContextRestoreGState(context);
Thanks,
//Scott
Upvotes: 1
Views: 608
Reputation: 490
So after a careful re-reading of the API for CGPDFPageGetDrawingTransform, I discover that the value of page's /ROTATE attribute is ADDED to the rotation angle you specify in the method call. In my situation above, the page's /ROTATE happend to be 180. was grabbing that in the rotationAngle var and passing that in to get the transform. So, 180+180 = 360 = no visibile rotation.
I now call GPDFPageGetDrawingTransform with a rotation angle of 0.0. Anything in the /ROTATE is added, and voila, I get the correct results.
Upvotes: 1