KMPDFPosterPrintWindowController.m 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020
  1. //
  2. // KMPDFPosterPrintWindowController.m
  3. // PDF Reader Pro Edition
  4. //
  5. // Created by 丁林圭 on 2018/4/3.
  6. //
  7. #import "PDF_Reader_Pro-Swift.h"
  8. #import "KMPDFPosterPrintWindowController.h"
  9. #import "KMPosterPrintManager.h"
  10. //#import "TextFieldFormatter.h"
  11. //#import "KMFileAttribute.h"
  12. //#import "KMBookletMaskView.h"
  13. //#import "PasswordWindowController.h"
  14. //#import "KMSavePanelAccessoryController.h"
  15. //#import "NSWindowController_SKExtensions.h"
  16. //#import "KMPageSizeTool.h"
  17. #define maxPrintWidth 300
  18. #define kPosterPrintFolderPath [[[NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:[NSBundle mainBundle].bundleIdentifier] stringByAppendingPathComponent:@"PosterPrint"]
  19. #define kPosterPrintFileSavePath [[[NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:[NSBundle mainBundle].bundleIdentifier] stringByAppendingPathComponent:@"PosterPrint.pdf"]
  20. #pragma mark KMPDFPosterPrintContentView
  21. @interface KMPDFPosterPrintContentView : NSView
  22. @property (nonatomic,retain) NSArray *horizontals;
  23. @property (nonatomic,retain) NSArray *verticals;
  24. @property (nonatomic,assign) CGFloat overlap;
  25. @property (nonatomic,assign) CGFloat spaceWidth;
  26. @property (nonatomic,assign) CGFloat spaceHeight;
  27. @property (nonatomic,assign) CGFloat scanle;
  28. @end
  29. @implementation KMPDFPosterPrintContentView
  30. -(void)dealloc
  31. {
  32. }
  33. -(void)drawRect:(NSRect)dirtyRect
  34. {
  35. [super drawRect:dirtyRect];
  36. CGContextRef context = [[NSGraphicsContext currentContext] graphicsPort];// 获取画布
  37. CGFloat width = self.bounds.size.width;
  38. CGFloat height = self.bounds.size.height;
  39. if (_horizontals.count> 0 || _verticals.count > 0) {
  40. NSArray *hourArray = _horizontals;
  41. NSArray *vertArray = _verticals;
  42. [NSGraphicsContext saveGraphicsState];
  43. if (context) {
  44. [NSGraphicsContext setCurrentContext:[NSGraphicsContext graphicsContextWithGraphicsPort:context flipped:NO]];
  45. }
  46. CGContextSetStrokeColorWithColor(context, [NSColor colorWithRed:51.0/255.0 green:186.0/255.0 blue:234.0/255.0 alpha:1.0].CGColor);
  47. CGContextSetLineWidth(context, 1.0);
  48. for (NSUInteger i =0; i< vertArray.count -1; i ++) {
  49. CGContextMoveToPoint(context, 0,height - ([vertArray[i] floatValue] /_scanle + _spaceHeight));
  50. CGContextAddLineToPoint(context,width, height - ([vertArray[i] floatValue] /_scanle +_spaceHeight));
  51. }
  52. for (NSUInteger j =0; j< hourArray.count - 1; j ++) {
  53. CGContextMoveToPoint(context, [hourArray[j] floatValue]/_scanle + _spaceWidth, 0);
  54. CGContextAddLineToPoint(context,[hourArray[j] floatValue]/_scanle + _spaceWidth, height);
  55. }
  56. CGFloat arr[] = {8,1};
  57. CGContextSetLineDash(context, 0, arr, 1);
  58. CGContextDrawPath(context, kCGPathStroke);
  59. [NSGraphicsContext restoreGraphicsState];
  60. }
  61. }
  62. @end
  63. #pragma mark -KMPDFPosterPrintWindowController
  64. @interface KMPDFPosterPrintWindowController ()<NSComboBoxDelegate>
  65. @property (nonatomic,retain) PDFDocument *PDFDocument;
  66. @property (nonatomic,retain) NSString *password;
  67. @property (nonatomic,retain) KMPDFPosterPrint *PDFPrint;
  68. @property (nonatomic,assign) IBOutlet NSTextField *currentPageLabel;
  69. @property (nonatomic,assign) IBOutlet NSTextField *totalPageLabel;
  70. @property (assign) IBOutlet NSTextField *scaleTextField;
  71. @property (assign) IBOutlet NSButton *cutMarksButton;
  72. @property (assign) IBOutlet NSButton *labelButton;
  73. @property (assign) IBOutlet NSButton *portraitButton;
  74. @property (assign) IBOutlet NSButton *landscapeButton;
  75. @property (nonatomic,assign) IBOutlet NSTextField *pageRangeLabel;
  76. @property (nonatomic,assign) IBOutlet NSComboBox *pageRangeComboBox;
  77. @property (nonatomic,assign) IBOutlet NSTextField *splitSizeLabel;
  78. @property (nonatomic,assign) IBOutlet NSComboBox *splitSizeComboBox;
  79. @property (nonatomic,assign) IBOutlet NSTextField *overlapLabel;
  80. @property (nonatomic,assign) IBOutlet NSTextField *overlapUnit;
  81. @property (assign) IBOutlet NSTextField *overlapTextField;
  82. @property (assign) IBOutlet NSTextField *sizeLabel;
  83. @property (assign) IBOutlet NSTextField *zoomLabel;
  84. @property (nonatomic,assign) IBOutlet NSComboBox *unitComboBox;
  85. @property (nonatomic,assign) IBOutlet NSImageView *thumImageView;
  86. @property (assign) IBOutlet NSLayoutConstraint *PDFHeight;
  87. @property (assign) IBOutlet NSLayoutConstraint *PDFWidth;
  88. @property (nonatomic,assign) IBOutlet NSView *PDFContentView;
  89. @property (assign) IBOutlet NSLayoutConstraint *contentHeight;
  90. @property (assign) IBOutlet NSLayoutConstraint *contentWidth;
  91. @property (assign) IBOutlet NSButton *cancelButton;
  92. @property (assign) IBOutlet NSButton *saveButton;
  93. @property (assign) IBOutlet NSButton *printButton;
  94. @property (assign,nonatomic) IBOutlet NSTextField *orientationLabel;
  95. @property (assign,nonatomic) IBOutlet NSTextField *unitLabel;
  96. @property (assign,nonatomic) IBOutlet NSTextField *titleScanlLabel;
  97. @property (assign,nonatomic) IBOutlet NSTextField *labelTextField;
  98. @property (assign,nonatomic) IBOutlet NSButton *reverseButton;
  99. @property (nonatomic,assign) IBOutlet NSTextField *commentsFormsLabel;
  100. @property (nonatomic,assign) IBOutlet NSComboBox *commentsFormsComboBox;
  101. @property (nonatomic,assign) IBOutlet NSTextField *pagesPerLabel;
  102. @property (nonatomic,assign) IBOutlet NSComboBox *selectPerComboBox;
  103. @property (assign,nonatomic) IBOutlet NSTextField *lineCountText;
  104. @property (assign,nonatomic) IBOutlet NSTextField *columnCountText;
  105. @property (assign,nonatomic) IBOutlet NSTextField *filePageSizeLabel;
  106. @property (assign,nonatomic) IBOutlet NSComboBox *filePageSizeBox;
  107. @property (assign) IBOutlet NSTextField *pageWidthTextField;
  108. @property (assign) IBOutlet NSTextField *pageHeightTextField;
  109. @property (assign) IBOutlet NSTextField *unitTLabel;
  110. @property (assign) IBOutlet NSTextField *paperWidthTextField;
  111. @property (assign) IBOutlet NSTextField *paperHeightTextField;
  112. @property (assign) IBOutlet NSTextField *unitT1Label;
  113. @property (assign) IBOutlet NSTabView *tableView;
  114. @property (nonatomic,retain) KMPDFPosterPrintContentView *preview;
  115. @property (nonatomic, retain) KMBookletMaskView *posterMaskView;
  116. @property (nonatomic,assign) NSUInteger currentPageIndex;
  117. @property (nonatomic,retain) NSArray * pages;;
  118. @end
  119. @implementation KMPDFPosterPrintWindowController
  120. - (void)dealloc
  121. {
  122. NSFileManager *manager = [NSFileManager defaultManager];
  123. if ([manager fileExistsAtPath:kPosterPrintFileSavePath]) {
  124. [manager removeItemAtPath:kPosterPrintFileSavePath error:nil];
  125. }
  126. }
  127. - (id)initWithPDFDocument:(PDFDocument *)pdfDocument
  128. {
  129. if (self = [super initWithWindowNibName:@"KMPDFPosterPrintWindowController"]) {
  130. [self showWaitting];
  131. NSDate *date = [NSDate date];
  132. NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  133. [formatter setDateFormat:@"YYYY-MM-dd hh:mm:ss"];
  134. NSString *contextString = [NSString stringWithFormat:@"(column,row) %@ %@",[pdfDocument.documentURL.path lastPathComponent],[formatter stringFromDate:date]];
  135. [self.labelTextField.cell setPlaceholderString:contextString];
  136. dispatch_async(dispatch_get_global_queue(0, 0), ^{
  137. [pdfDocument writeToURL:[NSURL fileURLWithPath:kPosterPrintFileSavePath]];
  138. self.PDFDocument = [[PDFDocument alloc] initWithURL:[NSURL fileURLWithPath:kPosterPrintFileSavePath]];
  139. dispatch_async(dispatch_get_main_queue(), ^{
  140. [self hideWaitting];
  141. if (self.PDFDocument) {
  142. [self getDrawPages];
  143. [self reloadPDFData];
  144. [self reloadPDFView];
  145. }else{
  146. [self close];
  147. }
  148. });
  149. });
  150. }
  151. return self;
  152. }
  153. - (id)initWithPDFFilePath:(NSString *)filePath password:(nullable NSString *)password{
  154. if (self = [super initWithWindowNibName:@"KMPDFPosterPrintWindowController"]) {
  155. PDFDocument * pdf = [[PDFDocument alloc] initWithURL:[NSURL fileURLWithPath:filePath]];
  156. self.PDFDocument = pdf;
  157. self.password = password;
  158. }
  159. return self;
  160. }
  161. - (BOOL)windowShouldClose:(id)sender
  162. {
  163. [self close];
  164. return YES;
  165. }
  166. - (void)close {
  167. if (self.window.isSheet) {
  168. [self.window.sheetParent endSheet:self.window];
  169. // [self dismissSheet:nil];
  170. } else {
  171. [super close];
  172. }
  173. }
  174. - (void)windowDidLoad
  175. {
  176. [super windowDidLoad];
  177. [self localizedString];
  178. _PDFContentView.wantsLayer = YES;
  179. _PDFContentView.layer.borderWidth = 2.0;
  180. _PDFContentView.layer.borderColor = [NSColor blackColor].CGColor;
  181. _PDFContentView.layer.backgroundColor = [NSColor whiteColor].CGColor;
  182. self.currentPageIndex = 0;
  183. self.scaleTextField.stringValue = @"100";
  184. [self.scaleTextField setFormatter:[[TextFieldFormatter alloc] init]];
  185. self.columnCountText.stringValue = [NSString stringWithFormat:@"%ld",self.PDFPrint.columnCount]?:@"";
  186. self.lineCountText.stringValue = [NSString stringWithFormat:@"%ld",self.PDFPrint.lineCount]?:@"";
  187. self.overlapTextField.stringValue = @"0";
  188. [self.overlapTextField setFormatter:[[TextFieldFormatter alloc] init]];
  189. [self.labelTextField setEditable:NO];
  190. if (!self.PDFDocument) {
  191. return;
  192. }
  193. if (![self.PDFDocument isLocked] && [self.PDFDocument unlockWithPassword:self.password]) {
  194. [self getDrawPages];
  195. [self reloadPDFData];
  196. [self reloadPDFView];
  197. NSDate *date = [NSDate date];
  198. NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  199. [formatter setDateFormat:@"YYYY-MM-dd hh:mm:ss"];
  200. NSString *contextString = [NSString stringWithFormat:@"(column,row) %@ %@",[self.PDFDocument.documentURL.path lastPathComponent],[formatter stringFromDate:date]];
  201. [self.labelTextField.cell setPlaceholderString:contextString];
  202. } else {
  203. // dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  204. // PasswordWindowController *com = [[PasswordWindowController alloc] initWithWindowNibName:@"PasswordWindowController"];
  205. // com.fileURL = self.PDFDocument.documentURL;
  206. // [com beginSheetModalForWindow:self.window completionHandler:^(NSString *password) {
  207. // if (password) {
  208. // [self.PDFDocument unlockWithPassword:password];
  209. // self.password = password;
  210. // [self getDrawPages];
  211. // [self reloadPDFData];
  212. // [self reloadPDFView];
  213. //
  214. // NSDate *date = [NSDate date];
  215. // NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
  216. // [formatter setDateFormat:@"YYYY-MM-dd hh:mm:ss"];
  217. // NSString *contextString = [NSString stringWithFormat:@"(column,row) %@ %@",[self.PDFDocument.documentURL.path lastPathComponent],[formatter stringFromDate:date]];
  218. //
  219. // [self.labelTextField.cell setPlaceholderString:contextString];
  220. //
  221. // } else {
  222. // [self close];
  223. //
  224. // }
  225. // }];
  226. // [com release];
  227. // });
  228. }
  229. }
  230. - (void)localizedString
  231. {
  232. self.window.title = NSLocalizedString(@"Poster", nil);
  233. self.pageRangeLabel.stringValue = NSLocalizedString(@"Page Range", nil);
  234. self.orientationLabel.stringValue = NSLocalizedString(@"Orientation:", nil);
  235. self.unitLabel.stringValue = NSLocalizedString(@"Unit:", nil);
  236. self.unitTLabel.stringValue = self.unitT1Label.stringValue = self.overlapUnit.stringValue = NSLocalizedString(@"mm", nil);
  237. self.titleScanlLabel.stringValue = NSLocalizedString(@"Tile Scale:", nil);
  238. self.overlapLabel.stringValue = NSLocalizedString(@"Overlap:",nil);
  239. self.commentsFormsLabel.stringValue = NSLocalizedString(@"Comments & Forms:",nil);
  240. [self.pageRangeComboBox addItemsWithObjectValues:@[NSLocalizedString(@"All Pages", nil),
  241. NSLocalizedString(@"Odd Pages Only", nil),
  242. NSLocalizedString(@"Even Pages Only",nil),
  243. NSLocalizedString(@"e.g. 1,3-5,10",nil)]];
  244. [self.pageRangeComboBox.cell setPlaceholderString:NSLocalizedString(@"e.g. 1,3-5,10", nil)];
  245. [self.commentsFormsComboBox addItemsWithObjectValues:@[NSLocalizedString(@"Document", nil),
  246. NSLocalizedString(@"Document and Markups", nil),
  247. NSLocalizedString(@"Document and Stamps",nil)
  248. ]];
  249. [self.commentsFormsComboBox selectItemAtIndex:1];
  250. [self.commentsFormsComboBox setEditable:NO];
  251. self.splitSizeLabel.stringValue = NSLocalizedString(@"Page size:", nil);
  252. [self.cancelButton setTitle:NSLocalizedString(@"Cancel", nil)];
  253. [self.saveButton setTitle:NSLocalizedString(@"Save as PDF", nil)];
  254. [self.printButton setTitle:NSLocalizedString(@"Print", nil)];
  255. [self.portraitButton setTitle:NSLocalizedString(@"Portrait",nil)];
  256. [self.landscapeButton setTitle:NSLocalizedString(@"Landscape",nil)];
  257. [self.cutMarksButton setTitle:NSLocalizedString(@"Cut Marks",nil)];
  258. [self.labelButton setTitle:NSLocalizedString(@"Label",nil)];
  259. [self.reverseButton setTitle:NSLocalizedString(@"Reverse pages",nil)];
  260. self.pagesPerLabel.stringValue = NSLocalizedString(@"Pages per sheet:", nil);
  261. self.filePageSizeLabel.stringValue = NSLocalizedString(@"Destination paper Size:", nil);
  262. [[self.tableView tabViewItemAtIndex:0] setLabel:NSLocalizedString(@"Tile", nil)];
  263. [[self.tableView tabViewItemAtIndex:1] setLabel:NSLocalizedString(@"Split", nil)];
  264. [self.pageRangeComboBox selectItemAtIndex:0];
  265. [self.pageRangeComboBox setEditable:NO];
  266. [self.unitComboBox selectItemAtIndex:1];
  267. NSArray *paperArray = [KMPageSizeTool paperSize];
  268. NSMutableArray *tPaperArray = [NSMutableArray arrayWithArray:paperArray];
  269. [tPaperArray addObject:NSLocalizedString(@"Custom",nil)];
  270. [self.splitSizeComboBox addItemsWithObjectValues:tPaperArray];
  271. [self.splitSizeComboBox selectItemAtIndex:1];
  272. [self.splitSizeComboBox setEditable:NO];
  273. self.pageWidthTextField.enabled = NO;
  274. self.pageHeightTextField.enabled = NO;
  275. NSMutableArray *tFileArray = [NSMutableArray arrayWithArray:paperArray];
  276. [tFileArray insertObject:NSLocalizedString(@"Actual Size", nil) atIndex:0];
  277. [tFileArray addObject:NSLocalizedString(@"Custom", nil)];
  278. [self.filePageSizeBox addItemsWithObjectValues:tFileArray];
  279. [self.filePageSizeBox selectItemAtIndex:0];
  280. [self.filePageSizeBox setEditable:NO];
  281. self.paperWidthTextField.enabled = NO;
  282. self.paperHeightTextField.enabled = NO;
  283. [self.selectPerComboBox addItemsWithObjectValues:@[@"2",
  284. @"4",
  285. @"6",
  286. @"9",
  287. @"16",
  288. NSLocalizedString(@"Custom", nil)]];
  289. [self.selectPerComboBox selectItemAtIndex:5];
  290. [self.selectPerComboBox setEditable:NO];
  291. }
  292. - (KMPDFPosterPrint *)PDFPrint
  293. {
  294. if (!_PDFPrint) {
  295. _PDFPrint = [[KMPDFPosterPrint alloc] init];
  296. _PDFPrint.scale = 1.0;
  297. _PDFPrint.direction = kKMPDFPosterPrintDirection_Portrait;
  298. _PDFPrint.commentsForms = kKMPDFPosterCommentsForms_DocumentMarkups;
  299. _PDFPrint.splitType = kKMPDFPosterSplitType_PageSize;
  300. _PDFPrint.lineCount = 2;
  301. _PDFPrint.columnCount = 2;
  302. _PDFPrint.isCut = NO;
  303. _PDFPrint.isLabel = NO;
  304. _PDFPrint.isOriginalPageSize = YES;
  305. _PDFPrint.fullPageSize =
  306. _PDFPrint.splitSize = CGSizeMake(595, 842);
  307. _PDFPrint.edgeInsets = NSEdgeInsetsMake(50, 60, 50, 60);
  308. _PDFPrint.fullPageEdgeInsets = NSEdgeInsetsMake(50, 60, 50, 60);
  309. }
  310. return _PDFPrint;
  311. }
  312. -(void)reloadPDFView
  313. {
  314. NSInteger indexPage = self.currentPageIndex;
  315. if (_PDFPrint.isReversePage) {
  316. indexPage = self.pages.count - self.currentPageIndex - 1;
  317. }
  318. PDFPage *page = self.pages[indexPage];
  319. CGRect rect = [page boundsForBox:kPDFDisplayBoxCropBox];
  320. [[KMPosterPrintManager defaultManager] posterPrintPage:page posterPrint:self.PDFPrint completionHandler:^(NSArray *horizontals, NSArray *verticals) {
  321. NSData *pageData = page.dataRepresentation;
  322. NSImage *img = [[NSImage alloc] initWithData:pageData];
  323. self.thumImageView.image = img;
  324. CGFloat printWidth = 0;
  325. CGFloat printHeight = 0;
  326. if (kKMPDFPosterPrintDirection_Landscape == self.PDFPrint.direction) {
  327. printHeight = self.PDFPrint.splitSize.width - self.PDFPrint.edgeInsets.top - self.PDFPrint.edgeInsets.bottom;
  328. printWidth = self.PDFPrint.splitSize.height- self.PDFPrint.edgeInsets.left - self.PDFPrint.edgeInsets.right;
  329. } else {
  330. printWidth = self.PDFPrint.splitSize.width - self.PDFPrint.edgeInsets.left - self.PDFPrint.edgeInsets.right;
  331. printHeight = self.PDFPrint.splitSize.height- self.PDFPrint.edgeInsets.top - self.PDFPrint.edgeInsets.bottom;
  332. }
  333. printWidth /= self.PDFPrint.scale;
  334. printHeight /= self.PDFPrint.scale;
  335. CGFloat contentWidth = printWidth + (printWidth - self.PDFPrint.overlap) * (horizontals.count -1); //裁剪Page的总宽度
  336. CGFloat contentHeight = printHeight + (printHeight- self.PDFPrint.overlap) * (verticals.count - 1); //裁剪Page的总高度
  337. CGFloat realityPDFW = rect.size.width;
  338. CGFloat realityPDFH = rect.size.height;
  339. NSInteger rotate = page.rotation;
  340. if (90 == rotate || 270 == rotate) {
  341. realityPDFW = rect.size.height;
  342. realityPDFH = rect.size.width;
  343. }
  344. if (self.PDFPrint.splitType == kKMPDFPosterSplitType_PageNumber) {
  345. contentWidth = realityPDFW;
  346. contentHeight = realityPDFH;
  347. }
  348. CGFloat max = MAX((contentWidth/maxPrintWidth), (contentHeight/maxPrintWidth));
  349. self.contentWidth.constant = contentWidth/max;
  350. self.contentHeight.constant = contentHeight/max;
  351. self.PDFWidth.constant = realityPDFW/max;
  352. self.PDFHeight.constant = realityPDFH/max;
  353. self.preview.frame = CGRectMake(0, 0, self.contentWidth.constant, self.contentHeight.constant);
  354. self.preview.horizontals = horizontals;
  355. self.preview.verticals = verticals;
  356. self.preview.overlap = self.PDFPrint.overlap / max;
  357. self.preview.scanle = max;
  358. self.preview.spaceWidth = (self.contentWidth.constant - self.PDFWidth.constant)/2;
  359. self.preview.spaceHeight = (self.contentHeight.constant - self.PDFHeight.constant)/2;
  360. CGSize paperSize = CGSizeZero;
  361. NSInteger tScale = 0;
  362. if (self.PDFPrint.splitType == kKMPDFPosterSplitType_PageNumber) {
  363. CGFloat w = (realityPDFW + self.PDFPrint.overlap * (horizontals.count -1))/self.PDFPrint.columnCount;
  364. CGFloat h = (realityPDFH + self.PDFPrint.overlap * (verticals.count -1))/self.PDFPrint.lineCount;
  365. if (self.PDFPrint.isOriginalPageSize) {
  366. tScale = 100;
  367. paperSize = CGSizeMake(realityPDFW/self.PDFPrint.columnCount, realityPDFW/self.PDFPrint.lineCount);
  368. }else {
  369. tScale = MIN(self.PDFPrint.fullPageSize.width/w, self.PDFPrint.fullPageSize.height/h) * 100;
  370. paperSize = self.PDFPrint.fullPageSize;
  371. }
  372. } else {
  373. paperSize = self.PDFPrint.splitSize;
  374. tScale = (NSInteger)(((realityPDFW + self.PDFPrint.overlap * (horizontals.count -1))/realityPDFW) *self.PDFPrint.scale*100);
  375. }
  376. NSInteger tPagesCount = (NSInteger)(verticals.count* horizontals.count);
  377. self.zoomLabel.stringValue = [NSString stringWithFormat:@"%@ %ld%% %@ %ld", NSLocalizedString(@"Scale:", nil), tScale, NSLocalizedString(@"Pages:", nil), tPagesCount];
  378. CGFloat W = (self.PDFPrint.splitSize.width *horizontals.count)/(595/210.0);
  379. CGFloat H = (self.PDFPrint.splitSize.height *verticals.count)/(842/297.0);
  380. paperSize.width = paperSize.width/(595/210.0);
  381. paperSize.height = paperSize.height/(842/297.0);
  382. self.sizeLabel.stringValue = [NSString stringWithFormat:@"%@ x %@ %@",[KMPageSizeTool conversionWithUnit:self.unitComboBox.stringValue value:W],[KMPageSizeTool conversionWithUnit:self.unitComboBox.stringValue value:H],self.unitComboBox.stringValue] ?:@"";
  383. self.pageWidthTextField.stringValue = [KMPageSizeTool conversionWithUnit:self.unitComboBox.stringValue value:(self.PDFPrint.splitSize.width/595 * 210)]?:@"";
  384. self.pageHeightTextField.stringValue = [KMPageSizeTool conversionWithUnit:self.unitComboBox.stringValue value:(self.PDFPrint.splitSize.height/842 * 297)]?:@"";
  385. if ([self.filePageSizeBox indexOfSelectedItem] == 0) {
  386. self.paperWidthTextField.stringValue = [KMPageSizeTool conversionWithUnit:self.unitComboBox.stringValue value:(self.PDFPrint.fullPageSize.width/585.0 * 210)];
  387. self.paperHeightTextField.stringValue = [KMPageSizeTool conversionWithUnit:self.unitComboBox.stringValue value:(self.PDFPrint.splitSize.height/842 * 297)]?:@"";
  388. } else {
  389. self.paperWidthTextField.stringValue = [KMPageSizeTool conversionWithUnit:self.unitComboBox.stringValue value:paperSize.width];
  390. self.paperHeightTextField.stringValue = [KMPageSizeTool conversionWithUnit:self.unitComboBox.stringValue value:paperSize.height]?:@"";
  391. }
  392. [self.preview setNeedsDisplay:YES];
  393. }];
  394. }
  395. - (KMPDFPosterPrintContentView *)preview
  396. {
  397. if (!_preview) {
  398. _preview = [[KMPDFPosterPrintContentView alloc] init];
  399. [self.PDFContentView addSubview:self.preview];
  400. }
  401. return _preview;
  402. }
  403. -(void)bringSubviewToFront:(NSView*)view
  404. {
  405. [view removeFromSuperview];
  406. [self.PDFContentView addSubview:view];
  407. }
  408. - (void)reloadPDFData
  409. {
  410. NSInteger pageCount = self.pages.count;
  411. self.currentPageLabel.stringValue = [NSString stringWithFormat:@"%@",@(self.currentPageIndex+1)];
  412. self.totalPageLabel.stringValue = [NSString stringWithFormat:@" / %@",@(pageCount)];
  413. }
  414. - (void)viewFileAtFinder:(NSString *)fileName
  415. {
  416. NSWorkspace *workspace = [NSWorkspace sharedWorkspace];
  417. NSURL *url = [NSURL fileURLWithPath:fileName];
  418. [workspace activateFileViewerSelectingURLs:[NSArray arrayWithObject:url]];
  419. }
  420. - (void)getDrawPages
  421. {
  422. NSMutableArray * pages = [NSMutableArray array];
  423. if (0 == self.pageRangeComboBox.indexOfSelectedItem) {
  424. for (NSUInteger i = 0; i<self.PDFDocument.pageCount; i++) {
  425. PDFPage * page = [self.PDFDocument pageAtIndex:i];
  426. [pages addObject:page];
  427. }
  428. } else if (1 == self.pageRangeComboBox.indexOfSelectedItem) {
  429. for (NSUInteger i = 0; i < self.PDFDocument.pageCount; i ++) {
  430. if (i % 2 == 0) {
  431. PDFPage * page = [self.PDFDocument pageAtIndex:i];
  432. [pages addObject:page];
  433. }
  434. }
  435. } else if (2 == self.pageRangeComboBox.indexOfSelectedItem) {
  436. for (NSUInteger i = 0; i < self.PDFDocument.pageCount; i ++) {
  437. if (i % 2 != 0) {
  438. PDFPage * page = [self.PDFDocument pageAtIndex:i];
  439. [pages addObject:page];
  440. }
  441. }
  442. } else {
  443. KMFileAttribute *fileAttribute = [[KMFileAttribute alloc] init];
  444. fileAttribute.filePath = [self.PDFDocument.documentURL path];
  445. fileAttribute.bAllPage = NO;
  446. fileAttribute.pagesString = _pageRangeComboBox.stringValue;
  447. if (![fileAttribute fetchSelectPages]) {
  448. NSAlert *alert = [[NSAlert alloc] init];
  449. [alert setAlertStyle:NSCriticalAlertStyle];
  450. [alert setMessageText:[NSString stringWithFormat:@"%@ %@",[fileAttribute.filePath lastPathComponent],NSLocalizedString(@"Invalid page range or the page number is out of range. Please try again.", nil)]];
  451. [alert runModal];
  452. return;
  453. }
  454. for (NSNumber * num in [fileAttribute fetchSelectPages]) {
  455. PDFPage * page = [self.PDFDocument pageAtIndex:(num.integerValue - 1)];
  456. [pages addObject:page];
  457. }
  458. }
  459. self.pages = pages;
  460. }
  461. #pragma mark - Button Event
  462. - (IBAction)buttonItemClick_Done:(id)sender
  463. {
  464. [self.window makeFirstResponder:self.preview];
  465. if (self.pages.count < 1) {
  466. NSAlert *alert = [[NSAlert alloc] init];
  467. [alert setAlertStyle:NSCriticalAlertStyle];
  468. [alert setMessageText:[NSString stringWithFormat:@"%@ %@",[[self.PDFDocument.documentURL path]lastPathComponent],NSLocalizedString(@"Invalid page range or the page number is out of range. Please try again.", nil)]];
  469. [alert runModal];
  470. return;
  471. }
  472. self.PDFPrint.cropPages = self.pages;
  473. if (self.PDFPrint.isLabel) {
  474. self.PDFPrint.labelString = self.labelTextField.stringValue;
  475. }
  476. NSString * fileName = [NSString stringWithFormat:@"%@_Poster",self.PDFDocument.documentURL.lastPathComponent.stringByDeletingPathExtension];
  477. KMSavePanelAccessoryController *panelAccessoryController = [[KMSavePanelAccessoryController alloc] init];
  478. NSSavePanel *savePanel = [NSSavePanel savePanel];
  479. savePanel.nameFieldStringValue = fileName;
  480. savePanel.allowedFileTypes = @[@"pdf"];
  481. savePanel.accessoryView = panelAccessoryController.view;
  482. [savePanel beginSheetModalForWindow:self.window completionHandler:^(NSInteger result) {
  483. if (result) {
  484. [self showWaitting];
  485. [[KMPosterPrintManager defaultManager] savePath:savePanel.URL.path posterPrint:self.PDFPrint completionHandler:^(BOOL success) {
  486. [self hideWaitting];
  487. if (success) {
  488. if (panelAccessoryController.openAutomaticButton.state == NSControlStateValueOn) {
  489. [self close];
  490. [[NSDocumentController sharedDocumentController] openDocumentWithContentsOfURL:savePanel.URL display:YES completionHandler:^(NSDocument * _Nullable document, BOOL documentWasAlreadyOpen, NSError * _Nullable error) {
  491. }];
  492. } else {
  493. [self viewFileAtFinder:savePanel.URL.path];
  494. }
  495. }
  496. }];
  497. }
  498. }];
  499. }
  500. - (IBAction)buttonItemClick_Cancel:(id)sender
  501. {
  502. [self close];
  503. }
  504. - (IBAction)buttonItemClick_Print:(id)sender
  505. {
  506. [self.window makeFirstResponder:self.preview];
  507. if (self.pages.count < 1) {
  508. NSAlert *alert = [[NSAlert alloc] init];
  509. [alert setAlertStyle:NSCriticalAlertStyle];
  510. [alert setMessageText:[NSString stringWithFormat:@"%@ %@",[[self.PDFDocument.documentURL path]lastPathComponent],NSLocalizedString(@"Invalid page range or the page number is out of range. Please try again.", nil)]];
  511. [alert runModal];
  512. return;
  513. }
  514. self.PDFPrint.cropPages = self.pages;
  515. if (self.PDFPrint.isLabel) {
  516. self.PDFPrint.labelString = self.labelTextField.stringValue;
  517. }
  518. NSString * fileName = nil;
  519. if (![[NSFileManager defaultManager] fileExistsAtPath:kPosterPrintFolderPath]) {
  520. if (![[NSFileManager defaultManager] createDirectoryAtPath:kPosterPrintFolderPath withIntermediateDirectories:NO attributes:nil error:nil]) {
  521. return;
  522. }
  523. }
  524. fileName = @"Poster.pdf";
  525. fileName = [kPosterPrintFolderPath stringByAppendingPathComponent:fileName];
  526. [self showWaitting];
  527. [[KMPosterPrintManager defaultManager] savePath:fileName posterPrint:self.PDFPrint completionHandler:^(BOOL success) {
  528. [self hideWaitting];
  529. if (success) {
  530. PDFDocument * pdf = [[PDFDocument alloc] initWithURL:[NSURL fileURLWithPath:fileName]];
  531. NSPrintInfo *printInfo = [NSPrintInfo sharedPrintInfo];
  532. NSPrintOperation *printOperation = nil;
  533. if ([pdf respondsToSelector:@selector(printOperationForPrintInfo:scalingMode:autoRotate:)]){
  534. printOperation = [pdf printOperationForPrintInfo:printInfo scalingMode:kPDFPrintPageScaleNone autoRotate:YES];
  535. }
  536. [printOperation runOperationModalForWindow:self.window delegate:self didRunSelector:nil contextInfo:NULL];
  537. }
  538. }];
  539. }
  540. - (IBAction)buttonItemClick_CutMarks:(id)sender
  541. {
  542. if ( NSOnState == self.cutMarksButton.state) {
  543. self.PDFPrint.isCut = YES;
  544. } else {
  545. self.PDFPrint.isCut = NO;
  546. }
  547. }
  548. - (IBAction)buttonItemClick_Labels:(id)sender
  549. {
  550. if ( NSOnState == self.labelButton.state) {
  551. self.PDFPrint.isLabel = YES;
  552. [self.labelTextField setEditable:YES];
  553. [self.labelTextField becomeFirstResponder];
  554. } else {
  555. self.PDFPrint.isLabel = NO;
  556. [self.labelTextField setEditable:NO];
  557. self.labelTextField.stringValue = @"";
  558. }
  559. }
  560. - (IBAction)buttonItemClick_Orientation:(id)sender
  561. {
  562. if ( NSOnState == self.landscapeButton.state) {
  563. self.PDFPrint.direction = kKMPDFPosterPrintDirection_Landscape;
  564. } else if(NSOnState == self.portraitButton.state){
  565. self.PDFPrint.direction = kKMPDFPosterPrintDirection_Portrait;
  566. }
  567. [self reloadPDFView];
  568. }
  569. - (IBAction)buttonItemClick_Next:(id)sender
  570. {
  571. if (self.pages.count > self.currentPageIndex + 1) {
  572. self.currentPageIndex ++;
  573. [self reloadPDFData];
  574. [self reloadPDFView];
  575. }
  576. }
  577. - (IBAction)buttonItemClick_Previous:(id)sender
  578. {
  579. if (self.currentPageIndex != 0) {
  580. self.currentPageIndex --;
  581. [self reloadPDFData];
  582. [self reloadPDFView];
  583. }
  584. }
  585. - (IBAction)comboBoxItemClick_PageRange:(id)sender
  586. {
  587. if (0 == [self.pageRangeComboBox indexOfSelectedItem] ||
  588. 1 == [self.pageRangeComboBox indexOfSelectedItem] ||
  589. 2 == [self.pageRangeComboBox indexOfSelectedItem]) {
  590. self.pageRangeComboBox.delegate = nil;
  591. [self.window makeFirstResponder:self];
  592. [self.pageRangeComboBox setEditable:NO];
  593. [self getDrawPages];
  594. if (self.pages.count > 0) {
  595. self.currentPageIndex = 0;
  596. [self reloadPDFView];
  597. [self reloadPDFData];
  598. }
  599. } else if (3 == [self.pageRangeComboBox indexOfSelectedItem]) {
  600. self.pageRangeComboBox.delegate = self;
  601. [self.pageRangeComboBox setStringValue:@""];
  602. [self.pageRangeComboBox setEditable:YES];
  603. [self.window makeFirstResponder:self.pageRangeComboBox];
  604. }
  605. }
  606. - (IBAction)comboBoxItemClick_SplitSize:(id)sender
  607. {
  608. self.overlapTextField.stringValue = @"0";
  609. self.PDFPrint.overlap = 0;
  610. self.pageWidthTextField.enabled = NO;
  611. self.pageHeightTextField.enabled = NO;
  612. if ([_splitSizeComboBox indexOfSelectedItem] == _splitSizeComboBox.numberOfItems - 1) {
  613. self.PDFPrint.splitSize = CGSizeMake(595, 842);
  614. self.pageWidthTextField.enabled = YES;
  615. self.pageHeightTextField.enabled = YES;
  616. } else {
  617. CGSize tSize = [KMPageSizeTool getPaperSizeWithPaperName:self.splitSizeComboBox.stringValue];
  618. self.PDFPrint.splitSize = CGSizeMake(tSize.width * 595.0/210, tSize.height * 842.0/297.0);
  619. }
  620. CGFloat multiple = (MAX(self.PDFPrint.splitSize.height, self.PDFPrint.splitSize.width)/842);
  621. _PDFPrint.edgeInsets = NSEdgeInsetsMake(50 * multiple, 60 * multiple, 50 * multiple, 60 * multiple);
  622. [self reloadPDFView];
  623. }
  624. - (IBAction)comboBoxItemClick_fullPageSize:(id)sender
  625. {
  626. self.PDFPrint.isOriginalPageSize = NO;
  627. self.paperWidthTextField.enabled = NO;
  628. self.paperHeightTextField.enabled = NO;
  629. if ([self.filePageSizeBox indexOfSelectedItem] == 0) {
  630. self.PDFPrint.isOriginalPageSize = YES;
  631. } else if ([self.filePageSizeBox indexOfSelectedItem] == _splitSizeComboBox.numberOfItems - 1){
  632. self.PDFPrint.fullPageSize = CGSizeMake(595, 842);
  633. self.paperWidthTextField.enabled = YES;
  634. self.paperHeightTextField.enabled = YES;
  635. } else {
  636. CGSize tSize = [KMPageSizeTool getPaperSizeWithPaperName:self.filePageSizeBox.stringValue];
  637. self.PDFPrint.fullPageSize = CGSizeMake(tSize.width * 595.0/210, tSize.height * 842.0/297);
  638. }
  639. self.overlapTextField.stringValue = @"0";
  640. self.PDFPrint.overlap = 0;
  641. CGFloat multiple = (MAX(self.PDFPrint.fullPageSize.height, self.PDFPrint.fullPageSize.width)/842);
  642. _PDFPrint.fullPageEdgeInsets = NSEdgeInsetsMake(50 * multiple, 60 * multiple, 50 * multiple, 60 * multiple);
  643. [self reloadPDFView];
  644. }
  645. - (IBAction)buttonItemClick_ReversePage:(id)sender
  646. {
  647. if (NSOnState == self.reverseButton.state) {
  648. self.PDFPrint.isReversePage = YES;
  649. } else {
  650. self.PDFPrint.isReversePage = NO;
  651. }
  652. [self reloadPDFView];
  653. }
  654. - (IBAction)comboBoxItemClick_Unit:(id)sender
  655. {
  656. if ([self.unitComboBox indexOfSelectedItem] == 1) {
  657. self.unitTLabel.stringValue = self.unitT1Label.stringValue = self.overlapUnit.stringValue = NSLocalizedString(@"mm", nil);
  658. } else if([self.unitComboBox indexOfSelectedItem] == 0){
  659. self.unitTLabel.stringValue = self.unitT1Label.stringValue = self.overlapUnit.stringValue = NSLocalizedString(@"cm", nil);
  660. } else {
  661. self.unitTLabel.stringValue = self.unitT1Label.stringValue = self.overlapUnit.stringValue = NSLocalizedString(@"in", nil);
  662. }
  663. self.overlapTextField.stringValue = @"0";
  664. self.PDFPrint.overlap = self.overlapTextField.integerValue;
  665. [self reloadPDFView];
  666. }
  667. - (IBAction)comboBoxItemClick_CommentsForms:(id)sender
  668. {
  669. if (0 == [self.commentsFormsComboBox indexOfSelectedItem]) {
  670. self.PDFPrint.commentsForms = kKMPDFPosterCommentsForms_Documents;
  671. } else if(1 == [self.commentsFormsComboBox indexOfSelectedItem]){
  672. self.PDFPrint.commentsForms = kKMPDFPosterCommentsForms_DocumentMarkups;
  673. } else if(2 == [self.commentsFormsComboBox indexOfSelectedItem]){
  674. self.PDFPrint.commentsForms = kKMPDFPosterCommentsForms_DocumentStamps;
  675. }
  676. [self reloadPDFView];
  677. }
  678. - (IBAction)comboBoxItemClick_PagesPer:(id)sender
  679. {
  680. if ( 5!= [self.selectPerComboBox indexOfSelectedItem]) {
  681. self.columnCountText.editable = NO;
  682. self.lineCountText.editable = NO;
  683. if (0 == [self.selectPerComboBox indexOfSelectedItem]) {
  684. self.PDFPrint.columnCount = 1;
  685. self.PDFPrint.lineCount = 2;
  686. } else if(1 == [self.selectPerComboBox indexOfSelectedItem]){
  687. self.PDFPrint.columnCount = 2;
  688. self.PDFPrint.lineCount = 2;
  689. } else if(2 == [self.selectPerComboBox indexOfSelectedItem]){
  690. self.PDFPrint.columnCount = 2;
  691. self.PDFPrint.lineCount = 3;
  692. } else if(3 == [self.selectPerComboBox indexOfSelectedItem]){
  693. self.PDFPrint.columnCount = 3;
  694. self.PDFPrint.lineCount = 3;
  695. } else if(4 == [self.selectPerComboBox indexOfSelectedItem]){
  696. self.PDFPrint.columnCount = 4;
  697. self.PDFPrint.lineCount = 4;
  698. }
  699. self.columnCountText.stringValue = [NSString stringWithFormat:@"%ld",self.PDFPrint.columnCount];
  700. self.lineCountText.stringValue = [NSString stringWithFormat:@"%ld",self.PDFPrint.lineCount];
  701. self.columnCountText.selectable = NO;
  702. self.lineCountText.selectable = NO;
  703. } else {
  704. self.PDFPrint.columnCount = 2;
  705. self.PDFPrint.lineCount = 2;
  706. self.columnCountText.stringValue = @"2";
  707. self.lineCountText.stringValue = @"2";
  708. self.columnCountText.editable = YES;
  709. self.lineCountText.editable = YES;
  710. self.columnCountText.selectable = YES;
  711. self.lineCountText.selectable = YES;
  712. }
  713. [self reloadPDFView];
  714. }
  715. - (void)tabView:(NSTabView *)tabView willSelectTabViewItem:(nullable NSTabViewItem *)tabViewItem
  716. {
  717. self.overlapTextField.stringValue = @"0";
  718. self.PDFPrint.overlap = 0;
  719. NSString * identifier = (NSString *)tabViewItem.identifier;
  720. if ([identifier isEqualToString:@"1"]) {
  721. _PDFPrint.splitType = kKMPDFPosterSplitType_PageSize;
  722. } else {
  723. _PDFPrint.splitType = kKMPDFPosterSplitType_PageNumber;
  724. }
  725. [self reloadPDFView];
  726. }
  727. #pragma mark - NSTextFieldDelegate Methods
  728. - (void)controlTextDidEndEditing:(NSNotification *)notification
  729. {
  730. NSTextField *textField = (NSTextField *)[notification object];
  731. if (textField == self.currentPageLabel) {
  732. NSInteger index = [[self.currentPageLabel stringValue] integerValue];
  733. NSInteger pageCount = self.pages.count;
  734. if (index > 0 && index <= pageCount) {
  735. self.currentPageIndex = index - 1;
  736. [self reloadPDFView];
  737. } else {
  738. self.currentPageLabel.stringValue = [NSString stringWithFormat:@"%@",@(1)];
  739. }
  740. } else if (textField == self.scaleTextField) {
  741. if (self.scaleTextField.integerValue <= 0) {
  742. self.scaleTextField.stringValue = @"100";
  743. }
  744. self.PDFPrint.scale = self.scaleTextField.integerValue / 100.0;
  745. [self reloadPDFView];
  746. } else if (textField == self.pageRangeComboBox) {
  747. [self getDrawPages];
  748. if (self.pages.count > 0) {
  749. self.currentPageIndex = 0;
  750. [self reloadPDFView];
  751. [self reloadPDFData];
  752. }
  753. } else if (textField == self.columnCountText) {
  754. if ( 5!= [self.selectPerComboBox indexOfSelectedItem]) {
  755. return;
  756. }
  757. if (self.columnCountText.integerValue < 1) {
  758. self.columnCountText.stringValue = @"1";
  759. } else if (self.columnCountText.integerValue > 99){
  760. self.columnCountText.stringValue = @"99";
  761. }
  762. self.PDFPrint.columnCount = self.columnCountText.integerValue;
  763. [self reloadPDFView];
  764. } else if (textField == self.lineCountText) {
  765. if ( 5!= [self.selectPerComboBox indexOfSelectedItem]) {
  766. return;
  767. }
  768. if (self.lineCountText.integerValue < 1) {
  769. self.lineCountText.stringValue = @"1";
  770. } else if (self.lineCountText.integerValue > 99){
  771. self.lineCountText.stringValue = @"99";
  772. }
  773. self.PDFPrint.lineCount = self.lineCountText.integerValue;
  774. [self reloadPDFView];
  775. } else if (textField == self.pageWidthTextField || textField == self.paperWidthTextField) {
  776. self.overlapTextField.stringValue = @"0";
  777. self.PDFPrint.overlap = 0;
  778. CGFloat over = [[textField stringValue] floatValue];
  779. if ([self.unitComboBox indexOfSelectedItem] == 0) {
  780. over *= 595.0/21;
  781. } else if([self.unitComboBox indexOfSelectedItem] == 3){
  782. over *= 595.0/210 * 25.4;
  783. } else {
  784. over *= 595.0/210;
  785. }
  786. CGSize size = CGSizeZero;
  787. if (textField == self.pageWidthTextField ) {
  788. size = self.PDFPrint.splitSize;
  789. size.width = over;
  790. self.PDFPrint.splitSize = size;
  791. } else {
  792. size = self.PDFPrint.fullPageSize;
  793. size.width = over;
  794. self.PDFPrint.fullPageSize = size;
  795. }
  796. [self reloadPDFView];
  797. } else if (textField == self.pageHeightTextField || textField == self.paperHeightTextField) {
  798. self.overlapTextField.stringValue = @"0";
  799. self.PDFPrint.overlap = 0;
  800. CGFloat over = [[textField stringValue] floatValue];
  801. if ([self.unitComboBox indexOfSelectedItem] == 0) {
  802. over *= 842.0/29.7;
  803. } else if([self.unitComboBox indexOfSelectedItem] == 3){
  804. over *= 842.0/297 * 25.4;
  805. } else {
  806. over *= 842.0/297;
  807. }
  808. CGSize size = CGSizeZero;
  809. if (textField == self.pageHeightTextField ) {
  810. size = self.PDFPrint.splitSize;
  811. size.height = over;
  812. self.PDFPrint.splitSize = size;
  813. } else {
  814. size = self.PDFPrint.fullPageSize;
  815. size.height = over;
  816. self.PDFPrint.fullPageSize = size;
  817. }
  818. [self reloadPDFView];
  819. }
  820. }
  821. - (void)controlTextDidChange:(NSNotification *)notification {
  822. NSTextField *textField = (NSTextField *)[notification object];
  823. if (textField == self.currentPageLabel || textField == self.columnCountText || textField == self.lineCountText) {
  824. NSString *string = [textField.formatter stringFromNumber:[NSNumber numberWithFloat:[textField.stringValue integerValue]]];
  825. textField.stringValue = string;
  826. } else if (textField == self.overlapTextField){
  827. CGFloat maxOverlap = MIN(self.PDFPrint.splitSize.width, self.PDFPrint.splitSize.height);
  828. if (self.PDFPrint.splitType == kKMPDFPosterSplitType_PageNumber) {
  829. maxOverlap = MIN(self.PDFPrint.fullPageSize.width, self.PDFPrint.fullPageSize.height);
  830. }
  831. CGFloat over = [[textField stringValue] floatValue];
  832. if ([self.unitComboBox indexOfSelectedItem] == 0) {
  833. over *= 28.4;
  834. } else if([self.unitComboBox indexOfSelectedItem] == 3){
  835. over *= 72.0;
  836. } else {
  837. over *= 2.84; //595/210
  838. }
  839. if (over <= 0 || over > maxOverlap/2) {
  840. self.overlapTextField.stringValue = @"0";
  841. self.PDFPrint.overlap = 0;
  842. } else {
  843. self.PDFPrint.overlap = over;
  844. }
  845. [self reloadPDFView];
  846. }
  847. }
  848. #pragma mark - show / hide Waiting
  849. - (void)showWaitting
  850. {
  851. if (!_posterMaskView) {
  852. _posterMaskView = [[KMBookletMaskView alloc] initWithFrame:NSMakeRect(0, 0, self.window.frame.size.width, self.window.frame.size.height)];
  853. }
  854. [self.window.contentView addSubview:self.posterMaskView];
  855. }
  856. - (void)hideWaitting
  857. {
  858. [self.posterMaskView removeFromSuperview];
  859. }
  860. @end