IOS的PDF的文件读取

@implementation QuartzView


- (id)initWithFrame:(CGRect)aRect

{

self = [super initWithFrame:aRect];

if(self != nil)

{

self.backgroundColor = [UIColor blackColor];

self.opaque = YES;

self.clearsContextBeforeDrawing = YES;

}

return self;

}


-(void)drawInContext:(CGContextRef)context

{

//do nothing

}


-(void)drawRect:(CGRect)rect

{

// Since we use the CGContextRef a lot, it is convienient for our demonstration classes to do the real work

// inside of a method that passes the context as a parameter, rather than having to query the context

// continuously, or setup that parameter for every subclass.

[self drawInContext:UIGraphicsGetCurrentContext()];

}


@end



@implementation QuartzPDFView


-(id) initWithFile:(NSString*) filename

{

CGRect frame = CGRectMake(0, 0, 640, 960);

if (self = [super initWithFrame:frame])

{

const char * filenamechar = [filename UTF8String];

CFURLRef pdfURL = CFBundleCopyResourceURL(CFBundleGetMainBundle(),CFStringCreateWithCString(NULL,filenamechar,kCFStringEncodingUTF8),NULL,NULL);

     

pdf = CGPDFDocumentCreateWithURL((CFURLRef)pdfURL);

assert(pdf);

CFRelease(pdfURL);

mpdfpagecount = 1;

endpage = CGPDFDocumentGetNumberOfPages(pdf);

}

return self;

}

-(void)dealloc

{

CGPDFDocumentRelease(pdf);

[super dealloc];

}

// 采用绘图的方法绘制pdf的文本文件。

-(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, (size_t)mpdfpagecount);

// 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);

}


@end

转载于:https://my.oschina.net/makeffort/blog/89343

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值