KMPDFEditAppendWindow.m 63 KB

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