KMPDFBookletWindowController.m 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075
  1. //
  2. // KMPDFBookletWindowController.m
  3. // PDF Reader
  4. //
  5. // Created by 蒋志鹏 on 2018/5/22.
  6. // Copyright © 2018 Kdan Mobile. All rights reserved.
  7. //
  8. #import "PDF_Reader_Pro-Swift.h"
  9. #import "KMPDFBookletWindowController.h"
  10. #import "KMBookletParameterModel.h"
  11. #import "KMBookletManager.h"
  12. #import "KMBookletPDFView.h"
  13. //#import "KMFileAttribute.h"
  14. //#import "KMBookletMaskView.h"
  15. #import "NSMutableArray+KMOddEvenPartFetch.h"
  16. //#import "PasswordWindowController.h"
  17. //#import "KMSavePanelAccessoryController.h"
  18. //#import "NSWindowController_SKExtensions.h"
  19. //#import "KMPageSizeTool.h"
  20. #pragma mark KMBookletPDFPage
  21. @interface KMBookletPDFPage : PDFPage
  22. @property (nonatomic,retain) KMBookletParameterModel *properties;
  23. @property (nonatomic,retain) NSArray * pages;
  24. @end
  25. @implementation KMBookletPDFPage
  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. KMBookletManager *manager = [KMBookletManager sharedManager];
  37. [manager drawPageWithContext:context parameterModel:self.properties printArray:self.pages];
  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. KMBookletManager *manager = [KMBookletManager sharedManager];
  46. [manager drawPageWithContext:context parameterModel:self.properties printArray:self.pages];
  47. }
  48. @end
  49. //combobox tag 枚举
  50. typedef NS_ENUM(NSUInteger, KBookletComboBoxTag) {
  51. KBookletComboBoxTag_Unit = 0,
  52. KBookletComboBoxTag_PageSize,
  53. KBookletComboBoxTag_Subset,
  54. KBookletComboBoxTag_Binding,
  55. KBookletComboBoxTag_PagenationFont,
  56. KBookletComboBoxTag_pageRange,
  57. KBookletComboBoxTag_Comments
  58. };
  59. //页码范围选择枚举
  60. typedef NS_ENUM(NSInteger,KBookletPageRangeSelectStyle){
  61. KBookletPageRangeSelectStyle_AllPages = 0,
  62. KBookletPageRangeSelectStyle_OddPages,//奇数页
  63. KBookletPageRangeSelectStyle_EvenPages,//偶数页
  64. KBookletPageRangeSelectStyle_SelectPages
  65. };
  66. //PDFView的最大宽高
  67. static CGFloat KMPDFBookletGenerate_PDFViewMAXWidth = 290.0;
  68. static CGFloat KMPDFBookletGenerate_PDFViewMAXHeight = 404.0;
  69. //打印时PDFDocument的暂存路径
  70. #define kBookletPrintFolderPath [[[NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:[NSBundle mainBundle].bundleIdentifier] stringByAppendingPathComponent:@"Booklet"]
  71. #define kBookletFileSavePath [[[NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:[NSBundle mainBundle].bundleIdentifier] stringByAppendingPathComponent:@"SaveBooklet.pdf"]
  72. @interface KMPDFBookletWindowController () <NSComboBoxDelegate, NSTextFieldDelegate, PDFViewDelegate>
  73. //尺寸标识Label
  74. @property (assign) IBOutlet NSTextField *bookletNewPageSizeLabel;
  75. //PDFView宽约束
  76. @property (assign) IBOutlet NSLayoutConstraint *bookletPDFPreViewWidthConstant;
  77. //PDFView高约束
  78. @property (assign) IBOutlet NSLayoutConstraint *bookletPDFPreViewHeightConstant;
  79. //单位label
  80. @property (assign) IBOutlet NSTextField *bookletUnitLabel;
  81. //尺寸label
  82. @property (assign) IBOutlet NSTextField *bookletPageSizeLabel;
  83. //子集Label
  84. @property (assign) IBOutlet NSTextField *bookletSubsetLable;
  85. //sheet开始Label
  86. @property (assign) IBOutlet NSTextField *bookletSheetFromLabel;
  87. //sheet结束Label
  88. @property (assign) IBOutlet NSTextField *bookletSheetPickToLabel;
  89. //装订线label
  90. @property (assign) IBOutlet NSTextField *bookletBindingLabel;
  91. //保留注释Label
  92. @property (assign) IBOutlet NSTextField *BookletCommentsFormsLabel;
  93. //页间距Label
  94. @property (assign) IBOutlet NSTextField *bookletPageGapLabel;
  95. //Gap单位
  96. @property (assign) IBOutlet NSTextField *bookletGapUnitLabel;
  97. //页码选择Label
  98. @property (assign) IBOutlet NSTextField *bookletPageRangeLabel;
  99. //Gap 步进器
  100. @property (assign) IBOutlet NSStepper *bookletPagesGapStepper;
  101. //取消按钮
  102. @property (assign) IBOutlet NSButton *bookletCacelButton;
  103. //生成PDF按钮
  104. @property (assign) IBOutlet NSButton *bookletDoneButton;
  105. //打印按钮
  106. @property (assign) IBOutlet NSButton *bookletPrintButton;
  107. //Preview PDFView
  108. @property (assign) IBOutlet KMBookletPDFView *bookletPDFView;
  109. //单位ComboBox
  110. @property (assign) IBOutlet NSComboBox *bookletUnitComboBox;
  111. //尺寸ComboBox
  112. @property (assign) IBOutlet NSComboBox *bookletPageSizeComboBox;
  113. //子集ComboBox
  114. @property (assign) IBOutlet NSComboBox *bookletSubsetComboBox;
  115. //装订线ComboBox
  116. @property (assign) IBOutlet NSComboBox *bookletBindingComboBox;
  117. //保留注释Combobox
  118. @property (assign) IBOutlet NSComboBox *bookletCommentFormsComboBox;
  119. //页码选择ComboBox
  120. @property (assign) IBOutlet NSComboBox *bookletPageRangeComboBox;
  121. //页间距TextField
  122. @property (assign) IBOutlet NSTextField *bookletPageGapTextField;
  123. //自动旋转Button
  124. @property (assign) IBOutlet NSButton *bookletNeedAutoRotatePagesButton;
  125. //当前页码TextField
  126. @property (assign) IBOutlet NSTextField *previewPaginationTextField;
  127. //总页数Label
  128. @property (assign) IBOutlet NSTextField *bookletTotalPageLabel;
  129. //页面交换Button
  130. @property (assign) IBOutlet NSButton *bookletReversePagesButton;
  131. //sheet开始TextField
  132. @property (assign) IBOutlet NSTextField *bookletSheetStartTextField;
  133. //sheet结束TextField
  134. @property (assign) IBOutlet NSTextField *bookletSheetEndTextField;
  135. //单位数组
  136. @property (nonatomic, retain) NSArray *unitArray;
  137. //打印样式尺寸数组
  138. @property (nonatomic, retain) NSArray *bookletSubsetArray;
  139. //布局样式数组
  140. @property (nonatomic, retain) NSArray *bookletBindingArray;
  141. //页面类型选择数组
  142. @property (nonatomic, retain) NSArray *bookletPageRangeArray;
  143. //参数对象
  144. @property (nonatomic, retain) KMBookletParameterModel *parameterModel;
  145. //选中页数组
  146. @property (nonatomic, retain) NSMutableArray *selectdPagesArray;
  147. //是否保留注释数组
  148. @property (nonatomic, retain) NSArray *bookletCommentsArray;
  149. //遮盖View
  150. @property (nonatomic, retain) KMBookletMaskView *bookletMaskView;
  151. //PDF文档
  152. @property (nonatomic, retain) PDFDocument *pdfDocument;
  153. @property (nonatomic,retain) NSString *password;
  154. @property (assign) IBOutlet NSTextField *pageWidthTextField;
  155. @property (assign) IBOutlet NSTextField *pageHeightTextField;
  156. @property (assign) IBOutlet NSTextField *unitTLabel;
  157. @end
  158. @implementation KMPDFBookletWindowController
  159. - (void)dealloc
  160. {
  161. //移除缓存
  162. NSFileManager *manager = [NSFileManager defaultManager];
  163. if ([manager fileExistsAtPath:kBookletFileSavePath]) {
  164. [manager removeItemAtPath:kBookletFileSavePath error:nil];
  165. }
  166. }
  167. #pragma mark - Lazy load
  168. - (NSArray *)unitArray
  169. {
  170. if (!_unitArray) {
  171. _unitArray = [[NSArray alloc] initWithArray:@[@"cm",@"mm",@"in"]];
  172. }
  173. return _unitArray;
  174. }
  175. - (NSArray *)bookletSubsetArray
  176. {
  177. if (!_bookletSubsetArray ){
  178. _bookletSubsetArray = [[NSArray alloc] initWithArray:@[
  179. NSLocalizedString(@"Both sides", nil),
  180. NSLocalizedString(@"Front side only", nil),
  181. NSLocalizedString(@"Back side only", nil)]];
  182. }
  183. return _bookletSubsetArray;
  184. }
  185. - (NSArray *)bookletBindingArray
  186. {
  187. if (!_bookletBindingArray) {
  188. _bookletBindingArray = [[NSArray alloc] initWithArray:@[
  189. NSLocalizedString(@"Left", nil), NSLocalizedString(@"Right", nil),
  190. NSLocalizedString(@"Left (Tall)",
  191. nil),NSLocalizedString(@"Right (Tall)", nil)]];
  192. }
  193. return _bookletBindingArray;
  194. }
  195. - (NSArray *)bookletPageRangeArray
  196. {
  197. if (!_bookletPageRangeArray) {
  198. _bookletPageRangeArray = [[NSArray alloc] initWithArray:@[NSLocalizedString(@"All Pages", nil),
  199. NSLocalizedString(@"Odd Pages Only", nil),
  200. NSLocalizedString(@"Even Pages Only", nil), NSLocalizedString(@"e.g. 1,3-5,10",nil)]];
  201. }
  202. return _bookletPageRangeArray;
  203. }
  204. - (NSArray *)bookletCommentsArray
  205. {
  206. if (!_bookletCommentsArray) {
  207. _bookletCommentsArray = [[NSArray alloc] initWithArray:@[
  208. NSLocalizedString(@"Document", nil),
  209. NSLocalizedString(@"Document and Markups", nil),
  210. NSLocalizedString(@"Document and Stamps",nil)
  211. ]];
  212. }
  213. return _bookletCommentsArray;
  214. }
  215. - (KMBookletParameterModel *)parameterModel
  216. {
  217. if (!_parameterModel) {
  218. _parameterModel = [[KMBookletParameterModel alloc] init];
  219. }
  220. return _parameterModel;
  221. }
  222. - (NSMutableArray *)selectdPagesArray
  223. {
  224. if (!_selectdPagesArray) {
  225. _selectdPagesArray = [NSMutableArray new];
  226. }
  227. return _selectdPagesArray;
  228. }
  229. #pragma mark - life cycle
  230. - (void)windowDidLoad
  231. {
  232. [super windowDidLoad];
  233. [self localizeLanguage];
  234. [self configuViews];
  235. if (!self.pdfDocument) {
  236. return;
  237. }
  238. if (![self.pdfDocument isLocked] && [self.pdfDocument unlockWithPassword:self.password]) {
  239. [self initData];
  240. } else {
  241. // dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  242. // PasswordWindowController *com = [[PasswordWindowController alloc] initWithWindowNibName:@"PasswordWindowController"];
  243. // com.fileURL = self.pdfDocument.documentURL;
  244. // [com beginSheetModalForWindow:self.window completionHandler:^(NSString *password) {
  245. // if (password) {
  246. // [self.pdfDocument unlockWithPassword:password];
  247. // self.password = password;
  248. // [self initData];
  249. // } else {
  250. // [self close];
  251. // }
  252. // }];
  253. // [com release];
  254. // });
  255. }
  256. }
  257. - (BOOL)windowShouldClose:(id)sender
  258. {
  259. [self close];
  260. return YES;
  261. }
  262. #pragma mark - Private Method
  263. //多语言
  264. - (void)localizeLanguage
  265. {
  266. self.bookletUnitLabel.stringValue = NSLocalizedString(@"Unit:", nil);
  267. self.bookletPageSizeLabel.stringValue = NSLocalizedString(@"Page size:", nil);
  268. self.bookletSubsetLable.stringValue = NSLocalizedString(@"Booklet Subset:", nil);
  269. self.bookletBindingLabel.stringValue = NSLocalizedString(@"Binding:", nil);
  270. self.bookletPageGapLabel.stringValue = [NSString stringWithFormat:@"%@:",NSLocalizedString(@"Margin", nil)];
  271. self.bookletNeedAutoRotatePagesButton.title = NSLocalizedString(@"Auto-rotate pages within each sheet", nil);
  272. self.bookletReversePagesButton.title = NSLocalizedString(@"Reverse pages", nil);
  273. self.bookletPageRangeLabel.stringValue = NSLocalizedString(@"Page Range", nil);
  274. self.bookletSheetFromLabel.stringValue = NSLocalizedString(@"Sheet from:", nil);
  275. self.bookletSheetPickToLabel.stringValue = NSLocalizedString(@"to:", nil);
  276. self.bookletCacelButton.title = NSLocalizedString(@"Cancel", nil);
  277. self.bookletDoneButton.title = NSLocalizedString(@"Save as PDF", nil);
  278. self.window.title = NSLocalizedString(@"Booklet", nil);
  279. self.bookletPrintButton.title = NSLocalizedString(@"Print", nil);
  280. self.BookletCommentsFormsLabel.stringValue = NSLocalizedString(@"Comments & Forms:", nil);
  281. }
  282. //view设置
  283. - (void) configuViews
  284. {
  285. [self.bookletUnitComboBox addItemsWithObjectValues:self.unitArray];
  286. self.bookletUnitComboBox.editable = NO;
  287. self.bookletUnitComboBox.selectable = NO;
  288. self.bookletUnitComboBox.tag = KBookletComboBoxTag_Unit;
  289. [self.bookletUnitComboBox selectItemAtIndex:1];
  290. self.bookletUnitComboBox.delegate = self;
  291. NSArray *paperArray = [KMPageSizeTool paperSize];
  292. NSMutableArray *tPaperArray = [NSMutableArray arrayWithArray:paperArray];
  293. [tPaperArray addObject:NSLocalizedString(@"Custom",nil)];
  294. [self.bookletPageSizeComboBox addItemsWithObjectValues:tPaperArray];
  295. self.bookletPageSizeComboBox.editable = NO;
  296. self.bookletPageSizeComboBox.selectable = NO;
  297. self.bookletPageSizeComboBox.tag = KBookletComboBoxTag_PageSize;
  298. [self.bookletPageSizeComboBox selectItemAtIndex:1];
  299. self.bookletPageSizeComboBox.delegate = self;
  300. self.pageWidthTextField.enabled = NO;
  301. self.pageHeightTextField.enabled = NO;
  302. [self.bookletSubsetComboBox addItemsWithObjectValues:self.bookletSubsetArray];
  303. self.bookletSubsetComboBox.editable = NO;
  304. self.bookletSubsetComboBox.selectable = NO;
  305. self.bookletSubsetComboBox.tag = KBookletComboBoxTag_Subset;
  306. [self.bookletSubsetComboBox selectItemAtIndex:0];
  307. self.bookletSubsetComboBox.delegate = self;
  308. [self.bookletBindingComboBox addItemsWithObjectValues:self.bookletBindingArray];
  309. self.bookletBindingComboBox.editable = NO;
  310. self.bookletBindingComboBox.selectable = NO;
  311. self.bookletBindingComboBox.tag = KBookletComboBoxTag_Binding;
  312. [self.bookletBindingComboBox selectItemAtIndex:2];
  313. self.bookletBindingComboBox.delegate = self;
  314. [self.bookletCommentFormsComboBox addItemsWithObjectValues:self.bookletCommentsArray];
  315. self.bookletCommentFormsComboBox.editable = NO;
  316. self.bookletCommentFormsComboBox.selectable = NO;
  317. self.bookletCommentFormsComboBox.tag = KBookletComboBoxTag_Comments;
  318. [self.bookletCommentFormsComboBox selectItemAtIndex:1];
  319. self.bookletCommentFormsComboBox.delegate = self;
  320. [self.bookletPageRangeComboBox addItemsWithObjectValues:self.bookletPageRangeArray];
  321. [self.bookletPageRangeComboBox.cell setPlaceholderString:NSLocalizedString(@"e.g. 1,3-5,10", nil)];
  322. [self.bookletPageRangeComboBox selectItemAtIndex:0];
  323. [self.bookletPageRangeComboBox setEditable:NO];
  324. self.bookletPageRangeComboBox.tag = KBookletComboBoxTag_pageRange;
  325. self.bookletPDFView.autoScales = YES;
  326. self.bookletPDFView.displayMode = kPDFDisplaySinglePage;
  327. self.bookletPDFView.backgroundColor = [NSColor whiteColor];
  328. self.bookletPDFView.wantsLayer = YES;
  329. self.bookletPDFView.layer.borderColor = [NSColor blackColor].CGColor;
  330. self.bookletPDFView.layer.borderWidth = 2;
  331. self.previewPaginationTextField.delegate = self;
  332. self.bookletPageGapTextField.delegate = self;
  333. self.bookletSheetStartTextField.delegate = self;
  334. self.bookletSheetEndTextField.delegate = self;
  335. self.bookletNewPageSizeLabel.stringValue = @"210 x 297 mm";
  336. self.pageWidthTextField.stringValue = [KMPageSizeTool conversionWithUnit:self.bookletUnitComboBox.stringValue value:(210)]?:@"";
  337. self.pageHeightTextField.stringValue = [KMPageSizeTool conversionWithUnit:self.bookletUnitComboBox.stringValue value:297]?:@"";
  338. }
  339. //初始化数据
  340. - (void) initData
  341. {
  342. //获取操作数组
  343. NSArray *firstPagesArray = [self fetchTargetArray:KBookletPageRangeSelectStyle_AllPages];
  344. KMBookletManager *manager = [KMBookletManager sharedManager];
  345. NSArray *secondPagesArray = [manager regroupBothSidesPrintArrayWithPrimaryArray:firstPagesArray];
  346. self.parameterModel.sheetEndindex = secondPagesArray.count/2;
  347. self.parameterModel.pageSize = CGSizeMake(595, 842);
  348. self.parameterModel.printStyle = kBLPrintStyle_BothSide;
  349. self.parameterModel.layoutStyle = kBLLayoutStyle_leftTall;
  350. self.parameterModel.commentForms = kBookletCommentForms_DocumentAndMarkups;
  351. self.parameterModel.sheetFromIndex = 0;
  352. self.bookletSheetEndTextField.stringValue = [NSString stringWithFormat:@"%ld",self.parameterModel.sheetEndindex];
  353. self.bookletSheetStartTextField.stringValue = @"1";
  354. self.bookletPageGapTextField.stringValue = self.bookletPagesGapStepper.stringValue = @"0";
  355. self.previewPaginationTextField.stringValue = @"1";
  356. self.parameterModel.gapUnit = kBookletPagesGapUnit_MM;
  357. [self refreshPreViewNeedRefreshCurrentPage:YES];
  358. }
  359. // 产生新的pdf
  360. - (void)generateNewPDFAutoOpen:(BOOL)autoOpen
  361. {
  362. // 如果document不存在或者document的页面个数为0,提示,return
  363. if (!self.bookletPDFView.document ||self.pdfDocument.pageCount < 1) {
  364. NSAlert *alert = [[NSAlert alloc] init];
  365. [alert setAlertStyle:NSCriticalAlertStyle];
  366. [alert setMessageText:[NSString stringWithFormat:@"%@",NSLocalizedString(@"Failed to generate booklet document!", nil)]];
  367. [alert runModal];
  368. return;
  369. }
  370. [self showWaitting];
  371. KMBookletManager *manager = [KMBookletManager sharedManager];
  372. [manager generateNewPDFWithParameterModel:self.parameterModel completionHandler:^(BOOL success, NSString *urlString) {
  373. [self hideWaitting];
  374. if (success) {
  375. if (autoOpen) {
  376. [self close];
  377. [[NSDocumentController sharedDocumentController] openDocumentWithContentsOfURL:[NSURL fileURLWithPath:urlString] display:YES completionHandler:^(NSDocument * _Nullable document, BOOL documentWasAlreadyOpen, NSError * _Nullable error) {
  378. }];
  379. } else {
  380. NSWorkspace *space = [NSWorkspace sharedWorkspace];
  381. [space activateFileViewerSelectingURLs:@[[NSURL fileURLWithPath:urlString]]];
  382. }
  383. }
  384. }];
  385. }
  386. //计算当前选择page样式下有多少张page
  387. - (NSInteger)bookletCaculateMaxPageCountWithPDFDocument:(PDFDocument *)aDocument
  388. printStyle:(kBLPrintStyle)printStyle
  389. pageSelectStyle:(KBookletPageRangeSelectStyle) selectStyle
  390. {
  391. NSArray * arr = [self fetchTargetArray:selectStyle];
  392. KMBookletManager *manager = [KMBookletManager sharedManager];
  393. NSMutableArray *printArr = [manager regroupBothSidesPrintArrayWithPrimaryArray:arr];
  394. if (self.parameterModel.printStyle == kBLPrintStyle_FrontOnly) {
  395. [printArr subArrayWithType:KMSubArrayTypeOdd];
  396. } else if (self.parameterModel.printStyle == kBLPrintStyle_BackOnly){
  397. [printArr subArrayWithType:KMSubArrayTypeEven];
  398. }
  399. return printArr.count;
  400. }
  401. //根据选择样式得到数组。全部,奇数,偶数,手动选择界面
  402. - (NSMutableArray *)fetchTargetArray:(KBookletPageRangeSelectStyle)selectStyle
  403. {
  404. NSMutableArray *arr = [NSMutableArray array];
  405. if (selectStyle == KBookletPageRangeSelectStyle_AllPages) {
  406. for (NSUInteger i = 0; i < self.pdfDocument.pageCount; i++) {
  407. PDFPage *page = [[self.pdfDocument pageAtIndex:i] copy];
  408. [arr addObject:page];
  409. }
  410. } else if (selectStyle == KBookletPageRangeSelectStyle_OddPages) {
  411. for (NSUInteger i = 0; i < self.pdfDocument.pageCount; i = i + 2) {
  412. PDFPage *page = [[self.pdfDocument pageAtIndex:i] copy];
  413. [arr addObject:page];
  414. }
  415. } else if (selectStyle == KBookletPageRangeSelectStyle_EvenPages) {
  416. for (NSUInteger i = 1; i < self.pdfDocument.pageCount; i = i + 2) {
  417. PDFPage *page = [[self.pdfDocument pageAtIndex:i] copy];
  418. [arr addObject:page];
  419. }
  420. } else {
  421. //除了以上三种,那么就是手动选择页面
  422. //如果数组个数为0
  423. if (self.selectdPagesArray.count == 0) {
  424. return arr;
  425. }
  426. for (NSUInteger i = 0; i < self.selectdPagesArray.count; i++) {
  427. NSNumber *num = self.selectdPagesArray[i];
  428. PDFPage *page = [[self.pdfDocument pageAtIndex:[num integerValue] - 1] copy];
  429. [arr addObject:page];
  430. }
  431. }
  432. return arr;
  433. }
  434. //单位转换
  435. - (CGFloat)unitConversion
  436. {
  437. CGFloat unitScanl = 1.0;
  438. if (0 == [self.bookletUnitComboBox indexOfSelectedItem]) {
  439. unitScanl = 595.0/21.0;
  440. } else if (1 == [self.bookletUnitComboBox indexOfSelectedItem])
  441. {
  442. unitScanl = 595.0/210.0;
  443. } else {
  444. unitScanl = (595.0/21.0) * 2.54;
  445. }
  446. return unitScanl;
  447. }
  448. - (NSString *)formatFloat:(float)f
  449. {
  450. if (fmodf(f, 1)==0) {//如果有一位小数点
  451. return [NSString stringWithFormat:@"%.0f",f];
  452. } else if (fmodf(f*10, 1)==0) {//如果有两位小数点
  453. return [NSString stringWithFormat:@"%.1f",f];
  454. } else {
  455. return [NSString stringWithFormat:@"%.2f",f];
  456. }
  457. }
  458. - (void)showCriticalAlert:(NSString *)alertMsg
  459. {
  460. NSAlert *alert = [[NSAlert alloc] init];
  461. [alert setAlertStyle:NSCriticalAlertStyle];
  462. if (alertMsg) {
  463. [alert setInformativeText:alertMsg];
  464. }else{
  465. [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)]];
  466. }
  467. [alert runModal];
  468. }
  469. - (void)refreshPreViewNeedRefreshCurrentPage:(BOOL)needRefreshPage
  470. {
  471. //page数组
  472. NSMutableArray *arr = [self fetchTargetArray:self.bookletPageRangeComboBox.indexOfSelectedItem];
  473. //如果得到的数组为空
  474. if (arr.count == 0) {
  475. return;
  476. }
  477. //将数组信息赋值给模型对象
  478. self.parameterModel.pageArray = arr;
  479. //获得将要用来PreView显示的数组
  480. KMBookletManager *manager = [KMBookletManager sharedManager];
  481. NSArray *printArr = [manager fetchDrawPageArraysWithModel:self.parameterModel];
  482. //调整宽高
  483. if (self.parameterModel.layoutStyle == kBLLayoutStyle_left || self.parameterModel.layoutStyle == kBLLayoutStyle_right) {
  484. if (self.parameterModel.pageSize.width < self.parameterModel.pageSize.height) {
  485. self.parameterModel.pageSize = CGSizeMake(self.parameterModel.pageSize.height, self.parameterModel.pageSize.width);
  486. }
  487. } else {
  488. if (self.parameterModel.pageSize.width > self.parameterModel.pageSize.height) {
  489. self.parameterModel.pageSize = CGSizeMake(self.parameterModel.pageSize.height,self.parameterModel.pageSize.width);
  490. }
  491. }
  492. //计算Preview的合适尺寸
  493. [self setPDFViewSizeWithPageSize:self.parameterModel.pageSize];
  494. //生成新的PDFDocument
  495. PDFDocument *newDocument = [[PDFDocument alloc] init];
  496. for (NSUInteger i = 0; i < printArr.count; i++) {
  497. KMBookletPDFPage *page = [[KMBookletPDFPage alloc] init];
  498. [page setBounds:CGRectMake(0, 0, self.parameterModel.pageSize.width, self.parameterModel.pageSize.height) forBox:kPDFDisplayBoxMediaBox];
  499. page.pages = printArr[i];
  500. page.properties = self.parameterModel;
  501. [newDocument insertPage:page atIndex:i];
  502. }
  503. //给preView传参
  504. self.bookletPDFView.document = newDocument;
  505. //需要刷新到第一页
  506. if (needRefreshPage) {
  507. if ([self.bookletPDFView canGoToFirstPage] ) {
  508. [self.bookletPDFView goToFirstPage:nil];
  509. self.previewPaginationTextField.stringValue = @"1";
  510. }
  511. }
  512. //当前页数和总页数修改
  513. self.previewPaginationTextField.stringValue = [NSString stringWithFormat:@"%ld",[newDocument indexForPage:[self.bookletPDFView currentPage]] + 1];
  514. self.bookletTotalPageLabel.stringValue = [NSString stringWithFormat:@" / %ld",printArr.count];
  515. }
  516. //计算Preview的合适尺寸
  517. - (void)setPDFViewSizeWithPageSize:(CGSize)size
  518. {
  519. CGFloat wRatio = KMPDFBookletGenerate_PDFViewMAXWidth/size.width;
  520. CGFloat hRatio = KMPDFBookletGenerate_PDFViewMAXHeight/size.height;
  521. CGFloat activeRatio = MIN(wRatio, hRatio);
  522. self.bookletPDFPreViewWidthConstant.constant = size.width *activeRatio;
  523. self.bookletPDFPreViewHeightConstant.constant = size.height *activeRatio;
  524. }
  525. #pragma mark - Public Method
  526. - (id)initWithDocument:(PDFDocument *)pdfDocument {
  527. if (self = [super initWithWindowNibName:@"KMPDFBookletWindowController"]) {
  528. [self showWaitting];
  529. dispatch_async(dispatch_get_global_queue(0, 0), ^{
  530. [pdfDocument writeToURL:[NSURL fileURLWithPath:kBookletFileSavePath]];
  531. self.pdfDocument = [[PDFDocument alloc] initWithURL:[NSURL fileURLWithPath:kBookletFileSavePath]];
  532. dispatch_async(dispatch_get_main_queue(), ^{
  533. if (self.pdfDocument) {
  534. [self hideWaitting];
  535. [self initData];
  536. }else{
  537. [self hideWaitting];
  538. [self close];
  539. }
  540. });
  541. });
  542. }
  543. return self;
  544. }
  545. - (void) close
  546. {
  547. if (self.window.isSheet) {
  548. // [self dismissSheet:nil];
  549. // [[NSApp mainWindow] endSheet:self.window];
  550. [self.window.sheetParent endSheet:self.window];
  551. } else {
  552. [super close];
  553. }
  554. }
  555. - (id)initWithPDFFilePath:(NSString *)filePath password:(nullable NSString *)password{
  556. if (self = [super initWithWindowNibName:@"KMPDFBookletWindowController"]) {
  557. PDFDocument * pdf = [[PDFDocument alloc] initWithURL:[NSURL fileURLWithPath:filePath]];
  558. self.pdfDocument = pdf;
  559. self.password = password;
  560. }
  561. return self;
  562. }
  563. #pragma mark - button Action
  564. //gap步进器
  565. - (IBAction)bookletStepperClicked_PagesGap:(id)sender {
  566. NSStepper *gapStepper = sender;
  567. CGFloat unitScanl = [self unitConversion];
  568. self.bookletPageGapTextField.stringValue = [NSString stringWithFormat:@"%.2f",self.bookletPagesGapStepper.floatValue];
  569. NSString * formatter = [self formatFloat:gapStepper.floatValue * unitScanl];
  570. self.parameterModel.gap = formatter.floatValue;
  571. [self.bookletPDFView setNeedsDisplay:YES];
  572. }
  573. //自动旋转
  574. - (IBAction)bookletButtonClicked_AutoRotatePage:(id)sender {
  575. NSButton *button = (NSButton *)sender;
  576. self.parameterModel.autoRotatePage = button.state;
  577. [self refreshPreViewNeedRefreshCurrentPage:NO];
  578. }
  579. //生成PDF
  580. - (IBAction)bookletButtonClicked_Done:(id)sender
  581. {
  582. [self.window makeFirstResponder:self];
  583. if (self.bookletPageRangeComboBox.indexOfSelectedItem == 3 || self.bookletPageRangeComboBox.indexOfSelectedItem == -1) {
  584. KMFileAttribute *fileAttribute = [[KMFileAttribute alloc] init];
  585. fileAttribute.filePath = [self.pdfDocument.documentURL path];
  586. fileAttribute.bAllPage = NO;
  587. fileAttribute.pagesString = self.bookletPageRangeComboBox.stringValue;
  588. if (![fileAttribute fetchSelectPages]) {
  589. [self showCriticalAlert:nil];
  590. return;
  591. }
  592. }
  593. KMSavePanelAccessoryController *panelAccessoryController = [[KMSavePanelAccessoryController alloc] init];
  594. NSSavePanel* panel = [NSSavePanel savePanel];
  595. NSString *fileName = [NSString stringWithFormat:@"%@_Booklet",self.pdfDocument.documentURL.lastPathComponent.stringByDeletingPathExtension];
  596. [panel setNameFieldStringValue:fileName];
  597. [panel setMessage:NSLocalizedString(@"Save as PDF", nil)];
  598. [panel setAllowedFileTypes:@[@"pdf"]];
  599. [panel setCanCreateDirectories:YES];
  600. panel.accessoryView = panelAccessoryController.view;
  601. [panel beginSheetModalForWindow:self.window completionHandler:^(NSModalResponse result) {
  602. if (result == NSFileHandlingPanelOKButton) {
  603. self.parameterModel.savePath = [[panel URL] path];
  604. [self generateNewPDFAutoOpen:panelAccessoryController.openAutomaticButton.state == NSControlStateValueOn ? YES : NO];
  605. }
  606. }];
  607. }
  608. //取消
  609. - (IBAction)bookletButtonClicked_Cancel:(id)sender
  610. {
  611. [self close];
  612. }
  613. //前一张page
  614. - (IBAction)bookletPreviewButtonClicked_PreviousPage:(id)sender
  615. {
  616. if ([self.bookletPDFView canGoToPreviousPage]) {
  617. [self.bookletPDFView goToPreviousPage:nil];
  618. NSInteger currentIndex = [self.bookletPDFView.document indexForPage:[self.bookletPDFView currentPage]];
  619. self.previewPaginationTextField.stringValue = [NSString stringWithFormat:@"%ld",currentIndex + 1];
  620. }
  621. }
  622. //下一张page
  623. - (IBAction)bookletPrevireButtonClicked_NextPage:(id)sender
  624. {
  625. if ([self.bookletPDFView canGoToNextPage]) {
  626. [self.bookletPDFView goToNextPage:nil];
  627. NSInteger currentIndex = [self.bookletPDFView.document indexForPage:[self.bookletPDFView currentPage]];
  628. self.previewPaginationTextField.stringValue = [NSString stringWithFormat:@"%ld",currentIndex + 1];
  629. }
  630. }
  631. //反转顺序
  632. - (IBAction)bookletButtonClicked_ReversePages:(id)sender
  633. {
  634. NSButton *button = sender;
  635. self.parameterModel.reversePages = button.state;
  636. [self refreshPreViewNeedRefreshCurrentPage:YES];
  637. }
  638. //打印PDF
  639. - (IBAction)bookletButtonClicked_PrintPDF:(id)sender
  640. {
  641. [self.window makeFirstResponder:self];
  642. if (self.bookletPageRangeComboBox.indexOfSelectedItem == 3 || self.bookletPageRangeComboBox.indexOfSelectedItem == -1) {
  643. KMFileAttribute *fileAttribute = [[KMFileAttribute alloc] init];
  644. fileAttribute.filePath = [self.pdfDocument.documentURL path];
  645. fileAttribute.bAllPage = NO;
  646. fileAttribute.pagesString = self.bookletPageRangeComboBox.stringValue;
  647. if (![fileAttribute fetchSelectPages]) {
  648. [self showCriticalAlert:nil];
  649. [self.window makeFirstResponder:self.bookletPageRangeComboBox];
  650. return;
  651. }
  652. }
  653. if (![[NSFileManager defaultManager] fileExistsAtPath:kBookletPrintFolderPath]) {
  654. if (![[NSFileManager defaultManager] createDirectoryAtPath:kBookletPrintFolderPath withIntermediateDirectories:NO attributes:nil error:nil]) {
  655. return;
  656. }
  657. }
  658. self.parameterModel.savePath = kBookletPrintFolderPath;
  659. if (!self.bookletPDFView.document ||self.pdfDocument.pageCount < 1) {
  660. NSAlert *alert = [[NSAlert alloc] init];
  661. [alert setAlertStyle:NSCriticalAlertStyle];
  662. [alert setMessageText:[NSString stringWithFormat:@"%@",NSLocalizedString(@"Failed to generate booklet document!", nil)]];
  663. [alert runModal];
  664. return;
  665. }
  666. [self showWaitting];
  667. KMBookletManager *manager = [KMBookletManager sharedManager];
  668. [manager generateNewPDFWithParameterModel:self.parameterModel completionHandler:^(BOOL success, NSString *urlString) {
  669. [self hideWaitting];
  670. if (success) {
  671. [self bookletStartPrint:urlString];
  672. }
  673. }];
  674. }
  675. //打印操作
  676. - (void)bookletStartPrint:(NSString *)filePath
  677. {
  678. PDFDocument * pdf = [[PDFDocument alloc] initWithURL:[NSURL fileURLWithPath:filePath]];
  679. NSPrintInfo *printInfo = [NSPrintInfo sharedPrintInfo];
  680. NSPrintOperation *printOperation = nil;
  681. if ([pdf respondsToSelector:@selector(printOperationForPrintInfo:scalingMode:autoRotate:)]){
  682. printOperation = [pdf printOperationForPrintInfo:printInfo scalingMode:kPDFPrintPageScaleNone autoRotate:YES];
  683. }
  684. [printOperation runOperationModalForWindow:self.window delegate:self didRunSelector:nil contextInfo:NULL];
  685. }
  686. //pageRange ComboBox点击
  687. -(IBAction)comboBoxClick_PageRanage:(id)sender
  688. {
  689. NSLog(@"self.bookletPageRangeComboBox indexOfSelectedItem,%ld",[self.bookletPageRangeComboBox indexOfSelectedItem]);
  690. self.bookletSheetStartTextField.delegate = nil;
  691. self.bookletSheetEndTextField.delegate = nil;
  692. if (1 == [self.bookletPageRangeComboBox indexOfSelectedItem] ||
  693. 2 == [self.bookletPageRangeComboBox indexOfSelectedItem] ||
  694. 0 == [self.bookletPageRangeComboBox indexOfSelectedItem]) {
  695. self.bookletPageRangeComboBox.delegate = nil;
  696. [self.window makeFirstResponder:self];
  697. [self.bookletPageRangeComboBox setEditable:NO];
  698. NSInteger selectIndex = [self.bookletPageRangeComboBox indexOfSelectedItem];
  699. NSInteger arrCount = [self bookletCaculateMaxPageCountWithPDFDocument:self.pdfDocument printStyle:self.parameterModel.printStyle pageSelectStyle:selectIndex];
  700. if (self.parameterModel.printStyle == kBLPrintStyle_BothSide) {
  701. self.parameterModel.sheetEndindex = arrCount/2;
  702. } else {
  703. self.parameterModel.sheetEndindex = arrCount;
  704. }
  705. self.parameterModel.sheetFromIndex = 0;
  706. self.bookletSheetEndTextField.stringValue = [NSString stringWithFormat:@"%ld",self.parameterModel.sheetEndindex];
  707. self.bookletSheetStartTextField.stringValue = @"1";
  708. [self refreshPreViewNeedRefreshCurrentPage:YES];
  709. } else if ([self.bookletPageRangeComboBox indexOfSelectedItem] == 3){
  710. [self.bookletPageRangeComboBox setStringValue:@""];
  711. [self.bookletPageRangeComboBox setEditable:YES];
  712. self.bookletPageRangeComboBox.delegate = self;
  713. [self.window makeFirstResponder:self.bookletPageRangeComboBox];
  714. } else if([self.bookletPageRangeComboBox indexOfSelectedItem] == -1){
  715. // self.bookletPageRangeComboBox.delegate = nil;
  716. }
  717. self.bookletSheetStartTextField.delegate = self;
  718. self.bookletSheetEndTextField.delegate = self;
  719. }
  720. #pragma mark - comboBoxDelegate
  721. - (void)comboBoxSelectionDidChange:(NSNotification *)notification
  722. {
  723. NSComboBox *box = notification.object;
  724. NSInteger selectIndex = [box indexOfSelectedItem];
  725. switch (box.tag) {
  726. case KBookletComboBoxTag_Unit: {
  727. NSInteger currentIndex = [self.bookletPageSizeComboBox indexOfSelectedItem];
  728. //当界面初始化的时候设置combobox的选择界面,此时的indexofselectItem为-1;因为这个时候没有选中的按钮
  729. if (currentIndex < 0) {
  730. return;
  731. }
  732. NSString * untiString = @"mm";
  733. if (selectIndex == 1) {
  734. self.parameterModel.gapUnit = kBookletPagesGapUnit_MM;
  735. self.unitTLabel.stringValue = self.bookletGapUnitLabel.stringValue = NSLocalizedString(@"mm", nil);
  736. untiString = @"mm";
  737. } else if (selectIndex == 0){
  738. self.parameterModel.gapUnit = kBookletPagesGapUnit_CM;
  739. self.unitTLabel.stringValue = self.bookletGapUnitLabel.stringValue = NSLocalizedString(@"cm", nil);
  740. untiString = @"cm";
  741. } else {
  742. self.parameterModel.gapUnit = kBookletPagesGapUnit_IN;
  743. self.unitTLabel.stringValue = self.bookletGapUnitLabel.stringValue = NSLocalizedString(@"in", nil);
  744. untiString = @"in";
  745. }
  746. CGFloat unitScanl = [self unitConversion];
  747. self.bookletPageGapTextField.stringValue =
  748. self.bookletPagesGapStepper.stringValue =
  749. [NSString stringWithFormat:@"%@",[self formatFloat:(self.parameterModel.gap / unitScanl)]];
  750. self.pageWidthTextField.stringValue = [KMPageSizeTool conversionWithUnit:untiString value:(self.parameterModel.pageSize.width/595 * 210)]?:@"";
  751. self.pageHeightTextField.stringValue = [KMPageSizeTool conversionWithUnit:untiString value:(self.parameterModel.pageSize.height/842 * 297)]?:@"";
  752. self.bookletNewPageSizeLabel.stringValue = [NSString stringWithFormat:@"%@ x %@ %@",[KMPageSizeTool conversionWithUnit:untiString value:(self.parameterModel.pageSize.width/595 * 210)],[KMPageSizeTool conversionWithUnit:untiString value:(self.parameterModel.pageSize.height/842 * 297)],untiString] ?:@"";
  753. }
  754. break;
  755. case KBookletComboBoxTag_PageSize: {
  756. if (selectIndex < 0) {
  757. return;
  758. }
  759. NSString *string = [self.bookletPageSizeComboBox itemObjectValueAtIndex:selectIndex];
  760. if (selectIndex == _bookletPageSizeComboBox.numberOfItems - 1) {
  761. self.parameterModel.pageSize = CGSizeMake(595, 842);
  762. self.pageWidthTextField.enabled = YES;
  763. self.pageHeightTextField.enabled = YES;
  764. } else {
  765. CGSize tSize = [KMPageSizeTool getPaperSizeWithPaperName:string];
  766. self.parameterModel.pageSize = CGSizeMake(tSize.width * 595.0/210, tSize.height * 842.0/297.0);
  767. }
  768. CGSize tSize = [KMPageSizeTool getPaperSizeWithPaperName:string];
  769. self.bookletNewPageSizeLabel.stringValue = [NSString stringWithFormat:@"%@ x %@ %@",[KMPageSizeTool conversionWithUnit:self.bookletUnitComboBox.stringValue value:tSize.width],[KMPageSizeTool conversionWithUnit:self.bookletUnitComboBox.stringValue value:tSize.height],self.bookletUnitComboBox.stringValue] ?:@"";
  770. self.pageWidthTextField.stringValue = [KMPageSizeTool conversionWithUnit:self.bookletUnitComboBox.stringValue value:(self.parameterModel.pageSize.width/595 * 210)]?:@"";
  771. self.pageHeightTextField.stringValue = [KMPageSizeTool conversionWithUnit:self.bookletUnitComboBox.stringValue value:(self.parameterModel.pageSize.height/842 * 297)]?:@"";
  772. [self refreshPreViewNeedRefreshCurrentPage:YES];
  773. }
  774. break;
  775. case KBookletComboBoxTag_Subset: {
  776. if (selectIndex == 0) {
  777. self.parameterModel.printStyle = kBLPrintStyle_BothSide;
  778. } else if (selectIndex == 1){
  779. self.parameterModel.printStyle = kBLPrintStyle_FrontOnly;
  780. } else {
  781. self.parameterModel.printStyle = kBLPrintStyle_BackOnly;
  782. }
  783. [self refreshPreViewNeedRefreshCurrentPage:NO];
  784. }
  785. break;
  786. case KBookletComboBoxTag_Binding:{
  787. if (selectIndex == 0) {
  788. self.parameterModel.layoutStyle = kBLLayoutStyle_left;
  789. } else if (selectIndex == 1){
  790. self.parameterModel.layoutStyle = kBLLayoutStyle_right;
  791. } else if (selectIndex == 2){
  792. self.parameterModel.layoutStyle = kBLLayoutStyle_leftTall;
  793. } else if (selectIndex == 3){
  794. self.parameterModel.layoutStyle = kBLLayoutStyle_rightTall;
  795. }
  796. [self refreshPreViewNeedRefreshCurrentPage:NO];
  797. }
  798. break;
  799. case KBookletComboBoxTag_Comments:
  800. self.parameterModel.commentForms = selectIndex;
  801. [self refreshPreViewNeedRefreshCurrentPage:NO];
  802. default:
  803. break;
  804. }
  805. }
  806. #pragma mark - NSTextFieldDelegate
  807. - (void)controlTextDidChange:(NSNotification *)obj
  808. {
  809. id object = obj.object;
  810. if ([object isEqual:self.bookletSheetStartTextField]) {
  811. NSString *string = [self.bookletSheetStartTextField.formatter stringFromNumber:[NSNumber numberWithFloat:[self.bookletSheetStartTextField.stringValue integerValue]]];
  812. self.bookletSheetStartTextField.stringValue = string;
  813. } else if ([object isEqual:self.bookletSheetEndTextField])
  814. {
  815. NSString *string = [self.bookletSheetEndTextField.formatter stringFromNumber:[NSNumber numberWithFloat:[self.bookletSheetEndTextField.stringValue integerValue]]];
  816. self.bookletSheetEndTextField.stringValue = string;
  817. }
  818. }
  819. - (void)controlTextDidEndEditing:(NSNotification *)obj
  820. {
  821. NSTextField * object = [obj object];
  822. NSString *string = object.stringValue;
  823. if ([object isEqual:self.bookletPageGapTextField]) {
  824. //改变了页间距
  825. CGFloat unitScanle = [self unitConversion];
  826. if(string.floatValue <= 0){
  827. self.bookletPageGapTextField.stringValue = @"0";
  828. string = @"0";
  829. }
  830. self.parameterModel.gap = [self formatFloat:[string floatValue] * unitScanle].floatValue;
  831. self.bookletPagesGapStepper.floatValue = [string floatValue];
  832. [self.bookletPDFView setNeedsDisplay:YES];
  833. } else if ([object isEqual:self.previewPaginationTextField]) {
  834. //改变页码
  835. if ([string integerValue] > self.bookletPDFView.document.pageCount) {
  836. PDFPage *page = self.bookletPDFView.currentPage;
  837. self.previewPaginationTextField.stringValue = [NSString stringWithFormat:@"%ld",[self.bookletPDFView.document indexForPage:page] + 1];
  838. return;
  839. }
  840. PDFPage *page = [self.bookletPDFView.document pageAtIndex:[string integerValue] - 1];
  841. if (page) {
  842. [self.bookletPDFView goToPage:page];
  843. }
  844. } else if([object isEqual:self.bookletSheetStartTextField]){
  845. //改变sheet起点
  846. //计算最大页数值,确定输入范围
  847. NSInteger max = [self bookletCaculateMaxPageCountWithPDFDocument:self.pdfDocument printStyle:self.parameterModel.printStyle pageSelectStyle:[self.bookletPageRangeComboBox indexOfSelectedItem]];
  848. //双页、单页区分
  849. if (self.parameterModel.printStyle == kBLPrintStyle_BothSide) {
  850. if ([string integerValue] > max/2 || [string integerValue] <= 0) {
  851. object.stringValue = @"1";
  852. } else {
  853. object.stringValue = [NSString stringWithFormat:@"%ld",[string integerValue]];
  854. }
  855. } else {
  856. if ([string integerValue] > max || [string integerValue] <= 0) {
  857. object.stringValue = @"1";
  858. } else {
  859. object.stringValue = [NSString stringWithFormat:@"%ld",[string integerValue]];
  860. }
  861. }
  862. if ([string integerValue] > self.bookletSheetEndTextField.integerValue) {
  863. object.stringValue = @"1";
  864. }
  865. self.parameterModel.sheetFromIndex = [object.stringValue integerValue] - 1;
  866. [self refreshPreViewNeedRefreshCurrentPage:YES];
  867. } else if ([object isEqual:self.bookletSheetEndTextField]){
  868. //改变sheet终点
  869. //max 得到的是当前选择条件下得到的页数最大值,如果是双面打印那么要除以2才得到sheet的最大值,如果是单面打印,那么就不需要除以2
  870. NSInteger max = [self bookletCaculateMaxPageCountWithPDFDocument:self.pdfDocument printStyle:self.parameterModel.printStyle pageSelectStyle:[self.bookletPageRangeComboBox indexOfSelectedItem]];
  871. //区分
  872. if (self.parameterModel.printStyle == kBLPrintStyle_BothSide) {
  873. //如果输入的值大于了所有页数的一半 或者小于等于0,设置为max/2
  874. if ([string integerValue] > max/2 || [string integerValue] <= 0) {
  875. object.stringValue = [NSString stringWithFormat:@"%ld",max/2];
  876. }
  877. if ([string integerValue] < self.bookletSheetStartTextField.integerValue) {
  878. object.stringValue = [NSString stringWithFormat:@"%ld",max/2];
  879. }
  880. } else {
  881. if ([string integerValue] > max || [string integerValue] <= 0) {
  882. object.stringValue = [NSString stringWithFormat:@"%ld",max];
  883. }
  884. if ([string integerValue] < self.bookletSheetStartTextField.integerValue) {
  885. object.stringValue = [NSString stringWithFormat:@"%ld",max];
  886. }
  887. }
  888. self.parameterModel.sheetEndindex = [object.stringValue integerValue];
  889. [self refreshPreViewNeedRefreshCurrentPage:YES];
  890. } else if ([object isEqual:self.bookletPageRangeComboBox]){
  891. //这种情况是用户从手动输入用户信息切换到选择全部或奇偶页
  892. if ([self.bookletPageRangeComboBox.stringValue isEqualToString:NSLocalizedString(@"All Pages", nil)] || [self.bookletPageRangeComboBox.stringValue isEqualToString:NSLocalizedString(@"Odd Pages Only", nil)] || [self.bookletPageRangeComboBox.stringValue isEqualToString:NSLocalizedString(@"Even Pages Only", nil)]) {
  893. return;
  894. }
  895. //这种情况是反复点击了手动选择页面,自动保持第一响应者
  896. if ([self.bookletPageRangeComboBox indexOfSelectedItem] == 3 && self.bookletPageRangeComboBox.stringValue.length == 0 ) {
  897. // [self showCriticalAlert:nil];
  898. return;
  899. }
  900. KMFileAttribute *fileAttribute = [[KMFileAttribute alloc] init];
  901. fileAttribute.filePath = [self.pdfDocument.documentURL path];
  902. fileAttribute.bAllPage = NO;
  903. fileAttribute.pagesString = self.bookletPageRangeComboBox.stringValue;
  904. if (![fileAttribute fetchSelectPages]) {
  905. //如果存在条件不满足,那么就要弹框,并且将第一响应者给输入框
  906. [self showCriticalAlert:nil];
  907. [self.window makeFirstResponder:self.bookletPageRangeComboBox];
  908. return;
  909. }
  910. self.selectdPagesArray = [fileAttribute fetchSelectPages];
  911. NSInteger arrAcount = [self bookletCaculateMaxPageCountWithPDFDocument:self.pdfDocument printStyle:self.parameterModel.printStyle pageSelectStyle:KBookletPageRangeSelectStyle_SelectPages];
  912. self.bookletSheetStartTextField.stringValue = @"1";
  913. if (self.parameterModel.printStyle == kBLPrintStyle_BothSide) {
  914. self.bookletSheetEndTextField.stringValue = [NSString stringWithFormat:@"%ld",arrAcount/2];
  915. } else {
  916. self.bookletSheetEndTextField.stringValue = [NSString stringWithFormat:@"%ld",arrAcount];
  917. }
  918. self.parameterModel.sheetFromIndex = 0;
  919. self.parameterModel.sheetEndindex = [self.bookletSheetEndTextField.stringValue integerValue];
  920. [self refreshPreViewNeedRefreshCurrentPage:YES];
  921. [self.window makeFirstResponder:self];
  922. }
  923. }
  924. #pragma mark - show / hide Waiting
  925. - (void)showWaitting
  926. {
  927. if (!_bookletMaskView) {
  928. _bookletMaskView = [[KMBookletMaskView alloc] initWithFrame:NSMakeRect(0, 0, self.window.frame.size.width, self.window.frame.size.height)];
  929. }
  930. [self.window.contentView addSubview:self.bookletMaskView];
  931. }
  932. - (void)hideWaitting
  933. {
  934. [self.bookletMaskView removeFromSuperview];
  935. }
  936. @end