KMPDFEditAppendWindow.m 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541
  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. [_progress setHidden:NO];
  893. [self.nCancelVC setEnabled:NO];
  894. self.canMerge = NO;
  895. [_progress startAnimation:nil];
  896. self.loading_backgroundView.hidden = NO;
  897. BOOL allPage = YES;//只有是全部才支持大纲的合并
  898. for (KMFileAttribute *file in _files) {
  899. if (!file.selectPages) {
  900. dispatch_async(dispatch_get_main_queue(), ^{
  901. NSAlert *alert = [[NSAlert alloc] init];
  902. [alert setAlertStyle:NSAlertStyleCritical];
  903. [alert setMessageText:[NSString stringWithFormat:@"%@ %@",[file.filePath lastPathComponent],NSLocalizedString(@"Invalid page range or the page number is out of range. Please try again.", nil)]];
  904. [alert runModal];
  905. [self->_progress setHidden:YES];
  906. [self.nCancelVC setEnabled:YES];
  907. self.canMerge = YES;
  908. self.loading_backgroundView.hidden = YES;
  909. });
  910. return;
  911. }
  912. }
  913. for(KMFileAttribute *file in _files){
  914. //只要有一个文件不是全部page,那么就不合并大纲
  915. if (allPage == YES) {
  916. allPage = file.bAllPage;
  917. }
  918. //通过路径获取文件
  919. PDFDocument *tdocument = file.myPDFDocument;
  920. if (tdocument == nil) {
  921. tdocument = [[PDFDocument alloc] initWithURL:[NSURL fileURLWithPath:[file filePath]]];
  922. file.myPDFDocument = tdocument;
  923. }
  924. //所有的outline数组
  925. NSMutableArray *PDFOutlineArr = [NSMutableArray new];
  926. //需要解锁
  927. if (file.isLocked) {
  928. [tdocument unlockWithPassword:file.password];
  929. }
  930. //如果根存在,那么遍历出所有的outline,添加到数组中,如果不存在,那么就创建一个根
  931. if (tdocument.outlineRoot) {
  932. [self.rootPDFOutlineArray addObject:tdocument.outlineRoot];
  933. [self fetchAllOfChildren:tdocument.outlineRoot containerArray:PDFOutlineArr];
  934. [PDFOutlineArr removeObject:tdocument.outlineRoot];
  935. } else {
  936. PDFOutline *rootOutline = [[PDFOutline alloc] init];
  937. tdocument.outlineRoot = rootOutline;
  938. if (tdocument.outlineRoot) {
  939. [self.rootPDFOutlineArray addObject:tdocument.outlineRoot];
  940. }
  941. }
  942. // for (CPDFOutline *tOutline in PDFOutlineArr) {
  943. //这段代码主要是调用他的getter方法;outline显示正确
  944. // [tOutline.destination.page dataRepresentation];
  945. // }
  946. for (NSNumber *number in file.selectPages) {
  947. PDFPage *page = [tdocument pageAtIndex:[number integerValue]-1];
  948. [self.PDFDocument insertPage:page atIndex:[self.PDFDocument pageCount]];
  949. // [self.PDFDocument insertPageObject:page atIndex:self.PDFDocument.pageCount];
  950. [self.insertIndexSet addIndex:(self.PDFDocument.pageCount - 1)];
  951. }
  952. _isSuccessfully = YES;
  953. }
  954. KMFileAttribute *file = self.files.firstObject;
  955. NSString *fileName = file.filePath.stringByDeletingPathExtension.lastPathComponent;
  956. fileName = [NSString stringWithFormat:@"%@_Merged", fileName];
  957. if (_editType == KMPDFPageEditMerge) {
  958. _isSuccessfully = NO;
  959. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  960. PDFOutline *rootline = [[PDFOutline alloc] init];
  961. self.PDFDocument.outlineRoot = rootline;
  962. if (allPage) {
  963. NSInteger insertIndex = 0;
  964. for (NSUInteger i = 0; i < self.rootPDFOutlineArray.count; i++) {
  965. PDFOutline *rootPDFOutline = self->_rootPDFOutlineArray[i];
  966. for (NSUInteger j = 0; j < rootPDFOutline.numberOfChildren; j ++) {
  967. [self.PDFDocument.outlineRoot insertChild:[rootPDFOutline childAtIndex:j] atIndex: insertIndex];
  968. insertIndex ++;
  969. }
  970. }
  971. }
  972. dispatch_async(dispatch_get_main_queue(), ^{
  973. // [self handerReDraw];
  974. KMSavePanelAccessoryController *savePanelAccessoryViewController = [[KMSavePanelAccessoryController alloc] init];
  975. NSSavePanel *savePanel = [NSSavePanel savePanel];
  976. savePanel.nameFieldStringValue = fileName;
  977. savePanel.allowedFileTypes = @[@"pdf"];
  978. savePanel.accessoryView = savePanelAccessoryViewController.view;
  979. self.savePanelAccessoryViewController = savePanelAccessoryViewController;
  980. [savePanel beginSheetModalForWindow:self.window completionHandler:^(NSInteger result) {
  981. if (result) {
  982. NSString * outputSavePanel = savePanel.URL.path;
  983. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  984. self->_isSuccessfully = [self.PDFDocument writeToFile:outputSavePanel];
  985. if (!self->_isSuccessfully) {
  986. self->_isSuccessfully = [self.PDFDocument.dataRepresentation writeToFile:outputSavePanel atomically:YES];
  987. }
  988. dispatch_async(dispatch_get_main_queue(), ^{
  989. [self->_progress setHidden:YES];
  990. [self.nCancelVC setEnabled:YES];
  991. self.canMerge = YES;
  992. self.loading_backgroundView.hidden = YES;
  993. if (self->_isSuccessfully) {
  994. [NSApp endSheet:[self window] returnCode:[(NSView *)sender tag]];
  995. [[self window] orderOut:self];
  996. if (self.saveAsPDFFilePath) {
  997. BOOL autoOpen = self.savePanelAccessoryViewController.needOpen;
  998. if (autoOpen) {
  999. self.saveAsPDFFilePath(outputSavePanel);
  1000. self.saveAsPDFFilePath = nil;
  1001. } else {
  1002. [self viewFileAtFinder:outputSavePanel];
  1003. }
  1004. } else {
  1005. BOOL autoOpen = self.savePanelAccessoryViewController.needOpen;
  1006. if (autoOpen) {
  1007. [[NSDocumentController sharedDocumentController] openDocumentWithContentsOfURL:[NSURL fileURLWithPath:outputSavePanel] display:YES completionHandler:^(NSDocument * _Nullable document, BOOL documentWasAlreadyOpen, NSError * _Nullable error) {
  1008. }];
  1009. } else {
  1010. [self viewFileAtFinder:outputSavePanel];
  1011. }
  1012. }
  1013. } else {
  1014. NSAlert *alert = [[NSAlert alloc] init];
  1015. [alert setAlertStyle:NSAlertStyleCritical];
  1016. [alert setMessageText:[NSString stringWithFormat:NSLocalizedString(@"Failed to merge!", nil)]];
  1017. NSModalResponse response = [alert runModal];
  1018. if (response == 0 ) {
  1019. [NSApp endSheet:[self window] returnCode:[(NSView *)sender tag]];
  1020. [[self window] orderOut:self];
  1021. }
  1022. }
  1023. });
  1024. });
  1025. } else {
  1026. [self->_progress setHidden:YES];
  1027. [self.nCancelVC setEnabled:YES];
  1028. self.canMerge = YES;
  1029. self.loading_backgroundView.hidden = YES;
  1030. }
  1031. }];
  1032. });
  1033. });
  1034. } else {
  1035. [self.nCancelVC setEnabled:YES];
  1036. self.canMerge = YES;
  1037. if (allPage) {
  1038. if (!self.PDFDocument.outlineRoot) {
  1039. PDFOutline *outline = [[PDFOutline alloc] init];
  1040. self.PDFDocument.outlineRoot = outline;
  1041. }
  1042. for (NSInteger i = 0; i < self.rootPDFOutlineArray.count; i++) {
  1043. PDFOutline *rootPDFOutline = _rootPDFOutlineArray[i];
  1044. for (NSInteger j = 0; j < rootPDFOutline.numberOfChildren; j ++) {
  1045. [self.PDFDocument.outlineRoot insertChild:[rootPDFOutline childAtIndex:j] atIndex:self.PDFDocument.outlineRoot.numberOfChildren];
  1046. }
  1047. }
  1048. }
  1049. if (_isSuccessfully) {
  1050. // [self handerReDraw];
  1051. KMSavePanelAccessoryController *savePanelAccessoryViewController = [[KMSavePanelAccessoryController alloc] init];
  1052. NSSavePanel *savePanel = [NSSavePanel savePanel];
  1053. savePanel.nameFieldStringValue = fileName;
  1054. savePanel.allowedFileTypes = @[@"pdf"];
  1055. savePanel.accessoryView = savePanelAccessoryViewController.view;
  1056. self.savePanelAccessoryViewController = savePanelAccessoryViewController;
  1057. [savePanel beginSheetModalForWindow:self.window completionHandler:^(NSInteger result) {
  1058. if (result) {
  1059. NSString * outputSavePanel = savePanel.URL.path;
  1060. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  1061. self->_isSuccessfully = [self.PDFDocument writeToFile:outputSavePanel];
  1062. if (!self->_isSuccessfully) {
  1063. self->_isSuccessfully = [self.PDFDocument.dataRepresentation writeToFile:outputSavePanel atomically:YES];
  1064. }
  1065. dispatch_async(dispatch_get_main_queue(), ^{
  1066. [self->_progress setHidden:YES];
  1067. [self.nCancelVC setEnabled:YES];
  1068. self.canMerge = YES;
  1069. self.loading_backgroundView.hidden = YES;
  1070. if (self->_isSuccessfully) {
  1071. [NSApp endSheet:[self window] returnCode:[(NSView *)sender tag]];
  1072. [[self window] orderOut:self];
  1073. if (self.saveAsPDFFilePath) {
  1074. BOOL autoOpen = self.savePanelAccessoryViewController.needOpen;
  1075. if (autoOpen) {
  1076. self.saveAsPDFFilePath(outputSavePanel);
  1077. self.saveAsPDFFilePath = nil;
  1078. } else {
  1079. [self viewFileAtFinder:outputSavePanel];
  1080. }
  1081. } else {
  1082. BOOL autoOpen = self.savePanelAccessoryViewController.needOpen;
  1083. if (autoOpen) {
  1084. [[NSDocumentController sharedDocumentController] openDocumentWithContentsOfURL:[NSURL fileURLWithPath:outputSavePanel] display:YES completionHandler:^(NSDocument * _Nullable document, BOOL documentWasAlreadyOpen, NSError * _Nullable error) {
  1085. }];
  1086. } else {
  1087. [self viewFileAtFinder:outputSavePanel];
  1088. }
  1089. }
  1090. } else {
  1091. NSAlert *alert = [[NSAlert alloc] init];
  1092. [alert setAlertStyle:NSAlertStyleCritical];
  1093. [alert setMessageText:[NSString stringWithFormat:NSLocalizedString(@"Failed to merge!", nil)]];
  1094. NSModalResponse response = [alert runModal];
  1095. if (response == 0 ) {
  1096. [NSApp endSheet:[self window] returnCode:[(NSView *)sender tag]];
  1097. [[self window] orderOut:self];
  1098. }
  1099. }
  1100. });
  1101. });
  1102. } else {
  1103. [self->_progress setHidden:YES];
  1104. [self.nCancelVC setEnabled:YES];
  1105. self.canMerge = YES;
  1106. self.loading_backgroundView.hidden = YES;
  1107. }
  1108. }];
  1109. } else {
  1110. NSAlert *alert = [[NSAlert alloc] init];
  1111. [alert setAlertStyle:NSAlertStyleCritical];
  1112. [alert setMessageText:[NSString stringWithFormat:NSLocalizedString(@"Failed to merge!", nil)]];
  1113. NSModalResponse response = [alert runModal];
  1114. if (response == 0 ) {
  1115. [NSApp endSheet:[self window] returnCode:[(NSView *)sender tag]];
  1116. [[self window] orderOut:self];
  1117. }
  1118. }
  1119. }
  1120. }
  1121. - (IBAction)buttonItemClicked_Clear:(id)sender
  1122. {
  1123. [_files removeAllObjects];
  1124. [self updateButtonState];
  1125. [self.tableView reloadData];
  1126. self.allFileSize = 0;
  1127. }
  1128. - (IBAction)buttonItemClicked_AddFile:(id)sender
  1129. {
  1130. NSMenu *menu = [[NSMenu alloc] init];
  1131. NSMenuItem *addFileItem = [[NSMenuItem alloc] initWithTitle:NSLocalizedString(@"add files", "") action:@selector(itemAddFileAction) keyEquivalent:@""];
  1132. addFileItem.target = self;
  1133. [menu addItem:addFileItem];
  1134. NSMenuItem *addFolderitem = [[NSMenuItem alloc] initWithTitle:NSLocalizedString(@"add Folder", "") action:@selector(itemAddFolderAction) keyEquivalent:@""];
  1135. addFolderitem.target = self;
  1136. [menu addItem:addFolderitem];
  1137. NSMenuItem *addOpenFileitem = [[NSMenuItem alloc] initWithTitle:NSLocalizedString(@"add Open Files", "") action:@selector(itemAddOpenFileAction) keyEquivalent:@""];
  1138. addOpenFileitem.target = self;
  1139. [menu addItem:addOpenFileitem];
  1140. [menu popUpMenuPositioningItem:nil atLocation:NSMakePoint(100, 10) inView:sender];
  1141. }
  1142. - (void)itemAddFileAction {
  1143. NSOpenPanel *openPanel = [[NSOpenPanel alloc] init];
  1144. [openPanel setAllowedFileTypes:[KMImageAccessoryController supportedImageTypes]];
  1145. [openPanel setAllowsMultipleSelection:YES];
  1146. openPanel.message = NSLocalizedString(@"Press ⌘/⇧ to select multiple files", nil);
  1147. [openPanel beginSheetModalForWindow:self.window completionHandler:^(NSInteger result){
  1148. if (result == NSModalResponseOK) {
  1149. NSMutableArray *array = [NSMutableArray array];
  1150. for (NSURL *fileURL in [openPanel URLs]) {
  1151. if (fileURL) {
  1152. if (![self isExistAtFilepath:fileURL.path]) {
  1153. continue;
  1154. }
  1155. NSDictionary *attrib = [[NSFileManager defaultManager] attributesOfItemAtPath:[fileURL path] error:nil];
  1156. CGFloat fileSize = [[attrib objectForKey:NSFileSize] floatValue];
  1157. self.allFileSize = self.allFileSize + fileSize;
  1158. [array addObject:fileURL.path];
  1159. }
  1160. }
  1161. [self addFiles:array];
  1162. }
  1163. }];
  1164. }
  1165. - (void)itemAddFolderAction {
  1166. NSOpenPanel *openPanel = [[NSOpenPanel alloc] init];
  1167. [openPanel setAllowedFileTypes:[KMImageAccessoryController supportedImageTypes]];
  1168. [openPanel setAllowsMultipleSelection:YES];
  1169. openPanel.canChooseFiles = NO;
  1170. openPanel.canChooseDirectories = YES;
  1171. openPanel.message = NSLocalizedString(@"Press ⌘/⇧ to select multiple files", nil);
  1172. [openPanel beginSheetModalForWindow:self.window completionHandler:^(NSInteger result){
  1173. if (result == NSModalResponseOK) {
  1174. NSMutableArray *result = [NSMutableArray array];
  1175. for (NSURL *url in openPanel.URLs) {
  1176. [self findAllFiles:url result:result];
  1177. }
  1178. NSMutableArray *array = [NSMutableArray array];
  1179. for (NSURL *fileURL in result) {
  1180. if (fileURL) {
  1181. if (![self isExistAtFilepath:fileURL.path]) {
  1182. continue;
  1183. }
  1184. NSDictionary *attrib = [[NSFileManager defaultManager] attributesOfItemAtPath:[fileURL path] error:nil];
  1185. CGFloat fileSize = [[attrib objectForKey:NSFileSize] floatValue];
  1186. self.allFileSize = self.allFileSize + fileSize;
  1187. [array addObject:fileURL.path];
  1188. }
  1189. }
  1190. [self addFiles:array];
  1191. }
  1192. }];
  1193. }
  1194. - (void)itemAddOpenFileAction {
  1195. [_lockFilePathArr removeAllObjects];
  1196. _lockFileIndex = 0;
  1197. for (NSWindow *window in NSApp.windows) {
  1198. NSWindowController *controller = [window windowController];
  1199. if (![controller isKindOfClass:[CTBrowserWindowController class]]) {
  1200. continue;
  1201. }
  1202. CTTabStripModel *model = ((CTBrowserWindowController *)controller).browser.tabStripModel;
  1203. for (int i = 0; i < model.count; i++) {
  1204. KMMainDocument *document = (KMMainDocument *)[model tabContentsAtIndex:i];
  1205. // if (document.windowControllers.count == 0) {
  1206. // break;
  1207. // }
  1208. if (document.isHome) {
  1209. continue;
  1210. }
  1211. KMMainViewController *mainViewController = document.mainViewController;
  1212. if ([self.PDFDocument.documentURL.path isEqualToString:document.fileURL.path]) {
  1213. continue;
  1214. }
  1215. if (![self isExistAtFilepath:document.fileURL.path]) {
  1216. continue;
  1217. }
  1218. CPDFDocument *pdfDocument = mainViewController.listView.document;
  1219. if (pdfDocument.isLocked || (!pdfDocument.allowsCopying || !pdfDocument.allowsPrinting)) {
  1220. [_lockFilePathArr addObject:pdfDocument.documentURL.path];
  1221. } else {
  1222. KMFileAttribute *file = [[KMFileAttribute alloc] init];
  1223. file.filePath = document.fileURL.path;
  1224. file.pdfDocument = mainViewController.document;
  1225. [_files addObject:file];
  1226. }
  1227. }
  1228. }
  1229. [self updateButtonState];
  1230. [_tableView reloadData];
  1231. if ([_lockFilePathArr count]) {
  1232. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  1233. if (self.files.count == 0) {
  1234. self.insertRow = 0;
  1235. } else {
  1236. self.insertRow = self.files.count;
  1237. }
  1238. [self openPasswordWindow];
  1239. });
  1240. }
  1241. }
  1242. - (void)findAllFiles:(NSURL *)folderURL result:(NSMutableArray *)result {
  1243. NSFileManager *fm = [NSFileManager defaultManager];
  1244. BOOL isDirectory = NO;
  1245. [fm fileExistsAtPath:folderURL.path isDirectory:&isDirectory];
  1246. if (!isDirectory) {
  1247. return;
  1248. }
  1249. NSArray *contents = [fm contentsOfDirectoryAtURL:folderURL includingPropertiesForKeys:nil options:0 error:nil];
  1250. if (contents.count == 0) {
  1251. return;
  1252. }
  1253. NSArray *array = @[@"jpg",@"cur",@"bmp",@"jpeg",@"gif",@"png",@"tiff",@"tif",/*@"pic",*/@"ico",@"icns",@"tga",@"psd",@"eps",@"hdr",@"jp2",@"jpc",@"pict",@"sgi",@"heic",@"pdf"];
  1254. for (NSURL *documentURL in contents) {
  1255. BOOL isDirectory = NO;
  1256. [fm fileExistsAtPath:documentURL.path isDirectory:&isDirectory];
  1257. if (isDirectory) {
  1258. [self findAllFiles:documentURL result:result];
  1259. } else {
  1260. if (![array containsObject:[documentURL.pathExtension lowercaseString]]) {
  1261. continue;
  1262. }
  1263. [result addObject:documentURL];
  1264. }
  1265. }
  1266. }
  1267. - (IBAction)buttonClicked_ChooseSize:(KMImageTitleButton *)sender {
  1268. int i = 0;
  1269. for (KMImageTitleButton *button in @[self.originalSizeButton, self.A4SizeButton, self.A3SizeButton,
  1270. self.USLetterSizeButton, self.USLegalButton, self.customSizeButton]) {
  1271. KMImageTitleButton *radio = button.subviews.lastObject;
  1272. radio.state = NSControlStateValueOff;
  1273. radio.imageName = @"btn_radio_unsel_on";
  1274. i += 1;
  1275. }
  1276. sender.state = NSControlStateValueOn;
  1277. sender.imageName = @"btn_radio_sel_on";
  1278. KMImageTitleButton *customSizeButton = self.customSizeButton.subviews.lastObject;
  1279. self.pageSizeHeightTextField.enabled = self.pageSizeWidthTextField.enabled = [sender isEqual:customSizeButton];
  1280. switch (sender.contentButton.tag) {
  1281. case 0:
  1282. break;
  1283. case 1:
  1284. self.newPageSize = CGSizeMake(595, 841);
  1285. break;
  1286. case 2:
  1287. self.newPageSize = CGSizeMake(841, 1190);
  1288. break;
  1289. case 3:
  1290. self.newPageSize = CGSizeMake(612, 792);
  1291. break;
  1292. case 4:
  1293. self.newPageSize = CGSizeMake(612, 1108);
  1294. break;
  1295. case 5:
  1296. self.newPageSize = CGSizeMake(595, 841);
  1297. self.pageSizeWidthTextField.stringValue = [NSString stringWithFormat:@"%.0f",self.newPageSize.width];
  1298. self.pageSizeHeightTextField.stringValue = [NSString stringWithFormat:@"%.0f",self.newPageSize.height];
  1299. break;
  1300. default:
  1301. break;
  1302. }
  1303. }
  1304. #pragma mark - Private Method
  1305. //获取所有的子节点
  1306. - (void)fetchAllOfChildren:(PDFOutline *)aOutline containerArray:(NSMutableArray *)aMArray
  1307. {
  1308. if (![aMArray containsObject:aOutline]) {
  1309. [aMArray addObject:aOutline];
  1310. }
  1311. for (NSInteger i = 0; i < aOutline.numberOfChildren; i++) {
  1312. [aMArray addObject:[aOutline childAtIndex:i]];
  1313. [self fetchAllOfChildren:[aOutline childAtIndex:i] containerArray:aMArray];
  1314. }
  1315. }
  1316. @end