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

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

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

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

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

IOS加载PDF文件的更多相关文章

  1. cordova程序加载pdf文件的2种方法(ios/android)

    前言 公司目前的前端架构是微信端由vue全家桶负责h5网站的单页应用,android端和ios端则选择cordova打包成apk和app.其中,有一个业务逻辑是点击某个链接进入pdf的展示,h5的方案 ...

  2. Android 如何本地加载pdf文件

    大部分app打开pdf文件是通过intent调起手机中能打开pdf文件的工具,来查看pdf文件,如果需求是,用户在app内下载好pdf文件后,不通过第三方的工具,本地打开. 这样的需求要怎么实现呢?上 ...

  3. iOS 加载pdf格式的文件

    可以加载的方式比较多,暂时先总结两种: 本地先导入一份pdf文件 type 1: 利用UIWebView加载 UIWebView *webView = [[UIWebView alloc] initW ...

  4. 使用 pdf.js 在网页中加载 pdf 文件

    在网页中加载并显示PDF文件是最常见的业务需求.例如以下应用场景:(1)在电商网站上购物之后,下载电子发票之前先预览发票.(2)电子商务管理系统中查看发布的公文,公文文件一般是PDF格式的文件. 目前 ...

  5. .net使用pdfobject.js加载pdf文件

    1.下载pdfobject.js文件 2. <script type="text/javascript" src="<%= Application[" ...

  6. webView 加载本地文件 - html/htm pdf docx tx

    - (void)viewDidLoad { [super viewDidLoad]; [self setupUI]; NSString *path = [[NSBundle mainBundle] p ...

  7. 在iPhoneApp中加载PDF

    原文: http://ios.biomsoft.com/2012/02/17/load-a-pdf-file-in-the-iphone-app-smoothly/ 本节将学习如何从服务器加载 pdf ...

  8. ios – 使用UINib加载xib文件实现UITableViewCell

    xib文件的实质是xml,描述界面对象,每个对象都有一个很重要的属性,identity inspector面板中class属性,加载xib文件的时候实际上是实例化界面对象相对应的这些class. xi ...

  9. Spire.XLS,生成Excel文件、加载Excel文件

    一.组件介绍 Spire.XLS是E-iceblue开发的一套基于企业级的专业Office文档处理的组件之一,全称Spire.Office for .NET.旗下有Spire.Doc,Spire XL ...

随机推荐

  1. 360急速浏览器JS的调试

    1.代码中添加debugger关键字 2.360急速浏览器中选择工具--开发人员选项,如下图所示,在sources面板中可以看到正在运行的JS代码,F10可以单步函数执行,在鼠标放在变量上可以跟踪变量 ...

  2. PKU 1094 Sorting It All Out(拓扑排序)

    题目大意:就是给定一组字母的大小关系判断他们是否能组成唯一的拓扑序列. 是典型的拓扑排序,但输出格式上确有三种形式: 1.该字母序列有序,并依次输出: 2.判断该序列是否唯一: 3.该序列字母次序之间 ...

  3. JavaScript 操作JSON总结

    JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,采用完全独立于语言的文本格式,是理想的数据交换格式.同时,JSON是 JavaScript 原生格式,这意 ...

  4. java容器的数据结构-ArrayList,LinkList,HashMap

    ArrayList: 初始容量为10,底层实现是一个数组,Object[] elementData 自动扩容机制,当添加一个元素时,数组长度超过了elementData.leng,则会按照1.5倍进行 ...

  5. Android开发环境配置到第一个程序所遇到的问题

    1.安装顺序 先jdk,最后是1.7或1.8吧,配置环境变量:然后是sdk,配置环境变量:sdk安装之后即可以通过SDK Manager进行其他包的安装. 2.sdk及其他包的安装,以一张图表示,对于 ...

  6. 【Python】装饰器 & 偏函数

    [装饰器] 1.最简单的Decorator. def author(f): def addName(): print('My name is xkfx.\n') f() return addName ...

  7. ionic android - Unable to start the daemon process. Could not reserve enough space for 2097152KB object heap

    Unzipping C:\Users\app\.gradle\wrapper\dists\gradle-4.1-all\bzyivzo6n839fup2jbap0tjew\gradle-4.1-all ...

  8. vs LNK2019 无法解析的外部符号 ***,该符号在函数 WinMain 中被引用

    一般链接错误都是因为包含头文件与lib库不匹配(无导出函数.lib库的release debug版本混乱.库引用的优先级.编译器设置mt/mtd等等)造成的. 错误    LNK2019    无法解 ...

  9. 20145240《网络对抗》逆向及Bof基础实践

    逆向及Bof基础实践 1.1 实践目标 本次实践的对象是一个名为pwn1的linux可执行文件. 该程序正常执行流程是:main调用foo函数,foo函数会简单回显任何用户输入的字符串. 该程序同时包 ...

  10. linux系统调用是通过软中断实现的吗

    软中断是利用硬件中断的概念,用软件方式进行模拟,实现宏观上的异步执行效果.很多情况下,软中断和信号有些类似,同时,软中断又是和硬中断相对应的,硬中断是外部设备对CPU的中断,软中断通常是硬中断服务程序 ...