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 ...
随机推荐
- jquery模拟点击A标签的问题
我尝试过多次用jQuery模拟用户点击a标签的功能,但都没有成功,并且困扰了很久. 先看下边的代码: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 <htm ...
- PATH_INFO, SCRIPT_NAME, REQUEST_URI区别示例
- Protobuf 数据类型
.proto Type Notes C++ Type Java Type double double double float float float int32 Uses var ...
- java反射子之获取方法信息(二)
一.获取方法 1.方法作用. 2. 二.获取方法信息.(修饰符,返回值,方法名称,参数列表,抛出的异常). ############################################## ...
- crontab + rsyncd同步方案
目的主机: rsync --daemon [root@iZ23ohdbxmrZ ~]# vim /etc/rsyncd.conf #global settingsport = 873pid file= ...
- Learning Phrase Representations using RNN Encoder–Decoder for Statistical Machine Translation
1.主要完成的任务是能够将英文转译为法文,使用了一个encoder-decoder模型,在encoder的RNN模型中是将序列转化为一个向量.在decoder中是将向量转化为输出序列,使用encode ...
- 《Java程序设计》实验1实验报告
20145318 <Java程序设计>实验1实验报告 实验题目 通过对500个数据进行操作,实现快速排序.选择排序.直接插入排序算法时间复杂度的比较:并在排序数据中快速查找某一数据,给出查 ...
- linux安全第一周总结——20135227黄晓妍
实验部分: 我将源代码做了修改,将其中一个数字修改为我学号27 2.在实验楼环境下将其保存为text.c并将其编译,得到text.s 3.将.开头的多余的语句删去了之后,我得到了32位环境的汇编代码 ...
- 一文弄懂神经网络中的反向传播法——BackPropagation【转】
本文转载自:https://www.cnblogs.com/charlotte77/p/5629865.html 一文弄懂神经网络中的反向传播法——BackPropagation 最近在看深度学习 ...
- LeetCode——Arithmetic Slices
Question A sequence of number is called arithmetic if it consists of at least three elements and if ...