KMPDFBookletWindowController.m 46 KB

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