PDFPageView.m 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. {
  21. CGPDFPageRef page = self.pageRef;
  22. CGContextSaveGState (myContext);
  23. // CGRect mediabox= CGPDFPageGetBoxRect(page, kCGPDFMediaBox);
  24. // CGContextTranslateCTM(myContext, 0, mediabox.size.height);
  25. // CGContextScaleCTM(myContext, 1, -1);
  26. // CGAffineTransform pdfTransform = CGPDFPageGetDrawingTransform(page, kCGPDFMediaBox, self.frame, 0, true);
  27. CGRect mediabox= CGPDFPageGetBoxRect(page, kCGPDFMediaBox);
  28. double scalex = self.frame.size.width/mediabox.size.width;
  29. double scaley = self.frame.size.height/mediabox.size.height;
  30. double scale = scalex<scaley?scalex:scaley;
  31. CGAffineTransform translate_transform = CGAffineTransformMakeTranslation ( (self.frame.size.width-mediabox.size.width*scale)/2, 0 );
  32. CGAffineTransform scale_transform=CGAffineTransformScale ( translate_transform, scale, scale );
  33. CGContextConcatCTM(myContext, scale_transform);
  34. CGContextScaleCTM(myContext, 1, -1);
  35. CGContextTranslateCTM(myContext, 0, -self.frame.size.height/scale_transform.a);
  36. CGContextDrawPDFPage (myContext, page);
  37. CGContextRestoreGState (myContext);
  38. }
  39. // CGPDFPageRef page = self.pageRef;
  40. //
  41. // CGContextSaveGState (myContext);
  42. //
  43. // CGRect mediabox= CGPDFPageGetBoxRect(page, kCGPDFMediaBox);
  44. //
  45. // CGContextTranslateCTM(myContext, 0, mediabox.size.height);
  46. // // Reverse the Y axis to grow from bottom to top.
  47. // CGContextScaleCTM(myContext, 1, -1);
  48. //
  49. // CGContextDrawPDFPage (myContext, page);
  50. // CGContextRestoreGState (myContext);
  51. // return ;
  52. // {
  53. //
  54. // // CGPDFDocumentRef document;
  55. // CGPDFPageRef page = self.pageRef;
  56. //
  57. // CGContextSaveGState (myContext);
  58. //
  59. //
  60. // CGRect mediabox= CGPDFPageGetBoxRect(page, kCGPDFMediaBox);
  61. //
  62. // CGContextTranslateCTM(myContext, 0, mediabox.size.height);
  63. // CGContextScaleCTM(myContext, 1, -1);
  64. //
  65. // CGContextDrawPDFPage (myContext, page);
  66. // CGContextRestoreGState (myContext);
  67. // }
  68. /*
  69. // CGPDFDocumentRef document;
  70. CGPDFPageRef page = self.pageRef;
  71. // document = [self OpenPDF:filename]; // 1 创建PDFDocument对象
  72. // page = CGPDFDocumentGetPage (document, pageNumber); // 2 获取指定页的PDF文档
  73. // CGAffineTransform m;
  74. // m = CGPDFPageGetDrawingTransform (page, kCGPDFMediaBox, self.bounds, -180, true);
  75. CGContextSaveGState (myContext);
  76. CGContextTranslateCTM(myContext, 80, self.frame.size.height);
  77. CGContextScaleCTM(myContext, 1, -1);
  78. // CGContextConcatCTM (myContext, m);
  79. // CGContextClipToRect (myContext,CGPDFPageGetBoxRect (page, kCGPDFCropBox));
  80. CGContextDrawPDFPage (myContext, page);
  81. CGContextRestoreGState (myContext);
  82. */
  83. // CGContextDrawPDFPage (myContext, page); // 3 将PDF绘制到图形上下文中
  84. // CGPDFDocumentRelease (document);
  85. }
  86. @end