iOS学习笔记14—PDF的文件的创建、显示和解析

本文详细介绍如何在iOS设备上使用Quartz2D库进行PDF文件的显示与创建。包括PDFView的实现方法及如何通过CGPDFContext绘制PDF文档。
PDF的文件的创建、显示和解析在上一篇文章的这里有介绍:

图形框架:

CoreGraphics.framework

包含 Quartz 2D 绘图 API 接口 。Quartz 是 Mac OS X 系统使用的向量绘图引擎,它支持基于路径绘图、抗锯齿渲染、渐变、图片、颜色、坐标空间转换、PDF 文件的创建、显示和解析。虽然 API 基于 语言,但是它使用基于对象的抽象以表示基本绘图对象,这样可以让开发者可以更方便地保存并复用图像内容。

具体PDF文件的创建、显示和解析在 官方文档里面有,大家可以点击看下。这里介绍的是简单的PDF的显示和创建。

一.PDF的显示

老外写的文章 点击打开链接
首先是要创建一个项目,导入一个.pdf文件
并导入一下这个框架   我总是喜欢在.pch文件中引入

#import<QuartzCore/QuartzCore.h>准备工作完成

先建一个基于UIView的PDFView

在PDFView.h文件中加载

#import <UIKit/UIKit.h>

@interface PDFView : UIView
{
    CGPDFDocumentRef pdf;
}
-(void)drawInContext:(CGContextRef)context;

@end
在PDFView.m文件中加载

#import "PDFView.h"

@implementation PDFView

- (void)dealloc
{
    CGPDFDocumentRelease(pdf);
    [super dealloc];
}


- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        if(self != nil)
        {
            CFURLRef pdfURL = CFBundleCopyResourceURL(CFBundleGetMainBundle(), CFSTR("苹果编码规范.PDF"), NULL, NULL);
            pdf = CGPDFDocumentCreateWithURL((CFURLRef)pdfURL);
            CFRelease(pdfURL);
        }
    }
    return self;
}

-(void)drawInContext:(CGContextRef)context
{
    // PDF page drawing expects a Lower-Left coordinate system, so we flip the coordinate system
    // before we start drawing.
    CGContextTranslateCTM(context, 0.0, self.bounds.size.height);
    CGContextScaleCTM(context, 1.0, -1.0);
    
    // Grab the first PDF page
    CGPDFPageRef page = CGPDFDocumentGetPage(pdf, 1);
    // We're about to modify the context CTM to draw the PDF page where we want it, so save the graphics state in case we want to do more drawing
    CGContextSaveGState(context);
    // CGPDFPageGetDrawingTransform provides an easy way to get the transform for a PDF page. It will scale down to fit, including any
    // base rotations necessary to display the PDF page correctly.
    CGAffineTransform pdfTransform = CGPDFPageGetDrawingTransform(page, kCGPDFCropBox, self.bounds, 0, true);
    // And apply the transform.
    CGContextConcatCTM(context, pdfTransform);
    // Finally, we draw the page and restore the graphics state for further manipulations!
    CGContextDrawPDFPage(context, page);
    CGContextRestoreGState(context);
}

- (void)drawRect:(CGRect)rect {
    [self drawInContext:UIGraphicsGetCurrentContext()];
}



@end


在mainViewController.m文件中

- (void)viewDidLoad
{
    [super viewDidLoad];
    CGRect frame = CGRectMake(0, 0, 320, 480);
    PDFView *pdfView =[[PDFView alloc]initWithFrame:frame];
    [self.view addSubview:pdfView];
    [pdfView release];
}

二.pdf的创建

pdf文档在iOS中是通过Quartz 2D库提供的api来操作的。iOS有两个图形库:

  • Quartz 2D,是iOS原生的,简单易用,缺点是只有2D,仅限于iOS
  • OpenGL ES,是开放标准的,2D和3D均有

使用Quartz 2D绘图到任意图形上下文(graphics context)创建pdf文件是很容易的事情。这需要:

  • 指定pdf文件的位置
  • 设置pdf的图形上下文(graphics context)

写了个特别简单的示例,效果如下:

image

完整的代码如下:

-(void)createPdf{ 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *saveDirectory = [paths objectAtIndex:0]; 
    NSString *saveFileName = @"myPDF.pdf"; 
    NSString *newFilePath = [saveDirectory stringByAppendingPathComponent:saveFileName]; 
    const char *filename = [newFilePath UTF8String]; 
    CGRect pageRect=CGRectMake(0, 0, 612, 792); 
    // This code block sets up our PDF Context so that we can draw to it 
    CGContextRef pdfContext; 
    CFStringRef path; 
    CFURLRef url; 
    CFMutableDictionaryRef myDictionary = NULL; 
    // Create a CFString from the filename we provide to this method when we call it 
    path = CFStringCreateWithCString (NULL, filename, 
                                      kCFStringEncodingUTF8); 
    // Create a CFURL using the CFString we just defined 
    url = CFURLCreateWithFileSystemPath (NULL, path, 
                                         kCFURLPOSIXPathStyle, 0); 
    CFRelease (path); 
    // This dictionary contains extra options mostly for ‘signing’ the PDF 
    myDictionary = CFDictionaryCreateMutable(NULL, 0, 
                                             &kCFTypeDictionaryKeyCallBacks, 
                                             &kCFTypeDictionaryValueCallBacks); 
    CFDictionarySetValue(myDictionary, kCGPDFContextTitle, CFSTR("My PDF File")); 
    CFDictionarySetValue(myDictionary, kCGPDFContextCreator, CFSTR("My Name")); 
    // Create our PDF Context with the CFURL, the CGRect we provide, and the above defined dictionary 
    pdfContext = CGPDFContextCreateWithURL (url, &pageRect, myDictionary); 
    // Cleanup our mess 
    CFRelease(myDictionary); 
    CFRelease(url); 
    // Done creating our PDF Context, now it’s time to draw to it 
    // Starts our first page 
    CGContextBeginPage (pdfContext, &pageRect); 
    // Draws a black rectangle around the page inset by 50 on all sides 
    CGContextStrokeRect(pdfContext, CGRectMake(50, 50, pageRect.size.width – 100, pageRect.size.height – 100)); 
    // Adding some text on top of the image we just added 
    CGContextSelectFont (pdfContext, "Helvetica", 30, kCGEncodingMacRoman); 
    CGContextSetTextDrawingMode (pdfContext, kCGTextFill); 
    CGContextSetRGBFillColor (pdfContext, 0, 0, 0, 1); 
    const char *text =  (char *)[@"Hello world" UTF8String]; 
    CGContextShowTextAtPoint (pdfContext, 260, 390, text, strlen(text)); 
    // End text 
    // We are done drawing to this page, let’s end it 
    // We could add as many pages as we wanted using CGContextBeginPage/CGContextEndPage 
    CGContextEndPage (pdfContext); 
    // We are done with our context now, so we release it 
    CGContextRelease (pdfContext); 
}

 

会在当前应用的Documents目录下创建一个myPDF.pdf文件,文件中只有一行字,Hello world。





苹果官方文档 目录 介绍 3 谁应该阅读本文? 3 先决条件 4 本文的组织 4 提供反馈 4 相关信息 5 核心应用程序 6 核心应用程序架构 6 应用程序的生命周期 6 事件处理周期 9 基本设计模式 11 应用程序运行环境 12 启动过程快,使用时间短 12 应用程序沙箱 13 虚拟内存系统 13 自动休眠定时器 14 应用程序的程序包 14 信息属性列表 16 应用程序图标启动图像 21 Nib文件 21 处理关键的应用程序任务 22 初始化终止 22 响应中断 23 观察低内存警告 25 定制应用程序的行为 25 以景观模式启动 25 其它应用程序进行通讯 26 实现定制的URL模式 27 显示应用程序的偏好设置 31 关闭屏幕锁定 31 国际化您的应用程序 32 性能响应速度的调优 34 不要阻塞主线程 34 有效地使用内存 34 浮点数学运算的考虑 36 减少电力消耗 36 代码的优化 38 窗口视图 39 什么是窗口视图? 39 UIWindow的作用 39 UIView是作用 40 UIKit的视图类 41 视图控制器的作用 43 视图架构几何属性 43 视图交互模型 44 视图渲染架构 46 视图坐标系统 48 边框、边界、中心的关系 49 坐标系统变换 51 内容模式与比例缩放 52 自动尺寸调整行为 54 创建管理视图层次 55 创建一个视图对象 57 添加移除子视图 57 视图层次中的坐标转换 60 标识视图 61 在运行时修改视图 61 实现视图动画 61 响应布局的变化 63 重画视图的内容 64 隐藏视图 65 创建一个定制视图 65 初始化您的定制视图 65 描画您的视图内容 66 响应事件 67 视图对象的清理 68 触摸事件 69 事件触摸 69 事件的传递 71 处理多点触摸事件 73 运动事件 80 拷贝、剪切、粘贴操作 81 UIKit中支持拷贝-粘贴操作的设施 82 粘贴板的概念 82 选择菜单管理 85 拷贝剪切选定的内容 87 粘贴选定内容 89 消除编辑菜单 90 图形描画 91 UIKit的图形系统 91 视图描画周期 91 坐标坐标变换 92 图形上下文 93 点像素的不同 93 颜色颜色空间 94 支持的图像格式 94 描画贴士 95 确定何时使用定制的描画代码 95 提高描画的性能 95 保持图像的质量 96 用QuartzUIKit进行描画 96 配置图形上下文 97 创建描画图像 99 创建描画路径 100 创建样式、渐变、阴影 101 用OpenGL ES进行描画 101 应用Core Animation的效果 101 关于层 102 关于动画 102 文本Web 103 关于文本Web的支持 103 文本视图 103 Web视图 104 键盘输入法 106 管理键盘 107 接收键盘通告 107 显示键盘 109 取消键盘 109 移动键盘下面的内容 109 描画文本 113 在Web视图中显示内容 113 文件网络 114 文件数据管理 114 常用目录 114 备份恢复 115 在应用程序更新过程中被保存的文件 116 Keychain数据 116 获取应用程序目录的路径 117 文件数据的读写 118 文件访问的指导原则 123 保存状态信息 123 大小写敏感性 124 网络 124 有效进行网络通讯的贴士 124 使用Wi-Fi 125 飞行模式警告 125 多媒体支持 127 在iPhone OS上使用声音 127 基础:硬件编解码器、音频格式、音频会话 128 播放音频 131 录制音频 141 解析音频流 144 iPhone OS系统上的音频单元支持 145 iPhone音频的最佳实践 145 在iPhone OS使用视频 147 录制视频 147 播放视频文件 147 设备支持 150 确定硬件支持是否存在 150 配件进行通讯 151 配件的基础 151 声明应用程序支持的协议 152 在运行时连接配件 152 监控与配件有关的事件 154 访问加速计事件 155 选择恰当的更新频率 156 从加速计数据中分离重力成分 157 从加速计数据中分离实时运动成分 157 取得当前设备的方向 158 使用位置方向服务 159 取得用户的当前位置 159 获取与方向有关的事件 161 显示地图注解 163 在用户界面中加入地图视图 163 显示注解 165 通过反向地理编码器获取地标信息 173 用照相机照相 174 从照片库中选取照片 176 使用邮件编辑界面 177 应用程序偏好设置 180 偏好设置的指导原则 180 偏好设置的接口 180 Settings程序包 182 Settings页面文件的格式 183 多层次的偏好设置 183 本地化资源 184 添加修改Settings程序包 185 添加Settings程序包 185 为Settings页面的编辑做准备 185 配置一个Settings页面:一个教程 186 创建额外的Settings页面文件 189 访问您的偏好设置 189 在仿真器中调试应用程序的偏好设置 190 文档修订历史 191
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值