KMPDFMultiplePrintWindowController.m 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111
  1. //
  2. // KMPDFMorePagePrintWindowController.m
  3. // PDF Reader Pro Edition
  4. //
  5. // Created by 丁林圭 on 2018/5/17.
  6. //
  7. #import "PDF_Reader_Pro-Swift.h"
  8. #import "KMPDFMultiplePrintWindowController.h"
  9. #import "KMPDFMultipleManager.h"
  10. //#import "KMBookletMaskView.h"
  11. //#import "KMSavePanelAccessoryController.h"
  12. //#import "NSWindowController_SKExtensions.h"
  13. //#import "KMPageSizeTool.h"
  14. #define KMPDFMultiplePrint_PrenViewWidth 315.0
  15. #define KMPDFMultiplePrint_PrenViewHeight 446.0
  16. #define KMPDFMultiplePrint_MaxEdge 80.0
  17. #define KMPDFMultiplePrint_MaxSpace 50.0
  18. #define kPrintFolderPath [[NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:[NSBundle mainBundle].bundleIdentifier]
  19. #define kKMPDFMultiplePrint_FolderPath [[[NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:[NSBundle mainBundle].bundleIdentifier] stringByAppendingPathComponent:@"Multiple"]
  20. #define kMultipleFileSavePath [[[NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:[NSBundle mainBundle].bundleIdentifier] stringByAppendingPathComponent:@"Multiple.pdf"]
  21. #pragma mark KMMultiplePDFPage
  22. @interface KMMultiplePDFPage : PDFPage
  23. @property (nonatomic,retain) KMEachPageProperties *properties;
  24. @property (nonatomic,retain) NSArray * pages;
  25. @end
  26. @implementation KMMultiplePDFPage
  27. - (void)dealloc
  28. {
  29. }
  30. - (void)drawWithBox:(PDFDisplayBox)box
  31. {
  32. [super drawWithBox:box];
  33. CGContextRef context = (CGContextRef)[[NSGraphicsContext currentContext] graphicsPort];
  34. if (context) {
  35. [NSGraphicsContext setCurrentContext:[NSGraphicsContext graphicsContextWithGraphicsPort:context flipped:NO]];
  36. }
  37. [[KMPDFMultipleManager defaultManager] drawPageToContext:context eachPages:self.pages multipleOption:self.properties];
  38. }
  39. - (void)drawWithBox:(PDFDisplayBox)box toContext:(CGContextRef)context
  40. {
  41. [super drawWithBox:box toContext:context];
  42. if (context) {
  43. [NSGraphicsContext setCurrentContext:[NSGraphicsContext graphicsContextWithGraphicsPort:context flipped:NO]];
  44. }
  45. [[KMPDFMultipleManager defaultManager] drawPageToContext:context eachPages:self.pages multipleOption:self.properties];
  46. }
  47. @end
  48. #pragma mark - KMMultiplePDFView
  49. @interface KMMultiplePDFView : PDFView
  50. @end
  51. @implementation KMMultiplePDFView
  52. @end
  53. #pragma mark - KMPDFMultiplePrintWindowController
  54. @interface KMPDFMultiplePrintWindowController () <NSComboBoxDelegate>
  55. @property (nonatomic,retain) PDFDocument *PDFDocument;
  56. @property (nonatomic,retain) NSString *password;
  57. @property (nonatomic,retain) PDFDocument *previewDocument;
  58. @property (nonatomic,assign) IBOutlet KMMultiplePDFView *PDFView;
  59. @property (assign) IBOutlet NSTextField *positionTextFiled;
  60. @property (assign) IBOutlet NSTextField *positionLabel;
  61. @property (assign,nonatomic) IBOutlet NSTextField *unitLabel;
  62. @property (nonatomic,assign) IBOutlet NSComboBox *unitComboBox;
  63. @property (nonatomic,assign) IBOutlet NSTextField *splitSizeLabel;
  64. @property (nonatomic,assign) IBOutlet NSComboBox *splitSizeComboBox;
  65. @property (nonatomic,assign) IBOutlet NSTextField *pagesPerLabel;
  66. @property (nonatomic,assign) IBOutlet NSComboBox *selectPerComboBox;
  67. @property (nonatomic,assign) IBOutlet NSTextField *byLabel;
  68. @property (assign,nonatomic) IBOutlet NSTextField *lineCountText;
  69. @property (assign,nonatomic) IBOutlet NSTextField *columnCountText;
  70. @property (nonatomic,assign) IBOutlet NSTextField *pagesOrderLabel;
  71. @property (nonatomic,assign) IBOutlet NSComboBox *pageOrderComboBox;
  72. @property (nonatomic,assign) IBOutlet NSTextField *commentsFormsLabel;
  73. @property (nonatomic,assign) IBOutlet NSComboBox *commentsFormsComboBox;
  74. @property (assign,nonatomic) IBOutlet NSButton *borderCheckButton;
  75. @property (assign,nonatomic) IBOutlet NSButton *autoRotaCheckButton;
  76. @property (assign,nonatomic) IBOutlet NSButton *reverseButton;
  77. @property (assign,nonatomic) IBOutlet NSTextField *pageEdgeLabel;
  78. @property (assign,nonatomic) IBOutlet NSTextField *topLabel;
  79. @property (assign,nonatomic) IBOutlet NSTextField *bottomLabel;
  80. @property (assign,nonatomic) IBOutlet NSTextField *leftLabel;
  81. @property (assign,nonatomic) IBOutlet NSTextField *rightLabel;
  82. @property (assign,nonatomic) IBOutlet NSTextField *topDistanceText;
  83. @property (assign,nonatomic) IBOutlet NSTextField *bottomDistanceText;
  84. @property (assign,nonatomic) IBOutlet NSTextField *leftDistanceText;
  85. @property (assign,nonatomic) IBOutlet NSTextField *rightDistanceText;
  86. @property (assign,nonatomic) IBOutlet NSStepper *topStepper;
  87. @property (assign,nonatomic) IBOutlet NSStepper *bottomStepper;
  88. @property (assign,nonatomic) IBOutlet NSStepper *leftStepper;
  89. @property (assign,nonatomic) IBOutlet NSStepper *rightStepper;
  90. @property (assign,nonatomic) IBOutlet NSTextField *pageSpaceLabel;
  91. @property (assign,nonatomic) IBOutlet NSTextField *lineLabel;
  92. @property (assign,nonatomic) IBOutlet NSTextField *columnLabel;
  93. @property (assign,nonatomic) IBOutlet NSTextField *lineSpaceText;
  94. @property (assign,nonatomic) IBOutlet NSTextField *columnSpaceText;
  95. @property (assign,nonatomic) IBOutlet NSStepper *lineStepper;
  96. @property (assign,nonatomic) IBOutlet NSStepper *columnStepper;
  97. @property (assign,nonatomic) IBOutlet NSTextField *orientationLabel;
  98. @property (assign,nonatomic) IBOutlet NSButton *portraitButton;
  99. @property (assign,nonatomic) IBOutlet NSButton *landscapeButton;
  100. @property (nonatomic,assign) IBOutlet NSTextField *pageRangeLabel;
  101. @property (nonatomic,assign) IBOutlet NSComboBox *pageRangeComboBox;
  102. @property (assign) IBOutlet NSButton *cancelButton;
  103. @property (assign) IBOutlet NSButton *saveButton;
  104. @property (assign) IBOutlet NSButton *printButton;
  105. @property (assign) IBOutlet NSLayoutConstraint *PDFHeight;
  106. @property (assign) IBOutlet NSLayoutConstraint *PDFWidth;
  107. @property (assign,nonatomic) IBOutlet NSTextField *spaceUnitLabel;
  108. @property (assign,nonatomic) IBOutlet NSTextField *edgeUnitLabel;
  109. @property (assign,nonatomic) IBOutlet NSTextField *allPageSizeLabel;
  110. @property (assign) IBOutlet NSTextField *pageWidthTextField;
  111. @property (assign) IBOutlet NSTextField *pageHeightTextField;
  112. @property (assign) IBOutlet NSTextField *unitTLabel;
  113. @property (nonatomic,retain) KMEachPageProperties *properties;
  114. @property (nonatomic,retain) NSArray *pages;
  115. @property (nonatomic, retain) KMBookletMaskView *multipleMaskView;
  116. @end
  117. @implementation KMPDFMultiplePrintWindowController
  118. - (void)dealloc
  119. {
  120. NSFileManager *manager = [NSFileManager defaultManager];
  121. if ([manager fileExistsAtPath:kMultipleFileSavePath]) {
  122. [manager removeItemAtPath:kMultipleFileSavePath error:nil];
  123. }
  124. }
  125. - (id)initWithPDFDocument:(PDFDocument *)pdfDocument
  126. {
  127. if (self = [super initWithWindowNibName:@"KMPDFMultiplePrintWindowController"]) {
  128. [self showWaitting];
  129. dispatch_async(dispatch_get_global_queue(0, 0), ^{
  130. if (![[NSFileManager defaultManager] fileExistsAtPath:kPrintFolderPath]) {
  131. if (![[NSFileManager defaultManager] createDirectoryAtPath:kPrintFolderPath withIntermediateDirectories:NO attributes:nil error:nil]) {
  132. return;
  133. }
  134. }
  135. [pdfDocument writeToURL:[NSURL fileURLWithPath:kMultipleFileSavePath]];
  136. self.PDFDocument = [[PDFDocument alloc] initWithURL:[NSURL fileURLWithPath:kMultipleFileSavePath]];
  137. dispatch_async(dispatch_get_main_queue(), ^{
  138. [self hideWaitting];
  139. if (self.PDFDocument) {
  140. [self changePrenView];
  141. [self reloadPDFView];
  142. [self reloadPDFWithIndex];
  143. } else {
  144. [self close];
  145. }
  146. });
  147. });
  148. }
  149. return self;
  150. }
  151. - (id)initWithPDFFilePath:(NSString *)filePath password:(nullable NSString *)password{
  152. if (self = [super initWithWindowNibName:@"KMPDFMultiplePrintWindowController"]) {
  153. PDFDocument * pdf = [[PDFDocument alloc] initWithURL:[NSURL fileURLWithPath:filePath]];
  154. self.PDFDocument = pdf;
  155. self.password = password;
  156. }
  157. return self;
  158. }
  159. - (BOOL)windowShouldClose:(id)sender
  160. {
  161. [self close];
  162. return YES;
  163. }
  164. - (void)close
  165. {
  166. if (self.window.isSheet) {
  167. // [self dismissSheet:nil];
  168. [self.window.sheetParent endSheet:self.window];
  169. } else {
  170. [super close];
  171. }
  172. }
  173. #pragma mark - NSWindowController
  174. - (void)windowDidLoad
  175. {
  176. [super windowDidLoad];
  177. self.PDFView.wantsLayer = YES;
  178. self.PDFView.layer.borderColor = [NSColor blackColor].CGColor;
  179. self.PDFView.layer.borderWidth = 2;
  180. self.PDFView.backgroundColor = [NSColor whiteColor];
  181. self.PDFView.autoScales = YES;
  182. [self localizedString];
  183. if (!self.PDFDocument) {
  184. return;
  185. }
  186. if (![self.PDFDocument isLocked] && [self.PDFDocument unlockWithPassword:self.password]) {
  187. [self changePrenView];
  188. [self reloadPDFView];
  189. [self reloadPDFWithIndex];
  190. } else {
  191. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  192. // PasswordWindowController *com = [[PasswordWindowController alloc] initWithWindowNibName:@"PasswordWindowController"];
  193. // com.fileURL = self.PDFDocument.documentURL;
  194. // [com beginSheetModalForWindow:self.window completionHandler:^(NSString *password) {
  195. // if (password) {
  196. // [self.PDFDocument unlockWithPassword:password];
  197. // self.password = password;
  198. // [self changePrenView];
  199. // [self reloadPDFView];
  200. // [self reloadPDFWithIndex];
  201. // } else {
  202. // [self close];
  203. // }
  204. // }];
  205. // [com release];
  206. });
  207. }
  208. }
  209. #pragma mark - Private Methods
  210. - (void)localizedString
  211. {
  212. self.window.title = NSLocalizedString(@"Multiple", nil);
  213. self.pageRangeLabel.stringValue = NSLocalizedString(@"Page Range", nil);
  214. self.orientationLabel.stringValue = NSLocalizedString(@"Orientation:", nil);
  215. self.unitLabel.stringValue = NSLocalizedString(@"Unit:", nil);
  216. self.splitSizeLabel.stringValue = NSLocalizedString(@"Page size:", nil);
  217. self.pagesPerLabel.stringValue = NSLocalizedString(@"Pages per sheet:", nil);
  218. self.pagesOrderLabel.stringValue = NSLocalizedString(@"Page order:", nil);
  219. self.pageEdgeLabel.stringValue = NSLocalizedString(@"Margin", nil);
  220. self.topLabel.stringValue = NSLocalizedString(@"Top", nil);
  221. self.bottomLabel.stringValue = NSLocalizedString(@"Bottom", nil);
  222. self.leftLabel.stringValue = NSLocalizedString(@"Left", nil);
  223. self.rightLabel.stringValue = NSLocalizedString(@"Right", nil);
  224. self.pageSpaceLabel.stringValue = NSLocalizedString(@"Spacing", nil);
  225. self.lineLabel.stringValue = NSLocalizedString(@"Line spacing", nil);
  226. self.columnLabel.stringValue = NSLocalizedString(@"Column spacing", nil);
  227. self.byLabel.stringValue = @"X";
  228. self.commentsFormsLabel.stringValue = NSLocalizedString(@"Comments & Forms:",nil);
  229. self.edgeUnitLabel.stringValue = self.spaceUnitLabel.stringValue = [NSString stringWithFormat:@"(%@)",NSLocalizedString(@"mm", nil)];
  230. self.unitTLabel.stringValue = NSLocalizedString(@"mm", nil);
  231. [self.pageRangeComboBox addItemsWithObjectValues:@[NSLocalizedString(@"All Pages", nil),
  232. NSLocalizedString(@"Odd Pages Only", nil),
  233. NSLocalizedString(@"Even Pages Only",nil),
  234. NSLocalizedString(@"e.g. 1,3-5,10",nil)]];
  235. [self.pageRangeComboBox.cell setPlaceholderString:NSLocalizedString(@"e.g. 1,3-5,10", nil)];
  236. [self.pageRangeComboBox selectItemAtIndex:0];
  237. [self.pageRangeComboBox setEditable:NO];
  238. [self.unitComboBox selectItemAtIndex:1];
  239. NSArray *paperArray = [KMPageSizeTool paperSize];
  240. NSMutableArray *tPaperArray = [NSMutableArray arrayWithArray:paperArray];
  241. [tPaperArray addObject:NSLocalizedString(@"Custom",nil)];
  242. [self.splitSizeComboBox addItemsWithObjectValues:tPaperArray];
  243. [self.splitSizeComboBox selectItemAtIndex:1];
  244. [self.splitSizeComboBox setEditable:NO];
  245. self.pageWidthTextField.enabled = NO;
  246. self.pageHeightTextField.enabled = NO;
  247. CGFloat unitScanel = [self unitConversion];
  248. self.topDistanceText.stringValue =
  249. self.bottomDistanceText.stringValue =
  250. self.leftDistanceText.stringValue =
  251. self.rightDistanceText.stringValue =
  252. self.bottomStepper.stringValue =
  253. self.topStepper.stringValue =
  254. self.leftStepper.stringValue =
  255. self.rightStepper.stringValue =
  256. [NSString stringWithFormat:@"%@",[self formatFloat:self.properties.edgeInsets.top/unitScanel]];
  257. self.lineStepper.stringValue =
  258. self.columnStepper.stringValue =
  259. self.lineSpaceText.stringValue =
  260. self.columnSpaceText.stringValue =
  261. [NSString stringWithFormat:@"%@",[self formatFloat:self.properties.rowSpace/unitScanel]];
  262. self.lineStepper.maxValue =
  263. self.columnStepper.maxValue = KMPDFMultiplePrint_MaxSpace/unitScanel;
  264. self.bottomStepper.maxValue =
  265. self.topStepper.maxValue =
  266. self.leftStepper.maxValue =
  267. self.rightStepper.maxValue = KMPDFMultiplePrint_MaxSpace/unitScanel;
  268. self.lineStepper.minValue =
  269. self.columnStepper.minValue = 0;
  270. self.bottomStepper.minValue =
  271. self.topStepper.minValue =
  272. self.leftStepper.minValue =
  273. self.rightStepper.minValue = 0;
  274. [self.selectPerComboBox addItemsWithObjectValues:@[@"2",
  275. @"4",
  276. @"6",
  277. @"9",
  278. @"16",
  279. NSLocalizedString(@"Custom", nil)]];
  280. [self.selectPerComboBox selectItemAtIndex:5];
  281. [self.selectPerComboBox setEditable:NO];
  282. [self.pageOrderComboBox addItemsWithObjectValues:@[NSLocalizedString(@"Horizontal", nil),
  283. NSLocalizedString(@"Horizontal Reversed", nil),
  284. NSLocalizedString(@"Vertical",nil),
  285. NSLocalizedString(@"Vertical Reversed",nil)]];
  286. [self.pageOrderComboBox selectItemAtIndex:0];
  287. [self.pageOrderComboBox setEditable:NO];
  288. [self.commentsFormsComboBox addItemsWithObjectValues:@[NSLocalizedString(@"Document", nil),
  289. NSLocalizedString(@"Document and Markups", nil),
  290. NSLocalizedString(@"Document and Stamps",nil)
  291. ]];
  292. [self.commentsFormsComboBox selectItemAtIndex:1];
  293. [self.commentsFormsComboBox setEditable:NO];
  294. self.columnCountText.stringValue = @"2";
  295. self.lineCountText.stringValue = @"2";
  296. [self.cancelButton setTitle:NSLocalizedString(@"Cancel", nil)];
  297. [self.saveButton setTitle:NSLocalizedString(@"Save as PDF", nil)];
  298. [self.printButton setTitle:NSLocalizedString(@"Print", nil)];
  299. [self.portraitButton setTitle:NSLocalizedString(@"Portrait",nil)];
  300. [self.landscapeButton setTitle:NSLocalizedString(@"Landscape",nil)];
  301. [self.borderCheckButton setTitle:NSLocalizedString(@"Print page border",nil)];
  302. [self.autoRotaCheckButton setTitle:NSLocalizedString(@"Auto-rotate pages within each sheet",nil)];
  303. [self.reverseButton setTitle:NSLocalizedString(@"Reverse pages",nil)];
  304. }
  305. - (KMEachPageProperties *)properties
  306. {
  307. if (!_properties) {
  308. _properties = [[KMEachPageProperties alloc] init];
  309. _properties.rowCount = 2;
  310. _properties.columnCout = 2;
  311. _properties.pageSize = CGSizeMake(595, 842);
  312. _properties.isAutoRotate = NO;
  313. _properties.edgeInsets = NSEdgeInsetsMake(20, 20, 20, 20);
  314. _properties.rowSpace = 10;
  315. _properties.columnSpace = 10;
  316. _properties.isHasBorder = NO;
  317. _properties.order = kKMPDFMultiplePageOrder_Transverse;
  318. _properties.commentsForms = kKMPDFCommentsForms_DocumentMarkups;
  319. }
  320. return _properties;
  321. }
  322. - (void)reloadPDFView
  323. {
  324. CGFloat width =self.properties.pageSize.width;
  325. CGFloat height =self.properties.pageSize.height;
  326. self.pageWidthTextField.stringValue = [KMPageSizeTool conversionWithUnit:self.unitComboBox.stringValue value:(width/595 * 210)]?:@"";
  327. self.pageHeightTextField.stringValue = [KMPageSizeTool conversionWithUnit:self.unitComboBox.stringValue value:(height/842 * 297)]?:@"";
  328. if (kKMPDFMultiplePageDirection_Landscape == self.properties.direction) {
  329. width =self.properties.pageSize.height;
  330. height =self.properties.pageSize.width;
  331. }
  332. CGFloat previewScanle = MIN(KMPDFMultiplePrint_PrenViewWidth/width, KMPDFMultiplePrint_PrenViewHeight/height);
  333. self.PDFWidth.constant = width * previewScanle;
  334. self.PDFHeight.constant = height * previewScanle;
  335. CGFloat unitScanel = [self unitConversion];
  336. if ([self.unitComboBox indexOfSelectedItem] == 0) {
  337. self.allPageSizeLabel.stringValue = [NSString stringWithFormat:@"%@ x %@ %@",[self formatFloat:width/unitScanel],[self formatFloat:height/unitScanel],NSLocalizedString(@"cm", nil)];
  338. } else if ([self.unitComboBox indexOfSelectedItem] == 1) {
  339. self.allPageSizeLabel.stringValue = [NSString stringWithFormat:@"%@ x %@ %@",[self formatFloat:width/unitScanel],[self formatFloat:height/unitScanel],NSLocalizedString(@"mm", nil)];
  340. } else {
  341. self.allPageSizeLabel.stringValue = [NSString stringWithFormat:@"%@ x %@ %@",[self formatFloat:width/unitScanel],[self formatFloat:height/unitScanel],NSLocalizedString(@"in", nil)];
  342. }
  343. [self.PDFView setNeedsDisplay:YES];
  344. }
  345. - (void)changePrenView
  346. {
  347. [self getDrawPages];
  348. NSInteger pagesCount = self.properties.rowCount * self.properties.columnCout;
  349. NSInteger allPagesCount = 0;
  350. if (self.pages.count % pagesCount == 0) {
  351. allPagesCount = self.pages.count / pagesCount;
  352. } else {
  353. allPagesCount = self.pages.count / pagesCount +1;
  354. }
  355. if (!_previewDocument) {
  356. _previewDocument = [[PDFDocument alloc] init];
  357. } else {
  358. _previewDocument = [[PDFDocument alloc] init];
  359. }
  360. for (NSUInteger i= 0; i < allPagesCount; i++) {
  361. NSMutableArray *pages = [NSMutableArray array];
  362. CGFloat width =self.properties.pageSize.width;
  363. CGFloat height =self.properties.pageSize.height;
  364. if (kKMPDFMultiplePageDirection_Landscape == _properties.direction) {
  365. width =self.properties.pageSize.height;
  366. height =self.properties.pageSize.width;
  367. }
  368. for (NSUInteger j = 0; j < pagesCount; j++) {
  369. NSUInteger index = j + i *pagesCount;
  370. if (index >= self.pages.count) {
  371. break;
  372. }
  373. [pages addObject:self.pages[index]];
  374. }
  375. if (pages.count < 1) {
  376. break;
  377. }
  378. KMMultiplePDFPage * page = [[KMMultiplePDFPage alloc] init];
  379. page.properties = self.properties;
  380. page.pages = pages;
  381. [page setBounds:CGRectMake(0, 0, width, height) forBox:kPDFDisplayBoxMediaBox];
  382. if (self.properties.isReversePage) {
  383. [_previewDocument insertPage:page atIndex:0];
  384. } else {
  385. [_previewDocument insertPage:page atIndex:_previewDocument.pageCount];
  386. }
  387. }
  388. self.PDFView.document = _previewDocument;
  389. [self.PDFView setNeedsDisplay:YES];
  390. [self.PDFView goToPage:[_previewDocument pageAtIndex:0]];
  391. [self reloadPDFWithIndex];
  392. }
  393. - (void)reloadPDFWithIndex
  394. {
  395. [self.PDFView.documentView.enclosingScrollView setHasVerticalScroller:NO];
  396. [self.PDFView.documentView.enclosingScrollView setHasHorizontalScroller:NO];
  397. NSInteger pageCount = self.previewDocument.pageCount;
  398. NSInteger currentPageIndex = [self.previewDocument indexForPage:self.PDFView.currentPage];
  399. self.positionTextFiled.stringValue = [NSString stringWithFormat:@"%@",@(currentPageIndex+1)];
  400. self.positionLabel.stringValue = [NSString stringWithFormat:NSLocalizedString(@" / %@", nil),@(pageCount)];
  401. }
  402. - (void)viewFileAtFinder:(NSString *)fileName
  403. {
  404. NSWorkspace *workspace = [NSWorkspace sharedWorkspace];
  405. NSURL *url = [NSURL fileURLWithPath:fileName];
  406. [workspace activateFileViewerSelectingURLs:[NSArray arrayWithObject:url]];
  407. }
  408. - (void)getDrawPages
  409. {
  410. NSMutableArray * pages = [NSMutableArray array];
  411. if (0 == self.pageRangeComboBox.indexOfSelectedItem) {
  412. for (NSUInteger i = 0; i<self.PDFDocument.pageCount; i++) {
  413. PDFPage * page = [[self.PDFDocument pageAtIndex:i] copy];
  414. [pages addObject:page];
  415. }
  416. } else if (1 == self.pageRangeComboBox.indexOfSelectedItem) {
  417. for (NSUInteger i = 0; i < self.PDFDocument.pageCount; i ++) {
  418. if (i % 2 == 0) {
  419. PDFPage * page = [[self.PDFDocument pageAtIndex:i] copy];
  420. [pages addObject:page];
  421. }
  422. }
  423. } else if (2 == self.pageRangeComboBox.indexOfSelectedItem) {
  424. for (NSUInteger i = 0; i < self.PDFDocument.pageCount; i ++) {
  425. if (i % 2 != 0) {
  426. PDFPage * page = [[self.PDFDocument pageAtIndex:i] copy];
  427. [pages addObject:page];
  428. }
  429. }
  430. } else {
  431. KMFileAttribute *fileAttribute = [[KMFileAttribute alloc] init];
  432. fileAttribute.filePath = [self.PDFDocument.documentURL path];
  433. fileAttribute.bAllPage = NO;
  434. fileAttribute.pagesString = _pageRangeComboBox.stringValue;
  435. if (![fileAttribute fetchSelectPages]) {
  436. NSAlert *alert = [[NSAlert alloc] init];
  437. [alert setAlertStyle:NSCriticalAlertStyle];
  438. [alert setMessageText:[NSString stringWithFormat:@"%@ %@",[fileAttribute.filePath lastPathComponent],NSLocalizedString(@"Invalid page range or the page number is out of range. Please try again.", nil)]];
  439. [alert runModal];
  440. return;
  441. }
  442. for (NSNumber * num in [fileAttribute fetchSelectPages]) {
  443. PDFPage * page = [[self.PDFDocument pageAtIndex:(num.integerValue - 1)] copy];
  444. [pages addObject:page];
  445. }
  446. }
  447. if (pages.count < 1 ) {
  448. NSAlert *alert = [[NSAlert alloc] init];
  449. [alert setAlertStyle:NSCriticalAlertStyle];
  450. [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)]];
  451. [alert runModal];
  452. } else {
  453. self.pages = pages;
  454. }
  455. }
  456. - (NSString *)formatFloat:(float)f
  457. {
  458. if (fmodf(f, 1)==0) {//如果有一位小数点
  459. return [NSString stringWithFormat:@"%.0f",f];
  460. } else if (fmodf(f*10, 1)==0) {//如果有两位小数点
  461. return [NSString stringWithFormat:@"%.1f",f];
  462. } else {
  463. return [NSString stringWithFormat:@"%.2f",f];
  464. }
  465. }
  466. - (CGFloat)unitConversion
  467. {
  468. CGFloat unitScanl = 1.0;
  469. if (0 == [self.unitComboBox indexOfSelectedItem]) {
  470. unitScanl = 595.0/21.0;
  471. } else if (1 == [self.unitComboBox indexOfSelectedItem])
  472. {
  473. unitScanl = 595.0/210.0;
  474. } else {
  475. unitScanl = (595.0/21.0) * 2.54;
  476. }
  477. return unitScanl;
  478. }
  479. #pragma mark - Button Event Action
  480. - (IBAction)buttonItemClicked_Previous:(id)sender
  481. {
  482. [self.PDFView goToPreviousPage:nil];
  483. [self reloadPDFWithIndex];
  484. }
  485. - (IBAction)buttonItemClicked_Next:(id)sender
  486. {
  487. [self.PDFView goToNextPage:nil];
  488. [self reloadPDFWithIndex];
  489. }
  490. - (IBAction)buttonItemClick_Cancel:(id)sender
  491. {
  492. [self close];
  493. }
  494. - (IBAction)buttonItemClick_Save:(id)sender
  495. {
  496. [self.window makeFirstResponder:self.PDFView];
  497. if (self.pages.count < 1) {
  498. NSAlert *alert = [[NSAlert alloc] init];
  499. [alert setAlertStyle:NSCriticalAlertStyle];
  500. [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)]];
  501. [alert runModal];
  502. return;
  503. }
  504. NSString * fileName = [NSString stringWithFormat:@"%@_Multipe",self.PDFDocument.documentURL.lastPathComponent.stringByDeletingPathExtension];
  505. KMSavePanelAccessoryController *panelAccessoryController = [[KMSavePanelAccessoryController alloc] init];
  506. NSSavePanel *savePanel = [NSSavePanel savePanel];
  507. savePanel.nameFieldStringValue = fileName;
  508. savePanel.allowedFileTypes = @[@"pdf"];
  509. savePanel.accessoryView = panelAccessoryController.view;
  510. [savePanel beginSheetModalForWindow:self.window completionHandler:^(NSInteger result) {
  511. if (result) {
  512. [self showWaitting];
  513. [[KMPDFMultipleManager defaultManager] savePDFDocumentPath:savePanel.URL.path drawPages:self.pages multipleOption:self.properties completionHandler:^(BOOL isSuccessfully) {
  514. [self hideWaitting];
  515. if (panelAccessoryController.openAutomaticButton.state == NSControlStateValueOn) {
  516. [self close];
  517. [[NSDocumentController sharedDocumentController] openDocumentWithContentsOfURL:savePanel.URL display:YES completionHandler:^(NSDocument * _Nullable document, BOOL documentWasAlreadyOpen, NSError * _Nullable error) {
  518. }];
  519. } else {
  520. [self viewFileAtFinder:savePanel.URL.path];
  521. }
  522. }];
  523. }
  524. }];
  525. }
  526. - (IBAction)buttonItemClick_Print:(id)sender
  527. {
  528. [self.window makeFirstResponder:self.PDFView];
  529. if (self.pages.count < 1) {
  530. NSAlert *alert = [[NSAlert alloc] init];
  531. [alert setAlertStyle:NSCriticalAlertStyle];
  532. [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)]];
  533. [alert runModal];
  534. return;
  535. }
  536. NSString * fileName = nil;
  537. if (![[NSFileManager defaultManager] fileExistsAtPath:kKMPDFMultiplePrint_FolderPath]) {
  538. if (![[NSFileManager defaultManager] createDirectoryAtPath:kKMPDFMultiplePrint_FolderPath withIntermediateDirectories:NO attributes:nil error:nil]) {
  539. return;
  540. }
  541. }
  542. fileName = @"Multiple.pdf";
  543. fileName = [kKMPDFMultiplePrint_FolderPath stringByAppendingPathComponent:fileName];
  544. [self showWaitting];
  545. [[KMPDFMultipleManager defaultManager] savePDFDocumentPath:fileName drawPages:self.pages multipleOption:self.properties completionHandler:^(BOOL isSuccessfully) {
  546. [self hideWaitting];
  547. if (isSuccessfully) {
  548. PDFDocument * pdf = [[PDFDocument alloc] initWithURL:[NSURL fileURLWithPath:fileName]];
  549. NSPrintInfo *printInfo = [NSPrintInfo sharedPrintInfo];
  550. NSPrintOperation *printOperation = nil;
  551. if ([pdf respondsToSelector:@selector(printOperationForPrintInfo:scalingMode:autoRotate:)]){
  552. printOperation = [pdf printOperationForPrintInfo:printInfo scalingMode:kPDFPrintPageScaleNone autoRotate:YES];
  553. }
  554. [printOperation runOperationModalForWindow:self.window delegate:self didRunSelector:nil contextInfo:NULL];
  555. }
  556. }];
  557. }
  558. - (IBAction)buttonItemClick_PageBorder:(id)sender
  559. {
  560. if (NSOnState == self.borderCheckButton.state) {
  561. self.properties.isHasBorder = YES;
  562. } else {
  563. self.properties.isHasBorder = NO;
  564. }
  565. [self reloadPDFView];
  566. }
  567. - (IBAction)buttonItemClick_AutoRoate:(id)sender
  568. {
  569. if (NSOnState == self.autoRotaCheckButton.state) {
  570. self.properties.isAutoRotate = YES;
  571. } else {
  572. self.properties.isAutoRotate = NO;
  573. }
  574. [self changePrenView];
  575. }
  576. - (IBAction)buttonItemClick_ReversePage:(id)sender
  577. {
  578. if (NSOnState == self.reverseButton.state) {
  579. self.properties.isReversePage = YES;
  580. } else {
  581. self.properties.isReversePage = NO;
  582. }
  583. [self changePrenView];
  584. }
  585. - (IBAction)buttonItemClick_Orientation:(id)sender
  586. {
  587. if ( NSOnState == self.landscapeButton.state) {
  588. self.properties.direction = kKMPDFMultiplePageDirection_Landscape;
  589. } else if(NSOnState == self.portraitButton.state){
  590. self.properties.direction = kKMPDFMultiplePageDirection_Portrait;
  591. }
  592. [self changePrenView];
  593. [self reloadPDFView];
  594. }
  595. - (IBAction)stepperItemClick_Distance:(id)sender
  596. {
  597. CGFloat unitScanl = [self unitConversion];
  598. if ([sender isEqual:_topStepper]) {
  599. self.topDistanceText.stringValue = [NSString stringWithFormat:@"%.2f",self.topStepper.floatValue];
  600. NSString * formatter = [self formatFloat:self.topStepper.floatValue * unitScanl];
  601. self.properties.edgeInsets = NSEdgeInsetsMake(formatter.floatValue, _properties.edgeInsets.left, _properties.edgeInsets.bottom, _properties.edgeInsets.right);
  602. } else if ([sender isEqual:_bottomStepper]){
  603. self.bottomDistanceText.stringValue = [NSString stringWithFormat:@"%.2f",self.bottomStepper.floatValue];
  604. NSString * formatter = [self formatFloat:_bottomStepper.floatValue * unitScanl];
  605. self.properties.edgeInsets = NSEdgeInsetsMake(_properties.edgeInsets.top, _properties.edgeInsets.left, formatter.floatValue, _properties.edgeInsets.right);
  606. } else if ([sender isEqual:_leftStepper]) {
  607. self.leftDistanceText.stringValue = [NSString stringWithFormat:@"%.2f",self.leftStepper.floatValue];
  608. NSString * formatter = [self formatFloat:_leftStepper.floatValue * unitScanl];
  609. self.properties.edgeInsets = NSEdgeInsetsMake(_properties.edgeInsets.top, formatter.floatValue, _properties.edgeInsets.bottom, _properties.edgeInsets.right);
  610. } else if ([sender isEqual:_rightStepper]) {
  611. self.rightDistanceText.stringValue = [NSString stringWithFormat:@"%.2f",self.rightStepper.floatValue];
  612. NSString * formatter = [self formatFloat:_rightStepper.floatValue * unitScanl];
  613. self.properties.edgeInsets = NSEdgeInsetsMake(_properties.edgeInsets.top, _properties.edgeInsets.left, _properties.edgeInsets.bottom, formatter.floatValue);
  614. } else if ([sender isEqual:_lineStepper]) {
  615. self.lineSpaceText.stringValue = [NSString stringWithFormat:@"%.2f",_lineStepper.floatValue];
  616. NSString * formatter = [self formatFloat:_lineStepper.floatValue * unitScanl];
  617. self.properties.rowSpace = formatter.floatValue;
  618. } else if ([sender isEqual:_columnStepper]) {
  619. self.columnSpaceText.stringValue = [NSString stringWithFormat:@"%.2f",_columnStepper.floatValue];
  620. NSString * formatter = [self formatFloat:_columnStepper.floatValue * unitScanl];
  621. self.properties.columnSpace = formatter.floatValue;
  622. }
  623. [self reloadPDFView];
  624. }
  625. - (IBAction)comboBoxItemClick_PageRange:(id)sender
  626. {
  627. if (0 == [self.pageRangeComboBox indexOfSelectedItem] ||
  628. 1 == [self.pageRangeComboBox indexOfSelectedItem] ||
  629. 2 == [self.pageRangeComboBox indexOfSelectedItem]) {
  630. self.pageRangeComboBox.delegate = nil;
  631. [self.window makeFirstResponder:self];
  632. [self.pageRangeComboBox setEditable:NO];
  633. [self changePrenView];
  634. } else if (3 == [self.pageRangeComboBox indexOfSelectedItem]) {
  635. self.pageRangeComboBox.delegate = self;
  636. [self.pageRangeComboBox setStringValue:@""];
  637. [self.pageRangeComboBox setEditable:YES];
  638. [self.window makeFirstResponder:self.pageRangeComboBox];
  639. }
  640. }
  641. - (IBAction)comboBoxItemClick_PageOrder:(id)sender
  642. {
  643. if (0 == [self.pageOrderComboBox indexOfSelectedItem]) {
  644. self.properties.order = kKMPDFMultiplePageOrder_Transverse;
  645. } else if(1 == [self.pageOrderComboBox indexOfSelectedItem]){
  646. self.properties.order = kKMPDFMultiplePageOrder_TransverseAntitone;
  647. } else if(2 == [self.pageOrderComboBox indexOfSelectedItem]){
  648. self.properties.order = kKMPDFMultiplePageOrder_Longitudinal;
  649. } else if(3 == [self.pageOrderComboBox indexOfSelectedItem]){
  650. self.properties.order = kKMPDFMultiplePageOrder_LongitudinalAntitone;
  651. }
  652. [self reloadPDFView];
  653. }
  654. - (IBAction)comboBoxItemClick_PagesPer:(id)sender
  655. {
  656. if ( 5!= [self.selectPerComboBox indexOfSelectedItem]) {
  657. self.columnCountText.editable = NO;
  658. self.lineCountText.editable = NO;
  659. if (0 == [self.selectPerComboBox indexOfSelectedItem]) {
  660. self.properties.columnCout = 1;
  661. self.properties.rowCount = 2;
  662. } else if(1 == [self.selectPerComboBox indexOfSelectedItem]){
  663. self.properties.columnCout = 2;
  664. self.properties.rowCount = 2;
  665. } else if(2 == [self.selectPerComboBox indexOfSelectedItem]){
  666. self.properties.columnCout = 2;
  667. self.properties.rowCount = 3;
  668. } else if(3 == [self.selectPerComboBox indexOfSelectedItem]){
  669. self.properties.columnCout = 3;
  670. self.properties.rowCount = 3;
  671. } else if(4 == [self.selectPerComboBox indexOfSelectedItem]){
  672. self.properties.columnCout = 4;
  673. self.properties.rowCount = 4;
  674. }
  675. self.columnCountText.stringValue = [NSString stringWithFormat:@"%ld",self.properties.columnCout];
  676. self.lineCountText.stringValue = [NSString stringWithFormat:@"%ld",self.properties.rowCount];
  677. self.columnCountText.selectable = NO;
  678. self.lineCountText.selectable = NO;
  679. } else {
  680. self.properties.columnCout = 2;
  681. self.properties.rowCount = 2;
  682. self.columnCountText.stringValue = @"2";
  683. self.lineCountText.stringValue = @"2";
  684. self.columnCountText.editable = YES;
  685. self.lineCountText.editable = YES;
  686. self.columnCountText.selectable = YES;
  687. self.lineCountText.selectable = YES;
  688. }
  689. [self changePrenView];
  690. }
  691. - (IBAction)comboBoxItemClick_SplitSize:(id)sender
  692. {
  693. self.pageWidthTextField.enabled = NO;
  694. self.pageHeightTextField.enabled = NO;
  695. if ([_splitSizeComboBox indexOfSelectedItem] == _splitSizeComboBox.numberOfItems - 1) {
  696. self.properties.pageSize = CGSizeMake(595, 842);
  697. self.pageWidthTextField.enabled = YES;
  698. self.pageHeightTextField.enabled = YES;
  699. } else {
  700. CGSize tSize = [KMPageSizeTool getPaperSizeWithPaperName:self.splitSizeComboBox.stringValue];
  701. self.properties.pageSize = CGSizeMake(tSize.width * 595.0/210, tSize.height * 842.0/297.0);
  702. }
  703. CGFloat unitScanl = [self unitConversion];
  704. self.topDistanceText.stringValue =
  705. self.bottomDistanceText.stringValue =
  706. self.leftDistanceText.stringValue =
  707. self.rightDistanceText.stringValue =
  708. self.bottomStepper.stringValue =
  709. self.topStepper.stringValue =
  710. self.leftStepper.stringValue =
  711. self.rightStepper.stringValue =
  712. [NSString stringWithFormat:@"%@",[self formatFloat:20/unitScanl]];
  713. self.lineStepper.stringValue =
  714. self.columnStepper.stringValue =
  715. self.lineSpaceText.stringValue =
  716. self.columnSpaceText.stringValue =
  717. [NSString stringWithFormat:@"%@",[self formatFloat:10/unitScanl]];
  718. _properties.edgeInsets = NSEdgeInsetsMake(20, 20, 20, 20);
  719. _properties.rowSpace = _properties.columnSpace = 10;
  720. [self changePrenView];
  721. [self reloadPDFView];
  722. }
  723. - (IBAction)comboBoxItemClick_Unit:(id)sender
  724. {
  725. if ([self.unitComboBox indexOfSelectedItem] == 1) {
  726. self.spaceUnitLabel.stringValue = [NSString stringWithFormat:@"(%@)",NSLocalizedString(@"mm", nil)];
  727. self.edgeUnitLabel.stringValue = [NSString stringWithFormat:@"(%@)",NSLocalizedString(@"mm", nil)];
  728. self.unitTLabel.stringValue = NSLocalizedString(@"mm", nil);
  729. } else if([self.unitComboBox indexOfSelectedItem] == 0){
  730. self.spaceUnitLabel.stringValue = [NSString stringWithFormat:@"(%@)",NSLocalizedString(@"cm", nil)];
  731. self.edgeUnitLabel.stringValue = [NSString stringWithFormat:@"(%@)",NSLocalizedString(@"cm", nil)];
  732. self.unitTLabel.stringValue = NSLocalizedString(@"cm", nil);
  733. } else {
  734. self.spaceUnitLabel.stringValue = [NSString stringWithFormat:@"(%@)",NSLocalizedString(@"in", nil)];
  735. self.edgeUnitLabel.stringValue = [NSString stringWithFormat:@"(%@)",NSLocalizedString(@"in", nil)];
  736. self.unitTLabel.stringValue = NSLocalizedString(@"in", nil);
  737. }
  738. CGFloat unitScanle = [self unitConversion];
  739. self.topDistanceText.stringValue =
  740. self.topStepper.stringValue =
  741. [NSString stringWithFormat:@"%@",[self formatFloat:(_properties.edgeInsets.top / unitScanle)]];
  742. self.bottomDistanceText.stringValue =
  743. self.bottomStepper.stringValue =
  744. [NSString stringWithFormat:@"%@",[self formatFloat:(_properties.edgeInsets.bottom / unitScanle)]];
  745. self.leftDistanceText.stringValue =
  746. self.leftStepper.stringValue =
  747. [NSString stringWithFormat:@"%@",[self formatFloat:(_properties.edgeInsets.left / unitScanle)]];
  748. self.rightDistanceText.stringValue =
  749. self.rightStepper.stringValue =
  750. [NSString stringWithFormat:@"%@",[self formatFloat:(_properties.edgeInsets.right / unitScanle)]];
  751. self.lineSpaceText.stringValue =
  752. self.lineStepper.stringValue =
  753. [NSString stringWithFormat:@"%@",[self formatFloat:(_properties.rowSpace / unitScanle)]];
  754. self.columnSpaceText.stringValue =
  755. self.columnStepper.stringValue =
  756. [NSString stringWithFormat:@"%@",[self formatFloat:(_properties.columnSpace / unitScanle)]];
  757. self.lineStepper.maxValue =
  758. self.columnStepper.maxValue = KMPDFMultiplePrint_MaxSpace/unitScanle;
  759. self.bottomStepper.maxValue =
  760. self.topStepper.maxValue =
  761. self.leftStepper.maxValue =
  762. self.rightStepper.maxValue = KMPDFMultiplePrint_MaxSpace/unitScanle;
  763. [self reloadPDFView];
  764. }
  765. - (IBAction)comboBoxItemClick_CommentsForms:(id)sender
  766. {
  767. if (0 == [self.commentsFormsComboBox indexOfSelectedItem]) {
  768. self.properties.commentsForms = kKMPDFCommentsForms_Documents;
  769. } else if(1 == [self.commentsFormsComboBox indexOfSelectedItem]){
  770. self.properties.commentsForms = kKMPDFCommentsForms_DocumentMarkups;
  771. } else if(2 == [self.commentsFormsComboBox indexOfSelectedItem]){
  772. self.properties.commentsForms = kKMPDFCommentsForms_DocumentStamps;
  773. }
  774. [self reloadPDFView];
  775. }
  776. #pragma mark - NSTextFieldDelegate Methods
  777. - (void)controlTextDidEndEditing:(NSNotification *)notification
  778. {
  779. NSTextField *textField = (NSTextField *)[notification object];
  780. if (textField == self.positionTextFiled) {
  781. NSInteger index = [[self.positionTextFiled stringValue] integerValue];
  782. NSInteger pageCount = self.previewDocument.pageCount;
  783. NSInteger currentPageIndex = [self.previewDocument indexForPage:self.PDFView.currentPage];
  784. if (index > 0 && index <= pageCount) {
  785. [self.PDFView goToPage:[self.previewDocument pageAtIndex:index-1]];
  786. [self reloadPDFView];
  787. } else {
  788. self.positionTextFiled.stringValue = [NSString stringWithFormat:@"%@",@(currentPageIndex+1)];
  789. }
  790. } else if (textField == self.columnCountText) {
  791. if ( 5!= [self.selectPerComboBox indexOfSelectedItem]) {
  792. return;
  793. }
  794. if (self.columnCountText.integerValue < 1) {
  795. self.columnCountText.stringValue = @"1";
  796. } else if (self.columnCountText.integerValue > 99){
  797. self.columnCountText.stringValue = @"99";
  798. }
  799. self.properties.columnCout = self.columnCountText.integerValue;
  800. [self changePrenView];
  801. } else if (textField == self.lineCountText) {
  802. if ( 5!= [self.selectPerComboBox indexOfSelectedItem]) {
  803. return;
  804. }
  805. if (self.lineCountText.integerValue < 1) {
  806. self.lineCountText.stringValue = @"1";
  807. } else if (self.lineCountText.integerValue > 99){
  808. self.lineCountText.stringValue = @"99";
  809. }
  810. self.properties.rowCount = self.lineCountText.integerValue;
  811. [self changePrenView];
  812. } else if (textField == self.pageRangeComboBox) {
  813. [self changePrenView];
  814. } else if (_topDistanceText == textField) {
  815. CGFloat unitScanle = [self unitConversion];
  816. NSString * unitMax = [self formatFloat:textField.floatValue * unitScanle];
  817. if (unitMax.floatValue > KMPDFMultiplePrint_MaxEdge) {
  818. unitMax = [NSString stringWithFormat:@"%f",KMPDFMultiplePrint_MaxEdge];
  819. textField.stringValue = [NSString stringWithFormat:@"%@",[self formatFloat:KMPDFMultiplePrint_MaxEdge/unitScanle]];
  820. } else if(unitMax.floatValue <= 0) {
  821. unitMax = 0;
  822. textField.stringValue = @"0";
  823. }
  824. _topStepper.stringValue = textField.stringValue;
  825. self.properties.edgeInsets = NSEdgeInsetsMake(unitMax.floatValue, _properties.edgeInsets.left, _properties.edgeInsets.bottom, _properties.edgeInsets.right);
  826. [self reloadPDFView];
  827. } else if (_bottomDistanceText == textField) {
  828. CGFloat unitScanle = [self unitConversion];
  829. NSString * unitMax = [self formatFloat:textField.floatValue * unitScanle];
  830. if (unitMax.floatValue > KMPDFMultiplePrint_MaxEdge) {
  831. unitMax = [NSString stringWithFormat:@"%f",KMPDFMultiplePrint_MaxEdge];
  832. textField.stringValue = [NSString stringWithFormat:@"%@",[self formatFloat:KMPDFMultiplePrint_MaxEdge/unitScanle]];
  833. } else if(unitMax.floatValue <= 0) {
  834. unitMax = 0;
  835. textField.stringValue = @"0";
  836. }
  837. _bottomStepper.stringValue = textField.stringValue;
  838. self.properties.edgeInsets = NSEdgeInsetsMake(_properties.edgeInsets.top, _properties.edgeInsets.left, unitMax.floatValue , _properties.edgeInsets.right);
  839. [self reloadPDFView];
  840. } else if (_leftDistanceText == textField) {
  841. CGFloat unitScanle = [self unitConversion];
  842. NSString * unitMax = [self formatFloat:textField.floatValue * unitScanle];
  843. if (unitMax.floatValue > KMPDFMultiplePrint_MaxEdge) {
  844. unitMax = [NSString stringWithFormat:@"%f",KMPDFMultiplePrint_MaxEdge];
  845. textField.stringValue = [NSString stringWithFormat:@"%@",[self formatFloat:KMPDFMultiplePrint_MaxEdge/unitScanle]];
  846. } else if(unitMax.floatValue <= 0) {
  847. unitMax = 0;
  848. textField.stringValue = @"0";
  849. }
  850. _leftStepper.stringValue = textField.stringValue;
  851. self.properties.edgeInsets = NSEdgeInsetsMake(_properties.edgeInsets.top,unitMax.floatValue , _properties.edgeInsets.bottom, _properties.edgeInsets.right);
  852. [self reloadPDFView];
  853. } else if (_rightDistanceText == textField) {
  854. CGFloat unitScanle = [self unitConversion];
  855. NSString * unitMax = [self formatFloat:textField.floatValue * unitScanle];
  856. if (unitMax.floatValue > KMPDFMultiplePrint_MaxEdge) {
  857. unitMax = [NSString stringWithFormat:@"%f",KMPDFMultiplePrint_MaxEdge];
  858. textField.stringValue = [NSString stringWithFormat:@"%@",[self formatFloat:KMPDFMultiplePrint_MaxEdge/unitScanle]];
  859. } else if(unitMax.floatValue <= 0) {
  860. unitMax = 0;
  861. textField.stringValue = @"0";
  862. }
  863. _rightStepper.stringValue = textField.stringValue;
  864. self.properties.edgeInsets = NSEdgeInsetsMake(_properties.edgeInsets.top, _properties.edgeInsets.left, _properties.edgeInsets.bottom, unitMax.floatValue);
  865. [self reloadPDFView];
  866. } else if (_lineSpaceText == textField) {
  867. CGFloat unitScanle = [self unitConversion];
  868. NSString * unitMax = [self formatFloat:textField.floatValue * unitScanle];
  869. if (unitMax.floatValue > KMPDFMultiplePrint_MaxSpace) {
  870. unitMax = [NSString stringWithFormat:@"%f",KMPDFMultiplePrint_MaxSpace];
  871. textField.stringValue = [NSString stringWithFormat:@"%@",[self formatFloat:KMPDFMultiplePrint_MaxSpace/unitScanle]];
  872. } else if(unitMax.floatValue <= 0) {
  873. unitMax = 0;
  874. textField.stringValue = @"0";
  875. }
  876. _lineStepper.stringValue = textField.stringValue;
  877. self.properties.rowSpace = unitMax.floatValue;
  878. [self reloadPDFView];
  879. } else if (_columnSpaceText == textField) {
  880. CGFloat unitScanle = [self unitConversion];
  881. NSString * unitMax = [self formatFloat:textField.floatValue * unitScanle];
  882. if (unitMax.floatValue > KMPDFMultiplePrint_MaxSpace) {
  883. unitMax = [NSString stringWithFormat:@"%f",KMPDFMultiplePrint_MaxSpace];
  884. textField.stringValue = [NSString stringWithFormat:@"%@",[self formatFloat:KMPDFMultiplePrint_MaxSpace/unitScanle]];
  885. } else if(unitMax.floatValue <= 0) {
  886. unitMax = 0;
  887. textField.stringValue = @"0";
  888. }
  889. _columnStepper.stringValue = textField.stringValue;
  890. self.properties.columnSpace = unitMax.floatValue;
  891. [self reloadPDFView];
  892. } else if (textField == self.pageWidthTextField) {
  893. CGFloat over = [[textField stringValue] floatValue];
  894. if ([self.unitComboBox indexOfSelectedItem] == 0) {
  895. over *= 595.0/21;
  896. } else if([self.unitComboBox indexOfSelectedItem] == 3){
  897. over *= 595.0/210 * 25.4;
  898. } else {
  899. over *= 595.0/210;
  900. }
  901. CGSize size = CGSizeZero;
  902. size = self.properties.pageSize;
  903. size.width = over;
  904. self.properties.pageSize = size;
  905. [self reloadPDFView];
  906. } else if (textField == self.pageHeightTextField) {
  907. CGFloat over = [[textField stringValue] floatValue];
  908. if ([self.unitComboBox indexOfSelectedItem] == 0) {
  909. over *= 842.0/29.7;
  910. } else if([self.unitComboBox indexOfSelectedItem] == 3){
  911. over *= 842.0/297 * 25.4;
  912. } else {
  913. over *= 842.0/297;
  914. }
  915. CGSize size = CGSizeZero;
  916. size = self.properties.pageSize;
  917. size.height = over;
  918. self.properties.pageSize = size;
  919. [self reloadPDFView];
  920. }
  921. }
  922. - (void)controlTextDidChange:(NSNotification *)notification
  923. {
  924. NSTextField *textField = (NSTextField *)[notification object];
  925. if (textField == self.positionTextFiled || textField == self.columnCountText || textField == self.lineCountText) {
  926. NSString *string = [textField.formatter stringFromNumber:[NSNumber numberWithFloat:[textField.stringValue integerValue]]];
  927. textField.stringValue = string;
  928. }
  929. }
  930. #pragma mark - show / hide Waiting
  931. - (void)showWaitting
  932. {
  933. if (!_multipleMaskView) {
  934. _multipleMaskView = [[KMBookletMaskView alloc] initWithFrame:NSMakeRect(0, 0, self.window.frame.size.width, self.window.frame.size.height)];
  935. }
  936. [self.window.contentView addSubview:self.multipleMaskView];
  937. }
  938. - (void)hideWaitting
  939. {
  940. [self.multipleMaskView removeFromSuperview];
  941. }
  942. @end