iPhone開發(fā)應(yīng)用中PDF案例實(shí)現(xiàn)
作者:佚名 
  iPhone開發(fā)應(yīng)用中PDF案例實(shí)現(xiàn)是本文要介紹的內(nèi)容,主要是來學(xué)習(xí)iPhone開發(fā)中PDF的解析內(nèi)容,文章內(nèi)容不多,主要是基于代碼來實(shí)現(xiàn)。來看詳細(xì)內(nèi)容。
 iPhone開發(fā)應(yīng)用中PDF案例實(shí)現(xiàn)是本文要介紹的內(nèi)容,主要是來學(xué)習(xí)iPhone開發(fā)中PDF的解析內(nèi)容,文章內(nèi)容不多,主要是基于代碼來實(shí)現(xiàn)。來看詳細(xì)內(nèi)容。
- #import <UIKit/UIKit.h>
 - @class PDFTestViewController;
 - @interface PDFView : UIView {
 - //這個(gè)類封裝了PDF畫圖得所有信息
 - CGPDFDocumentRef pdf;
 - //PDFDocument 中得一頁
 - CGPDFPageRef page;
 - //總共頁數(shù)
 - int totalPages;
 - //當(dāng)前得頁面
 - int currentPage;
 - PDFTestViewController *pdftest;
 - }
 - @property(nonatomic,retain)IBOutlet PDFTestViewController *pdftest;
 - //當(dāng)前視圖初始化類,在該方法中會創(chuàng)建一個(gè)CGPDFDocuemntRef對象,傳遞一個(gè)PDF文件得名字,和所需要頁面得大小,
 - - (id)initWithFrame:(CGRect)frame andFileName:(NSString *)fileName;
 - //創(chuàng)建一個(gè)PDF對象,此方法在初始化方法中被調(diào)用
 - - (CGPDFDocumentRef)createPDFFromExistFile:(NSString *)aFilePath;
 - -(void)reloadView;
 - /*
 - 頁面之間得跳轉(zhuǎn)
 - */
 - -(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 mainBundle] resourcePath] stringByAppendingPathComponent: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, kCFURLPOSIXPathStyle, NO);
 - CFRelease(path);
 - document = CGPDFDocumentCreateWithURL(url);
 - CFRelease(url);
 - totalPages = CGPDFDocumentGetNumberOfPages(document);
 - currentPage=1;
 - if (totalPages == 0) {
 - return NULL;
 - }
 - return document;
 - }
 - - (void)drawRect:(CGRect)rect {
 - //得到繪圖上下文環(huán)境
 - CGContextRef context = UIGraphicsGetCurrentContext();
 - //得到一個(gè)PDF頁面
 - page = CGPDFDocumentGetPage(pdf, currentPage);
 - /*進(jìn)行坐標(biāo)轉(zhuǎn)換向右移動(dòng)100個(gè)單位,并且向下移動(dòng)當(dāng)前視圖得高度,
 - 這是因?yàn)镼uartz畫圖得坐標(biāo)系統(tǒng)是以左下角為開始點(diǎn),但iphone視圖是以左上角為開始點(diǎn)
 - */
 - CGContextTranslateCTM(context, 100.0,self.bounds.size.height);
 - //轉(zhuǎn)變坐標(biāo)系
 - 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
 
小結(jié):iPhone開發(fā)應(yīng)用中PDF案例實(shí)現(xiàn)的內(nèi)容介紹完了,希望通過本文的學(xué)習(xí)能對你有所幫助!
責(zé)任編輯:zhaolei 
                    來源:
                    互聯(lián)網(wǎng)
 














 
 
 
 
 
 
 