iOS开发CoreGraphics核心图形框架之九——PDF文件的渲染与创建(一)

简介: iOS开发CoreGraphics核心图形框架之九——PDF文件的渲染与创建

一、渲染已有的PDF文档


   在CoreGraphics框架中,有两个类型与PDF文档的渲染有关,分别为CGPDFDocumentRef与CGPDFPageRef。其中,CGPDFDocumentRef对应整个PDF文档,里面封装了许多文档相关的信息,CGPDFPageRef对应PDF文档中某一页的内容,通过它开发者可以将PDF内容通过CGContext上下文渲染到指定目标上。


   如下代码演示了在自定义View的drawRect:方法中进行PDF文档的绘制:


-(void)drawRect:(CGRect)rect{

   //由于坐标系不同,需要进行翻转

   CGContextRef contextRef = UIGraphicsGetCurrentContext();

   //进行坐标系的翻转

   CGContextTranslateCTM(contextRef, 0, rect.size.height);

   CGContextScaleCTM(contextRef, 1.0, -1.0);

   //获取pdf文件的路径

   NSString * path = [[NSBundle mainBundle] pathForResource:@"MyText" ofType:@"pdf"];

   CFStringRef pathString = CFStringCreateWithCString(NULL, [path cStringUsingEncoding:NSUTF8StringEncoding], kCFStringEncodingUTF8);

   //创建url

   CFURLRef url = CFURLCreateWithFileSystemPath(NULL, pathString, kCFURLPOSIXPathStyle, 0);

   CFRelease(pathString);

   //进行CGPDFDocumentRef引用的创建

   CGPDFDocumentRef document = CGPDFDocumentCreateWithURL(url);

   CFRelease(url);

   //获取文档的第1页

   CGPDFPageRef page1 = CGPDFDocumentGetPage(document, 1);

   //进行绘制

   CGContextDrawPDFPage(contextRef, page1);

   CGPDFPageRelease(page1);

   CGPDFDocumentRelease(document);

}

效果如下:

image.png



CGPDFDocument中提供的方法解析如下:


//通过数据提供者类来创建PDF文档对象

CGPDFDocumentRef  CGPDFDocumentCreateWithProvider(CGDataProviderRef cg_nullable provider);

//通过url来创建PDF文档

CGPDFDocumentRef  CGPDFDocumentCreateWithURL(CFURLRef cg_nullable url);

//进行引用计数+1

CGPDFDocumentRef  CGPDFDocumentRetain(CGPDFDocumentRef cg_nullable document);

//进行引用计数-1,需要注意,其作用和CFRelease()相似,不同的是如果document为NULL,不是发生crash

void CGPDFDocumentRelease(CGPDFDocumentRef cg_nullable document);

//获取PDF文档的版本

void CGPDFDocumentGetVersion(CGPDFDocumentRef cg_nullable document, int *  majorVersion, int *  minorVersion);

//判断文档是否是加密的

bool CGPDFDocumentIsEncrypted(CGPDFDocumentRef cg_nullable document);

//使用密码对PDF文档进行解密 返回值为1表示解密成功

bool CGPDFDocumentUnlockWithPassword(CGPDFDocumentRef cg_nullable document, const char *  password);

//判断PDF文档是否已经解锁

bool CGPDFDocumentIsUnlocked(CGPDFDocumentRef cg_nullable document);

//获取此PDF文档是否允许绘制

bool CGPDFDocumentAllowsPrinting(CGPDFDocumentRef cg_nullable document);

//获取此文档是否允许拷贝

bool CGPDFDocumentAllowsCopying(CGPDFDocumentRef cg_nullable document);

//获取PDF文档的总页数

size_t CGPDFDocumentGetNumberOfPages(CGPDFDocumentRef cg_nullable document);

//获取文档中某页数据

CGPDFPageRef __nullable CGPDFDocumentGetPage(CGPDFDocumentRef cg_nullable document, size_t pageNumber);

//获取文档的目录信息

CGPDFDictionaryRef __nullable CGPDFDocumentGetCatalog(CGPDFDocumentRef cg_nullable document);

//获取文档详情信息

CGPDFDictionaryRef __nullable CGPDFDocumentGetInfo(CGPDFDocumentRef cg_nullable document);

//获取文档id

CGPDFArrayRef __nullable CGPDFDocumentGetID(CGPDFDocumentRef cg_nullable document);

//获取CGPDFDocument类在CoreGraphics框架中的id

CFTypeID CGPDFDocumentGetTypeID(void);


CGPDFDocument中还有一些已经弃用的方法,这些方法现在封装在CGPDFPage中,弃用的方法如下:


CGRect CGPDFDocumentGetMediaBox(CGPDFDocumentRef cg_nullable document,int page);

CGRect CGPDFDocumentGetCropBox(CGPDFDocumentRef cg_nullable document, int page);

CGRect CGPDFDocumentGetBleedBox(CGPDFDocumentRef cg_nullable document, int page);

CGRect CGPDFDocumentGetTrimBox(CGPDFDocumentRef cg_nullable document, int page);

CGRect CGPDFDocumentGetArtBox(CGPDFDocumentRef cg_nullable document, int page);

int CGPDFDocumentGetRotationAngle(CGPDFDocumentRef cg_nullable document, int page);

CGPDFPage中的主要方法列举如下:


//进行引用计数+1

CGPDFPageRef CGPDFPageRetain(CGPDFPageRef cg_nullable page);

//进行引用计数-1

void CGPDFPageRelease(CGPDFPageRef cg_nullable page);

//获取对应的PDF文档对象

CGPDFDocumentRef __nullable CGPDFPageGetDocument(CGPDFPageRef cg_nullable page);

//获取当前页是文档中的第几页

size_t CGPDFPageGetPageNumber(CGPDFPageRef cg_nullable page);

//获取与文档此页相关联的媒体区域

/*

typedef CF_ENUM (int32_t, CGPDFBox) {

 kCGPDFMediaBox = 0,

 kCGPDFCropBox = 1,

 kCGPDFBleedBox = 2,

 kCGPDFTrimBox = 3,

 kCGPDFArtBox = 4

};

*/

CGRect CGPDFPageGetBoxRect(CGPDFPageRef cg_nullable page, CGPDFBox box);

//获取此页的旋转角度

int CGPDFPageGetRotationAngle(CGPDFPageRef cg_nullable page);

//transform变换

CGAffineTransform CGPDFPageGetDrawingTransform(CGPDFPageRef cg_nullable page, CGPDFBox box, CGRect rect, int rotate, bool preserveAspectRatio);


目录
相关文章
|
2月前
|
XML 缓存 JSON
为什么浏览器中有些图片、PDF等文件点击后有些是预览,有些是下载
为什么浏览器中有些图片、PDF等文件点击后有些是预览,有些是下载
148 0
|
8天前
|
Python
Python办公自动化:提取pdf文件中的图片
Python办公自动化:提取pdf文件中的图片
11 0
|
10天前
pdf文件转化为png照片 (PyMuPDF下面的fitz)
pdf文件转化为png照片 (PyMuPDF下面的fitz)
|
2月前
|
移动开发 资源调度 JavaScript
Vue移动端网页(H5)预览pdf文件(pdfh5和vue-pdf)
这篇文章介绍了在Vue移动端网页中使用`pdfh5`和`vue-pdf`两个插件来实现PDF文件的预览,包括滚动查看、缩放、添加水印、分页加载、跳转指定页数等功能。
193 0
Vue移动端网页(H5)预览pdf文件(pdfh5和vue-pdf)
|
2月前
|
JSON JavaScript 数据格式
打印插件 hiprint 使用、回单打印PDF保存本地、将列表数据打印成pdf文件保存到本地
这篇文章介绍了如何使用hiprint打印插件将列表数据打印成PDF文件并保存到本地,包括插件的配置、依赖安装、项目代码案例以及如何预览和打印数据。
打印插件 hiprint 使用、回单打印PDF保存本地、将列表数据打印成pdf文件保存到本地
|
2月前
|
C# 开发者 Windows
WPF与PDF文档:解锁创建和编辑PDF文件的新技能——从环境配置到代码实践,手把手教你如何在WPF应用中高效处理PDF,提升文档管理效率
【8月更文挑战第31天】随着数字文档的普及,PDF因跨平台兼容性和高保真度成为重要格式。WPF虽不直接支持PDF处理,但借助第三方库(如iTextSharp)可在WPF应用中实现PDF的创建与编辑。本文通过具体案例和示例代码,详细介绍了如何在WPF中集成PDF库,并展示了从设计用户界面到实现PDF创建与编辑的完整流程。不仅包括创建新文档的基本步骤,还涉及在现有PDF中添加页眉页脚等高级功能。通过这些示例,WPF开发者可以更好地掌握PDF处理技术,提升应用程序的功能性和实用性。
47 0
|
2月前
|
Java
JAVA PDF 截取N页,生成新文件,转图片,多个PDF 合并
JAVA PDF 截取N页,生成新文件,转图片,多个PDF 合并
69 0
|
2月前
|
Linux Python Windows
Python PDF文件转Word格式,只需要3秒(附打包)
Python PDF文件转Word格式,只需要3秒(附打包)
63 3
Python PDF文件转Word格式,只需要3秒(附打包)
|
2月前
|
Python
Python——批量将PDF文件转为图片
Python——批量将PDF文件转为图片
32 2
|
2月前
|
Python
Python——将PPT和Word转为PDF文件
Python——将PPT和Word转为PDF文件
47 1
下一篇
无影云桌面