Reputation: 41
I have attached the code snippet that I am using for displaying a PDF. The following code displays the PDF, but it seems that it is either squeezed or not using the full size of the iPad display, resulting in a page that is too small.
How can I display a PDF that fits in the boundary of an iPad display or in a zoomed state? I have tried using a different approach (approach-2), but it creates a problem with the PDF appearing rotated at a 90-degree angle.
Approach-1:
CGContextSaveGState(ctx);
CGContextTranslateCTM(ctx, 0.0, [self.view bounds].size.height);
CGContextScaleCTM(ctx, 1.0, -1.0);
CGContextConcatCTM(ctx,
CGPDFPageGetDrawingTransform(page, kCGPDFCropBox,
[self.view bounds], 0, true));
CGContextDrawPDFPage(ctx, page);
CGContextRestoreGState(ctx);
Approach-2:
CGPDFPageRef page = CGPDFDocumentGetPage(pdfdocument, PageNo+1);
if(page){
CFRetain(page);
}
CGRect pageRect =CGPDFPageGetBoxRect(page, kCGPDFMediaBox);
int angle= CGPDFPageGetRotationAngle(page);
float pdfScale = self.bounds.size.width/pageRect.size.width;
CGContextSetRGBFillColor(context, 1.0,1.0,1.0,1.0);
CGContextFillRect(context,self.bounds);
CGContextSaveGState(context);
// Flip the context so that the PDF page is rendered
// right side up.
CGContextTranslateCTM(context, 0.0, self.bounds.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
// Scale the context so that the PDF page is rendered
// at the correct size for the zoom level.
CGContextScaleCTM(context, pdfScale,pdfScale);
CGContextDrawPDFPage(context, page);
CGContextRestoreGState(context);
Can anyone suggest to me a solution that allows any PDF of any size and any angle to be displayed full screen on an iPad in both orientations? It would be great if you can provide me a code snippet or pseudo-code. Thanks
Upvotes: 4
Views: 3932
Reputation: 724
This will display your PDF Full Screen : not sure this is the best way to handle PDF though
- (void)drawRect:(CGRect)rect
{
CGContextRef ctx = UIGraphicsGetCurrentContext();
//PDF might be transparent, assume white paper - set White Background
[[UIColor whiteColor] set];
CGContextFillRect(ctx, rect);
//Flip coordinates
CGContextGetCTM(ctx);
CGContextScaleCTM(ctx, 1, -1);
CGContextTranslateCTM(ctx, 0, -rect.size.height);
//PDF File Path
NSURL *pdfURL = [[NSBundle mainBundle] URLForResource:@"TEST" withExtension:@"pdf"];
CGPDFDocumentRef pdf = CGPDFDocumentCreateWithURL((__bridge CFURLRef)pdfURL);
CGPDFPageRef page1 = CGPDFDocumentGetPage(pdf, 1);
//Get the rectangle of the cropped inside
CGRect mediaRect = CGPDFPageGetBoxRect(page1, kCGPDFCropBox);
CGContextScaleCTM(ctx, rect.size.width / mediaRect.size.width,
rect.size.height / mediaRect.size.height);
//Draw PDF
CGContextDrawPDFPage(ctx, page1);
CGPDFDocumentRelease(pdf);
}
Copy the PDF you want to display into your project library, in the example above the name of the PDF file is "TEST" : you want to name yours with the name of your file
This works for me to display the PDF in full screen within a UIView,
I am not sure this is your best option though : there are issues : i.e. you need to handle zooming, and if you do that, you need to handle panning. Also Orientation is a big mess (when you flip the device to landscape mode) the file loses its Aspect Ration (and gets squished)
Play a round with it .....
Though jumping into PDF handling in IOS is nothing but a pain . . . .
Hope this helps you out a little
Upvotes: 0
Reputation: 6295
Hopefully this helps. It shows how to render the first page of a PDF referenced by an URL.
This code is a collection of snippets of my own codebase so don't just copy paste it in 1 file and expect it to build and run. I put some comments so you see what belongs where, and you'll need to declare some ivars for it to work.
// helper function
CGRect CGRectScaleAspectFit(CGRect sourceRect,CGRect fitRect)
{
if ( sourceRect.size.width > fitRect.size.width)
{
float scale = fitRect.size.width / sourceRect.size.width;
sourceRect.size.width = fitRect.size.width;
sourceRect.size.height = (int)(sourceRect.size.height * scale);
}
if ( sourceRect.size.height > fitRect.size.height)
{
float scale = fitRect.size.height / sourceRect.size.height;
sourceRect.size.height = fitRect.size.height;
sourceRect.size.width = (int)(sourceRect.size.width * scale);
}
return sourceRect;
}
// in your UIView subclass init method
CGPDFDocumentRef pdf = CGPDFDocumentCreateWithURL((CFURLRef)pdfURL);
pdfPage = CGPDFDocumentGetPage(pdf, 1);
CGRect fitrect = CGRectMake(0, 0, self.frame.size.width,self.frame.size.height);
CGRect pageRect = CGPDFPageGetBoxRect(pdfPage, kCGPDFMediaBox);
CGRect f;
f.origin.x=0;
f.origin.y=0;
f.size.height = self.frame.size.height;
f.size.width = self.frame.size.height * pageRect.size.width/pageRect.size.height;
f = CGRectScaleAspectFit(f,fitrect); // this is the actual pdf frame rectangle to fill the screen as much as possible
pdfScale = f.size.height/pageRect.size.height;
// in your UIView subclass drawRect method
CGContextSetRGBFillColor(context, 1.0,1.0,1.0,1.0);
CGContextFillRect(context,self.bounds);
CGContextSaveGState(context);
CGContextTranslateCTM(context, 0.0, self.bounds.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
CGContextScaleCTM(context, pdfScale,pdfScale);
CGContextDrawPDFPage(context, pdfPage);
CGContextRestoreGState(context);
Upvotes: 1