本文方法参考了:官方文档。见A function that draw a PDF page的代码部分:
void MyDisplayPDFPage (CGContextRef myContext,
size_t pageNumber,
const char *filename)
{
CGPDFDocumentRef document;
CGPDFPageRef page;
CGRect box;
 
document = MyGetPDFDocumentRef (filename);// 1
page = CGPDFDocumentGetPage (document, pageNumber);// 2
CGContextDrawPDFPage (myContext, page);// 3
CGPDFDocumentRelease (document);// 4
}
可见,编写读取的代码很简单,只需给定三个参数即可。后两个很容易,pageNumber是int型的数字,表示第几页,filename是肯定知道的。问题是如何获取CGContextRef,这个类型对象是用于绘图的上下文对象引用,没有它就没法绘制到屏幕界面上。

查了一下文档,特别是这个帖子:

看来要继承UIView,才能得到当前视图的Context。基本思路是覆盖UIView的drawRect方法,在该方法中:

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

调用UIGraphicsGetCurrentContext方法,将当前的图形上下文设置给调用PDF的代码。drawRect方法会在iOS系统绘制界面的时候调用。

下面来说说编写代码的步骤,首先创建一个view-based application,然后,通过IB,设置控制器到view的关联。

以下不再用IB了,PDF的UIView是通过程序生成的。

创建PdfView类,是UIView的子类。头文件:

1
2
3
4
5
6
7
8
9
#import <Foundation/Foundation.h>
 
@interface PdfView : UIView {
CGPDFDocumentRef pdf;
}
 
-(void)drawInContext:(CGContextRef)context;
 
@end

里面带一个成员,pdf,代表pdf文档对象的引用。一个方法,用于根据图形上下文在视图中绘制制定的pdf页面。

m文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
    #import "PdfView.h"
 
@implementation PdfView
- (id)initWithFrame:(CGRect)frame{
 
if ((self = [super initWithFrame:frame]))
{
// Initialization code
if(self != nil)
{
CFURLRef pdfURL = CFBundleCopyResourceURL(CFBundleGetMainBundle(), CFSTR("test.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()];
}

在这里使用的pdf文档,是放在项目的Resources目录下的。

再往下,就是在Controller中通过程序创建PdfView实例,并将它关联为Controller根视图的子视图:

1
2
3
4
5
6
7
8
9
- (void)viewDidLoad {
[super viewDidLoad];
CGRect frame = CGRectMake(0, 0, 768, 1000);
 
PdfView *pdfView = [[PdfView alloc] initWithFrame:frame];
pdfView.backgroundColor=[UIColor whiteColor];
[self.view addSubview:pdfView];
 
}

这里因为是使用iPad,因此长宽是1000(上面留点空间)和768。另外,需要设置底色,默认情况下底色是黑色的,和黑体的文字在一起就显示不出文字了,我设置的是白色:

pdfView.backgroundColor=[UIColor whiteColor];

这样就可以了,而且中文啥的都没问题。

http://stackoverflow.com/questions/2643150/load-pdf-into-layer-offscreen

最新文章

  1. 图解CSS3制作圆环形进度条的实例教程
  2. Java特性-HashMap
  3. Android实现登录
  4. 如何在Vue2中实现组件props双向绑定
  5. android 完美退出所有Activity的demo
  6. 测试C#代码执行时间
  7. dedecms设置文章分页后,标题会带有序号的解决方法
  8. 安装mysql-python报错
  9. 远程登录Linux服务器修改ssh端口
  10. Python中星号的本质和使用方式
  11. python学习日记(常用模块)
  12. softmax in pytorch
  13. Spark基本架构
  14. [转]Docker基础-使用Dockerfile创建镜像
  15. kettle spoon中“表输入”到“表输出”的乱码问题
  16. datatable的点击事件
  17. CentOS ping: unknown host 解决方法
  18. 网站简介-为什么网站的ICO图标更新后,ie浏览器没有更新过来?
  19. 【区块链Go语言实现】Part 1:区块链基本原型
  20. SQLServer&#160;学习笔记之超详细基础SQL语句&#160;Part&#160;8

热门文章

  1. CSS盒模型总结(一)
  2. Bzoj 1085: [SCOI2005]骑士精神 (dfs)
  3. 概述「并查集补集转化」模型&amp;&amp;luoguP1330 封锁阳光大学
  4. Centos7.2 上部署 FastDFS_V5.05
  5. Python模块概念
  6. (转)Duplicate Symbol链接错的原因总结和解决方法
  7. shell中test的使用
  8. Java-获取Class对象的名称
  9. Centos7 安装python3详细教程,解决升级后不兼容问题
  10. apache下虚拟域名配置