iPhone开发应用中PDF案例实现

移动开发 iOS
iPhone开发应用中PDF案例实现是本文要介绍的内容,主要是来学习iPhone开发中PDF的解析内容,文章内容不多,主要是基于代码来实现。来看详细内容。

iPhone开发应用中PDF案例实现是本文要介绍的内容,主要是来学习iPhone开发PDF的解析内容,文章内容不多,主要是基于代码来实现。来看详细内容。

  1. #import <UIKit/UIKit.h> 
  2. @class PDFTestViewController;  
  3. @interface PDFView : UIView {  
  4.  //这个类封装了PDF画图得所有信息  
  5.  CGPDFDocumentRef pdf;  
  6.  //PDFDocument 中得一页  
  7.  CGPDFPageRef page;  
  8.  //总共页数  
  9.  int totalPages;  
  10.  //当前得页面  
  11.  int currentPage;  
  12.  PDFTestViewController *pdftest;  
  13. }  
  14. @property(nonatomic,retain)IBOutlet PDFTestViewController *pdftest;  
  15. //当前视图初始化类,在该方法中会创建一个CGPDFDocuemntRef对象,传递一个PDF文件得名字,和所需要页面得大小,  
  16. - (id)initWithFrame:(CGRect)frame andFileName:(NSString *)fileName;  
  17. //创建一个PDF对象,此方法在初始化方法中被调用  
  18. - (CGPDFDocumentRef)createPDFFromExistFile:(NSString *)aFilePath;  
  19.  
  20. -(void)reloadView;  
  21. /*  
  22.  页面之间得跳转  
  23.  */  
  24. -(void)goUpPage;     
  25. -(void)goDownPage;  
  26. @end  
  27. //  
  28. //  PDFView.m  
  29. //  PDFViewTest  
  30. //  
  31. //  Created by Evan Lynn on 10-6-20.  
  32. //  Copyright 2010 Tera Age. All rights reserved.  
  33. //  
  34.  
  35. #import "PDFView.h"  
  36.  
  37. //#import "PDFTestViewController.h"  
  38. @implementation PDFView  
  39. @synthesize pdftest;  
  40. - (id)initWithFrame:(CGRect)frame andFileName:(NSString *)fileName{  
  41.  if (self = [super initWithFrame:frame]) {  
  42.  NSString *dataPathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:fileName];  
  43.  pdf = [self createPDFFromExistFile:dataPathFromApp];  
  44.  self.backgroundColor = [UIColor clearColor];  
  45.  }  
  46.  return self;  
  47. }  
  48. - (CGPDFDocumentRef)createPDFFromExistFile:(NSString *)aFilePath{  
  49.  CFStringRef path;  
  50.  CFURLRef url;  
  51.  CGPDFDocumentRef document;  
  52. path = CFStringCreateWithCString(NULL, [aFilePath UTF8String], kCFStringEncodingUTF8);  
  53. url = CFURLCreateWithFileSystemPath(NULL, path, kCFURLPOSIXPathStyle, NO);  
  54.  CFRelease(path);  
  55. document = CGPDFDocumentCreateWithURL(url);  
  56.  CFRelease(url);  
  57. totalPages = CGPDFDocumentGetNumberOfPages(document);  
  58. currentPage=1;  
  59. if (totalPages == 0) {  
  60. return NULL;  
  61. }  
  62. return document;  
  63. }  
  64. - (void)drawRect:(CGRect)rect {  
  65. //得到绘图上下文环境  
  66.     CGContextRef context = UIGraphicsGetCurrentContext();  
  67. //得到一个PDF页面  
  68.     page = CGPDFDocumentGetPage(pdf, currentPage);  
  69. /*进行坐标转换向右移动100个单位,并且向下移动当前视图得高度,  
  70. 这是因为Quartz画图得坐标系统是以左下角为开始点,但iphone视图是以左上角为开始点  
  71.  */  
  72.     CGContextTranslateCTM(context, 100.0,self.bounds.size.height);  
  73. //转变坐标系  
  74.     CGContextScaleCTM(context, 1.0, -1);  
  75.     CGContextDrawPDFPage(context, page);  
  76. }  
  77. - (void)dealloc {  
  78.     [super dealloc];  
  79. }  
  80. -(void)reloadView{  
  81. [self setNeedsDisplay];  
  82. }  
  83. -(void)goUpPage{  
  84. if(currentPage < 2)  
  85. return;  
  86. --currentPage;  
  87. [self reloadView];  
  88. }  
  89. -(void)goDownPage{  
  90. if(currentPage >=totalPages)  
  91. return;  
  92. ++currentPage;  
  93. [self reloadView];  
  94. }  
  95. @end 

小结:iPhone开发应用中PDF案例实现的内容介绍完了,希望通过本文的学习能对你有所帮助!

责任编辑:zhaolei 来源: 互联网
相关推荐

2011-08-18 16:24:44

iPhone开发图片

2011-08-16 15:48:37

iPhone开发抓图程序

2011-08-19 10:13:05

iPhone开发

2011-08-19 11:10:31

iPhone应用

2011-08-18 15:24:40

iPhone国际化

2011-08-19 10:05:30

iPhone开发

2011-08-17 16:12:20

iPhone应用程序

2011-08-18 16:42:07

iPhone应用APNS推送

2011-08-15 11:23:41

iPhone开发循环滚动UIScrollVie

2011-08-15 18:02:32

iPhone开发表视图

2011-08-10 17:37:00

iPhoneASIHTTPRequ

2011-08-16 15:36:47

iPhone应用测试

2011-08-17 16:23:31

iPhone开发UIViewContr

2011-08-09 17:12:30

iPhoneCFRunLoop

2011-08-12 14:33:06

iPhone缓存文件

2011-08-22 14:12:48

iPhone开发NSTableView

2011-08-15 11:37:20

iPhone开发Mask

2011-08-19 10:01:09

iPhone应用SqliteUITableView

2012-04-26 13:26:58

iPhone应用技巧

2011-07-25 14:44:41

iPhone iPhone开发 截屏
点赞
收藏

51CTO技术栈公众号