本文方法参考了:官方文档。见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

关于PDF的读取与绘制的更多相关文章

  1. Java 在PDF文档中绘制图形

    本篇文档将介绍通过Java编程在PDF文档中绘制图形的方法.包括绘制矩形.椭圆形.不规则多边形.线条.弧线.曲线.扇形等等.针对方法中提供的思路,也可以自行变换图形设计思路,如菱形.梯形或者组合图形等 ...

  2. MeteoInfoLab脚本示例:读取文本文件绘制散度图

    MeteoInfoLab中读取文本文件数据的函数是asciiread,获取文本文件行.列数的函数是numasciirow和numasciicol,和NCL中函数名一致,但都是小写字母.本例中的示例数据 ...

  3. 【opencv】c++ 读取图片 & 绘制点 & 绘制文字 & 保存图片

    //read pic ]; sprintf(path, "%s%d/%s", image_dir.c_str(), cam_num, filename.c_str()); cv:: ...

  4. PDF.JS 读取文件流前端展示 C#

    最近再搞PDF得展示问题,因为aspose.pdf成本太高,只能使用pdf.js这个开源强大的前端东东了. 在百度了很久后 网上大都是node,java,php的事例,有位大哥的是C#的后台代码按他写 ...

  5. iOS:quartz2D绘图(显示绘制在PDF上的图片)

    quart2D既可以用来绘制图像到pdf上,也可以从pdf上读取图像并显示出来.在使用这种方式之前,还有一种方式可以用来读取显示pdf上的图像,即使用UIWebView网页视图控件- (void)lo ...

  6. C#生成PDF文档,读取TXT文件内容

    using System.IO;using iTextSharp.text;using iTextSharp.text.pdf; //需要在项目里引用ICSharpCode.SharpZipLib.d ...

  7. C# 在PDF中绘制动态图章

    我们知道,动态图章,因图章中的时间.日期可以动态的生成,因而具有较强的时效性.在本篇文章中将介绍通过C#编程在PDF中绘制动态图章的方法,该方法可自动获取当前系统登录用户名.日期及时间信息并生成图章. ...

  8. C# 绘制PDF图形——基本图形、自定义图形、色彩透明度

    引言 在PDF中我们可以通过C#程序代码来添加非常丰富的元素来呈现我们想要表达的内容,如绘制表格.文字,添加图形.图像等等.在本篇文章中,我将介绍如何在PDF中绘制图形,并设置图形属性的操作. 文章中 ...

  9. pdf及word文档的读取 pyPDF2,docx

    #!python3 #-*- coding:utf8 -*- #PyPDF2可能会打不开某些pdf文档,也不能提取图片,图表或者其他媒介从PDF文件中.但是它能提取文本从PDF中,转化为字符. imp ...

随机推荐

  1. 121. Best Time to Buy and Sell Stock@python

    Say you have an array for which the ith element is the price of a given stock on day i. If you were ...

  2. CSS在线压缩

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  3. perl学习二:简单变量

    字符串变量:${}1.单引号:不进行变量替换,不进行转义,字符串可以跨行.2.双引号:变量替换(贪婪匹配原则).支持转义字符(转义字符可以另外看)3.反引号 字符串的特殊表示方法:qq(...) q( ...

  4. python爬虫基础03-requests库

    优雅到骨子里的Requests 本文地址:https://www.jianshu.com/p/678489e022c8 简介 上一篇文章介绍了Python的网络请求库urllib和urllib3的使用 ...

  5. Python爬虫-字体反爬-猫眼国内票房榜

    偶然间知道到了字体反爬这个东西, 所以决定了解一下. 目标:  https://maoyan.com/board/1 问题: 类似下图中的票房数字无法获取, 直接复制粘贴的话会显示 □ 等无法识别的字 ...

  6. Python基础知识-day2

    格式化输出 %占位符,s字符串,d 数字, 表示%  用%% name = input("请输入姓名: ") age = input("请输入年龄: ") he ...

  7. 快速入门Matplotlib

    十分钟快速入门Matplotlib 函数式绘图 这个库主要有两种绘图方式,一种是像这样的类matlab的函数式绘图方法. import matplotlib.pyplot as plt import ...

  8. UnicodeDecodeError: 'gbk' codec can't decode byte 0xae in position 167: illegal multibyte sequence

    UnicodeDecodeError: 'gbk' codec can't decode byte 0xae in position 167: illegal multibyte sequence文件 ...

  9. 三丶人生苦短,我用python【第三篇】 pycharm

    1 pycharm的下载安装 下载地址:https://www.jetbrains.com/pycharm/download/#section=windows ....安装没啥好说的 建议购买正版,其 ...

  10. ActionProxy相关实现类

    package com.opensymphony.xwork2; import com.opensymphony.xwork2.config.Configuration; import com.ope ...