Reputation: 3556
I'm using Apple's example «PDFScrollView» to display PDFs on an iPad. This works fine, as long as the PDF-document is in Portrait-Mode. When the document is in Landscape-mode, though, the document is always shown rotated by 90 degrees. I found this How to detect the orientation of a PDF document in iPhone SDK - but when I try to get the dimensions of the PDF, the height is always bigger than the width, no matter what orientation the PDF has...
Any ideas?
Upvotes: 0
Views: 1583
Reputation: 5765
Is there a particular reason you are drawing PDFs yourself? You could use QLPreviewController
on iOS 4.0 or later, and UIDocumentInteractionController
for previous versions.
Upvotes: 1
Reputation: 3556
As mentionend in, the CGPDFPageGetBoxRect(page, kCGPDFMediaBox)
always returns the same size for a PDF, no matter what orientation the PDF has.
But the rotation of the page is returned correctly.
So I get the rotation using this code:
rotate = CGPDFPageGetRotationAngle(page);
And then use the code below which I found here: http://ipdfdev.com/2011/03/23/display-a-pdf-page-on-the-iphone-and-ipad/
switch (rotate) {
case 0:
// Translate the origin of the coordinate system at the
// bottom left corner of the page rectangle.
CGContextTranslateCTM(context, 0, cropBox.size.height);
// Reverse the Y axis to grow from bottom to top.
CGContextScaleCTM(context, 1, -1);
break;
case 90:
// Reverse the Y axis to grow from bottom to top.
CGContextScaleCTM(context, 1, -1);
// Rotate the coordinate system.
CGContextRotateCTM(context, -M_PI / 2);
break;
case 180:
case -180:
// Reverse the Y axis to grow from bottom to top.
CGContextScaleCTM(context, 1, -1);
// Translate the origin of the coordinate system at the
// top right corner of the page rectangle.
CGContextTranslateCTM(context, cropBox.size.width, 0);
// Rotate the coordinate system with 180 degrees.
CGContextRotateCTM(context, M_PI);
break;
case 270:
case -90:
// Translate the origin of the coordinate system at the
// bottom right corner of the page rectangle.
CGContextTranslateCTM(context, cropBox.size.height, cropBox.size.width);
// Rotate the coordinate system.
CGContextRotateCTM(context, M_PI / 2);
// Reverse the X axis.
CGContextScaleCTM(context, -1, 1);
break;
}
Et voilà - the PDF is displayed in the right orientation
Upvotes: 3
Reputation: 1666
I am writing this with assumption that your problem is about rotating document when user rotates device.... For that you have to redraw document in view. TO achieve that do following....
change frame of the view in which document is drawn, and CALayer(OR CATiledLayer) being used in that view in following method.
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
Call setNeedsDisplay method of CATiledLayer after changing frame.
Then return YES in
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
.
Above steps will redraw the document in new frame of you View accordingly so it will appear properly.
->IF your view is in full screen or then only step 2 ans 3 will be enough.
Post here if any further assistance is required....
Upvotes: 1