今天的任务是:在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. Java基础知识陷阱(二)

    本文发表于本人博客. 上次说了一些关于字符串的知识,都是比较基础的,那这次也说下关于对象地址问题,比如传参.先看下面代码: public void changeInt(int a){ a = ; } ...

  2. 从两道题看go channel的用法

    在知乎看到有人分享了几道笔试题,自己总结了一下其中与channel有关的题目.全部题目在这里: https://zhuanlan.zhihu.com/p/35058068 题目 5.请找出下面代码的问 ...

  3. 『NiFi 学习之路』简介

    『NiFi 学习之路』简介 『NiFi 学习之路』入门 -- 下载.安装与简单使用 『NiFi 学习之路』资源 -- 资料汇总 『NiFi 学习之路』把握 -- 架构及主要组件 『NiFi 学习之路』 ...

  4. Spring-1-F Dice(HDU 5012)解题报告及测试数据

    Dice Time Limit:1000MS     Memory Limit:65536KB Description There are 2 special dices on the table. ...

  5. NGUI混合FingerGesture《卷一》 统一坐标

    问题背景 使用FingerGesture 获取触碰点2D坐标, 将该2D坐标赋值给NGUI元素,发现位置出现偏差. 排查思路 1:NGUI的 (0,0,0)默认位置是在屏幕正中心.而FingerGes ...

  6. TOSCA自动化测试工具--new Project

    1.在默认页面点击Project 进入new project页面 2.第一步出来的页面上点击new 3. 第2步弹出的页面上选择路径,project 名 3.OK之后就创建好了

  7. Ubuntu 14.04.5 imx6 开发环境搭建

    1,下载VMware Workstation虚拟机 地址:http://1.xp510.com:801/xp2011/VMware10.7z 2,下载Ubuntu 14.04.5 LTS 32位Ubu ...

  8. window连接linux共享

    前提说明:windows主机信息:192.168.1.100 帐号:abc 密码:123 共享文件夹:sharelinux主机信息:192.168.1.200 帐号:def 密码:456 共享文件夹: ...

  9. Spring编码过滤器:解决中文乱码

    Spring编码过滤器:解决中文乱码 针对问题: 前台JSP页面和JAVA代码中使用了不同的字符集进行编码的时候就会出现表单提交的数据或者上传/下载中文名称文件出现乱码的问题: 解决方案: web.x ...

  10. FactoryBean

    总结自:https://www.cnblogs.com/davidwang456/p/3688250.html Spring中有两种类型的Bean,一种是普通Bean,另一种是工厂Bean,即xxxF ...