KMPDFMultipleManager.m 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  1. //
  2. // KMPDFMorePagePrintManager.m
  3. // PDF Reader Pro Edition
  4. //
  5. // Created by 丁林圭 on 2018/5/17.
  6. //
  7. #import "KMPDFMultipleManager.h"
  8. #pragma mark - KMPDFMultiple
  9. @interface KMEachPageProperties()
  10. @end
  11. @implementation KMEachPageProperties
  12. -(void)dealloc {
  13. }
  14. @end
  15. #pragma mark - KMPDFMultipleManager
  16. @interface KMPDFMultipleManager()
  17. @end
  18. @implementation KMPDFMultipleManager
  19. - (void)dealloc {
  20. }
  21. #pragma mark Private Action
  22. - (void)PDFKitDrawPageToContext:(CGContextRef)context eachPages:(NSArray *)pages multipleOption:(KMEachPageProperties *)properties
  23. {
  24. CGSize size = properties.pageSize;
  25. CGFloat width = size.width;
  26. CGFloat height =size.height;
  27. if (kKMPDFMultiplePageDirection_Landscape == properties.direction) {
  28. width = size.height;
  29. height = size.width;
  30. }
  31. CGFloat contextWidth = width- properties.edgeInsets.left - properties.edgeInsets.right;//实际画page的总宽度
  32. CGFloat contextHeight = height - properties.edgeInsets.bottom - properties.edgeInsets.top;//实际画page的总高度
  33. NSInteger columnCout = properties.columnCout;
  34. NSInteger rowCount = properties.rowCount;
  35. CGFloat columnSpace = properties.columnSpace;
  36. NSInteger rowSpace = properties.rowSpace;
  37. CGFloat drawPageMaxW = (contextWidth - (columnCout - 1) * columnSpace)/ columnCout; //page的能画的最大宽度
  38. CGFloat drawPageMaxH = (contextHeight - (rowCount -1) * rowSpace) / rowCount;//page能画的最大高度
  39. CGFloat scanl = 1.0;
  40. CGFloat xTransform = 0;
  41. CGFloat yTransform = 0;
  42. if (kKMPDFMultiplePageOrder_LongitudinalAntitone == properties.order || kKMPDFMultiplePageOrder_Longitudinal == properties.order) {
  43. rowCount = properties.columnCout;
  44. columnCout = properties.rowCount;
  45. }
  46. for (NSInteger i = 0; i < rowCount; i++) {
  47. for (NSInteger j = 0;j < columnCout; j++) {
  48. NSUInteger index = j + i * columnCout;
  49. if (index >= pages.count) {
  50. break;
  51. }
  52. PDFPage *drawPage = pages[index];
  53. NSRect pageBounds = [drawPage boundsForBox:kPDFDisplayBoxCropBox];
  54. CGFloat orgPageWidth = pageBounds.size.width;//原始page的宽度(不管有没有旋转,都是旋转度数等于0)
  55. CGFloat orgPageHeight = pageBounds.size.height;//原始page的高度
  56. NSMutableArray *annotations = [NSMutableArray array];
  57. for (PDFAnnotation *annotation in drawPage.annotations) {
  58. [annotations addObject:annotation];
  59. }
  60. if (kKMPDFCommentsForms_Documents == properties.commentsForms) {
  61. for (PDFAnnotation *annotation in annotations) {
  62. [annotation.page removeAnnotation:annotation];
  63. }
  64. } else if (kKMPDFCommentsForms_DocumentStamps == properties.commentsForms) {
  65. for (PDFAnnotation *annotation in annotations) {
  66. if (![annotation isKindOfClass:[PDFAnnotationStamp class]]) {
  67. [annotation.page removeAnnotation:annotation];
  68. }
  69. }
  70. }
  71. NSInteger rotate = drawPage.rotation;
  72. if (90 == rotate || 270 == rotate) {
  73. orgPageWidth = pageBounds.size.height;
  74. orgPageHeight = pageBounds.size.width;
  75. }
  76. scanl = MIN(drawPageMaxW/orgPageWidth,drawPageMaxH/orgPageHeight);
  77. //取出page横竖时能显示的最大Rect
  78. if (properties.isAutoRotate) {
  79. CGFloat tScanl1 = MIN(drawPageMaxW/pageBounds.size.width,drawPageMaxH/pageBounds.size.height);//page旋转度数为0度或者180度时,在指定的大小内,能显示的比例
  80. CGFloat tScanl2 = MIN(drawPageMaxW/pageBounds.size.height,drawPageMaxH/pageBounds.size.width);//page旋转度数为90度或者270度时,在指定的大小内,能显示的比例
  81. scanl = MAX(tScanl1, tScanl2);
  82. if (tScanl1 > tScanl2) {
  83. //高为竖直排列时,显示最大,则需将page时90度或者270度需要进行旋转
  84. if (rotate == 90 || rotate == 270) {
  85. [drawPage setRotation:rotate - 90];
  86. }
  87. } else {
  88. //宽为竖直排列时,显示最大,则需将page时0度或者180度需要进行旋转
  89. if (rotate == 0 || rotate == 180) {
  90. [drawPage setRotation:rotate - 90];
  91. }
  92. }
  93. if ( 90 == drawPage.rotation || 270 == drawPage.rotation) {
  94. orgPageWidth = pageBounds.size.height;
  95. orgPageHeight = pageBounds.size.width;
  96. } else {
  97. orgPageWidth = pageBounds.size.width;
  98. orgPageHeight = pageBounds.size.height;
  99. }
  100. }
  101. if (scanl >1) {
  102. scanl = 1;
  103. }
  104. //每一页都是它居中;
  105. CGFloat spaceX = (drawPageMaxW - orgPageWidth * scanl)/2;
  106. CGFloat spaceY = (drawPageMaxH - orgPageHeight* scanl)/2;
  107. if (kKMPDFMultiplePageOrder_Transverse == properties.order) {
  108. xTransform = (drawPageMaxW + columnSpace) * j + properties.edgeInsets.left + spaceX;
  109. //页面内容高度 + 下边的行间距 - 第几个cell的高度 +居中
  110. yTransform = contextHeight + properties.edgeInsets.bottom - (drawPageMaxH + rowSpace) * (i+1) + spaceY + rowSpace;
  111. } else if (kKMPDFMultiplePageOrder_TransverseAntitone == properties.order) {
  112. xTransform = contextWidth + properties.edgeInsets.left - (drawPageMaxW + columnSpace) * (j + 1) + spaceX + columnSpace;
  113. yTransform = contextHeight - (drawPageMaxH + rowSpace) * (i+1) + spaceY + properties.edgeInsets.bottom + rowSpace;
  114. } else if (kKMPDFMultiplePageOrder_Longitudinal == properties.order) {
  115. xTransform = (drawPageMaxW + columnSpace) * i + properties.edgeInsets.left + spaceX;
  116. yTransform = contextHeight - (drawPageMaxH + rowSpace) * (j+1) + spaceY + properties.edgeInsets.bottom + rowSpace;
  117. } else if (kKMPDFMultiplePageOrder_LongitudinalAntitone == properties.order) {
  118. xTransform = contextWidth + properties.edgeInsets.left - (drawPageMaxW + columnSpace) * (i + 1) + spaceX + columnSpace;
  119. yTransform = contextHeight - (drawPageMaxH + rowSpace) * (j+1) + spaceY + properties.edgeInsets.bottom + rowSpace;
  120. }
  121. [NSGraphicsContext saveGraphicsState];
  122. //平移
  123. CGContextTranslateCTM(context, xTransform,yTransform);
  124. //缩放
  125. CGContextScaleCTM(context, scanl, scanl);
  126. if (@available(macOS 10.12, *)) {
  127. [drawPage drawWithBox:kPDFDisplayBoxCropBox toContext:context];
  128. [drawPage transformContext:context forBox:kPDFDisplayBoxCropBox];
  129. } else {
  130. [NSGraphicsContext saveGraphicsState];
  131. [NSGraphicsContext setCurrentContext:[NSGraphicsContext graphicsContextWithGraphicsPort:context flipped:YES]];
  132. [drawPage drawWithBox:kPDFDisplayBoxCropBox];
  133. [NSGraphicsContext restoreGraphicsState];
  134. [drawPage transformContextForBox:kPDFDisplayBoxCropBox];
  135. }
  136. if (properties.isHasBorder) {
  137. CGFloat borderWidth = pageBounds.size.width;
  138. CGFloat borderHeight = pageBounds.size.height;
  139. if ( 90 == drawPage.rotation || 270 == drawPage.rotation) {
  140. borderWidth = pageBounds.size.height;
  141. borderWidth = pageBounds.size.width;
  142. }
  143. CGRect dirtyRect = CGRectMake(pageBounds.origin.x, pageBounds.origin.y, borderWidth, borderHeight);
  144. CGContextAddRect(context, dirtyRect);
  145. CGContextSetRGBStrokeColor(context, 0.0, 0.0, 0.0, 1.0);
  146. CGContextStrokePath(context);
  147. }
  148. [NSGraphicsContext restoreGraphicsState];
  149. }
  150. }
  151. }
  152. - (void)CGDrawPageToContext:(CGContextRef)context eachPages:(NSArray *)pages multipleOption:(KMEachPageProperties *)properties
  153. {
  154. CGSize size = properties.pageSize;
  155. CGFloat width = size.width;
  156. CGFloat height =size.height;
  157. if (kKMPDFMultiplePageDirection_Landscape == properties.direction) {
  158. width = size.height;
  159. height = size.width;
  160. }
  161. CGFloat contextWidth = width- properties.edgeInsets.left - properties.edgeInsets.right;
  162. CGFloat contextHeight = height - properties.edgeInsets.bottom - properties.edgeInsets.top;
  163. NSInteger columnCout = properties.columnCout;
  164. NSInteger rowCount = properties.rowCount;
  165. CGFloat columnSpace = properties.columnSpace;
  166. NSInteger rowSpace = properties.rowSpace;
  167. CGFloat drawPageW = (contextWidth - (columnCout - 1) * columnSpace)/ columnCout; //画page的宽度
  168. CGFloat drawPageH = (contextHeight - (rowCount -1) * rowSpace) / rowCount;//画page的高度
  169. CGFloat scanl = 1.0;
  170. CGFloat xTransform = 0;
  171. CGFloat yTransform = 0;
  172. if (kKMPDFMultiplePageOrder_LongitudinalAntitone == properties.order || kKMPDFMultiplePageOrder_Longitudinal == properties.order) {
  173. rowCount = properties.columnCout;
  174. columnCout = properties.rowCount;
  175. }
  176. for (NSInteger i = 0; i < rowCount; i++) {
  177. for (NSInteger j = 0;j < columnCout; j++) {
  178. NSUInteger index = j + i * columnCout;
  179. if (index >= pages.count) {
  180. break;
  181. }
  182. PDFPage *drawPage = pages[index];
  183. NSRect pageBounds = [drawPage boundsForBox:kPDFDisplayBoxCropBox];
  184. CGFloat orgPageWidth = pageBounds.size.width;//原始page的宽度
  185. CGFloat orgPageHeight = pageBounds.size.height;//原始page的高度
  186. NSInteger rotate = drawPage.rotation;
  187. if (90 == rotate || 270 == rotate) {
  188. orgPageWidth = pageBounds.size.height;
  189. orgPageHeight = pageBounds.size.width;
  190. }
  191. if (properties.isAutoRotate) {
  192. CGFloat tScanl1 = MIN(drawPageW/pageBounds.size.width,drawPageH/pageBounds.size.height);
  193. CGFloat tScanl2 = MIN(drawPageW/pageBounds.size.height,drawPageH/pageBounds.size.width);
  194. scanl = MAX(tScanl1, tScanl2);
  195. if (tScanl1 > tScanl2) {
  196. if (rotate == 90 || rotate == 270) {
  197. rotate = rotate - 90;
  198. }
  199. } else {
  200. if (rotate != 90 || rotate != 270) {
  201. rotate = rotate - 90;
  202. }
  203. }
  204. if (rotate == -90) {
  205. rotate = 270;
  206. }
  207. }
  208. if ( 90 == rotate || 270 == rotate) {
  209. orgPageWidth = pageBounds.size.height;
  210. orgPageHeight = pageBounds.size.width;
  211. } else {
  212. orgPageWidth = pageBounds.size.width;
  213. orgPageHeight = pageBounds.size.height;
  214. }
  215. scanl = MIN(drawPageW/orgPageWidth,drawPageH/orgPageHeight);
  216. if (scanl >1) {
  217. scanl = 1;
  218. }
  219. // //每一页都是它居中;
  220. CGFloat spaceX = (drawPageW - orgPageWidth * scanl)/2;
  221. CGFloat spaceY = (drawPageH - orgPageHeight* scanl)/2;
  222. CGFloat offsetX = 0;
  223. CGFloat offsetY = 0;
  224. if (kKMPDFMultiplePageOrder_Transverse == properties.order) {
  225. if (rotate == 270) {
  226. offsetX = (properties.edgeInsets.bottom+ spaceY)/scanl;
  227. offsetY = - pageBounds.size.height - (properties.edgeInsets.left + spaceX)/scanl;
  228. xTransform = offsetX + (drawPageH + rowSpace)/scanl * (rowCount - i -1) ;
  229. yTransform = offsetY - (drawPageW + columnSpace)/scanl * j;
  230. } else if (rotate == 180) {
  231. offsetX = -(pageBounds.size.width + properties.edgeInsets.left/scanl + spaceX/scanl);
  232. offsetY = -(pageBounds.size.height + properties.edgeInsets.bottom/scanl + spaceY/scanl);
  233. xTransform = offsetX - (drawPageW + columnSpace)/scanl * j ;
  234. yTransform = offsetY - (drawPageH + rowSpace)/scanl * (rowCount - i -1);
  235. } else if (rotate == 90) {
  236. offsetX = - (pageBounds.size.width + (properties.edgeInsets.bottom+ spaceY)/scanl);
  237. offsetY = (properties.edgeInsets.left + spaceX)/scanl;
  238. xTransform = offsetX - (drawPageH + rowSpace)/scanl * (rowCount - i -1) ;
  239. yTransform = offsetY + (drawPageW + columnSpace)/scanl * j;
  240. } else {
  241. offsetX = (properties.edgeInsets.left + spaceX)/scanl;
  242. offsetY = (properties.edgeInsets.bottom+spaceY)/scanl;
  243. xTransform = offsetX + (drawPageW + columnSpace)/scanl * j ;
  244. yTransform = (offsetY + (drawPageH + rowSpace)/scanl * (rowCount - i -1));
  245. }
  246. } else if (kKMPDFMultiplePageOrder_TransverseAntitone == properties.order) {
  247. if (rotate == 270) {
  248. offsetX = (properties.edgeInsets.bottom+ spaceY)/scanl;
  249. offsetY = - pageBounds.size.height - (properties.edgeInsets.left + spaceX)/scanl;
  250. xTransform = offsetX + (drawPageH + rowSpace)/scanl * (rowCount - i -1) ;
  251. yTransform = offsetY - (drawPageW + columnSpace)/scanl * (columnCout - 1 - j);
  252. } else if (rotate == 180) {
  253. offsetX = -(pageBounds.size.width + properties.edgeInsets.left/scanl + spaceX/scanl);
  254. offsetY = -(pageBounds.size.height + properties.edgeInsets.bottom/scanl + spaceY/scanl);
  255. xTransform = offsetX - (drawPageW + columnSpace)/scanl * (columnCout - 1 - j) ;
  256. yTransform = offsetY - (drawPageH + rowSpace)/scanl * (rowCount - i -1);
  257. } else if (rotate == 90) {
  258. offsetX = - (pageBounds.size.width + (properties.edgeInsets.bottom+ spaceY)/scanl);
  259. offsetY = (properties.edgeInsets.left + spaceX)/scanl;
  260. xTransform = offsetX - (drawPageH + rowSpace)/scanl * (rowCount - i -1) ;
  261. yTransform = offsetY + (drawPageW + columnSpace)/scanl * (columnCout - 1 - j);
  262. } else {
  263. offsetX = (properties.edgeInsets.left + spaceX)/scanl;
  264. offsetY = (properties.edgeInsets.bottom+spaceY)/scanl;
  265. xTransform = offsetX + (drawPageW + columnSpace)/scanl * (columnCout - 1 - j) ;
  266. yTransform = (offsetY + (drawPageH + rowSpace)/scanl * (rowCount - i -1));
  267. }
  268. } else if (kKMPDFMultiplePageOrder_Longitudinal == properties.order) {
  269. if (rotate == 270) {
  270. offsetX = (properties.edgeInsets.bottom+ spaceY)/scanl;
  271. offsetY = - pageBounds.size.height - (properties.edgeInsets.left + spaceX)/scanl;
  272. xTransform = offsetX + (drawPageH + rowSpace)/scanl * (rowCount - j -1) ;
  273. yTransform = offsetY - (drawPageW + columnSpace)/scanl * i;
  274. } else if (rotate == 180) {
  275. offsetX = -(pageBounds.size.width + properties.edgeInsets.left/scanl + spaceX/scanl);
  276. offsetY = -(pageBounds.size.height + properties.edgeInsets.bottom/scanl + spaceY/scanl);
  277. xTransform = offsetX - (drawPageW + columnSpace)/scanl * i ;
  278. yTransform = offsetY - (drawPageH + rowSpace)/scanl * (rowCount - j -1);
  279. } else if (rotate == 90) {
  280. offsetX = - (pageBounds.size.width + (properties.edgeInsets.bottom+ spaceY)/scanl);
  281. offsetY = (properties.edgeInsets.left + spaceX)/scanl;
  282. xTransform = offsetX - (drawPageH + rowSpace)/scanl * (rowCount - j -1) ;
  283. yTransform = offsetY + (drawPageW + columnSpace)/scanl * i;
  284. } else {
  285. offsetX = (properties.edgeInsets.left + spaceX)/scanl;
  286. offsetY = (properties.edgeInsets.bottom+spaceY)/scanl;
  287. xTransform = offsetX + (drawPageW + columnSpace)/scanl * i ;
  288. yTransform = (offsetY + (drawPageH + rowSpace)/scanl * (rowCount - j -1));
  289. }
  290. } else if (kKMPDFMultiplePageOrder_LongitudinalAntitone == properties.order) {
  291. if (rotate == 270) {
  292. offsetX = (properties.edgeInsets.bottom+ spaceY)/scanl;
  293. offsetY = - pageBounds.size.height - (properties.edgeInsets.left + spaceX)/scanl;
  294. xTransform = offsetX + (drawPageH + rowSpace)/scanl * (rowCount - j -1) ;
  295. yTransform = offsetY - (drawPageW + columnSpace)/scanl * (columnCout - 1 - i);
  296. } else if (rotate == 180) {
  297. offsetX = -(pageBounds.size.width + properties.edgeInsets.left/scanl + spaceX/scanl);
  298. offsetY = -(pageBounds.size.height + properties.edgeInsets.bottom/scanl + spaceY/scanl);
  299. xTransform = offsetX - (drawPageW + columnSpace)/scanl * (columnCout - 1 - i) ;
  300. yTransform = offsetY - (drawPageH + rowSpace)/scanl * (rowCount - j -1);
  301. } else if (rotate == 90) {
  302. offsetX = - (pageBounds.size.width + (properties.edgeInsets.bottom+ spaceY)/scanl);
  303. offsetY = (properties.edgeInsets.left + spaceX)/scanl;
  304. xTransform = offsetX - (drawPageH + rowSpace)/scanl * (rowCount - j -1) ;
  305. yTransform = offsetY + (drawPageW + columnSpace)/scanl * (columnCout - 1 - i);
  306. } else {
  307. offsetX = (properties.edgeInsets.left + spaceX)/scanl;
  308. offsetY = (properties.edgeInsets.bottom+spaceY)/scanl;
  309. xTransform = offsetX + (drawPageW + columnSpace)/scanl * (columnCout - 1 - i) ;
  310. yTransform = (offsetY + (drawPageH + rowSpace)/scanl * (rowCount - j -1));
  311. }
  312. }
  313. [NSGraphicsContext saveGraphicsState];
  314. //缩放
  315. CGContextScaleCTM(context, scanl, scanl);
  316. CGContextRotateCTM(context, -rotate*M_PI/180);
  317. CGContextTranslateCTM(context,xTransform,yTransform);
  318. CGContextDrawPDFPage(context,drawPage.pageRef);
  319. if (properties.isHasBorder) {
  320. CGFloat borderWidth = pageBounds.size.width;
  321. CGFloat borderHeight = pageBounds.size.height;
  322. CGRect dirtyRect = CGRectMake(0, 0, borderWidth, borderHeight);
  323. CGContextAddRect(context, dirtyRect);
  324. CGContextSetRGBStrokeColor(context, 0.0, 0.0, 0.0, 1.0);
  325. CGContextStrokePath(context);
  326. }
  327. [NSGraphicsContext restoreGraphicsState];
  328. }
  329. }
  330. }
  331. #pragma mark Publick Action
  332. + (KMPDFMultipleManager *)defaultManager
  333. {
  334. static KMPDFMultipleManager *singleton = nil;
  335. static dispatch_once_t pred;
  336. dispatch_once(&pred, ^{
  337. singleton = [[KMPDFMultipleManager alloc] init];
  338. });
  339. return singleton;
  340. }
  341. - (void)savePDFDocumentPath:(NSString *)path drawPages:(NSArray *)pages multipleOption:(KMEachPageProperties *)properties completionHandler:(void (^)(BOOL isSuccessfully))handler
  342. {
  343. CFStringRef filePath = (__bridge CFStringRef)path;
  344. CFURLRef url = CFURLCreateWithFileSystemPath(NULL, filePath, kCFURLPOSIXPathStyle, 0);
  345. CFMutableDictionaryRef myDictionary = CFDictionaryCreateMutable(NULL,
  346. 0,
  347. &kCFTypeDictionaryKeyCallBacks,
  348. &kCFTypeDictionaryValueCallBacks);
  349. CFDictionarySetValue(myDictionary, kCGPDFContextCreator, CFSTR("PDF Reader Pro"));
  350. CGContextRef pdfContext = CGPDFContextCreateWithURL(url, &CGRectZero, myDictionary);
  351. CFRelease(myDictionary);
  352. CFRelease(url);
  353. if (!pdfContext) {
  354. handler(NO);
  355. return;
  356. }
  357. [NSGraphicsContext setCurrentContext:[NSGraphicsContext graphicsContextWithGraphicsPort:pdfContext flipped:NO]];
  358. NSInteger eachPageCount = (properties.columnCout * properties.rowCount);
  359. NSInteger numPages = 0;
  360. if (pages.count % eachPageCount == 0) {
  361. numPages = pages.count / eachPageCount;
  362. } else {
  363. numPages = pages.count / eachPageCount +1;
  364. }
  365. for (NSInteger index = 0; index < numPages; index ++) {
  366. NSMutableArray * eachPages = [NSMutableArray array];
  367. for (NSInteger i = 0; i<eachPageCount; i++) {
  368. NSUInteger currentIndex = i + eachPageCount * index;
  369. if (properties.isReversePage) {
  370. currentIndex = i + eachPageCount * (numPages - index - 1);
  371. }
  372. if (currentIndex >= pages.count) {
  373. break;
  374. }
  375. PDFPage * page = [pages[currentIndex] copy];
  376. [eachPages addObject:page];
  377. }
  378. if (eachPages.count < 1) {
  379. break;
  380. }
  381. CGSize size = properties.pageSize;
  382. CGFloat width = size.width;
  383. CGFloat height =size.height;
  384. if (kKMPDFMultiplePageDirection_Landscape == properties.direction) {
  385. width = size.height;
  386. height = size.width;
  387. }
  388. CGRect mediaBox = CGRectMake(0, 0, width,height);
  389. CGContextBeginPage(pdfContext, &mediaBox);
  390. [self drawPageToContext:pdfContext eachPages:eachPages multipleOption:properties];
  391. CGContextEndPage(pdfContext);
  392. }
  393. CGPDFContextClose(pdfContext);
  394. CGContextRelease (pdfContext);
  395. handler(YES);
  396. }
  397. - (void)drawPageToContext:(CGContextRef)context eachPages:(NSArray *)pages multipleOption:(KMEachPageProperties *)properties
  398. {
  399. /****
  400. *可以统一使用CG类来进行缩放、平移、旋转
  401. *PDFkit 自带画page的方法,这样能够保留注释存在
  402. if (@available(macOS 10.12, *)) {
  403. [self PDFKitDrawPageToContext:context eachPages:pages multipleOption:properties];
  404. } else {
  405. [self CGDrawPageToContext:context eachPages:pages multipleOption:properties];
  406. }
  407. *****/
  408. [self PDFKitDrawPageToContext:context eachPages:pages multipleOption:properties];
  409. }
  410. @end