zoukankan      html  css  js  c++  java
  • IOS加载PDF文件

    今天的任务是:在iOS上加载显示pdf文件。

    方法一:利用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. }  
    [cpp] view plain copy
    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. }  


    利:1.实现简单

            2.还是实现简单

    弊:1.仅能浏览,拿不到任何回调,safari不会鸟任何人。

            2.固定竖版拖动,想实现翻页动效果就扒瞎

            

            下面的方法可以解决webview 显示pdf的弊,相对的,要付出一些汗水作为代价了。

    方法二:利用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! ", [filename UTF8String]);  
    17.         return NULL;   
    18.     } else {  
    19.         printf("[%ld] pages loaded in this PDF! ", 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. }  
    [cpp] view plain copy
    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! ", [filename UTF8String]);  
    17.         return NULL;   
    18.     } else {  
    19.         printf("[%ld] pages loaded in this PDF! ", 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);  
    [cpp] view plain copy
    1. CGContextRef context = UIGraphicsGetCurrentContext();  
    2. CGContextTranslateCTM(context, 80, self.frame.size.height-60);  
    3. CGContextScaleCTM(context, 1, -1);  



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


    后续将完成涂鸦pdf后保存创建新pdf的功能。

    转载:http://blog.csdn.net/wanglang3081/article/details/7663624

  • 相关阅读:
    Kubernetes进阶实战读书笔记:持久化存储卷(pvc详解)
    Nginx核心知识100讲学习笔记(陶辉)详解HTTP模块(接受请求模块|正则表达式|冲突合并)
    Nginx核心知识100讲学习笔记(陶辉)详解HTTP模块(详解11阶段)
    WIndows 编辑的文件的文件在Linux下查看每行末尾有 ^M
    JQuery ajax 使用
    查看oracle死锁进程并结束死锁
    Oracle 死锁处理(查看锁表语句及解锁)
    BS表单编程注意事项
    Delphi MDI多文档架构几个问题解决
    Delphi MDI子窗体
  • 原文地址:https://www.cnblogs.com/oc-bowen/p/5570104.html
Copyright © 2011-2022 走看看