KMPDFMultiplePrintWindowController.m 46 KB

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