KMPDFEditAppendWindow.m 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545
  1. //
  2. // KMPDFEditAppendWindow.m
  3. // PDF Reader
  4. //
  5. // Created by 丁林圭 on 2017/5/11.
  6. // Copyright © 2017年 zhangjie. All rights reserved.
  7. //
  8. #import "KMPDFEditAppendWindow.h"
  9. #import "PasswordWindowController.h"
  10. #import "KMFileAttribute.h"
  11. #import <Quartz/Quartz.h>
  12. #import "KMSavePanelAccessoryController.h"
  13. #import "KMBatchTableRowView.h"
  14. #import "KMBlankView.h"
  15. #import <Masonry/Masonry.h>
  16. #import "KMImageAccessoryController.h"
  17. #if VERSION_DMG
  18. #import <PDF_Master-Swift.h>
  19. #else
  20. #import <PDF_Master-Swift.h>
  21. #endif
  22. #import "CTBrowserWindowController.h"
  23. #import "NSButton+TitleColor.h"
  24. #import "KMPDFMergeFileNameTabelViewCell.h"
  25. #import "PDFPage_SKExtensions.h"
  26. #import "TextFieldFormatter.h"
  27. @interface KMPDFEditAppendTabelViewCell()
  28. @property (assign) IBOutlet NSTextField *pageCountLabel;
  29. @property (assign) IBOutlet NSTextField *indexLabel;
  30. @property (assign) IBOutlet NSTextField *fileNameLabel;
  31. @property (assign) IBOutlet NSTextField *pageRangeLabel;
  32. @property (assign) IBOutlet NSTextField *sizeLabel;
  33. @property (assign) IBOutlet NSImageView *fileImage;
  34. @property (assign) IBOutlet NSComboBox *pageRangeBox;
  35. @property (nonatomic, copy) void(^removeCallBack)(void);
  36. @end
  37. @implementation KMPDFEditAppendTabelViewCell
  38. - (void)dealloc {}
  39. - (IBAction)buttonClicked_Remove:(id)sender {
  40. if (self.removeCallBack) {
  41. self.removeCallBack();
  42. }
  43. }
  44. - (void)awakeFromNib {
  45. [super awakeFromNib];
  46. }
  47. @end
  48. @interface KMAppendPDFPage: PDFPage
  49. @property (nonatomic, strong)PDFPage *drawingPage;
  50. @end
  51. @implementation KMAppendPDFPage
  52. - (void)dealloc {}
  53. //- (void)drawWithBox:(PDFDisplayBox)box {
  54. // [super drawWithBox:box];
  55. //
  56. // CGContextRef context = (CGContextRef)[[NSGraphicsContext currentContext] graphicsPort];
  57. // CGSize pageSize = [self boundsForBox:kPDFDisplayBoxCropBox].size;
  58. // [self drawPageWithContext:context Page:self.drawingPage PageSize:pageSize];
  59. //}
  60. - (void)drawWithBox:(PDFDisplayBox)box toContext:(CGContextRef)context {
  61. [super drawWithBox:box toContext:context];
  62. CGSize pageSize = [self boundsForBox:kPDFDisplayBoxCropBox].size;
  63. [self drawPageWithContext:context Page:self.drawingPage PageSize:pageSize];
  64. }
  65. - (void)drawPageWithContext:(CGContextRef) context
  66. Page:(PDFPage *)page
  67. PageSize:(CGSize)pageSize
  68. {
  69. CGSize originalSize = [page boundsForBox:kPDFDisplayBoxCropBox].size;
  70. //如果page的旋转角度为90,或者270,宽高交换
  71. if (page.rotation%180) {
  72. originalSize = CGSizeMake(originalSize.height, originalSize.width);
  73. }
  74. CGFloat wRatio = pageSize.width/originalSize.width;
  75. CGFloat hRatio = pageSize.height/originalSize.height;
  76. CGFloat ratio = MIN(wRatio, hRatio);
  77. CGContextSaveGState(context);
  78. CGFloat xTransform = (pageSize.width - originalSize.width * ratio)/2;
  79. CGFloat yTransform = (pageSize.height - originalSize.height * ratio)/2;
  80. CGContextTranslateCTM(context, xTransform, yTransform);
  81. CGContextScaleCTM(context, ratio, ratio);
  82. if (@available(macOS 10.12, *)) {
  83. [page drawWithBox:kPDFDisplayBoxCropBox toContext:context];
  84. [page transformContext:context forBox:kPDFDisplayBoxCropBox];
  85. } else {
  86. [NSGraphicsContext saveGraphicsState];
  87. [NSGraphicsContext setCurrentContext:[NSGraphicsContext graphicsContextWithGraphicsPort:context flipped:YES]];
  88. [page drawWithBox:kPDFDisplayBoxCropBox toContext:context];
  89. [NSGraphicsContext restoreGraphicsState];
  90. [page transformContext:context forBox:kPDFDisplayBoxCropBox];
  91. }
  92. CGContextRestoreGState(context);
  93. }
  94. @end
  95. #define MyTableCellViewDataType @"MyTableCellViewDataType"
  96. static NSString * const KMTableColumnFileNameID = @"fileName";
  97. static NSString * const KMTableColumnPageRangeID = @"pageRangD";
  98. static NSString * const KMTableColumnSizeID = @"size";
  99. static KMPDFEditAppendWindow *windowController = nil;
  100. @interface KMPDFEditAppendWindow ()<NSTableViewDataSource,NSTableViewDelegate,NSComboBoxDelegate, KMSelectPopButtonDelegate>
  101. {
  102. BOOL _isSuccessfully;
  103. NSInteger _insertRow;
  104. }
  105. @property (nonatomic, assign) NSInteger insertRow;
  106. @property (weak) IBOutlet NSBox *tableHeaderBox;
  107. @property (weak) IBOutlet NSTextField *fileNameLabel;
  108. @property (weak) IBOutlet NSTextField *pageRangeLabel;
  109. @property (weak) IBOutlet NSTextField *sizeLabel;
  110. @property (weak) IBOutlet NSBox *tableHeaderBottomLine;
  111. @property (assign) IBOutlet NSTableView *tableView;
  112. @property (assign) IBOutlet NSButton *removeButton;
  113. @property (assign) IBOutlet NSButton *appendButton;
  114. @property (assign) IBOutlet NSButton *cancelButton;
  115. //@property (assign) IBOutlet NSButton *addFileButton;
  116. @property (assign) IBOutlet NSBox *clearBox;
  117. @property (assign) IBOutlet NSBox *nMergeBox;
  118. @property (assign) IBOutlet NSBox *nCancelBox;
  119. @property (weak) IBOutlet NSBox *addBox;
  120. @property (nonatomic, strong) KMDesignSelect *addFileButton;
  121. @property (assign) IBOutlet NSProgressIndicator *progress;
  122. //宽输入框
  123. @property (assign) IBOutlet NSTextField *pageSizeWidthTextField;
  124. //宽高输入连接符
  125. @property (assign) IBOutlet NSTextField *pageSizeWidthHeightConnectorTextField;
  126. //高输入框
  127. @property (assign) IBOutlet NSTextField *pageSizeHeightTextField;
  128. //宽高单位
  129. @property (assign) IBOutlet NSTextField *pageSizeUnitLabel;
  130. @property (nonatomic,retain) NSMutableArray *files;
  131. @property (nonatomic,retain) NSArray *addFiles;
  132. @property (nonatomic, retain) NSMutableIndexSet *insertIndexSet;
  133. @property (nonatomic,retain) NSString *password;
  134. @property (nonatomic,retain) PDFDocument *PDFDocument;
  135. @property (nonatomic,assign) KMPDFPageEditType editType;
  136. @property (nonatomic,assign) CGFloat allFileSize;
  137. @property (nonatomic,retain) NSMutableArray *rootPDFOutlineArray;
  138. @property (nonatomic,assign) CGSize newPageSize;
  139. @property (nonatomic,retain) NSMutableArray *lockFilePathArr;
  140. @property (nonatomic,assign) NSInteger lockFileIndex;
  141. @property (weak) IBOutlet NSView *originalSizeButton;
  142. @property (weak) IBOutlet KMImageTitleButton *A4SizeButton;
  143. @property (weak) IBOutlet KMImageTitleButton *A3SizeButton;
  144. @property (weak) IBOutlet KMImageTitleButton *USLetterSizeButton;
  145. @property (weak) IBOutlet KMImageTitleButton *USLegalButton;
  146. @property (weak) IBOutlet KMImageTitleButton *customSizeButton;
  147. @property (assign) IBOutlet NSBox *box;
  148. @property (assign) IBOutlet NSTextField *boxLabel;
  149. @property (assign) IBOutlet KMBlankView *myBlankView;
  150. @property (nonatomic, strong) PasswordWindowController *passwordWindowController;
  151. @property (nonatomic, strong) KMSavePanelAccessoryController *savePanelAccessoryViewController;
  152. @property (nonatomic, assign) BOOL canMerge;
  153. @property (nonatomic, strong) KMDesignButton *clearVC;
  154. @property (nonatomic, strong) KMDesignButton *nMergeVC;
  155. @property (nonatomic, strong) KMDesignButton *nCancelVC;
  156. @property (nonatomic, strong) NSButton *loading_backgroundView;
  157. @end
  158. @implementation KMPDFEditAppendWindow
  159. - (void)dealloc
  160. {
  161. _tableView.delegate = nil;
  162. _tableView.dataSource = nil;
  163. }
  164. //没有打开文档,选择he'bing
  165. - (id)initWithFilePaths:(NSArray *)files
  166. {
  167. if (self = [super initWithWindowNibName:@"KMPDFEditAppendWindow"]) {
  168. self.PDFDocument = [[PDFDocument alloc] init];
  169. self.addFiles = files;
  170. self.editType = KMPDFPageEditMerge;
  171. _lockFilePathArr = [[NSMutableArray alloc] init];
  172. _files = [[NSMutableArray alloc] init];
  173. }
  174. return self;
  175. }
  176. - (id)initWithPDFDocument:(PDFDocument *)document password:(NSString *)password
  177. {
  178. if (self = [super initWithWindowNibName:@"KMPDFEditAppendWindow"]) {
  179. // self.PDFDocument = document;
  180. self.PDFDocument = [[PDFDocument alloc] init];
  181. self.editType = KMPDFPageEditAppend;
  182. _lockFilePathArr = [[NSMutableArray alloc] init];
  183. _files = [[NSMutableArray alloc] init];
  184. KMFileAttribute *file = [[KMFileAttribute alloc] init];
  185. file.myPDFDocument = document;
  186. file.filePath = document.documentURL.path;
  187. if (password && password.length > 0) {
  188. file.password = password;
  189. file.isLocked = YES;
  190. }
  191. [self.files addObject:file];
  192. }
  193. return self;
  194. }
  195. - (void)windowDidLoad {
  196. [super windowDidLoad];
  197. self.window.contentView.wantsLayer = YES;
  198. self.window.appearance = [NSAppearance appearanceNamed:NSAppearanceNameAqua];
  199. self.clearVC = [[KMDesignButton alloc] initWithType: DesignButtonTypeImage];
  200. self.nMergeVC = [[KMDesignButton alloc] initWithType: DesignButtonTypeText];
  201. self.nCancelVC = [[KMDesignButton alloc] initWithType: DesignButtonTypeText];
  202. // self.nMergeVC.button.keyEquivalent = @"\\r";
  203. self.clearBox.fillColor = NSColor.clearColor;
  204. self.clearBox.contentView = self.clearVC.view;
  205. self.nMergeBox.fillColor = NSColor.clearColor;
  206. self.nMergeBox.contentView = self.nMergeVC.view;
  207. self.nCancelBox.fillColor = NSColor.clearColor;
  208. self.nCancelBox.contentView = self.nCancelVC.view;
  209. self.box.cornerRadius = 0;
  210. self.box.fillColor = [NSColor colorWithRed:247/255.f green:248/255.f blue:250/255.f alpha:1.f];
  211. self.window.title = NSLocalizedString(@"Merge PDF Files",nil);
  212. self.pageSizeWidthTextField.enabled = NO;
  213. self.pageSizeHeightTextField.enabled = NO;
  214. self.pageSizeWidthTextField.stringValue = @"595";
  215. self.pageSizeHeightTextField.stringValue = @"841";
  216. self.pageSizeWidthTextField.formatter = [[TextFieldFormatter alloc] init];
  217. self.pageSizeHeightTextField.formatter = [[TextFieldFormatter alloc] init];
  218. NSArray *titles = @[NSLocalizedString(@"Original Size", nil),@"A4",@"A3",
  219. NSLocalizedString(@"U.S.Letter", nil),NSLocalizedString(@"U.S.Legal", nil),NSLocalizedString(@"Custom", nil)];
  220. __weak typeof(self) weakSelf = self;
  221. int i = 0;
  222. for (KMImageTitleButton *button in @[self.originalSizeButton, self.A4SizeButton, self.A3SizeButton,
  223. self.USLetterSizeButton, self.USLegalButton, self.customSizeButton]) {
  224. KMImageTitleButton *radio = [[KMImageTitleButton alloc] init];
  225. [button addSubview:radio];
  226. radio.frame = button.bounds;
  227. radio.autoresizingMask = NSViewWidthSizable | NSViewHeightSizable;
  228. radio.titleLabel.textColor = [NSColor colorWithRed:223/255.f green:225/255.f blue:229/255.f alpha:1.f];
  229. radio.title = titles[i];
  230. if (i == 0) {
  231. radio.state = NSControlStateValueOn;
  232. radio.imageName = @"btn_radio_sel_on";
  233. } else {
  234. radio.state = NSControlStateValueOff;
  235. radio.imageName = @"btn_radio_unsel_on";
  236. }
  237. radio.titleLabel.textColor = [NSColor colorWithRed:223/255.f green:225/255.f blue:229/255.f alpha:1.f];
  238. radio.titleLabel.font = [NSFont fontWithName:@"SFProText-Regular" size:14];
  239. radio.action = ^(KMImageTitleButton *view, NSButton *button) {
  240. [weakSelf buttonClicked_ChooseSize:view];
  241. };
  242. radio.contentButton.tag = i;
  243. i += 1;
  244. }
  245. self.nCancelVC.target = self;
  246. self.nCancelVC.action = @selector(buttonItemClicked_Cancel:);
  247. self.nCancelVC.stringValue = NSLocalizedString(@"Cancel", nil);
  248. [self.nCancelVC buttonWithType:TokenButtonTypeSec size:TokenButtonSizeM height:[[NSLayoutConstraint alloc] init]];
  249. _removeButton.title = NSLocalizedString(@"Remove", nil);
  250. self.clearVC.target = self;
  251. self.clearVC.action = @selector(buttonItemClicked_Clear:);
  252. self.clearVC.image = [NSImage imageNamed:@"icon_btn_clear_false_norm"];
  253. self.clearVC.image_hov = [NSImage imageNamed:@"icon_btn_clear_false_hov"];
  254. self.clearVC.image_act = [NSImage imageNamed:@"icon_btn_clear_false_act"];
  255. self.clearVC.image_disabled = [NSImage imageNamed:@"icon_btn_clear_Disabled_norm"];
  256. [self.clearVC buttonWithType:TokenButtonTypeSec_Icon size:TokenButtonSizeM height:[[NSLayoutConstraint alloc] init]];
  257. [_addFileButton setTitle:NSLocalizedString(@"Add Files", nil)];
  258. self.nMergeVC.target = self;
  259. self.nMergeVC.action = @selector(buttonItemClicked_Append:);
  260. self.nMergeVC.stringValue = NSLocalizedString(@"Merge", nil);
  261. [self.nMergeVC buttonWithType:TokenButtonTypeCta size:TokenButtonSizeM height:[[NSLayoutConstraint alloc] init]];
  262. self.tableView.delegate = self;
  263. self.tableView.dataSource = self;
  264. self.tableView.allowsMultipleSelection = YES;
  265. [_tableView registerForDraggedTypes:[NSArray arrayWithObjects:NSFilenamesPboardType, NSPasteboardTypeString, NSPasteboardTypePDF, MyTableCellViewDataType, nil]];
  266. NSArray *tableHeaderTitles = @[NSLocalizedString(@"File Name", nil),NSLocalizedString(@"Page Range", nil),NSLocalizedString(@"Size", nil)];
  267. i = 0;
  268. for (NSTextField *label in @[self.fileNameLabel, self.pageRangeLabel, self.sizeLabel]) {
  269. label.stringValue = tableHeaderTitles[i];
  270. label.textColor = [NSColor colorWithRed:97/255.f green:100/255.f blue:105/255.f alpha:1.f];
  271. label.font = [NSFont fontWithName:@"SFProText-Regular" size:12];
  272. i += 1;
  273. }
  274. self.tableHeaderBottomLine.fillColor = [NSColor colorWithRed:223/255.f green:225/255.f blue:229/255.f alpha:1.f];
  275. [self.tableView removeTableColumn:self.tableView.tableColumns.firstObject];
  276. NSTableColumn *column1 = [[NSTableColumn alloc] initWithIdentifier:KMTableColumnFileNameID];
  277. column1.width = 446+16;
  278. column1.title = NSLocalizedString(@"File Name", nil);
  279. [self.tableView addTableColumn:column1];
  280. NSTableColumn *column2 = [[NSTableColumn alloc] initWithIdentifier:KMTableColumnPageRangeID];
  281. column2.width = 160+8;
  282. column2.title = NSLocalizedString(@"Page Range", nil);
  283. [self.tableView addTableColumn:column2];
  284. NSTableColumn *column3 = [[NSTableColumn alloc] initWithIdentifier:KMTableColumnSizeID];
  285. column3.width = 139+12;
  286. column3.title = NSLocalizedString(@"Size", nil);
  287. [self.tableView addTableColumn:column3];
  288. self.tableView.rowHeight = 76;
  289. _insertIndexSet = [[NSMutableIndexSet alloc] init];
  290. [self addFiles:_addFiles];
  291. [_progress setHidden:YES];
  292. [self.tableView addSubview:self.myBlankView];
  293. [self.myBlankView mas_makeConstraints:^(MASConstraintMaker *make) {
  294. make.left.top.width.equalTo(self.tableView);
  295. make.height.equalTo(self.tableView).offset(0);
  296. }];
  297. self.myBlankView.wantsLayer = YES;
  298. self.myBlankView.layer.backgroundColor = [NSColor clearColor].CGColor;
  299. self.myBlankView.titleLabel.stringValue = NSLocalizedString(@"Select Files", nil);
  300. self.myBlankView.titleLabel.textColor = [NSColor colorWithRed:97/255.f green:100/255.f blue:105/255.f alpha:1];
  301. self.myBlankView.titleLabel.font = [NSFont fontWithName:@"SFProText-Regular" size:14];
  302. self.myBlankView.secondTitleLabel.stringValue = NSLocalizedString(@"Drop files here or Click “+”. Drag files to reorder as you need. You can also add files or folders via the lower left button.", nil);
  303. self.myBlankView.secondTitleLabel.textColor = [NSColor colorWithRed:148/255.f green:152/255.f blue:156/255.f alpha:1];
  304. self.myBlankView.secondTitleLabel.font = [NSFont fontWithName:@"SFProText-Regular" size:12];
  305. self.myBlankView.imageView.image = [NSImage imageNamed:@"KMImageNameEmptyAddNormal"];
  306. self.myBlankView.allowedFileTypes = [KMImageAccessoryController supportedImageTypes];
  307. self.myBlankView.mouseActionCallBack = ^(KMBlankViewMouseEventType mouseType) {
  308. if (mouseType == KMBlankViewMouseEventType_MouseEnter) {
  309. weakSelf.myBlankView.imageView.image = [NSImage imageNamed:@"KMImageNameEmptyAddHover"];
  310. } else if (mouseType == KMBlankViewMouseEventType_MouseExit) {
  311. weakSelf.myBlankView.imageView.image = [NSImage imageNamed:@"KMImageNameEmptyAddNormal"];
  312. } else if (mouseType == KMBlankViewMouseEventType_MouseDown) {
  313. weakSelf.myBlankView.imageView.image = [NSImage imageNamed:@"KMImageNameEmptyAddHover"];
  314. [weakSelf itemAddFileAction];
  315. } else if (mouseType == KMBlankViewMouseEventType_MouseUp) {
  316. weakSelf.myBlankView.imageView.image = [NSImage imageNamed:@"KMImageNameEmptyAddHover"];
  317. }
  318. };
  319. self.myBlankView.dragSuccessBlock = ^(NSMutableArray *fileNames) {
  320. NSMutableArray *array = [NSMutableArray array];
  321. for(NSString *path in fileNames){
  322. if (![weakSelf isExistAtFilepath:path]) {
  323. continue;
  324. }
  325. NSDictionary *attrib = [[NSFileManager defaultManager] attributesOfItemAtPath:path error:nil];
  326. CGFloat fileSize = [[attrib objectForKey:NSFileSize] floatValue];
  327. weakSelf.allFileSize = weakSelf.allFileSize + fileSize;
  328. // weakSelf.allFileSize = weakSelf.allFileSize - fileSize;
  329. [array addObject:path];
  330. }
  331. [weakSelf addFiles:array];
  332. };
  333. [self updateButtonState];
  334. id outputBookMarkObject = [[NSUserDefaults standardUserDefaults] objectForKey:@"kmAppendOutputPath"];
  335. if ([outputBookMarkObject isKindOfClass:[NSData class]]) {
  336. NSData* outputBookMarkData = (NSData*)outputBookMarkObject;
  337. NSURL *bookmarkedURL = [NSURL URLByResolvingBookmarkData:outputBookMarkData options:NSURLBookmarkResolutionWithSecurityScope relativeToURL:nil bookmarkDataIsStale:nil error:nil];
  338. [bookmarkedURL startAccessingSecurityScopedResource];
  339. if (bookmarkedURL) {
  340. // _outputText.stringValue = [bookmarkedURL path];
  341. }
  342. }
  343. self.boxLabel.stringValue = NSLocalizedString(@"Page size:", nil);
  344. self.boxLabel.textColor = [NSColor colorWithRed:37/255.f green:38/255.f blue:41/255.f alpha:1.f];
  345. self.boxLabel.font = [NSFont fontWithName:@"SFProText-Regular" size:16];
  346. self.addFileButton = [[KMDesignSelect alloc] initWithType:1];
  347. self.addBox.fillColor = [NSColor clearColor];
  348. self.addBox.contentView = self.addFileButton.view;
  349. [self.addFileButton removeAllItems];
  350. [self.addFileButton addItemsWithObjectValues:@[NSLocalizedString(@"Add Files", ""),NSLocalizedString(@"Add Folder", ""),NSLocalizedString(@"Add Open Files", "")]];
  351. [self.addFileButton selectItemAt:0];
  352. self.addFileButton.editable = false; //默认属性,初始化可不设置
  353. self.addFileButton.delete = self;
  354. [self.addFileButton updateUI];
  355. self.loading_backgroundView = [[NSButton alloc] init];
  356. [self.window.contentView addSubview:self.loading_backgroundView];
  357. self.loading_backgroundView.frame = self.window.contentView.bounds;
  358. self.loading_backgroundView.autoresizingMask = NSViewWidthSizable | NSViewHeightSizable;
  359. self.loading_backgroundView.title = @"";
  360. self.loading_backgroundView.bordered = NO;
  361. self.loading_backgroundView.wantsLayer = YES;
  362. self.loading_backgroundView.layer.backgroundColor = [NSColor clearColor].CGColor;
  363. self.loading_backgroundView.hidden = YES;
  364. }
  365. - (void)updateButtonState
  366. {
  367. if (_files.count > 0) {
  368. self.myBlankView.hidden = YES;
  369. self.tableHeaderBox.hidden = NO;
  370. [_removeButton setEnabled:YES];
  371. [self.clearVC setEnabled:YES];
  372. if (self.files.count > 1) {
  373. [self.nMergeVC setEnabled:YES];
  374. self.canMerge = YES;
  375. } else {
  376. self.canMerge = NO;
  377. [self.nMergeVC setEnabled:NO];
  378. }
  379. } else {
  380. self.myBlankView.hidden = NO;
  381. self.tableHeaderBox.hidden = YES;
  382. [_removeButton setEnabled:NO];
  383. [self.clearVC setEnabled:NO];
  384. self.canMerge = NO;
  385. [self.nMergeVC setEnabled:NO];
  386. }
  387. }
  388. #pragma mark -NSTextFieldDelegate
  389. - (void)controlTextDidChange:(NSNotification *)obj
  390. {
  391. NSTextField *textField = (NSTextField*)[obj object];
  392. NSInteger index = textField.tag;
  393. [[_files objectAtIndex:index] setPagesString:textField.stringValue];
  394. }
  395. #pragma mark Show Methods
  396. - (void)didEndSheet:(NSWindow *)sheet returnCode:(NSInteger)returnCode contextInfo:(void *)contextInfo {
  397. if (contextInfo != NULL) {
  398. void (^handler)(BOOL isSuccessfully ,NSIndexSet * selectedIndexSet) = CFBridgingRelease(contextInfo);
  399. handler(_isSuccessfully,self.insertIndexSet);
  400. }
  401. windowController = nil;
  402. }
  403. - (void)beginSheetModalForWindow:(NSWindow *)window completionHandler:(void (^)(BOOL isSuccessfully,NSIndexSet * selectedIndexSet))handler
  404. {
  405. if (windowController.window.isSheet) {
  406. #if DEBUG
  407. NSLog(@"合并窗口已显示");
  408. #endif
  409. return;
  410. }
  411. windowController = self;
  412. [NSApp beginSheet:[self window]
  413. modalForWindow:window
  414. modalDelegate:self
  415. didEndSelector:@selector(didEndSheet:returnCode:contextInfo:)
  416. contextInfo:handler ? CFBridgingRetain(handler) : NULL];
  417. }
  418. #pragma mark Private methor
  419. - (void)viewFileAtFinder:(NSString *)filePath
  420. {
  421. NSWorkspace *workspace = [NSWorkspace sharedWorkspace];
  422. NSURL *url = [NSURL fileURLWithPath:filePath];
  423. [workspace activateFileViewerSelectingURLs:[NSArray arrayWithObject:url]];
  424. }
  425. - (void)addFiles:(NSArray*)files
  426. {
  427. [_lockFilePathArr removeAllObjects];
  428. _lockFileIndex = 0;
  429. for (NSString *filePath in files) {
  430. if ([filePath.pathExtension.lowercaseString isEqualToString:@"pdf"]) {
  431. KMFileAttribute *file = [[KMFileAttribute alloc] init];
  432. file.filePath = filePath;
  433. PDFDocument *document = [[PDFDocument alloc] initWithURL:[NSURL fileURLWithPath:filePath]];
  434. NSDictionary *attrib = [[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:nil];
  435. self.allFileSize = [[attrib objectForKey:NSFileSize] floatValue];
  436. if ([document isLocked]) {
  437. [_lockFilePathArr addObject:filePath];
  438. } else if ([KMTools hasPermissionsLimit:document]) {
  439. [_lockFilePathArr addObject:filePath];
  440. } else {
  441. if ([NSDocument isDamageWithUrl:[NSURL fileURLWithPath:filePath]]) {
  442. NSAlert *alert = [[NSAlert alloc] init];
  443. alert.messageText = NSLocalizedString(@"The file was not added successfully, please select the file again.", @"");
  444. [alert runModal];
  445. } else {
  446. [_files addObject:file];
  447. }
  448. }
  449. } else {
  450. KMFileAttribute *file = [[KMFileAttribute alloc] init];
  451. file.filePath = filePath;
  452. PDFDocument *document = [[PDFDocument alloc] init];
  453. NSImage *image = [[NSImage alloc] initWithContentsOfFile:filePath];
  454. if (image == nil) {
  455. continue;
  456. }
  457. // [document insertPage:image.size withImage:filePath atIndex:0];
  458. [document insertPage:[[PDFPage alloc] initWithImage:image] atIndex:0];
  459. file.myPDFDocument = document;
  460. NSDictionary *attrib = [[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:nil];
  461. self.allFileSize = [[attrib objectForKey:NSFileSize] floatValue];
  462. if ([NSDocument isDamageWithUrl:[NSURL fileURLWithPath:filePath]]) {
  463. NSAlert *alert = [[NSAlert alloc] init];
  464. alert.messageText = NSLocalizedString(@"The file was not added successfully, please select the file again.", @"");
  465. [alert runModal];
  466. } else {
  467. [_files addObject:file];
  468. }
  469. }
  470. }
  471. [self updateButtonState];
  472. [_tableView reloadData];
  473. if ([_lockFilePathArr count]) {
  474. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  475. if (self.files.count == 0) {
  476. self.insertRow = 0;
  477. } else {
  478. self.insertRow = self.files.count;
  479. }
  480. [self openPasswordWindow];
  481. });
  482. }
  483. }
  484. - (BOOL)isExistAtFilepath:(NSString *)filePath
  485. {
  486. NSMutableArray *array = [NSMutableArray array];
  487. for (KMFileAttribute *file in self.files) {
  488. [array addObject:file.filePath];
  489. }
  490. if ([array containsObject:filePath]) {
  491. return NO;
  492. }
  493. return YES;
  494. }
  495. -(void)openPasswordWindow
  496. {
  497. if ([_lockFilePathArr count] > _lockFileIndex) {
  498. NSString *filePath = [_lockFilePathArr objectAtIndex:_lockFileIndex];
  499. __weak typeof(self) weakSelf = self;
  500. [KMPasswordInputWindow openWindowWithWindow:self.window type:KMPasswordInputWindowTypeOwner url:[NSURL fileURLWithPath:filePath] callback:^(enum KMPasswordInputWindowResult result, NSString * _Nullable password) {
  501. if (password.length > 0) {
  502. KMFileAttribute *file = [[KMFileAttribute alloc] init];
  503. file.filePath = filePath;
  504. file.isLocked = YES;
  505. file.password = password;
  506. [weakSelf.files insertObject:file atIndex:weakSelf.insertRow];
  507. weakSelf.insertRow ++;
  508. }
  509. weakSelf.lockFileIndex++;
  510. // if ([self.lockFilePathArr count] > self.lockFileIndex) {
  511. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  512. [weakSelf openPasswordWindow];
  513. });
  514. // } else {
  515. [weakSelf.tableView reloadData];
  516. [weakSelf updateButtonState];
  517. // }
  518. }];
  519. }
  520. }
  521. - (void)comboBoxAction:(NSInteger)itemIndex atRow:(NSInteger)row
  522. {
  523. NSInteger index = row;
  524. // 数组不存在,或者索引超出数组长度
  525. if ((!_files) || (index > ((NSInteger)[_files count] - 1))) {
  526. return;
  527. }
  528. if (itemIndex == 0) {
  529. KMFileAttribute *currentFile = [_files objectAtIndex:index];
  530. [self.window makeFirstResponder:self];
  531. [currentFile setBAllPage:YES];
  532. currentFile.pagesType = KMPDFSeleectPageType_AllPages;
  533. } else if (itemIndex == 1) {
  534. [self.window makeFirstResponder:self];
  535. KMFileAttribute *currentFile = [_files objectAtIndex:index];
  536. currentFile.pagesType = KMPDFSeleectPageType_OnlyOdd;
  537. [currentFile setBAllPage:NO];
  538. PDFDocument *tDocument = currentFile.myPDFDocument;
  539. if (tDocument == nil) {
  540. tDocument = [[PDFDocument alloc] initWithURL:[NSURL fileURLWithPath:currentFile.filePath]];
  541. }
  542. if ([currentFile isLocked]) {
  543. [tDocument unlockWithPassword:currentFile.password];
  544. }
  545. NSString *tPagesString = @"";
  546. for (NSInteger i = 0; i < tDocument.pageCount; i++) {
  547. if (i%2 == 0) {
  548. if (tPagesString.length == 0) {
  549. tPagesString = [tPagesString stringByAppendingString:[NSString stringWithFormat:@"%ld",i +1]];
  550. }else{
  551. tPagesString = [tPagesString stringByAppendingString:[NSString stringWithFormat:@",%ld",i +1]];
  552. }
  553. }
  554. }
  555. currentFile.pagesString = tPagesString;
  556. } else if (itemIndex == 2) {
  557. [self.window makeFirstResponder:self];
  558. KMFileAttribute *currentFile = [_files objectAtIndex:index];
  559. currentFile.pagesType = KMPDFSeleectPageType_OnlyEven;
  560. [currentFile setBAllPage:NO];
  561. NSString *tPagesString = @"";
  562. PDFDocument *tDocument = currentFile.myPDFDocument;
  563. if (tDocument == nil) {
  564. tDocument = [[PDFDocument alloc] initWithURL:[NSURL fileURLWithPath:currentFile.filePath]];
  565. }
  566. if ([currentFile isLocked]) {
  567. [tDocument unlockWithPassword:currentFile.password];
  568. }
  569. for (NSInteger i = 0; i <tDocument.pageCount; i++) {
  570. if (i%2) {
  571. if (tPagesString.length == 0) {
  572. tPagesString = [tPagesString stringByAppendingString:[NSString stringWithFormat:@"%ld",i +1]];
  573. }else{
  574. tPagesString = [tPagesString stringByAppendingString:[NSString stringWithFormat:@",%ld",i +1]];
  575. }
  576. }
  577. }
  578. currentFile.pagesString = tPagesString;
  579. } else {
  580. KMFileAttribute *currentFile = [_files objectAtIndex:index];
  581. currentFile.pagesType = KMPDFSeleectPageType_PagesString;
  582. [currentFile setBAllPage:NO];
  583. }
  584. }
  585. - (void)handerReDraw
  586. {
  587. //如果选择的是原始尺寸,不处理
  588. KMImageTitleButton *originalSizeButton = self.originalSizeButton.subviews.lastObject;
  589. if (originalSizeButton.state == NSControlStateValueOn) {
  590. }else{
  591. //重新选择了界面尺寸,那么就要重绘
  592. if (self.newPageSize.width < 0) {
  593. NSBeep();
  594. return;
  595. }else{
  596. NSMutableArray *pagesArray = [NSMutableArray array];
  597. NSInteger pageCount = self.PDFDocument.pageCount;
  598. for (NSInteger i = 0; i < pageCount; i ++) {
  599. [pagesArray addObject:[self.PDFDocument pageAtIndex:0]];
  600. [self.PDFDocument removePageAtIndex:0];
  601. }
  602. for (NSInteger i = 0; i < pageCount; i ++) {
  603. KMAppendPDFPage *page = [[KMAppendPDFPage alloc] init];
  604. [page setBounds:NSMakeRect(0, 0, self.newPageSize.width, self.newPageSize.height) forBox:kPDFDisplayBoxMediaBox];
  605. page.drawingPage = pagesArray[i];
  606. // [self.PDFDocument insertPageObject:page atIndex:i];
  607. [self.PDFDocument insertPage:page atIndex:i];
  608. }
  609. //如果是自定义大小,删除所有的outline,因为合并出来的outline是无效的
  610. if (self.PDFDocument.outlineRoot) {
  611. NSInteger childCount = [self.PDFDocument.outlineRoot numberOfChildren];
  612. NSMutableArray *PDFOutlineArray = [NSMutableArray array];
  613. for (NSInteger i = 0 ; i < childCount; i ++) {
  614. [PDFOutlineArray addObject:[self.PDFDocument.outlineRoot childAtIndex:i]];
  615. }
  616. for (PDFOutline *deleteOutline in PDFOutlineArray) {
  617. [deleteOutline removeFromParent];
  618. }
  619. }
  620. }
  621. }
  622. }
  623. - (BOOL)hasOpenFiles {
  624. NSArray *array = [KMTools getOpenDocumentURLs];
  625. if (array.count == 0) {
  626. return NO;
  627. }
  628. for (NSURL *fileURL in array) {
  629. if ([self isExistAtFilepath:fileURL.path]) {
  630. return YES;
  631. }
  632. }
  633. return NO;
  634. }
  635. #pragma mark - KMSelectPopButtonDelegate
  636. - (void)km_comboBoxSelectionDidChange:(KMDesignSelect *)obj {
  637. if ([self.addFileButton isEqual:obj]) {
  638. NSInteger index = self.addFileButton.indexOfSelectedItem;
  639. if (index < 0) {
  640. index = 0;
  641. }
  642. if (index == 0) {
  643. [self itemAddFileAction];
  644. } else if (index == 1) {
  645. [self itemAddFolderAction];
  646. } else if (index == 2) {
  647. [self itemAddOpenFileAction];
  648. }
  649. }
  650. }
  651. - (void)km_controlTextDidChange:(KMDesignSelect *)obj {
  652. }
  653. - (void)km_controlTextDidEndEditing:(KMDesignSelect *)obj {
  654. }
  655. - (void)km_SelectPopoverWillShow:(KMDesignSelect *)obj {
  656. if ([self hasOpenFiles]) {
  657. obj.disItems = @[];
  658. } else {
  659. obj.disItems = @[NSLocalizedString(@"Add Open Files", "")];
  660. }
  661. }
  662. #pragma mark tabelView
  663. - (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView
  664. {
  665. return _files.count;
  666. }
  667. - (nullable id)tableView:(NSTableView *)tableView viewForTableColumn:(nullable NSTableColumn *)tableColumn row:(NSInteger)row
  668. {
  669. KMFileAttribute *fileAttribute = [_files objectAtIndex:row];
  670. PDFDocument *pdf = fileAttribute.myPDFDocument;
  671. if (pdf == nil) {
  672. pdf = [[PDFDocument alloc] initWithURL:[NSURL fileURLWithPath:fileAttribute.filePath]];
  673. }
  674. if ([pdf isLocked]) {
  675. [pdf unlockWithPassword:fileAttribute.password];
  676. }
  677. if (tableColumn.identifier == KMTableColumnPageRangeID) {
  678. KMPDFMergePageRangeTabelViewCell *cell = [tableView makeViewWithIdentifier:tableColumn.identifier owner:self];
  679. if (cell == nil) {
  680. cell = [[KMPDFMergePageRangeTabelViewCell alloc] init];
  681. }
  682. cell.wantsLayer = YES;
  683. cell.layer.backgroundColor = [NSColor clearColor].CGColor;
  684. cell.pageCount = pdf.pageCount;
  685. [cell updatePageRange:fileAttribute.pagesType pageStrings:fileAttribute.pagesString];
  686. __weak typeof(self) weakSelf = self;
  687. cell.callback = ^(NSInteger index) {
  688. [weakSelf comboBoxAction:index atRow:row];
  689. };
  690. cell.textDidChange = ^(NSString * _Nonnull string) {
  691. if (row >= weakSelf.files.count) {
  692. return;
  693. }
  694. [[weakSelf.files objectAtIndex:row] setPagesString:string];
  695. };
  696. return cell;
  697. }
  698. if (tableColumn.identifier == KMTableColumnSizeID) {
  699. KMPDFMergeSizeTabelViewCell *cell = [tableView makeViewWithIdentifier:tableColumn.identifier owner:self];
  700. if (cell == nil) {
  701. cell = [[KMPDFMergeSizeTabelViewCell alloc] init];
  702. }
  703. NSString *filePath = pdf.documentURL.path;
  704. if (!filePath) {
  705. filePath = fileAttribute.filePath;
  706. }
  707. NSDictionary *attributesDic = [[NSFileManager defaultManager]attributesOfItemAtPath:filePath error:nil];
  708. float size = [[attributesDic objectForKey:NSFileSize] floatValue]/1024;
  709. cell.label.stringValue = [NSString stringWithFormat:@"%0.1f %c", truesize(size) , moniker(size)];
  710. cell.button.hidden = YES;
  711. __weak typeof(self) weakSelf = self;
  712. cell.callback = ^{
  713. NSDictionary *attrib = [[NSFileManager defaultManager] attributesOfItemAtPath:fileAttribute.filePath error:nil];
  714. CGFloat fileSize = [[attrib objectForKey:NSFileSize] floatValue];
  715. weakSelf.allFileSize = weakSelf.allFileSize - fileSize;
  716. [weakSelf.files removeObject:fileAttribute];
  717. [weakSelf.tableView reloadData];
  718. [weakSelf updateButtonState];
  719. };
  720. return cell;
  721. }
  722. KMPDFMergeFileNameTabelViewCell *cell = [tableView makeViewWithIdentifier:@"fileName" owner:self];
  723. if (cell == nil) {
  724. cell = [[KMPDFMergeFileNameTabelViewCell alloc] init];
  725. }
  726. cell.numberLabel.stringValue = [NSString stringWithFormat:@"%ld",(row + 1)];;
  727. cell.fileNameLabel.stringValue = [fileAttribute.filePath lastPathComponent];
  728. cell.pageNumberLabel.stringValue = [NSString stringWithFormat:@"%ld %@",pdf.pageCount,NSLocalizedString(@"Pages", nil)];;
  729. // cell.iconImageView.image = [[pdf pageAtIndex:0] thumbnailWithSize:60*2 forBox:kPDFDisplayBoxCropBox];
  730. if (pdf.documentURL) {
  731. cell.iconImageView.image = [NSImage previewForFileWithPath:pdf.documentURL ofSize:NSMakeSize(120, 120) asIcon:YES];
  732. } else if (fileAttribute.filePath) {
  733. // if ([fileAttribute.filePath.pathExtension.lowercaseString isEqualToString:@"pdf"]) {
  734. cell.iconImageView.image = [NSImage previewForFileWithPath:[NSURL fileURLWithPath:fileAttribute.filePath] ofSize:NSMakeSize(120, 120) asIcon:YES];
  735. // } else {
  736. // cell.iconImageView.image = [NSImage previewForFileWithPath:fileAttribute.filePath ofSize:NSMakeSize(120, 120) asIcon:YES];
  737. // }
  738. } else {
  739. cell.iconImageView.image = nil;
  740. }
  741. return cell;
  742. }
  743. - (BOOL)tableView:(NSTableView *)tableView shouldSelectTableColumn:(NSTableColumn *)tableColumn
  744. {
  745. return NO;
  746. }
  747. #pragma mark -NSTableViewDelegate
  748. - (BOOL)tableView:(NSTableView *)tv writeRowsWithIndexes:(NSIndexSet *)rowIndexes toPasteboard:(NSPasteboard*)pboard
  749. {
  750. // Copy the row numbers to the pasteboard.
  751. NSData *zNSIndexSetData = [NSKeyedArchiver archivedDataWithRootObject:rowIndexes];
  752. [pboard declareTypes:[NSArray arrayWithObject:MyTableCellViewDataType] owner:self];
  753. [pboard setData:zNSIndexSetData forType:MyTableCellViewDataType];
  754. return YES;
  755. }
  756. - (NSDragOperation)tableView:(NSTableView *)tableView validateDrop:(id <NSDraggingInfo>)info proposedRow:(NSInteger)row proposedDropOperation:(NSTableViewDropOperation)dropOperation
  757. {
  758. if (dropOperation == NSTableViewDropOn) {
  759. return NSDragOperationNone;
  760. }
  761. BOOL isCanDrag = NO;
  762. NSDragOperation result = NSDragOperationNone;
  763. NSPasteboard *pboard = [info draggingPasteboard];
  764. if ([pboard availableTypeFromArray:[NSArray arrayWithObject:NSFilenamesPboardType]])
  765. {
  766. NSArray *fileNames = [pboard propertyListForType:NSFilenamesPboardType];
  767. for (NSString* path in fileNames) {
  768. if ([[KMImageAccessoryController supportedImageTypes] containsObject:path.pathExtension.lowercaseString]) {
  769. isCanDrag = YES;
  770. }else{
  771. isCanDrag = NO;
  772. break;
  773. }
  774. }
  775. }else if ([pboard availableTypeFromArray:[NSArray arrayWithObject:MyTableCellViewDataType]]){
  776. result = NSDragOperationEvery;
  777. }
  778. if (isCanDrag) {
  779. result = NSDragOperationCopy;
  780. }
  781. return result;
  782. }
  783. - (BOOL)tableView:(NSTableView *)tableView acceptDrop:(id <NSDraggingInfo>)info row:(NSInteger)row dropOperation:(NSTableViewDropOperation)dropOperation
  784. {
  785. BOOL result = NO;
  786. NSPasteboard *pboard = [info draggingPasteboard];
  787. _insertRow = row;
  788. if ([pboard availableTypeFromArray:[NSArray arrayWithObject:NSFilenamesPboardType]])
  789. {
  790. NSMutableArray *fileNames = [pboard propertyListForType:NSFilenamesPboardType];
  791. NSMutableArray *array = [NSMutableArray array];
  792. for(NSString *path in fileNames){
  793. if (![self isExistAtFilepath:path]) {
  794. continue;
  795. }
  796. NSDictionary *attrib = [[NSFileManager defaultManager] attributesOfItemAtPath:path error:nil];
  797. CGFloat fileSize = [[attrib objectForKey:NSFileSize] floatValue];
  798. self.allFileSize = self.allFileSize + fileSize;
  799. [array addObject:path];
  800. }
  801. [self addFiles:array];
  802. result = YES;
  803. } else if ([pboard availableTypeFromArray:[NSArray arrayWithObject:MyTableCellViewDataType]]){
  804. NSData* rowData = [pboard dataForType:MyTableCellViewDataType];
  805. NSIndexSet* rowIndexes = [NSKeyedUnarchiver unarchiveObjectWithData:rowData];
  806. NSMutableArray *moveArray = [[NSMutableArray alloc] init];
  807. NSMutableArray *allPhoto = [_files mutableCopy];
  808. NSInteger index = 0;
  809. [rowIndexes enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL * _Nonnull stop) {
  810. KMFileAttribute * file = allPhoto[idx];
  811. [moveArray addObject:file];
  812. [_files removeObject:file];
  813. }];
  814. if (row > 0) {
  815. KMFileAttribute * indexFile = allPhoto[row -1];
  816. while([moveArray containsObject:indexFile]){
  817. row --;
  818. if ((row < 0)) {
  819. indexFile = nil;
  820. break;
  821. } else {
  822. indexFile = allPhoto[row];
  823. }
  824. }
  825. if(indexFile){
  826. index = [_files indexOfObject:indexFile]+1;
  827. }
  828. }
  829. for (NSInteger i = 0; i< moveArray.count; i++) {
  830. [_files insertObject:moveArray[i] atIndex:(index +i)];
  831. }
  832. [_tableView reloadData];
  833. result = YES;
  834. // [moveArray release];
  835. // [allPhoto release];
  836. } else {
  837. result = NO;
  838. }
  839. return result;
  840. }
  841. - (NSTableRowView *)tableView:(NSTableView *)tableView rowViewForRow:(NSInteger)row {
  842. KMBatchTableRowView *rowView = [[KMBatchTableRowView alloc] init];
  843. rowView.backgroundView.layer.backgroundColor = [NSColor colorWithRed:237/255.f green:238/255.f blue:240/255.f alpha:1.f].CGColor;
  844. rowView.backgroundView.layer.cornerRadius = 4.f;
  845. rowView.selectionInset = NSEdgeInsetsMake(0, 16, 0, 16);
  846. rowView.selectionRadius = 4.f;
  847. rowView.selectionBackgroundColorBlock = ^NSColor * _Nonnull{
  848. return [NSColor colorWithRed:206/255.f green:208/255.f blue:212/255.f alpha:0.6f];
  849. };
  850. return rowView;
  851. }
  852. - (void)tableViewSelectionDidChange:(NSNotification *)notification {
  853. }
  854. #pragma mark Buutton Action
  855. - (IBAction)buttonItemClicked_Cancel:(id)sender
  856. {
  857. _isSuccessfully = NO;
  858. [NSApp endSheet:[self window] returnCode:[(NSView *)sender tag]];
  859. [[self window] orderOut:self];
  860. }
  861. - (IBAction)buttonItemClicked_Append:(id)sender
  862. {
  863. if (!self.canMerge) {
  864. return;
  865. }
  866. KMImageTitleButton *customSizeButton = self.customSizeButton.subviews.lastObject;
  867. if (customSizeButton.state == NSControlStateValueOn) {
  868. NSInteger widthFloat = [self.pageSizeWidthTextField.stringValue integerValue];
  869. NSInteger heightFloat = [self.pageSizeHeightTextField.stringValue integerValue];
  870. NSString *widthString = [NSString stringWithFormat:@"%ld",widthFloat];
  871. NSString *heightString = [NSString stringWithFormat:@"%ld",heightFloat];
  872. if (![widthString isEqualToString:self.pageSizeWidthTextField.stringValue] ||![heightString isEqualToString:self.pageSizeHeightTextField.stringValue]||widthFloat <= 0 ||heightFloat <= 0 ) {
  873. NSBeep();
  874. return;
  875. }else{
  876. self.newPageSize = CGSizeMake(widthFloat, heightFloat);
  877. }
  878. }
  879. //初始化数组
  880. if (!self.rootPDFOutlineArray) {
  881. self.rootPDFOutlineArray = [NSMutableArray new];
  882. }else{
  883. [self.rootPDFOutlineArray removeAllObjects];
  884. }
  885. //如果是合并,文件路径不存在,或者文件个数小于等于1 return
  886. if (_editType == KMPDFPageEditMerge) {
  887. if (_files.count <= 1 ) {
  888. NSAlert *alert = [[NSAlert alloc] init];
  889. [alert setAlertStyle:NSAlertStyleCritical];
  890. [alert setMessageText:NSLocalizedString(@"To start merging, please select at least 2 files.", nil)];
  891. [alert runModal];
  892. return;
  893. }
  894. }
  895. _isSuccessfully = NO;
  896. [_progress setHidden:NO];
  897. [self.nCancelVC setEnabled:NO];
  898. self.canMerge = NO;
  899. [_progress startAnimation:nil];
  900. self.loading_backgroundView.hidden = NO;
  901. BOOL allPage = YES;//只有是全部才支持大纲的合并
  902. for (KMFileAttribute *file in _files) {
  903. if (!file.selectPages) {
  904. dispatch_async(dispatch_get_main_queue(), ^{
  905. NSAlert *alert = [[NSAlert alloc] init];
  906. [alert setAlertStyle:NSAlertStyleCritical];
  907. [alert setMessageText:[NSString stringWithFormat:@"%@ %@",[file.filePath lastPathComponent],NSLocalizedString(@"Invalid page range or the page number is out of range. Please try again.", nil)]];
  908. [alert runModal];
  909. [self->_progress setHidden:YES];
  910. [self.nCancelVC setEnabled:YES];
  911. self.canMerge = YES;
  912. self.loading_backgroundView.hidden = YES;
  913. });
  914. return;
  915. }
  916. }
  917. for(KMFileAttribute *file in _files){
  918. //只要有一个文件不是全部page,那么就不合并大纲
  919. if (allPage == YES) {
  920. allPage = file.bAllPage;
  921. }
  922. //通过路径获取文件
  923. PDFDocument *tdocument = file.myPDFDocument;
  924. if (tdocument == nil) {
  925. tdocument = [[PDFDocument alloc] initWithURL:[NSURL fileURLWithPath:[file filePath]]];
  926. file.myPDFDocument = tdocument;
  927. }
  928. //所有的outline数组
  929. NSMutableArray *PDFOutlineArr = [NSMutableArray new];
  930. //需要解锁
  931. if (file.isLocked) {
  932. [tdocument unlockWithPassword:file.password];
  933. }
  934. //如果根存在,那么遍历出所有的outline,添加到数组中,如果不存在,那么就创建一个根
  935. if (tdocument.outlineRoot) {
  936. [self.rootPDFOutlineArray addObject:tdocument.outlineRoot];
  937. [self fetchAllOfChildren:tdocument.outlineRoot containerArray:PDFOutlineArr];
  938. [PDFOutlineArr removeObject:tdocument.outlineRoot];
  939. } else {
  940. PDFOutline *rootOutline = [[PDFOutline alloc] init];
  941. tdocument.outlineRoot = rootOutline;
  942. if (tdocument.outlineRoot) {
  943. [self.rootPDFOutlineArray addObject:tdocument.outlineRoot];
  944. }
  945. }
  946. // for (CPDFOutline *tOutline in PDFOutlineArr) {
  947. //这段代码主要是调用他的getter方法;outline显示正确
  948. // [tOutline.destination.page dataRepresentation];
  949. // }
  950. for (NSNumber *number in file.selectPages) {
  951. PDFPage *page = [tdocument pageAtIndex:[number integerValue]-1];
  952. [self.PDFDocument insertPage:page atIndex:[self.PDFDocument pageCount]];
  953. // [self.PDFDocument insertPageObject:page atIndex:self.PDFDocument.pageCount];
  954. [self.insertIndexSet addIndex:(self.PDFDocument.pageCount - 1)];
  955. }
  956. _isSuccessfully = YES;
  957. }
  958. KMFileAttribute *file = self.files.firstObject;
  959. NSString *fileName = file.filePath.stringByDeletingPathExtension.lastPathComponent;
  960. fileName = [NSString stringWithFormat:@"%@_Merged", fileName];
  961. if (_editType == KMPDFPageEditMerge) {
  962. _isSuccessfully = NO;
  963. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  964. PDFOutline *rootline = [[PDFOutline alloc] init];
  965. self.PDFDocument.outlineRoot = rootline;
  966. if (allPage) {
  967. NSInteger insertIndex = 0;
  968. for (NSUInteger i = 0; i < self.rootPDFOutlineArray.count; i++) {
  969. PDFOutline *rootPDFOutline = self->_rootPDFOutlineArray[i];
  970. for (NSUInteger j = 0; j < rootPDFOutline.numberOfChildren; j ++) {
  971. [self.PDFDocument.outlineRoot insertChild:[rootPDFOutline childAtIndex:j] atIndex: insertIndex];
  972. insertIndex ++;
  973. }
  974. }
  975. }
  976. dispatch_async(dispatch_get_main_queue(), ^{
  977. // [self handerReDraw];
  978. KMSavePanelAccessoryController *savePanelAccessoryViewController = [[KMSavePanelAccessoryController alloc] init];
  979. NSSavePanel *savePanel = [NSSavePanel savePanel];
  980. savePanel.nameFieldStringValue = fileName;
  981. savePanel.allowedFileTypes = @[@"pdf"];
  982. savePanel.accessoryView = savePanelAccessoryViewController.view;
  983. self.savePanelAccessoryViewController = savePanelAccessoryViewController;
  984. [savePanel beginSheetModalForWindow:self.window completionHandler:^(NSInteger result) {
  985. if (result) {
  986. NSString * outputSavePanel = savePanel.URL.path;
  987. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  988. self->_isSuccessfully = [self.PDFDocument writeToFile:outputSavePanel];
  989. if (!self->_isSuccessfully) {
  990. self->_isSuccessfully = [self.PDFDocument.dataRepresentation writeToFile:outputSavePanel atomically:YES];
  991. }
  992. dispatch_async(dispatch_get_main_queue(), ^{
  993. [self->_progress setHidden:YES];
  994. [self.nCancelVC setEnabled:YES];
  995. self.canMerge = YES;
  996. self.loading_backgroundView.hidden = YES;
  997. if (self->_isSuccessfully) {
  998. [NSApp endSheet:[self window] returnCode:[(NSView *)sender tag]];
  999. [[self window] orderOut:self];
  1000. if (self.saveAsPDFFilePath) {
  1001. BOOL autoOpen = self.savePanelAccessoryViewController.needOpen;
  1002. if (autoOpen) {
  1003. self.saveAsPDFFilePath(outputSavePanel);
  1004. self.saveAsPDFFilePath = nil;
  1005. } else {
  1006. [self viewFileAtFinder:outputSavePanel];
  1007. }
  1008. } else {
  1009. BOOL autoOpen = self.savePanelAccessoryViewController.needOpen;
  1010. if (autoOpen) {
  1011. [[NSDocumentController sharedDocumentController] openDocumentWithContentsOfURL:[NSURL fileURLWithPath:outputSavePanel] display:YES completionHandler:^(NSDocument * _Nullable document, BOOL documentWasAlreadyOpen, NSError * _Nullable error) {
  1012. }];
  1013. } else {
  1014. [self viewFileAtFinder:outputSavePanel];
  1015. }
  1016. }
  1017. } else {
  1018. NSAlert *alert = [[NSAlert alloc] init];
  1019. [alert setAlertStyle:NSAlertStyleCritical];
  1020. [alert setMessageText:[NSString stringWithFormat:NSLocalizedString(@"Failed to merge!", nil)]];
  1021. NSModalResponse response = [alert runModal];
  1022. if (response == 0 ) {
  1023. [NSApp endSheet:[self window] returnCode:[(NSView *)sender tag]];
  1024. [[self window] orderOut:self];
  1025. }
  1026. }
  1027. });
  1028. });
  1029. } else {
  1030. [self->_progress setHidden:YES];
  1031. [self.nCancelVC setEnabled:YES];
  1032. self.canMerge = YES;
  1033. self.loading_backgroundView.hidden = YES;
  1034. }
  1035. }];
  1036. });
  1037. });
  1038. } else {
  1039. [self.nCancelVC setEnabled:YES];
  1040. self.canMerge = YES;
  1041. if (allPage) {
  1042. if (!self.PDFDocument.outlineRoot) {
  1043. PDFOutline *outline = [[PDFOutline alloc] init];
  1044. self.PDFDocument.outlineRoot = outline;
  1045. }
  1046. for (NSInteger i = 0; i < self.rootPDFOutlineArray.count; i++) {
  1047. PDFOutline *rootPDFOutline = _rootPDFOutlineArray[i];
  1048. for (NSInteger j = 0; j < rootPDFOutline.numberOfChildren; j ++) {
  1049. [self.PDFDocument.outlineRoot insertChild:[rootPDFOutline childAtIndex:j] atIndex:self.PDFDocument.outlineRoot.numberOfChildren];
  1050. }
  1051. }
  1052. }
  1053. if (_isSuccessfully) {
  1054. // [self handerReDraw];
  1055. KMSavePanelAccessoryController *savePanelAccessoryViewController = [[KMSavePanelAccessoryController alloc] init];
  1056. NSSavePanel *savePanel = [NSSavePanel savePanel];
  1057. savePanel.nameFieldStringValue = fileName;
  1058. savePanel.allowedFileTypes = @[@"pdf"];
  1059. savePanel.accessoryView = savePanelAccessoryViewController.view;
  1060. self.savePanelAccessoryViewController = savePanelAccessoryViewController;
  1061. [savePanel beginSheetModalForWindow:self.window completionHandler:^(NSInteger result) {
  1062. if (result) {
  1063. NSString * outputSavePanel = savePanel.URL.path;
  1064. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  1065. self->_isSuccessfully = [self.PDFDocument writeToFile:outputSavePanel];
  1066. if (!self->_isSuccessfully) {
  1067. self->_isSuccessfully = [self.PDFDocument.dataRepresentation writeToFile:outputSavePanel atomically:YES];
  1068. }
  1069. dispatch_async(dispatch_get_main_queue(), ^{
  1070. [self->_progress setHidden:YES];
  1071. [self.nCancelVC setEnabled:YES];
  1072. self.canMerge = YES;
  1073. self.loading_backgroundView.hidden = YES;
  1074. if (self->_isSuccessfully) {
  1075. [NSApp endSheet:[self window] returnCode:[(NSView *)sender tag]];
  1076. [[self window] orderOut:self];
  1077. if (self.saveAsPDFFilePath) {
  1078. BOOL autoOpen = self.savePanelAccessoryViewController.needOpen;
  1079. if (autoOpen) {
  1080. self.saveAsPDFFilePath(outputSavePanel);
  1081. self.saveAsPDFFilePath = nil;
  1082. } else {
  1083. [self viewFileAtFinder:outputSavePanel];
  1084. }
  1085. } else {
  1086. BOOL autoOpen = self.savePanelAccessoryViewController.needOpen;
  1087. if (autoOpen) {
  1088. [[NSDocumentController sharedDocumentController] openDocumentWithContentsOfURL:[NSURL fileURLWithPath:outputSavePanel] display:YES completionHandler:^(NSDocument * _Nullable document, BOOL documentWasAlreadyOpen, NSError * _Nullable error) {
  1089. }];
  1090. } else {
  1091. [self viewFileAtFinder:outputSavePanel];
  1092. }
  1093. }
  1094. } else {
  1095. NSAlert *alert = [[NSAlert alloc] init];
  1096. [alert setAlertStyle:NSAlertStyleCritical];
  1097. [alert setMessageText:[NSString stringWithFormat:NSLocalizedString(@"Failed to merge!", nil)]];
  1098. NSModalResponse response = [alert runModal];
  1099. if (response == 0 ) {
  1100. [NSApp endSheet:[self window] returnCode:[(NSView *)sender tag]];
  1101. [[self window] orderOut:self];
  1102. }
  1103. }
  1104. });
  1105. });
  1106. } else {
  1107. [self->_progress setHidden:YES];
  1108. [self.nCancelVC setEnabled:YES];
  1109. self.canMerge = YES;
  1110. self.loading_backgroundView.hidden = YES;
  1111. }
  1112. }];
  1113. } else {
  1114. NSAlert *alert = [[NSAlert alloc] init];
  1115. [alert setAlertStyle:NSAlertStyleCritical];
  1116. [alert setMessageText:[NSString stringWithFormat:NSLocalizedString(@"Failed to merge!", nil)]];
  1117. NSModalResponse response = [alert runModal];
  1118. if (response == 0 ) {
  1119. [NSApp endSheet:[self window] returnCode:[(NSView *)sender tag]];
  1120. [[self window] orderOut:self];
  1121. }
  1122. }
  1123. }
  1124. }
  1125. - (IBAction)buttonItemClicked_Clear:(id)sender
  1126. {
  1127. [_files removeAllObjects];
  1128. [self updateButtonState];
  1129. [self.tableView reloadData];
  1130. self.allFileSize = 0;
  1131. }
  1132. - (IBAction)buttonItemClicked_AddFile:(id)sender
  1133. {
  1134. NSMenu *menu = [[NSMenu alloc] init];
  1135. NSMenuItem *addFileItem = [[NSMenuItem alloc] initWithTitle:NSLocalizedString(@"add files", "") action:@selector(itemAddFileAction) keyEquivalent:@""];
  1136. addFileItem.target = self;
  1137. [menu addItem:addFileItem];
  1138. NSMenuItem *addFolderitem = [[NSMenuItem alloc] initWithTitle:NSLocalizedString(@"add Folder", "") action:@selector(itemAddFolderAction) keyEquivalent:@""];
  1139. addFolderitem.target = self;
  1140. [menu addItem:addFolderitem];
  1141. NSMenuItem *addOpenFileitem = [[NSMenuItem alloc] initWithTitle:NSLocalizedString(@"add Open Files", "") action:@selector(itemAddOpenFileAction) keyEquivalent:@""];
  1142. addOpenFileitem.target = self;
  1143. [menu addItem:addOpenFileitem];
  1144. [menu popUpMenuPositioningItem:nil atLocation:NSMakePoint(100, 10) inView:sender];
  1145. }
  1146. - (void)itemAddFileAction {
  1147. NSOpenPanel *openPanel = [[NSOpenPanel alloc] init];
  1148. [openPanel setAllowedFileTypes:[KMImageAccessoryController supportedImageTypes]];
  1149. [openPanel setAllowsMultipleSelection:YES];
  1150. openPanel.message = NSLocalizedString(@"Press ⌘/⇧ to select multiple files", nil);
  1151. [openPanel beginSheetModalForWindow:self.window completionHandler:^(NSInteger result){
  1152. if (result == NSModalResponseOK) {
  1153. NSMutableArray *array = [NSMutableArray array];
  1154. for (NSURL *fileURL in [openPanel URLs]) {
  1155. if (fileURL) {
  1156. if (![self isExistAtFilepath:fileURL.path]) {
  1157. continue;
  1158. }
  1159. NSDictionary *attrib = [[NSFileManager defaultManager] attributesOfItemAtPath:[fileURL path] error:nil];
  1160. CGFloat fileSize = [[attrib objectForKey:NSFileSize] floatValue];
  1161. self.allFileSize = self.allFileSize + fileSize;
  1162. [array addObject:fileURL.path];
  1163. }
  1164. }
  1165. [self addFiles:array];
  1166. }
  1167. }];
  1168. }
  1169. - (void)itemAddFolderAction {
  1170. NSOpenPanel *openPanel = [[NSOpenPanel alloc] init];
  1171. [openPanel setAllowedFileTypes:[KMImageAccessoryController supportedImageTypes]];
  1172. [openPanel setAllowsMultipleSelection:YES];
  1173. openPanel.canChooseFiles = NO;
  1174. openPanel.canChooseDirectories = YES;
  1175. openPanel.message = NSLocalizedString(@"Press ⌘/⇧ to select multiple files", nil);
  1176. [openPanel beginSheetModalForWindow:self.window completionHandler:^(NSInteger result){
  1177. if (result == NSModalResponseOK) {
  1178. NSMutableArray *result = [NSMutableArray array];
  1179. for (NSURL *url in openPanel.URLs) {
  1180. [self findAllFiles:url result:result];
  1181. }
  1182. NSMutableArray *array = [NSMutableArray array];
  1183. for (NSURL *fileURL in result) {
  1184. if (fileURL) {
  1185. if (![self isExistAtFilepath:fileURL.path]) {
  1186. continue;
  1187. }
  1188. NSDictionary *attrib = [[NSFileManager defaultManager] attributesOfItemAtPath:[fileURL path] error:nil];
  1189. CGFloat fileSize = [[attrib objectForKey:NSFileSize] floatValue];
  1190. self.allFileSize = self.allFileSize + fileSize;
  1191. [array addObject:fileURL.path];
  1192. }
  1193. }
  1194. [self addFiles:array];
  1195. }
  1196. }];
  1197. }
  1198. - (void)itemAddOpenFileAction {
  1199. [_lockFilePathArr removeAllObjects];
  1200. _lockFileIndex = 0;
  1201. for (NSWindow *window in NSApp.windows) {
  1202. NSWindowController *controller = [window windowController];
  1203. if (![controller isKindOfClass:[CTBrowserWindowController class]]) {
  1204. continue;
  1205. }
  1206. CTTabStripModel *model = ((CTBrowserWindowController *)controller).browser.tabStripModel;
  1207. for (int i = 0; i < model.count; i++) {
  1208. KMMainDocument *document = (KMMainDocument *)[model tabContentsAtIndex:i];
  1209. // if (document.windowControllers.count == 0) {
  1210. // break;
  1211. // }
  1212. if (document.isHome) {
  1213. continue;
  1214. }
  1215. KMMainViewController *mainViewController = document.mainViewController;
  1216. if ([self.PDFDocument.documentURL.path isEqualToString:document.fileURL.path]) {
  1217. continue;
  1218. }
  1219. if (![self isExistAtFilepath:document.fileURL.path]) {
  1220. continue;
  1221. }
  1222. CPDFDocument *pdfDocument = mainViewController.listView.document;
  1223. if (pdfDocument.isLocked || (!pdfDocument.allowsCopying || !pdfDocument.allowsPrinting)) {
  1224. [_lockFilePathArr addObject:pdfDocument.documentURL.path];
  1225. } else {
  1226. KMFileAttribute *file = [[KMFileAttribute alloc] init];
  1227. file.filePath = document.fileURL.path;
  1228. file.pdfDocument = mainViewController.document;
  1229. [_files addObject:file];
  1230. }
  1231. }
  1232. }
  1233. [self updateButtonState];
  1234. [_tableView reloadData];
  1235. if ([_lockFilePathArr count]) {
  1236. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  1237. if (self.files.count == 0) {
  1238. self.insertRow = 0;
  1239. } else {
  1240. self.insertRow = self.files.count;
  1241. }
  1242. [self openPasswordWindow];
  1243. });
  1244. }
  1245. }
  1246. - (void)findAllFiles:(NSURL *)folderURL result:(NSMutableArray *)result {
  1247. NSFileManager *fm = [NSFileManager defaultManager];
  1248. BOOL isDirectory = NO;
  1249. [fm fileExistsAtPath:folderURL.path isDirectory:&isDirectory];
  1250. if (!isDirectory) {
  1251. return;
  1252. }
  1253. NSArray *contents = [fm contentsOfDirectoryAtURL:folderURL includingPropertiesForKeys:nil options:0 error:nil];
  1254. if (contents.count == 0) {
  1255. return;
  1256. }
  1257. NSArray *array = @[@"jpg",@"cur",@"bmp",@"jpeg",@"gif",@"png",@"tiff",@"tif",/*@"pic",*/@"ico",@"icns",@"tga",@"psd",@"eps",@"hdr",@"jp2",@"jpc",@"pict",@"sgi",@"heic",@"pdf"];
  1258. for (NSURL *documentURL in contents) {
  1259. BOOL isDirectory = NO;
  1260. [fm fileExistsAtPath:documentURL.path isDirectory:&isDirectory];
  1261. if (isDirectory) {
  1262. [self findAllFiles:documentURL result:result];
  1263. } else {
  1264. if (![array containsObject:[documentURL.pathExtension lowercaseString]]) {
  1265. continue;
  1266. }
  1267. [result addObject:documentURL];
  1268. }
  1269. }
  1270. }
  1271. - (IBAction)buttonClicked_ChooseSize:(KMImageTitleButton *)sender {
  1272. int i = 0;
  1273. for (KMImageTitleButton *button in @[self.originalSizeButton, self.A4SizeButton, self.A3SizeButton,
  1274. self.USLetterSizeButton, self.USLegalButton, self.customSizeButton]) {
  1275. KMImageTitleButton *radio = button.subviews.lastObject;
  1276. radio.state = NSControlStateValueOff;
  1277. radio.imageName = @"btn_radio_unsel_on";
  1278. i += 1;
  1279. }
  1280. sender.state = NSControlStateValueOn;
  1281. sender.imageName = @"btn_radio_sel_on";
  1282. KMImageTitleButton *customSizeButton = self.customSizeButton.subviews.lastObject;
  1283. self.pageSizeHeightTextField.enabled = self.pageSizeWidthTextField.enabled = [sender isEqual:customSizeButton];
  1284. switch (sender.contentButton.tag) {
  1285. case 0:
  1286. break;
  1287. case 1:
  1288. self.newPageSize = CGSizeMake(595, 841);
  1289. break;
  1290. case 2:
  1291. self.newPageSize = CGSizeMake(841, 1190);
  1292. break;
  1293. case 3:
  1294. self.newPageSize = CGSizeMake(612, 792);
  1295. break;
  1296. case 4:
  1297. self.newPageSize = CGSizeMake(612, 1108);
  1298. break;
  1299. case 5:
  1300. self.newPageSize = CGSizeMake(595, 841);
  1301. self.pageSizeWidthTextField.stringValue = [NSString stringWithFormat:@"%.0f",self.newPageSize.width];
  1302. self.pageSizeHeightTextField.stringValue = [NSString stringWithFormat:@"%.0f",self.newPageSize.height];
  1303. break;
  1304. default:
  1305. break;
  1306. }
  1307. }
  1308. #pragma mark - Private Method
  1309. //获取所有的子节点
  1310. - (void)fetchAllOfChildren:(PDFOutline *)aOutline containerArray:(NSMutableArray *)aMArray
  1311. {
  1312. if (![aMArray containsObject:aOutline]) {
  1313. [aMArray addObject:aOutline];
  1314. }
  1315. for (NSInteger i = 0; i < aOutline.numberOfChildren; i++) {
  1316. [aMArray addObject:[aOutline childAtIndex:i]];
  1317. [self fetchAllOfChildren:[aOutline childAtIndex:i] containerArray:aMArray];
  1318. }
  1319. }
  1320. @end