PDF的显示和浏览

来源:互联网 发布:clrs 算法导论 中文版 编辑:程序博客网 时间:2024/06/05 05:06

方法一:利用webview

  1. -(void)loadDocument:(NSString *)documentName inView:(UIWebView *)webView  
  2. {  
  3.     NSString *path = [[NSBundle mainBundle] pathForResource:documentName ofType:nil];  
  4.     NSURL *url = [NSURL fileURLWithPath:path];  
  5.     NSURLRequest *request = [NSURLRequest requestWithURL:url];  
  6.     [webView loadRequest:request];  
  7. }  

好处是:实现起来简单

但是:仅能浏览,拿不到任何回调,而且固定竖版拖动,想实现翻页动效果就会出错


方法二:利用CGContextDrawPDFPage

    1. CGPDFDocumentRef GetPDFDocumentRef(NSString *filename)  
    2. {  
    3.     CFStringRef path;  
    4.     CFURLRef url;  
    5.     CGPDFDocumentRef document;  
    6.     size_t count;  
    7.       
    8.     path = CFStringCreateWithCString (NULL, [filename UTF8String], kCFStringEncodingUTF8);  
    9.     url = CFURLCreateWithFileSystemPath (NULL, path, kCFURLPOSIXPathStyle, 0);  
    10.       
    11.     CFRelease (path);  
    12.     document = CGPDFDocumentCreateWithURL (url);  
    13.     CFRelease(url);  
    14.     count = CGPDFDocumentGetNumberOfPages (document);  
    15.     if (count == 0) {  
    16.         printf("[%s] needs at least one page!\n", [filename UTF8String]);  
    17.         return NULL;   
    18.     } else {  
    19.         printf("[%ld] pages loaded in this PDF!\n", count);  
    20.     }  
    21.     return document;  
    22. }  
    23.   
    24. void DisplayPDFPage (CGContextRef myContext, size_t pageNumber, NSString *filename)  
    25. {  
    26.     CGPDFDocumentRef document;  
    27.     CGPDFPageRef page;  
    28.       
    29.     document = GetPDFDocumentRef (filename);  
    30.     page = CGPDFDocumentGetPage (document, pageNumber);  
    31.     CGContextDrawPDFPage (myContext, page);  
    32.     CGPDFDocumentRelease (document);  
    33. }  
    1. CGPDFDocumentRef GetPDFDocumentRef(NSString *filename)  
    2. {  
    3.     CFStringRef path;  
    4.     CFURLRef url;  
    5.     CGPDFDocumentRef document;  
    6.     size_t count;  
    7.       
    8.     path = CFStringCreateWithCString (NULL, [filename UTF8String], kCFStringEncodingUTF8);  
    9.     url = CFURLCreateWithFileSystemPath (NULL, path, kCFURLPOSIXPathStyle, 0);  
    10.       
    11.     CFRelease (path);  
    12.     document = CGPDFDocumentCreateWithURL (url);  
    13.     CFRelease(url);  
    14.     count = CGPDFDocumentGetNumberOfPages (document);  
    15.     if (count == 0) {  
    16.         printf("[%s] needs at least one page!\n", [filename UTF8String]);  
    17.         return NULL;   
    18.     } else {  
    19.         printf("[%ld] pages loaded in this PDF!\n", count);  
    20.     }  
    21.     return document;  
    22. }  
    23.   
    24. void DisplayPDFPage (CGContextRef myContext, size_t pageNumber, NSString *filename)  
    25. {  
    26.     CGPDFDocumentRef document;  
    27.     CGPDFPageRef page;  
    28.       
    29.     document = GetPDFDocumentRef (filename);  
    30.     page = CGPDFDocumentGetPage (document, pageNumber);  
    31.     CGContextDrawPDFPage (myContext, page);  
    32.     CGPDFDocumentRelease (document);  
    33. }  

    这样显示出来的pdf单页是倒立的,Quartz坐标系和UIView坐标系不一样所致,调整坐标系,使pdf正立:

    1. CGContextRef context = UIGraphicsGetCurrentContext();  
    2. CGContextTranslateCTM(context, 80, self.frame.size.height-60);  
    3. CGContextScaleCTM(context, 1, -1);  

    配合UIPageViewController实现翻页浏览

    1. - (PDFViewController *)viewControllerAtIndex:(NSUInteger)index   
    2. {  
    3.     //Return the PDFViewController for the given index.  
    4.     if (([self.pagePDF count] == 0 )|| (index > [self.pagePDF count]) ) {  
    5.         return nil;  
    6.     }  
    7.       
    8.     //Create a new view controller and pass suitable data.  
    9.     PDFViewController *dataViewController = [[PDFViewController alloc]initWithNibName:@"PDFViewController" bundle:nil];  
    10.     //dataViewController.pdfview = [self.pagePDF objectAtIndex:index];  
    11.     dataViewController.pdfview = [[PDFView alloc]initWithFrame:self.view.frame atPage:index];  
    12.     [dataViewController.view addSubview:dataViewController.pdfview];  
    13.     NSLog(@"index = %d",index);  
    14.     return dataViewController;  
    15. }  
    16.   
    17. - (NSUInteger) indexOfViewController:(PDFViewController *)viewController  
    18. {  
    19.     return [self.pagePDF indexOfObject:viewController.pdfview];  
    20. }  
    21.   
    22. - (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController  
    23. {  
    24.     NSUInteger index = [self indexOfViewController:(PDFViewController *)viewController];  
    25.     if ((index == 0 ) || (index == NSNotFound)){  
    26.         return nil;  
    27.     }  
    28.       
    29.     index--;  
    30.     return  [self viewControllerAtIndex:index];  
    31. }  
    32.   
    33. - (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController  
    34. {  
    35.     NSUInteger index = [self indexOfViewController:(PDFViewController *)viewController];  
    36.     if (index == NSNotFound)  
    37.     {  
    38.         return nil;  
    39.     }  
    40.       
    41.     index++;  
    42.       
    43.     if (index == [self.pagePDF count]){  
    44.         return  nil;  
    45.     }  
    46.       
    47.     return [self viewControllerAtIndex:index];  
    48. }  


  1. CGPDFDocumentRef GetPDFDocumentRef(NSString *filename)  
  2. {  
  3.     CFStringRef path;  
  4.     CFURLRef url;  
  5.     CGPDFDocumentRef document;  
  6.     size_t count;  
  7.       
  8.     path = CFStringCreateWithCString (NULL, [filename UTF8String], kCFStringEncodingUTF8);  
  9.     url = CFURLCreateWithFileSystemPath (NULL, path, kCFURLPOSIXPathStyle, 0);  
  10.       
  11.     CFRelease (path);  
  12.     document = CGPDFDocumentCreateWithURL (url);  
  13.     CFRelease(url);  
  14.     count = CGPDFDocumentGetNumberOfPages (document);  
  15.     if (count == 0) {  
  16.         printf("[%s] needs at least one page!\n", [filename UTF8String]);  
  17.         return NULL;   
  18.     } else {  
  19.         printf("[%ld] pages loaded in this PDF!\n", count);  
  20.     }  
  21.     return document;  
  22. }  
  23.   
  24. void DisplayPDFPage (CGContextRef myContext, size_t pageNumber, NSString *filename)  
  25. {  
  26.     CGPDFDocumentRef document;  
  27.     CGPDFPageRef page;  
  28.       
  29.     document = GetPDFDocumentRef (filename);  
  30.     page = CGPDFDocumentGetPage (document, pageNumber);  
  31.     CGContextDrawPDFPage (myContext, page);  
  32.     CGPDFDocumentRelease (document);  
  33. }  
0 0
原创粉丝点击