IOS Development notes-PDF display and browsing

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.