PDFPageView.m 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. //
  2. // PDFPageView.m
  3. // AntsContract
  4. //
  5. // Created by Ray on 12/16/16.
  6. // Copyright © 2016 United Software Applications, Inc. All rights reserved.
  7. //
  8. #import "PDFPageView.h"
  9. @implementation PDFPageView
  10. // Only override drawRect: if you perform custom drawing.
  11. // An empty implementation adversely affects performance during animation.
  12. - (void)drawRect:(CGRect)rect {
  13. // Drawing code
  14. //CGPDFDocumentRef document = [self OpenPDF:@"GEIC - Home Improvement Contract 2016.pdf"];
  15. CGContextRef context = UIGraphicsGetCurrentContext();
  16. [self MyDisplayPDFPage:context index:self.pageIndex ];//file:@"GEIC - Home Improvement Contract 2016.pdf"];
  17. }
  18. -(void) MyDisplayPDFPage:(CGContextRef) myContext index:(size_t)pageNumber //file:(NSString*) filename//(, , const char *filename)
  19. {
  20. // double vwidth=self.frame.size.width;
  21. // double vheight = self.frame.size.height;
  22. {
  23. CGPDFPageRef page = self.pageRef;
  24. CGContextSaveGState (myContext);
  25. // CGRect mediabox= CGPDFPageGetBoxRect(page, kCGPDFMediaBox);
  26. // CGContextTranslateCTM(myContext, 0, mediabox.size.height);
  27. // CGContextScaleCTM(myContext, 1, -1);
  28. // CGAffineTransform pdfTransform = CGPDFPageGetDrawingTransform(page, kCGPDFMediaBox, self.frame, 0, true);
  29. CGRect mediabox= CGPDFPageGetBoxRect(page, kCGPDFMediaBox);
  30. double scalex = self.frame.size.width/mediabox.size.width;
  31. double scaley = self.frame.size.height/mediabox.size.height;
  32. double scale = scalex<scaley?scalex:scaley;
  33. CGAffineTransform translate_transform = CGAffineTransformMakeTranslation ( (self.frame.size.width-mediabox.size.width*scale)/2, 0 );
  34. CGAffineTransform scale_transform=CGAffineTransformScale ( translate_transform, scale, scale );
  35. CGContextConcatCTM(myContext, scale_transform);
  36. CGContextScaleCTM(myContext, 1, -1);
  37. CGContextTranslateCTM(myContext, 0, -self.frame.size.height/scale_transform.a);
  38. CGContextDrawPDFPage (myContext, page);
  39. CGContextRestoreGState (myContext);
  40. }
  41. // CGPDFPageRef page = self.pageRef;
  42. //
  43. // CGContextSaveGState (myContext);
  44. //
  45. // CGRect mediabox= CGPDFPageGetBoxRect(page, kCGPDFMediaBox);
  46. //
  47. // CGContextTranslateCTM(myContext, 0, mediabox.size.height);
  48. // // Reverse the Y axis to grow from bottom to top.
  49. // CGContextScaleCTM(myContext, 1, -1);
  50. //
  51. // CGContextDrawPDFPage (myContext, page);
  52. // CGContextRestoreGState (myContext);
  53. // return ;
  54. // {
  55. //
  56. // // CGPDFDocumentRef document;
  57. // CGPDFPageRef page = self.pageRef;
  58. //
  59. // CGContextSaveGState (myContext);
  60. //
  61. //
  62. // CGRect mediabox= CGPDFPageGetBoxRect(page, kCGPDFMediaBox);
  63. //
  64. // CGContextTranslateCTM(myContext, 0, mediabox.size.height);
  65. // CGContextScaleCTM(myContext, 1, -1);
  66. //
  67. // CGContextDrawPDFPage (myContext, page);
  68. // CGContextRestoreGState (myContext);
  69. // }
  70. /*
  71. // CGPDFDocumentRef document;
  72. CGPDFPageRef page = self.pageRef;
  73. // document = [self OpenPDF:filename]; // 1 创建PDFDocument对象
  74. // page = CGPDFDocumentGetPage (document, pageNumber); // 2 获取指定页的PDF文档
  75. // CGAffineTransform m;
  76. // m = CGPDFPageGetDrawingTransform (page, kCGPDFMediaBox, self.bounds, -180, true);
  77. CGContextSaveGState (myContext);
  78. CGContextTranslateCTM(myContext, 80, self.frame.size.height);
  79. CGContextScaleCTM(myContext, 1, -1);
  80. // CGContextConcatCTM (myContext, m);
  81. // CGContextClipToRect (myContext,CGPDFPageGetBoxRect (page, kCGPDFCropBox));
  82. CGContextDrawPDFPage (myContext, page);
  83. CGContextRestoreGState (myContext);
  84. */
  85. // CGContextDrawPDFPage (myContext, page); // 3 将PDF绘制到图形上下文中
  86. // CGPDFDocumentRelease (document);
  87. }
  88. @end