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];