在iPhone程序上读取PDF文件

怎么在iPhone程序中读取PDF的内容呢?答案是,苹果为我们准备了一个很神奇的framework Q2D(Quartz 2D)。Q2D提供了全套的PDF读取API,接下来我们来看看如果简单的使用Q2D来读取PDF文件:

 我建立了一个工程叫iPhonePDF, 添加了一个UIScrollView(不知道怎么添加UIScrollView? 添加一个UIView然后把interface上的UIView改成UIScrollView就可以啦…)名为PDFView

看看PDFView里面有什么吧

C/C++代码
  1. @interface PDFView : UIScrollView {   
  2.  NSString *filePath;   
  3.  CGPDFDocumentRef pdfDocument;   
  4.  CGPDFPageRef page;   
  5.  int pageNumber;   
  6. }   
  7.     
  8. @property (copy, nonatomic) NSString *filePath;   
  9. @property int pageNumber;   
  10.     
  11. -(CGPDFDocumentRef)MyGetPDFDocumentRef;   
  12. -(void)reloadView;   
  13. -(IBAction)goUpPage:(id)sender;   
  14. -(IBAction)goDownPage:(id)sender;   
  15. @end  

filePath是储存pdf文件的位置的,得到文件位置就是老话题了:[NSBundle mainBundle]… 后面的会写吧… 不记得了在我博客里面搜索吧

CGPDFDocumentRef是PDF文档索引文件,Q2D是Core Foundation的API,所以没看到那个星星~

CGPDFPageRef是PDF页面索引文件

pageNumber是页码

下面的几个函数其实一看就明了了,翻页的,和刷新页面的。第一个是自定义的getter

然后我们看看m文件里面有用的方法:

C/C++代码
  1. @implementation PDFView   
  2. @synthesize filePath,pageNumber;   
  3.     
  4. - (void)drawRect:(CGRect)rect //只要是UIView都有的绘图函数,基础哟~   
  5. {   
  6.  if(filePath == nil) //如果没被初始化的话,就初始化   
  7.  {   
  8.   pageNumber = 10;   //这个其实应该由外部函数控制,不过谁让这个程序特别简单呢   
  9.   filePath = [[NSBundle mainBundle] pathForResource:@"zhaomu" ofType:@"pdf"];   
  10.                 //这里,文件在这里!   
  11.   pdfDocument = [self MyGetPDFDocumentRef]; //从自定义getter得到文件索引   
  12.  }   
  13.     
  14.  CGContextRef myContext = UIGraphicsGetCurrentContext();   
  15.         //这个我研究了一段时间呢,不过就照打就可以了   
  16.     
  17.  page = CGPDFDocumentGetPage(pdfDocument, pageNumber);   
  18.         //便捷函数,告诉人家文档,告诉人家页码,就给你页面索引   
  19.     
  20.  CGContextDrawPDFPage(myContext, page);   
  21.         //画!   
  22. }   
  23.     
  24. //此getter可以考虑照打... 都是CF函数,我看到就恶心。   
  25. //其实不是很难了,得到文件,转换成URL,然后通过   
  26. //CGPDFDocumentCreateWithURL(url)得到文件内容索引   
  27. //Ta Daaa~~   
  28. - (CGPDFDocumentRef)MyGetPDFDocumentRef   
  29. {   
  30.  CFStringRef path;   
  31.  CFURLRef url;   
  32.  CGPDFDocumentRef document;   
  33.  path = CFStringCreateWithCString(NULL, [filePath UTF8String], kCFStringEncodingUTF8);   
  34.  url = CFURLCreateWithFileSystemPath(NULL, path, kCFURLPOSIXPathStyle, 0);   
  35.  CFRelease(path);   
  36.  document = CGPDFDocumentCreateWithURL(url);   
  37.  CFRelease(url);   
  38.  return document;   
  39. }   
  40.     
  41. -(void)reloadView   
  42. {   
  43.  [self setNeedsDisplay]; //每次需要重画视图了,就call这个   
  44. }   
  45.     
  46. -(IBAction)goUpPage:(id)sender   
  47. {   
  48.  pageNumber++;   
  49.     
  50.  [self reloadView];   
  51. }   
  52. <mce:script type="text/javascript" src="http://hi.images.csdn.net/js/blog/tiny_mce/themes/advanced/langs/zh.js" mce_src="http://hi.images.csdn.net/js/blog/tiny_mce/themes/advanced/langs/zh.js"></mce:script><mce:script type="text/javascript" src="http://hi.images.csdn.net/js/blog/tiny_mce/plugins/syntaxhl/langs/zh.js" mce_src="http://hi.images.csdn.net/js/blog/tiny_mce/plugins/syntaxhl/langs/zh.js"></mce:script>-(IBAction)goDownPage:(id)sender   
  53. {   
  54.  pageNumber--;   
  55.  [self reloadView];   
  56. }   
  57. @end  
posted @ 2014-02-22 23:44  左左木  阅读(345)  评论(0编辑  收藏  举报