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 ...
随机推荐
- 360急速浏览器JS的调试
1.代码中添加debugger关键字 2.360急速浏览器中选择工具--开发人员选项,如下图所示,在sources面板中可以看到正在运行的JS代码,F10可以单步函数执行,在鼠标放在变量上可以跟踪变量 ...
- PKU 1094 Sorting It All Out(拓扑排序)
题目大意:就是给定一组字母的大小关系判断他们是否能组成唯一的拓扑序列. 是典型的拓扑排序,但输出格式上确有三种形式: 1.该字母序列有序,并依次输出: 2.判断该序列是否唯一: 3.该序列字母次序之间 ...
- JavaScript 操作JSON总结
JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,采用完全独立于语言的文本格式,是理想的数据交换格式.同时,JSON是 JavaScript 原生格式,这意 ...
- java容器的数据结构-ArrayList,LinkList,HashMap
ArrayList: 初始容量为10,底层实现是一个数组,Object[] elementData 自动扩容机制,当添加一个元素时,数组长度超过了elementData.leng,则会按照1.5倍进行 ...
- Android开发环境配置到第一个程序所遇到的问题
1.安装顺序 先jdk,最后是1.7或1.8吧,配置环境变量:然后是sdk,配置环境变量:sdk安装之后即可以通过SDK Manager进行其他包的安装. 2.sdk及其他包的安装,以一张图表示,对于 ...
- 【Python】装饰器 & 偏函数
[装饰器] 1.最简单的Decorator. def author(f): def addName(): print('My name is xkfx.\n') f() return addName ...
- 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 ...
- vs LNK2019 无法解析的外部符号 ***,该符号在函数 WinMain 中被引用
一般链接错误都是因为包含头文件与lib库不匹配(无导出函数.lib库的release debug版本混乱.库引用的优先级.编译器设置mt/mtd等等)造成的. 错误 LNK2019 无法解 ...
- 20145240《网络对抗》逆向及Bof基础实践
逆向及Bof基础实践 1.1 实践目标 本次实践的对象是一个名为pwn1的linux可执行文件. 该程序正常执行流程是:main调用foo函数,foo函数会简单回显任何用户输入的字符串. 该程序同时包 ...
- linux系统调用是通过软中断实现的吗
软中断是利用硬件中断的概念,用软件方式进行模拟,实现宏观上的异步执行效果.很多情况下,软中断和信号有些类似,同时,软中断又是和硬中断相对应的,硬中断是外部设备对CPU的中断,软中断通常是硬中断服务程序 ...