Hi Guys.
Simple way to show pdf file on UIView. You just use the following class and alloc your UIView with PDFView class.
PDFView.h
#import
@interface PDFView : UIView {
CGPDFDocumentRef pdf1;
int pageNo;
}
-(void)drawInContext:(CGContextRef)context pno:(int)pageNo;
-(int)numberOfPages;
@end
PDFView.m
#import "PDFView.h"
@implementation PDFView
- (id)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {
CFURLRef pdfURL = CFBundleCopyResourceURL(CFBundleGetMainBundle(), CFSTR("iphoneos.pdf"), NULL, NULL);
pdf1 = CGPDFDocumentCreateWithURL((CFURLRef)pdfURL);
CFRelease(pdfURL);
self.backgroundColor = [UIColor whiteColor];
self.opaque = YES;
}
return self;
}
- (void)drawRect:(CGRect)rect {
[self drawInContext:UIGraphicsGetCurrentContext()];
}
-(int)incrementPage :(int)no{
pageNo =no;
return pageNo;
}
- (void)dealloc {
[super dealloc];
}
-(void)drawInContext:(CGContextRef)context{
// before we start drawing.
CGContextTranslateCTM(context, 0.0, self.bounds.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
// Grab the first PDF page
CGPDFPageRef page = CGPDFDocumentGetPage(pdf1, pageNo);
// We're about to modify the context CTM to draw the PDF page where we want it, so save the graphics state in case we want to do more drawing
CGContextSaveGState(context);
// CGPDFPageGetDrawingTransform provides an easy way to get the transform for a PDF page. It will scale down to fit, including any
// base rotations necessary to display the PDF page correctly.
CGAffineTransform pdfTransform = CGPDFPageGetDrawingTransform(page, kCGPDFCropBox, self.bounds, 0, true);
// And apply the transform.
CGContextConcatCTM(context, pdfTransform);
// Finally, we draw the page and restore the graphics state for further manipulations!
CGContextDrawPDFPage(context, page);
CGContextRestoreGState(context);
}
//Returns Number of Pages
-(int)numberOfPages{
CFURLRef pdfURL = CFBundleCopyResourceURL(CFBundleGetMainBundle(), CFSTR("iphoneos.pdf"), NULL, NULL);
pdf1 = CGPDFDocumentCreateWithURL((CFURLRef)pdfURL);
int noOfPage =CGPDFDocumentGetNumberOfPages((CFURLRef)pdf1);
return noOfPage;
}
@end
And your Viewcontroller class, just implement the following code
UIView *myView=[[PDFView alloc]initWithFrame:rect];
int totalpage = [myView numberOfPages];
[myView drawRect:CGRectMake(0, 0, rect.size.width, rect.size.height)];
[self.view addSubView:myView];
Thanks for this. But there are some problems in your code I think.
ReplyDeleteYou have not declared or initialized the rect variable that you address with initWithFrame.
Also, I can get the ViewController code to compile OK, but the program crashes at the addSubVIew line
And ...
ReplyDeleteI do not think you can call drawRect directly. It is executed on a view by doing something like
[myView performSelectorOnMainThread:@selector(setNeedsDisplay) withObject:0 waitUntilDone:YES];
@EOD rect is variable.
ReplyDeleteCGRect rect=[self.view frame];
@EOD you give me your mail-id. i will mail you the sample zip file.
cheers
thank you, but a question... How to make an action for change the page?
ReplyDeleteBye
you can declare method in PDFView.m which set the value of pageNo and call drawrect() method in it.
ReplyDeletefor example..
-(void)showViewWithPageNumber:(int)pno rectangle:(CGRect)rect{
pageNo=pno;
[self drawRect:rect];
}
And on view tap/touch action..
you can call the method
[myView showViewWithPageNumber:2 rectangle:rect];
hope this will helps you...
if any you have query.. Email me at mandeep.dhiman@sify.com
Hi
ReplyDeleteI have use this code.
But i got error in this line.
CGContextTranslateCTM(context, 0.0, self.bounds.size.height);
error: request for member 'bounds' in something not a structure or union
Please i need your help....
I have uploaded a sample application. just click the following link.
ReplyDeletehttp://mandeepdhiman.blogspot.com/2010/06/pdf-reader-on-iphone-and-ipad.html
This an great example Mandeep, thank you!
ReplyDeleteI was thinking of trying this in the context of a zooming scroll view.
Have you tried this?
Thanks again.
- Nick Porcaro nick (at) porcaro.org