关于PDF的读取与绘制
2024-09-07 12:33:20
本文方法参考了:官方文档。见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 |
- (void)drawRect:(CGRect)rect { |
调用UIGraphicsGetCurrentContext方法,将当前的图形上下文设置给调用PDF的代码。drawRect方法会在iOS系统绘制界面的时候调用。
下面来说说编写代码的步骤,首先创建一个view-based application,然后,通过IB,设置控制器到view的关联。
以下不再用IB了,PDF的UIView是通过程序生成的。
创建PdfView类,是UIView的子类。头文件:
1 |
#import <Foundation/Foundation.h> |
里面带一个成员,pdf,代表pdf文档对象的引用。一个方法,用于根据图形上下文在视图中绘制制定的pdf页面。
m文件:
1 |
#import "PdfView.h" |
在这里使用的pdf文档,是放在项目的Resources目录下的。
再往下,就是在Controller中通过程序创建PdfView实例,并将它关联为Controller根视图的子视图:
1 |
- (void)viewDidLoad { |
这里因为是使用iPad,因此长宽是1000(上面留点空间)和768。另外,需要设置底色,默认情况下底色是黑色的,和黑体的文字在一起就显示不出文字了,我设置的是白色:
pdfView.backgroundColor=[UIColor whiteColor];
这样就可以了,而且中文啥的都没问题。
http://stackoverflow.com/questions/2643150/load-pdf-into-layer-offscreen
最新文章
- 图解CSS3制作圆环形进度条的实例教程
- Java特性-HashMap
- Android实现登录
- 如何在Vue2中实现组件props双向绑定
- android 完美退出所有Activity的demo
- 测试C#代码执行时间
- dedecms设置文章分页后,标题会带有序号的解决方法
- 安装mysql-python报错
- 远程登录Linux服务器修改ssh端口
- Python中星号的本质和使用方式
- python学习日记(常用模块)
- softmax in pytorch
- Spark基本架构
- [转]Docker基础-使用Dockerfile创建镜像
- kettle spoon中“表输入”到“表输出”的乱码问题
- datatable的点击事件
- CentOS ping: unknown host 解决方法
- 网站简介-为什么网站的ICO图标更新后,ie浏览器没有更新过来?
- 【区块链Go语言实现】Part 1:区块链基本原型
- SQLServer&#160;学习笔记之超详细基础SQL语句&#160;Part&#160;8
热门文章
- CSS盒模型总结(一)
- Bzoj 1085: [SCOI2005]骑士精神 (dfs)
- 概述「并查集补集转化」模型&;&;luoguP1330 封锁阳光大学
- Centos7.2 上部署 FastDFS_V5.05
- Python模块概念
- (转)Duplicate Symbol链接错的原因总结和解决方法
- shell中test的使用
- Java-获取Class对象的名称
- Centos7 安装python3详细教程,解决升级后不兼容问题
- apache下虚拟域名配置