KMPosterPrintManager.m 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  1. //
  2. // KMPosterPrintManager.m
  3. // PDF Reader Pro Edition
  4. //
  5. // Created by 丁林圭 on 2018/4/3.
  6. //
  7. #import "KMPosterPrintManager.h"
  8. #pragma mark - KMPDFPosterPrint
  9. @implementation KMPDFPosterPrint
  10. - (void)dealloc {
  11. }
  12. - (id)copyWithZone:(NSZone *)zone
  13. {
  14. KMPDFPosterPrint *print = [[[self class] allocWithZone:zone] init];
  15. print.isLabel = self.isLabel;
  16. print.isCut = self.isLabel;
  17. print.direction = self.direction;
  18. print.cropPages = self.cropPages;
  19. print.scale = self.scale;
  20. print.overlap = self.overlap;
  21. return print;
  22. }
  23. @end
  24. @interface KMPosterPrintManager()
  25. @property (nonatomic,retain) KMPDFPosterPrint * PDFPrint;
  26. @property (nonatomic,retain) NSArray * hourArray;
  27. @property (nonatomic,retain) NSArray * vertArray;
  28. @property (nonatomic,retain) NSString * filePath;
  29. @property (assign) NSUInteger lineIndex;//记录水平第几行
  30. @property (assign) NSUInteger columnIndex;//记录竖直第几列
  31. @property (assign) CGRect currentPageRect;
  32. @end
  33. @implementation KMPosterPrintManager
  34. -(void)dealloc
  35. {
  36. }
  37. #pragma mark - Private method
  38. -(void)splitPage:(PDFPage *)page rect:(CGRect)newRect toContext:(CGContextRef)pdfContext
  39. {
  40. PDFPage * newPage = page.copy;
  41. NSMutableArray *annotations = [NSMutableArray array];
  42. for (PDFAnnotation *annotation in newPage.annotations) {
  43. [annotations addObject:annotation];
  44. }
  45. if (kKMPDFPosterCommentsForms_Documents == _PDFPrint.commentsForms) {
  46. for (PDFAnnotation *annotation in annotations) {
  47. [annotation.page removeAnnotation:annotation];
  48. }
  49. } else if (kKMPDFPosterCommentsForms_DocumentStamps == _PDFPrint.commentsForms) {
  50. for (PDFAnnotation *annotation in annotations) {
  51. if (![annotation isKindOfClass:[PDFAnnotationStamp class]]) {
  52. [annotation.page removeAnnotation:annotation];
  53. }
  54. }
  55. }
  56. [newPage setBounds:newRect forBox:kPDFDisplayBoxCropBox];
  57. [self drawPDFPage:newPage cropRect:newRect toContext:pdfContext];
  58. }
  59. //将裁剪到的页面补到A4纸面上去
  60. -(void)drawPDFPage:(PDFPage *)page cropRect:(CGRect )cropRect toContext:(CGContextRef)context;
  61. {
  62. CGRect mediaBox = CGRectZero;
  63. CGFloat KBlankA4W =0;
  64. CGFloat KBlankA4H =0;
  65. if (self.PDFPrint.splitType == kKMPDFPosterSplitType_PageNumber) {
  66. if (self.PDFPrint.isOriginalPageSize) {
  67. if (page.rotation == 90 || page.rotation == 270) {
  68. KBlankA4W = cropRect.size.height;
  69. KBlankA4H = cropRect.size.width;
  70. } else {
  71. KBlankA4W = cropRect.size.width;
  72. KBlankA4H = cropRect.size.height;
  73. }
  74. } else {
  75. if(kKMPDFPosterPrintDirection_Portrait == self.PDFPrint.direction){
  76. KBlankA4W = self.PDFPrint.fullPageSize.width;
  77. KBlankA4H = self.PDFPrint.fullPageSize.height;
  78. } else {
  79. KBlankA4W = self.PDFPrint.fullPageSize.height;
  80. KBlankA4H = self.PDFPrint.fullPageSize.width;
  81. }
  82. }
  83. } else {
  84. if(kKMPDFPosterPrintDirection_Portrait == self.PDFPrint.direction){
  85. KBlankA4W = self.PDFPrint.splitSize.width;
  86. KBlankA4H = self.PDFPrint.splitSize.height;
  87. } else {
  88. KBlankA4W = self.PDFPrint.splitSize.height;
  89. KBlankA4H = self.PDFPrint.splitSize.width;
  90. }
  91. }
  92. mediaBox = CGRectMake(0, 0, KBlankA4W, KBlankA4H);
  93. CGContextBeginPage(context, &mediaBox);
  94. [NSGraphicsContext saveGraphicsState];
  95. [NSGraphicsContext setCurrentContext:[NSGraphicsContext graphicsContextWithGraphicsPort:context flipped:NO]];
  96. if (self.PDFPrint.isCut) {
  97. [self drawCutMarkContext:context contextSize:CGSizeMake(KBlankA4W, KBlankA4H)];
  98. }
  99. if (self.PDFPrint.isLabel){
  100. [self drawLabelTextContextSize:CGSizeMake(KBlankA4W, KBlankA4H)];
  101. }
  102. NSInteger rotate = page.rotation;
  103. CGFloat PDFWidth = cropRect.size.width;
  104. CGFloat PDFHeight = cropRect.size.height;
  105. if (90 == rotate || 270 == rotate) {
  106. PDFWidth = cropRect.size.height;
  107. PDFHeight = cropRect.size.width;
  108. }
  109. CGFloat scanl = self.PDFPrint.scale;
  110. CGFloat left = self.PDFPrint.edgeInsets.left;
  111. CGFloat right = self.PDFPrint.edgeInsets.right;
  112. CGFloat top = self.PDFPrint.edgeInsets.top;
  113. CGFloat bottom = self.PDFPrint.edgeInsets.bottom;
  114. if (kKMPDFPosterSplitType_PageNumber == self.PDFPrint.splitType) {
  115. left = self.PDFPrint.fullPageEdgeInsets.left;
  116. right = self.PDFPrint.fullPageEdgeInsets.right;
  117. top = self.PDFPrint.fullPageEdgeInsets.top;
  118. bottom = self.PDFPrint.fullPageEdgeInsets.bottom;
  119. CGFloat contextA4W = KBlankA4W - left - right;//实际内容的宽度
  120. CGFloat contextA4H = KBlankA4H - top - bottom;//实际内容的高度
  121. scanl = MIN(contextA4W/PDFWidth, contextA4H/PDFHeight);
  122. }
  123. CGFloat xTransform = 0;
  124. CGFloat yTransform = 0;
  125. if (rotate == 90) {
  126. if (self.hourArray.count == 1 ) {
  127. xTransform = (mediaBox.size.width - PDFWidth *scanl)/2;
  128. } else {
  129. if (cropRect.size.height - cropRect.origin.y > 0) {
  130. xTransform = mediaBox.size.width - PDFWidth * scanl - right;
  131. } else {
  132. xTransform = left;
  133. }
  134. }
  135. if (self.vertArray.count == 1) {
  136. yTransform = (mediaBox.size.height - PDFHeight * scanl)/2;
  137. } else {
  138. if (cropRect.size.width - cropRect.origin.x > 0) {
  139. yTransform = bottom;
  140. } else {
  141. yTransform = mediaBox.size.height - PDFHeight * scanl - top;
  142. }
  143. }
  144. } else if (rotate == 180) {
  145. if (self.hourArray.count == 1 ) {
  146. xTransform = (mediaBox.size.width - PDFWidth * scanl)/2;
  147. } else {
  148. if (cropRect.origin.x > 0) {
  149. xTransform = mediaBox.size.width - PDFWidth * scanl -right;
  150. } else {
  151. xTransform = left;
  152. }
  153. }
  154. if (self.vertArray.count == 1) {
  155. yTransform = (mediaBox.size.height - PDFHeight * scanl)/2;
  156. } else {
  157. if (cropRect.origin.y > 0) {
  158. yTransform = mediaBox.size.height - PDFHeight * scanl - top;
  159. } else {
  160. yTransform = bottom;
  161. }
  162. }
  163. } else if (rotate == 270) {
  164. if (self.hourArray.count == 1 ) {
  165. xTransform = (mediaBox.size.width - PDFWidth * scanl)/2;
  166. } else {
  167. if (cropRect.size.height - cropRect.origin.y > 0) {
  168. xTransform = left;
  169. } else {
  170. xTransform = mediaBox.size.width - PDFWidth * scanl - right;
  171. }
  172. }
  173. if (self.vertArray.count == 1) {
  174. yTransform = (mediaBox.size.height - PDFHeight * scanl)/2;
  175. } else {
  176. if (cropRect.size.width - cropRect.origin.x > 0) {
  177. yTransform = mediaBox.size.height - PDFHeight * scanl - top;
  178. } else {
  179. yTransform = bottom;
  180. }
  181. }
  182. } else {
  183. if (self.hourArray.count == 1 ) {
  184. xTransform = (mediaBox.size.width - PDFWidth * scanl)/2;
  185. } else {
  186. if (cropRect.origin.x > 0) {
  187. xTransform = left;
  188. } else {
  189. xTransform = mediaBox.size.width - PDFWidth * scanl - right;
  190. }
  191. }
  192. if (self.vertArray.count == 1) {
  193. yTransform = (mediaBox.size.height - PDFHeight * scanl)/2;
  194. } else {
  195. if (cropRect.origin.y > 0) {
  196. yTransform = bottom;
  197. } else {
  198. yTransform = mediaBox.size.height - PDFHeight * scanl - top;
  199. }
  200. }
  201. }
  202. CGContextTranslateCTM(context, xTransform,yTransform);
  203. CGContextScaleCTM(context, scanl, scanl);
  204. if (@available(macOS 10.12, *)) {
  205. [page drawWithBox:kPDFDisplayBoxCropBox toContext:context];
  206. [page transformContext:context forBox:kPDFDisplayBoxCropBox];
  207. } else {
  208. [NSGraphicsContext saveGraphicsState];
  209. [NSGraphicsContext setCurrentContext:[NSGraphicsContext graphicsContextWithGraphicsPort:context flipped:YES]];
  210. [page drawWithBox:kPDFDisplayBoxCropBox];
  211. [NSGraphicsContext restoreGraphicsState];
  212. [page transformContextForBox:kPDFDisplayBoxCropBox];
  213. }
  214. [NSGraphicsContext restoreGraphicsState];
  215. CGContextEndPage(context);
  216. }
  217. -(void)drawCutMarkContext:(CGContextRef)context contextSize:(CGSize)size
  218. {
  219. CGContextSetStrokeColorWithColor(context, [NSColor blackColor].CGColor);
  220. CGContextSetLineWidth(context, 1.0);
  221. CGContextSetLineCap(context, kCGLineCapSquare);
  222. if(self.columnIndex == 1 && self.lineIndex == 1) {
  223. [self drawLeftBottomCutMarks:context size:size];
  224. [self drawRightTopCutMarks:context size:size];
  225. [self drawRightBottomCutMarks:context size:size];
  226. } else if (self.lineIndex == 1 && self.columnIndex == self.vertArray.count) {
  227. [self drawLeftTopCutMarks:context size:size];
  228. [self drawRightTopCutMarks:context size:size];
  229. [self drawRightBottomCutMarks:context size:size];
  230. } else if (self.columnIndex == 1 && self.lineIndex == self.hourArray.count) {
  231. [self drawLeftTopCutMarks:context size:size];
  232. [self drawLeftBottomCutMarks:context size:size];
  233. [self drawRightBottomCutMarks:context size:size];
  234. } else if (self.columnIndex == self.vertArray.count && self.hourArray.count == self.lineIndex) {
  235. [self drawLeftTopCutMarks:context size:size];
  236. [self drawLeftBottomCutMarks:context size:size];
  237. [self drawRightTopCutMarks:context size:size];
  238. } else {
  239. [self drawLeftTopCutMarks:context size:size];
  240. [self drawLeftBottomCutMarks:context size:size];
  241. [self drawRightTopCutMarks:context size:size];
  242. [self drawRightBottomCutMarks:context size:size];
  243. }
  244. //绘制完成
  245. CGContextStrokePath(context);
  246. }
  247. //左上角切割标记
  248. -(void)drawLeftTopCutMarks:(CGContextRef)context size:(CGSize)size
  249. {
  250. CGFloat KBlankA4H =size.height;
  251. CGPoint point_LeftTop = CGPointZero;
  252. if (self.PDFPrint.splitType == kKMPDFPosterSplitType_PageNumber) {
  253. point_LeftTop.x = self.PDFPrint.fullPageEdgeInsets.left;
  254. } else {
  255. point_LeftTop.x = self.PDFPrint.edgeInsets.left;
  256. }
  257. point_LeftTop.y = KBlankA4H - self.PDFPrint.edgeInsets.top;
  258. CGContextMoveToPoint(context, 10,point_LeftTop.y);
  259. CGContextAddLineToPoint(context,point_LeftTop.x, point_LeftTop.y);
  260. CGContextMoveToPoint(context, point_LeftTop.x - 10,KBlankA4H -10);
  261. CGContextAddLineToPoint(context,point_LeftTop.x - 10,point_LeftTop.y);
  262. }
  263. //右上角切割标记
  264. -(void)drawRightTopCutMarks:(CGContextRef)context size:(CGSize)size
  265. {
  266. CGFloat KBlankA4W =size.width;
  267. CGFloat KBlankA4H =size.height;
  268. CGPoint point_RightTop = CGPointZero;//右上角
  269. if (self.PDFPrint.splitType == kKMPDFPosterSplitType_PageNumber) {
  270. point_RightTop.x = KBlankA4W - self.PDFPrint.fullPageEdgeInsets.right;
  271. point_RightTop.y = KBlankA4H - self.PDFPrint.fullPageEdgeInsets.top;
  272. } else {
  273. point_RightTop.x = KBlankA4W - self.PDFPrint.edgeInsets.right;
  274. point_RightTop.y = KBlankA4H - self.PDFPrint.edgeInsets.top;
  275. }
  276. CGContextMoveToPoint(context,point_RightTop.x,point_RightTop.y);
  277. CGContextAddLineToPoint(context,KBlankA4W - 10,point_RightTop.y);
  278. CGContextMoveToPoint(context,point_RightTop.x + 10,KBlankA4H - 10);
  279. CGContextAddLineToPoint(context,point_RightTop.x + 10,point_RightTop.y);
  280. }
  281. //左下角切割标记
  282. -(void)drawLeftBottomCutMarks:(CGContextRef)context size:(CGSize)size
  283. {
  284. CGPoint point_LeftBottom = CGPointZero;//左下角
  285. if (self.PDFPrint.splitType == kKMPDFPosterSplitType_PageNumber) {
  286. point_LeftBottom.x = self.PDFPrint.fullPageEdgeInsets.left;
  287. point_LeftBottom.y = self.PDFPrint.fullPageEdgeInsets.bottom;
  288. } else {
  289. point_LeftBottom.x = self.PDFPrint.edgeInsets.left;
  290. point_LeftBottom.y = self.PDFPrint.edgeInsets.bottom;
  291. }
  292. //左下角
  293. CGContextMoveToPoint(context, 10,point_LeftBottom.y);
  294. CGContextAddLineToPoint(context,point_LeftBottom.x, point_LeftBottom.y);
  295. CGContextMoveToPoint(context, point_LeftBottom.x- 10,10);
  296. CGContextAddLineToPoint(context,point_LeftBottom.x - 10,point_LeftBottom.y);
  297. }
  298. //右下角切割标记
  299. -(void)drawRightBottomCutMarks:(CGContextRef)context size:(CGSize)size
  300. {
  301. CGFloat KBlankA4W =size.width;
  302. CGPoint point_RightBottom = CGPointZero;//右下角
  303. if (self.PDFPrint.splitType == kKMPDFPosterSplitType_PageNumber) {
  304. point_RightBottom.x = KBlankA4W - self.PDFPrint.fullPageEdgeInsets.right;
  305. point_RightBottom.y = self.PDFPrint.fullPageEdgeInsets.bottom;
  306. } else {
  307. point_RightBottom.x = KBlankA4W - self.PDFPrint.edgeInsets.right;
  308. point_RightBottom.y = self.PDFPrint.edgeInsets.bottom;
  309. }
  310. CGContextMoveToPoint(context,KBlankA4W - 10,point_RightBottom.y);
  311. CGContextAddLineToPoint(context,point_RightBottom.x,point_RightBottom.y);
  312. CGContextMoveToPoint(context,point_RightBottom.x+ 10,10);
  313. CGContextAddLineToPoint(context,point_RightBottom.x+ 10,point_RightBottom.y);
  314. }
  315. - (void)drawLabelTextContextSize:(CGSize)contextSize
  316. {
  317. CGFloat KBlankA4W =contextSize.width;
  318. CGFloat KBlankA4H =contextSize.height;
  319. NSString *contextString = nil;
  320. if (self.PDFPrint.labelString && self.PDFPrint.labelString.length > 0) {
  321. contextString = self.PDFPrint.labelString;
  322. } else {
  323. NSDate *date = [NSDate date];
  324. NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  325. [formatter setDateFormat:@"YYYY-MM-dd hh:mm:ss"];
  326. contextString = [NSString stringWithFormat:@"(%ld,%ld) %@ %@",self.columnIndex,self.lineIndex,[self.filePath lastPathComponent],[formatter stringFromDate:date]];
  327. }
  328. CGFloat fontSize = 12.0 * (MAX(KBlankA4W, KBlankA4H)/842);
  329. NSFont *font = [NSFont systemFontOfSize:fontSize];
  330. NSColor *color = [NSColor blackColor];
  331. NSSize size = NSZeroSize;
  332. NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
  333. [style setAlignment:NSCenterTextAlignment];
  334. [style setLineBreakMode:NSLineBreakByCharWrapping];
  335. NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
  336. [dictionary setObject:style forKey:NSParagraphStyleAttributeName];
  337. [dictionary setObject:color forKey:NSForegroundColorAttributeName];
  338. [dictionary setObject:font forKey:NSFontAttributeName];
  339. size = [contextString boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT)
  340. options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
  341. attributes:dictionary].size;
  342. if (self.PDFPrint.splitType == kKMPDFPosterSplitType_PageNumber) {
  343. [contextString drawInRect:CGRectMake(self.PDFPrint.fullPageEdgeInsets.left +10,
  344. KBlankA4H - self.PDFPrint.fullPageEdgeInsets.top + size.height,
  345. size.width, size.height)
  346. withAttributes:dictionary];
  347. } else {
  348. [contextString drawInRect:CGRectMake(self.PDFPrint.edgeInsets.left +10,
  349. KBlankA4H - self.PDFPrint.edgeInsets.top + size.height,
  350. size.width, size.height)
  351. withAttributes:dictionary];
  352. }
  353. }
  354. #pragma mark Publick Action
  355. + (KMPosterPrintManager *)defaultManager
  356. {
  357. static KMPosterPrintManager *singleton = nil;
  358. static dispatch_once_t pred;
  359. dispatch_once(&pred, ^{
  360. singleton = [[KMPosterPrintManager alloc] init];
  361. });
  362. return singleton;
  363. }
  364. - (void)savePath:(NSString *)path posterPrint:(KMPDFPosterPrint *)print completionHandler:(void (^)(BOOL))complet
  365. {
  366. self.filePath = path;
  367. self.PDFPrint = print;
  368. NSArray * cropPages = print.cropPages;
  369. if (print.isReversePage) {
  370. cropPages = [[print.cropPages reverseObjectEnumerator] allObjects];
  371. }
  372. CFStringRef filePath = (__bridge CFStringRef)path;
  373. CFURLRef url = CFURLCreateWithFileSystemPath(NULL, filePath, kCFURLPOSIXPathStyle, 0);
  374. CFMutableDictionaryRef myDictionary = CFDictionaryCreateMutable(NULL,
  375. 0,
  376. &kCFTypeDictionaryKeyCallBacks,
  377. &kCFTypeDictionaryValueCallBacks);
  378. CFDictionarySetValue(myDictionary, kCGPDFContextCreator, CFSTR("PDF Reader Pro"));
  379. CGContextRef pdfContext = CGPDFContextCreateWithURL(url, &CGRectZero, myDictionary);
  380. CFRelease(myDictionary);
  381. CFRelease(url);
  382. if (!pdfContext) {
  383. complet(NO);
  384. return;
  385. }
  386. for (PDFPage * page in cropPages) {
  387. [self posterPrintPage:page posterPrint:print completionHandler:^(NSArray *horizontals, NSArray *verticals) {
  388. self.hourArray = horizontals;
  389. self.vertArray = [[verticals reverseObjectEnumerator] allObjects];//裁剪的时候,应从上自下依次裁剪打印
  390. NSInteger rotate = page.rotation;
  391. if (90 == rotate || 270 == rotate) {
  392. self.hourArray = verticals;
  393. self.vertArray = [[horizontals reverseObjectEnumerator] allObjects];
  394. }
  395. if (kKMPDFPosterSplitType_PageNumber == print.splitType) {
  396. for (NSUInteger j=0; j< _vertArray.count; j++) {
  397. self.columnIndex = j + 1;
  398. for (NSUInteger i=0; i< _hourArray.count; i++) {
  399. CGRect newRect = CGRectZero;
  400. self.lineIndex = i + 1;
  401. if (i != 0) {
  402. newRect.origin.x = [_hourArray[i - 1] floatValue] -print.overlap;
  403. }
  404. if (j + 1 != _vertArray.count) {
  405. newRect.origin.y = [_vertArray[j + 1] floatValue] -print.overlap;
  406. }
  407. newRect.size.width = [_hourArray[i] floatValue] - newRect.origin.x;
  408. newRect.size.height = [_vertArray[j] floatValue] - newRect.origin.y;
  409. [self splitPage:page rect:newRect toContext:pdfContext];
  410. }
  411. }
  412. } else {
  413. for (NSUInteger j=0; j< _vertArray.count; j++) {
  414. self.columnIndex = j + 1;
  415. for (NSUInteger i=0; i< _hourArray.count; i++) {
  416. self.lineIndex = i + 1;
  417. CGRect newRect = CGRectZero;
  418. if (i != 0) {
  419. newRect.origin.x = [_hourArray[i - 1] floatValue] -print.overlap/print.scale;
  420. }
  421. if (j + 1 != _vertArray.count) {
  422. newRect.origin.y = [_vertArray[j + 1] floatValue] -print.overlap/print.scale;
  423. }
  424. newRect.size.width = [_hourArray[i] floatValue] - newRect.origin.x;
  425. newRect.size.height = [_vertArray[j] floatValue] - newRect.origin.y;
  426. [self splitPage:page rect:newRect toContext:pdfContext];
  427. }
  428. }
  429. }
  430. }];
  431. }
  432. CGPDFContextClose(pdfContext);
  433. CGContextRelease (pdfContext);
  434. complet(YES);
  435. }
  436. //得到裁剪点数组
  437. - (void)posterPrintPage:(PDFPage *)page posterPrint:(KMPDFPosterPrint *)properties completionHandler:(postPrintCallback)handler
  438. {
  439. NSMutableArray *horizontalArray = [NSMutableArray array];//水平坐标数组
  440. NSMutableArray *verticalArray = [NSMutableArray array];//垂直坐标数组
  441. NSRect rect = [page boundsForBox:kPDFDisplayBoxCropBox];
  442. CGFloat orgPDFWidth = rect.size.width;//待裁剪的Page宽度
  443. CGFloat orgPDFHeight = rect.size.height;//待裁剪的Page高度
  444. NSInteger rotate = page.rotation;
  445. if (90 == rotate || 270 == rotate) {
  446. orgPDFWidth = rect.size.height;
  447. orgPDFHeight = rect.size.width;
  448. }
  449. CGFloat overlap = properties.overlap;
  450. if (kKMPDFPosterSplitType_PageNumber == properties.splitType) {
  451. NSInteger lineCount = properties.lineCount;
  452. if (lineCount>0) {
  453. CGFloat splitPDFW = orgPDFWidth + (lineCount - 1) * overlap;
  454. CGFloat spaceW = splitPDFW/lineCount; //均分宽度
  455. [horizontalArray addObject:@(spaceW)];
  456. for (NSInteger m =1; m< lineCount; m ++) {
  457. [horizontalArray addObject:@(spaceW + m *(spaceW - overlap))];
  458. }
  459. }
  460. NSInteger columnCount = properties.columnCount;
  461. if (columnCount>0) {
  462. CGFloat splitPDFH = orgPDFHeight + (columnCount- 1) * overlap;
  463. CGFloat spaceH = splitPDFH/columnCount; //均分高度
  464. [verticalArray addObject:@(spaceH)];
  465. for (NSInteger n =1; n< columnCount; n ++) {
  466. [verticalArray addObject:@(spaceH + n *(spaceH - overlap))];
  467. }
  468. }
  469. } else {
  470. CGFloat scale = properties.scale;
  471. CGFloat splitWidth = properties.splitSize.width;
  472. CGFloat splitHeight = properties.splitSize.height;
  473. if (kKMPDFPosterPrintDirection_Landscape == properties.direction) {
  474. splitWidth = properties.splitSize.height;
  475. splitHeight = properties.splitSize.width;
  476. }
  477. CGFloat contextA4W = splitWidth - properties.edgeInsets.left - properties.edgeInsets.right;//实际内容的宽度
  478. CGFloat contextA4H = splitHeight - properties.edgeInsets.bottom - properties.edgeInsets.top;//实际内容的高度
  479. splitWidth = contextA4W/scale;
  480. splitHeight = contextA4H/scale;
  481. NSInteger columnCount = 0;//宽能切完整的个数
  482. CGFloat postPrintX = (contextA4W - overlap)/scale;
  483. while ((splitWidth + columnCount * postPrintX)< orgPDFWidth ) {
  484. columnCount ++;
  485. }
  486. CGFloat totalWidth = orgPDFWidth + columnCount * overlap;
  487. CGFloat firstX = (totalWidth - (columnCount-1) * splitWidth)/ 2; //计算第一个横向切割点
  488. //计算切割点
  489. if (columnCount >0) {
  490. if (firstX > 0) {
  491. CGFloat ww = firstX;
  492. [horizontalArray addObject:@(ww)];
  493. for (NSInteger i =1; i< columnCount; i ++) {
  494. [horizontalArray addObject:@(postPrintX *i + ww)];
  495. }
  496. [horizontalArray addObject:@(orgPDFWidth)];
  497. } else if(0 == (int)firstX){
  498. [horizontalArray addObject:@(splitWidth)];
  499. for (NSInteger j =1; j< columnCount; j ++) {
  500. [horizontalArray addObject:@(splitWidth + postPrintX *j)];
  501. }
  502. }
  503. } else {
  504. [horizontalArray addObject:@(orgPDFWidth)];
  505. }
  506. NSInteger lineCount = 0;//高能切完整的个数
  507. CGFloat postPrintY = (contextA4H - overlap)/scale;
  508. while ((splitHeight + lineCount* postPrintY)< orgPDFHeight ) {
  509. lineCount ++;
  510. }
  511. CGFloat totalHeight = orgPDFHeight + lineCount * overlap;
  512. CGFloat firstY = (totalHeight - (lineCount-1) * splitHeight)/ 2; //计算第一个纵向的切割点
  513. if (lineCount >0) {
  514. if (firstY > 0) {
  515. CGFloat hh = firstY;
  516. [verticalArray addObject:@(hh)];
  517. for (NSInteger m =1; m< lineCount; m ++) {
  518. [verticalArray addObject:@(postPrintY *m + hh)];
  519. }
  520. [verticalArray addObject:@(orgPDFHeight)];
  521. } else if(0 == (int)firstY){
  522. [verticalArray addObject:@(splitHeight)];
  523. for (NSInteger n =1; n< lineCount; n++) {
  524. [verticalArray addObject:@(splitHeight + postPrintY *n)];
  525. }
  526. }
  527. } else {
  528. [verticalArray addObject:@(orgPDFHeight)];
  529. }
  530. }
  531. handler(horizontalArray,verticalArray);
  532. }
  533. @end