IOS读取和显示PDF文档

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


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值