现在的位置: 首页 > 综合 > 正文

iphone PDF 解析

2012年07月30日 ⁄ 综合 ⁄ 共 2233字 ⁄ 字号 评论关闭

#import <UIKit/UIKit.h>

 

@class PDFTestViewController;

@interface PDFView : UIView {

 //这个类封装了PDF画图得所有信息

 CGPDFDocumentRef pdf;

 //PDFDocument 中得一页

 CGPDFPageRef page;

 //总共页数

 int totalPages;

 //当前得页面

 int currentPage;

 

 PDFTestViewController *pdftest;

}

@property(nonatomic,retain)IBOutlet PDFTestViewController *pdftest;

//当前视图初始化类,在该方法中会创建一个CGPDFDocuemntRef对象,传递一个PDF文件得名字,和所需要页面得大小,

- (id)initWithFrame:(CGRect)frame andFileName:(NSString *)fileName;

//创建一个PDF对象,此方法在初始化方法中被调用

- (CGPDFDocumentRef)createPDFFromExistFile:(NSString *)aFilePath;

 

-(void)reloadView;

/*

 页面之间得跳转

 */

-(void)goUpPage;   

-(void)goDownPage;

 

@end

//

//  PDFView.m

//  PDFViewTest

//

//  Created by Evan Lynn on 10-6-20.

//  Copyright 2010 Tera Age. All rights reserved.

//

 

#import "PDFView.h"

//#import "PDFTestViewController.h"

 

@implementation PDFView

@synthesize pdftest;

- (id)initWithFrame:(CGRect)frame andFileName:(NSString *)fileName{

 if (self = [super initWithFrame:frame]) {

 NSString *dataPathFromApp = [[[NSBundle mainBundleresourcePathstringByAppendingPathComponent:fileName];

 pdf = [self createPDFFromExistFile:dataPathFromApp];

 self.backgroundColor = [UIColor clearColor];

 }

 return self;

}

 

- (CGPDFDocumentRef)createPDFFromExistFile:(NSString *)aFilePath{

 CFStringRef path;

 CFURLRef url;

 CGPDFDocumentRef document;

path = CFStringCreateWithCString(NULL, [aFilePath UTF8String], kCFStringEncodingUTF8);

url = CFURLCreateWithFileSystemPath(NULL, path, kCFURLPOSIXPathStyleNO);

 CFRelease(path);

document = CGPDFDocumentCreateWithURL(url);

 CFRelease(url);

totalPages = CGPDFDocumentGetNumberOfPages(document);

currentPage=1;

 

if (totalPages == 0) {

 

return NULL;

}

return document;

}

 

- (void)drawRect:(CGRect)rect {

//得到绘图上下文环境

    CGContextRef context = UIGraphicsGetCurrentContext();

//得到一个PDF页面

    page = CGPDFDocumentGetPage(pdfcurrentPage);

/*进行坐标转换向右移动100个单位,并且向下移动当前视图得高度,

这是因为Quartz画图得坐标系统是以左下角为开始点,但iphone视图是以左上角为开始点

 */

    CGContextTranslateCTM(context, 100.0,self.bounds.size.height);

//转变坐标系

    CGContextScaleCTM(context, 1.0, -1);

    CGContextDrawPDFPage(context, page);

 

}

 

- (void)dealloc {

    [super dealloc];

}

 

-(void)reloadView{

[self setNeedsDisplay];

}

 

-(void)goUpPage{

if(currentPage < 2)

return;

--currentPage;

[self reloadView];

}

 

-(void)goDownPage{

if(currentPage >=totalPages)

return;

++currentPage;

[self reloadView];

}

 

 

@end

抱歉!评论已关闭.