Today's task is to load and display PDF files on IOS.
Method 1: Use webview
-(void)loadDocument:(NSString *)documentName inView:(UIWebView *)webView{ NSString *path = [[NSBundle mainBundle] pathForResource:documentName ofType:nil]; NSURL *url = [NSURL fileURLWithPath:path]; NSURLRequest *request = [NSURLRequest requestWithURL:url]; [webView loadRequest:request];}
Benefits: 1. Easy to implement
2. Simple implementation
Disadvantages: 1. You can only browse and get no callback. Safari will not bird anyone.
2. Fixed vertical drag, blind if you want to achieve the page flip Effect
The following method can solve the disadvantages of displaying PDF in webview. In contrast, you have to pay some sweat as a price.
Method 2: Use cgcontextdrawpdfpage
CGPDFDocumentRef GetPDFDocumentRef(NSString *filename){ CFStringRef path; CFURLRef url; CGPDFDocumentRef document; size_t count; path = CFStringCreateWithCString (NULL, [filename UTF8String], kCFStringEncodingUTF8); url = CFURLCreateWithFileSystemPath (NULL, path, kCFURLPOSIXPathStyle, 0); CFRelease (path); document = CGPDFDocumentCreateWithURL (url); CFRelease(url); count = CGPDFDocumentGetNumberOfPages (document); if (count == 0) { printf("[%s] needs at least one page!\n", [filename UTF8String]); return NULL; } else { printf("[%ld] pages loaded in this PDF!\n", count); } return document;}void DisplayPDFPage (CGContextRef myContext, size_t pageNumber, NSString *filename){ CGPDFDocumentRef document; CGPDFPageRef page; document = GetPDFDocumentRef (filename); page = CGPDFDocumentGetPage (document, pageNumber); CGContextDrawPDFPage (myContext, page); CGPDFDocumentRelease (document);}
In this way, the displayed PDF single page is inverted, And the quartz coordinate system is different from the uiview coordinate system. Adjust the coordinate system so that the PDF is upright:
CGContextRef context = UIGraphicsGetCurrentContext(); CGContextTranslateCTM(context, 80, self.frame.size.height-60); CGContextScaleCTM(context, 1, -1);
Use ios5's powerful uipageviewcontroller to browse pages
- (PDFViewController *)viewControllerAtIndex:(NSUInteger)index { //Return the PDFViewController for the given index. if (([self.pagePDF count] == 0 )|| (index > [self.pagePDF count]) ) { return nil; } //Create a new view controller and pass suitable data. PDFViewController *dataViewController = [[PDFViewController alloc]initWithNibName:@"PDFViewController" bundle:nil]; //dataViewController.pdfview = [self.pagePDF objectAtIndex:index]; dataViewController.pdfview = [[PDFView alloc]initWithFrame:self.view.frame atPage:index]; [dataViewController.view addSubview:dataViewController.pdfview]; NSLog(@"index = %d",index); return dataViewController;}- (NSUInteger) indexOfViewController:(PDFViewController *)viewController{ return [self.pagePDF indexOfObject:viewController.pdfview];}- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController{ NSUInteger index = [self indexOfViewController:(PDFViewController *)viewController]; if ((index == 0 ) || (index == NSNotFound)){ return nil; } index--; return [self viewControllerAtIndex:index];}- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController{ NSUInteger index = [self indexOfViewController:(PDFViewController *)viewController]; if (index == NSNotFound) { return nil; } index++; if (index == [self.pagePDF count]){ return nil; } return [self viewControllerAtIndex:index];}
Save and create a new pdf.
Thank you for your help and guidance.