IOS加载PDF文件
今天的任务是:在iOS上加载显示pdf文件。
方法一:利用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];
- }
- -(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];
- }
利:1.实现简单
2.还是实现简单
弊:1.仅能浏览,拿不到任何回调,safari不会鸟任何人。
2.固定竖版拖动,想实现翻页动效果就扒瞎

下面的方法可以解决webview 显示pdf的弊,相对的,要付出一些汗水作为代价了。
方法二:利用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);
- }
- 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);
- }
这样显示出来的pdf单页是倒立的,Quartz坐标系和UIView坐标系不一样所致,调整坐标系,使pdf正立:
- CGContextRef context = UIGraphicsGetCurrentContext();
- CGContextTranslateCTM(context, 80, self.frame.size.height-60);
- CGContextScaleCTM(context, 1, -1);
- CGContextRef context = UIGraphicsGetCurrentContext();
- CGContextTranslateCTM(context, 80, self.frame.size.height-60);
- CGContextScaleCTM(context, 1, -1);
配合iOS5强大的UIPageViewController实现翻页浏览
- - (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];
- }
- -
- (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];
- }


后续将完成涂鸦pdf后保存创建新pdf的功能。
转载:http://blog.csdn.net/wanglang3081/article/details/7663624
IOS加载PDF文件的更多相关文章
- cordova程序加载pdf文件的2种方法(ios/android)
前言 公司目前的前端架构是微信端由vue全家桶负责h5网站的单页应用,android端和ios端则选择cordova打包成apk和app.其中,有一个业务逻辑是点击某个链接进入pdf的展示,h5的方案 ...
- Android 如何本地加载pdf文件
大部分app打开pdf文件是通过intent调起手机中能打开pdf文件的工具,来查看pdf文件,如果需求是,用户在app内下载好pdf文件后,不通过第三方的工具,本地打开. 这样的需求要怎么实现呢?上 ...
- iOS 加载pdf格式的文件
可以加载的方式比较多,暂时先总结两种: 本地先导入一份pdf文件 type 1: 利用UIWebView加载 UIWebView *webView = [[UIWebView alloc] initW ...
- 使用 pdf.js 在网页中加载 pdf 文件
在网页中加载并显示PDF文件是最常见的业务需求.例如以下应用场景:(1)在电商网站上购物之后,下载电子发票之前先预览发票.(2)电子商务管理系统中查看发布的公文,公文文件一般是PDF格式的文件. 目前 ...
- .net使用pdfobject.js加载pdf文件
1.下载pdfobject.js文件 2. <script type="text/javascript" src="<%= Application[" ...
- webView 加载本地文件 - html/htm pdf docx tx
- (void)viewDidLoad { [super viewDidLoad]; [self setupUI]; NSString *path = [[NSBundle mainBundle] p ...
- 在iPhoneApp中加载PDF
原文: http://ios.biomsoft.com/2012/02/17/load-a-pdf-file-in-the-iphone-app-smoothly/ 本节将学习如何从服务器加载 pdf ...
- ios – 使用UINib加载xib文件实现UITableViewCell
xib文件的实质是xml,描述界面对象,每个对象都有一个很重要的属性,identity inspector面板中class属性,加载xib文件的时候实际上是实例化界面对象相对应的这些class. xi ...
- Spire.XLS,生成Excel文件、加载Excel文件
一.组件介绍 Spire.XLS是E-iceblue开发的一套基于企业级的专业Office文档处理的组件之一,全称Spire.Office for .NET.旗下有Spire.Doc,Spire XL ...
随机推荐
- Java基础知识陷阱(二)
本文发表于本人博客. 上次说了一些关于字符串的知识,都是比较基础的,那这次也说下关于对象地址问题,比如传参.先看下面代码: public void changeInt(int a){ a = ; } ...
- 从两道题看go channel的用法
在知乎看到有人分享了几道笔试题,自己总结了一下其中与channel有关的题目.全部题目在这里: https://zhuanlan.zhihu.com/p/35058068 题目 5.请找出下面代码的问 ...
- 『NiFi 学习之路』简介
『NiFi 学习之路』简介 『NiFi 学习之路』入门 -- 下载.安装与简单使用 『NiFi 学习之路』资源 -- 资料汇总 『NiFi 学习之路』把握 -- 架构及主要组件 『NiFi 学习之路』 ...
- Spring-1-F Dice(HDU 5012)解题报告及测试数据
Dice Time Limit:1000MS Memory Limit:65536KB Description There are 2 special dices on the table. ...
- NGUI混合FingerGesture《卷一》 统一坐标
问题背景 使用FingerGesture 获取触碰点2D坐标, 将该2D坐标赋值给NGUI元素,发现位置出现偏差. 排查思路 1:NGUI的 (0,0,0)默认位置是在屏幕正中心.而FingerGes ...
- TOSCA自动化测试工具--new Project
1.在默认页面点击Project 进入new project页面 2.第一步出来的页面上点击new 3. 第2步弹出的页面上选择路径,project 名 3.OK之后就创建好了
- Ubuntu 14.04.5 imx6 开发环境搭建
1,下载VMware Workstation虚拟机 地址:http://1.xp510.com:801/xp2011/VMware10.7z 2,下载Ubuntu 14.04.5 LTS 32位Ubu ...
- window连接linux共享
前提说明:windows主机信息:192.168.1.100 帐号:abc 密码:123 共享文件夹:sharelinux主机信息:192.168.1.200 帐号:def 密码:456 共享文件夹: ...
- Spring编码过滤器:解决中文乱码
Spring编码过滤器:解决中文乱码 针对问题: 前台JSP页面和JAVA代码中使用了不同的字符集进行编码的时候就会出现表单提交的数据或者上传/下载中文名称文件出现乱码的问题: 解决方案: web.x ...
- FactoryBean
总结自:https://www.cnblogs.com/davidwang456/p/3688250.html Spring中有两种类型的Bean,一种是普通Bean,另一种是工厂Bean,即xxxF ...