KMPDFEditAppendWindow.m 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527
  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 Add Files at upper left corner. You can drag files to reorder as you need.", 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. // [document insertPage:image.size withImage:filePath atIndex:0];
  451. [document insertPage:[[PDFPage alloc] initWithImage:image] atIndex:0];
  452. file.myPDFDocument = document;
  453. NSDictionary *attrib = [[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:nil];
  454. self.allFileSize = [[attrib objectForKey:NSFileSize] floatValue];
  455. if ([NSDocument isDamageWithUrl:[NSURL fileURLWithPath:filePath]]) {
  456. NSAlert *alert = [[NSAlert alloc] init];
  457. alert.messageText = NSLocalizedString(@"The file was not added successfully, please select the file again.", @"");
  458. [alert runModal];
  459. } else {
  460. [_files addObject:file];
  461. }
  462. }
  463. }
  464. [self updateButtonState];
  465. [_tableView reloadData];
  466. if ([_lockFilePathArr count]) {
  467. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  468. if (self.files.count == 0) {
  469. self.insertRow = 0;
  470. } else {
  471. self.insertRow = self.files.count;
  472. }
  473. [self openPasswordWindow];
  474. });
  475. }
  476. }
  477. - (BOOL)isExistAtFilepath:(NSString *)filePath
  478. {
  479. NSMutableArray *array = [NSMutableArray array];
  480. for (KMFileAttribute *file in self.files) {
  481. [array addObject:file.filePath];
  482. }
  483. if ([array containsObject:filePath]) {
  484. return NO;
  485. }
  486. return YES;
  487. }
  488. -(void)openPasswordWindow
  489. {
  490. if ([_lockFilePathArr count] > _lockFileIndex) {
  491. NSString *filePath = [_lockFilePathArr objectAtIndex:_lockFileIndex];
  492. __weak typeof(self) weakSelf = self;
  493. [KMPasswordInputWindow openWindowWithWindow:self.window type:KMPasswordInputWindowTypeOwner url:[NSURL fileURLWithPath:filePath] callback:^(enum KMPasswordInputWindowResult result, NSString * _Nullable password) {
  494. if (password.length > 0) {
  495. KMFileAttribute *file = [[KMFileAttribute alloc] init];
  496. file.filePath = filePath;
  497. file.isLocked = YES;
  498. file.password = password;
  499. [weakSelf.files insertObject:file atIndex:weakSelf.insertRow];
  500. weakSelf.insertRow ++;
  501. }
  502. weakSelf.lockFileIndex++;
  503. // if ([self.lockFilePathArr count] > self.lockFileIndex) {
  504. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  505. [weakSelf openPasswordWindow];
  506. });
  507. // } else {
  508. [weakSelf.tableView reloadData];
  509. [weakSelf updateButtonState];
  510. // }
  511. }];
  512. }
  513. }
  514. - (void)comboBoxAction:(NSInteger)itemIndex atRow:(NSInteger)row
  515. {
  516. NSInteger index = row;
  517. // 数组不存在,或者索引超出数组长度
  518. if ((!_files) || (index > ((NSInteger)[_files count] - 1))) {
  519. return;
  520. }
  521. if (itemIndex == 0) {
  522. KMFileAttribute *currentFile = [_files objectAtIndex:index];
  523. [self.window makeFirstResponder:self];
  524. [currentFile setBAllPage:YES];
  525. currentFile.pagesType = KMPDFSeleectPageType_AllPages;
  526. } else if (itemIndex == 1) {
  527. [self.window makeFirstResponder:self];
  528. KMFileAttribute *currentFile = [_files objectAtIndex:index];
  529. currentFile.pagesType = KMPDFSeleectPageType_OnlyOdd;
  530. [currentFile setBAllPage:NO];
  531. PDFDocument *tDocument = currentFile.myPDFDocument;
  532. if (tDocument == nil) {
  533. tDocument = [[PDFDocument alloc] initWithURL:[NSURL fileURLWithPath:currentFile.filePath]];
  534. }
  535. if ([currentFile isLocked]) {
  536. [tDocument unlockWithPassword:currentFile.password];
  537. }
  538. NSString *tPagesString = @"";
  539. for (NSInteger i = 0; i < tDocument.pageCount; i++) {
  540. if (i%2 == 0) {
  541. if (tPagesString.length == 0) {
  542. tPagesString = [tPagesString stringByAppendingString:[NSString stringWithFormat:@"%ld",i +1]];
  543. }else{
  544. tPagesString = [tPagesString stringByAppendingString:[NSString stringWithFormat:@",%ld",i +1]];
  545. }
  546. }
  547. }
  548. currentFile.pagesString = tPagesString;
  549. } else if (itemIndex == 2) {
  550. [self.window makeFirstResponder:self];
  551. KMFileAttribute *currentFile = [_files objectAtIndex:index];
  552. currentFile.pagesType = KMPDFSeleectPageType_OnlyEven;
  553. [currentFile setBAllPage:NO];
  554. NSString *tPagesString = @"";
  555. PDFDocument *tDocument = currentFile.myPDFDocument;
  556. if (tDocument == nil) {
  557. tDocument = [[PDFDocument alloc] initWithURL:[NSURL fileURLWithPath:currentFile.filePath]];
  558. }
  559. if ([currentFile isLocked]) {
  560. [tDocument unlockWithPassword:currentFile.password];
  561. }
  562. for (NSInteger i = 0; i <tDocument.pageCount; i++) {
  563. if (i%2) {
  564. if (tPagesString.length == 0) {
  565. tPagesString = [tPagesString stringByAppendingString:[NSString stringWithFormat:@"%ld",i +1]];
  566. }else{
  567. tPagesString = [tPagesString stringByAppendingString:[NSString stringWithFormat:@",%ld",i +1]];
  568. }
  569. }
  570. }
  571. currentFile.pagesString = tPagesString;
  572. } else {
  573. KMFileAttribute *currentFile = [_files objectAtIndex:index];
  574. currentFile.pagesType = KMPDFSeleectPageType_PagesString;
  575. [currentFile setBAllPage:NO];
  576. }
  577. }
  578. - (void)handerReDraw
  579. {
  580. //如果选择的是原始尺寸,不处理
  581. KMImageTitleButton *originalSizeButton = self.originalSizeButton.subviews.lastObject;
  582. if (originalSizeButton.state == NSControlStateValueOn) {
  583. }else{
  584. //重新选择了界面尺寸,那么就要重绘
  585. if (self.newPageSize.width < 0) {
  586. NSBeep();
  587. return;
  588. }else{
  589. NSMutableArray *pagesArray = [NSMutableArray array];
  590. NSInteger pageCount = self.PDFDocument.pageCount;
  591. for (NSInteger i = 0; i < pageCount; i ++) {
  592. [pagesArray addObject:[self.PDFDocument pageAtIndex:0]];
  593. [self.PDFDocument removePageAtIndex:0];
  594. }
  595. for (NSInteger i = 0; i < pageCount; i ++) {
  596. KMAppendPDFPage *page = [[KMAppendPDFPage alloc] init];
  597. [page setBounds:NSMakeRect(0, 0, self.newPageSize.width, self.newPageSize.height) forBox:kPDFDisplayBoxMediaBox];
  598. page.drawingPage = pagesArray[i];
  599. // [self.PDFDocument insertPageObject:page atIndex:i];
  600. [self.PDFDocument insertPage:page atIndex:i];
  601. }
  602. //如果是自定义大小,删除所有的outline,因为合并出来的outline是无效的
  603. if (self.PDFDocument.outlineRoot) {
  604. NSInteger childCount = [self.PDFDocument.outlineRoot numberOfChildren];
  605. NSMutableArray *PDFOutlineArray = [NSMutableArray array];
  606. for (NSInteger i = 0 ; i < childCount; i ++) {
  607. [PDFOutlineArray addObject:[self.PDFDocument.outlineRoot childAtIndex:i]];
  608. }
  609. for (PDFOutline *deleteOutline in PDFOutlineArray) {
  610. [deleteOutline removeFromParent];
  611. }
  612. }
  613. }
  614. }
  615. }
  616. - (BOOL)hasOpenFiles {
  617. NSArray *array = [KMTools getOpenDocumentURLs];
  618. if (array.count == 0) {
  619. return NO;
  620. }
  621. for (NSURL *fileURL in array) {
  622. if ([self isExistAtFilepath:fileURL.path]) {
  623. return YES;
  624. }
  625. }
  626. return NO;
  627. }
  628. #pragma mark - KMSelectPopButtonDelegate
  629. - (void)km_comboBoxSelectionDidChange:(KMDesignSelect *)obj {
  630. if ([self.addFileButton isEqual:obj]) {
  631. NSInteger index = self.addFileButton.indexOfSelectedItem;
  632. if (index < 0) {
  633. index = 0;
  634. }
  635. if (index == 0) {
  636. [self itemAddFileAction];
  637. } else if (index == 1) {
  638. [self itemAddFolderAction];
  639. } else if (index == 2) {
  640. [self itemAddOpenFileAction];
  641. }
  642. }
  643. }
  644. - (void)km_controlTextDidChange:(KMDesignSelect *)obj {
  645. }
  646. - (void)km_controlTextDidEndEditing:(KMDesignSelect *)obj {
  647. }
  648. - (void)km_SelectPopoverWillShow:(KMDesignSelect *)obj {
  649. if ([self hasOpenFiles]) {
  650. obj.disItems = @[];
  651. } else {
  652. obj.disItems = @[NSLocalizedString(@"Add Open Files", "")];
  653. }
  654. }
  655. #pragma mark tabelView
  656. - (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView
  657. {
  658. return _files.count;
  659. }
  660. - (nullable id)tableView:(NSTableView *)tableView viewForTableColumn:(nullable NSTableColumn *)tableColumn row:(NSInteger)row
  661. {
  662. KMFileAttribute *fileAttribute = [_files objectAtIndex:row];
  663. PDFDocument *pdf = fileAttribute.myPDFDocument;
  664. if (pdf == nil) {
  665. pdf = [[PDFDocument alloc] initWithURL:[NSURL fileURLWithPath:fileAttribute.filePath]];
  666. }
  667. if ([pdf isLocked]) {
  668. [pdf unlockWithPassword:fileAttribute.password];
  669. }
  670. if (tableColumn.identifier == KMTableColumnPageRangeID) {
  671. KMPDFMergePageRangeTabelViewCell *cell = [tableView makeViewWithIdentifier:tableColumn.identifier owner:self];
  672. if (cell == nil) {
  673. cell = [[KMPDFMergePageRangeTabelViewCell alloc] init];
  674. }
  675. cell.wantsLayer = YES;
  676. cell.layer.backgroundColor = [NSColor clearColor].CGColor;
  677. cell.pageCount = pdf.pageCount;
  678. [cell updatePageRange:fileAttribute.pagesType pageStrings:fileAttribute.pagesString];
  679. __weak typeof(self) weakSelf = self;
  680. cell.callback = ^(NSInteger index) {
  681. [weakSelf comboBoxAction:index atRow:row];
  682. };
  683. cell.textDidChange = ^(NSString * _Nonnull string) {
  684. if (row >= weakSelf.files.count) {
  685. return;
  686. }
  687. [[weakSelf.files objectAtIndex:row] setPagesString:string];
  688. };
  689. return cell;
  690. }
  691. if (tableColumn.identifier == KMTableColumnSizeID) {
  692. KMPDFMergeSizeTabelViewCell *cell = [tableView makeViewWithIdentifier:tableColumn.identifier owner:self];
  693. if (cell == nil) {
  694. cell = [[KMPDFMergeSizeTabelViewCell alloc] init];
  695. }
  696. NSString *filePath = pdf.documentURL.path;
  697. if (!filePath) {
  698. filePath = fileAttribute.filePath;
  699. }
  700. NSDictionary *attributesDic = [[NSFileManager defaultManager]attributesOfItemAtPath:filePath error:nil];
  701. float size = [[attributesDic objectForKey:NSFileSize] floatValue]/1024;
  702. cell.label.stringValue = [NSString stringWithFormat:@"%0.1f %c", truesize(size) , moniker(size)];
  703. cell.button.hidden = YES;
  704. __weak typeof(self) weakSelf = self;
  705. cell.callback = ^{
  706. NSDictionary *attrib = [[NSFileManager defaultManager] attributesOfItemAtPath:fileAttribute.filePath error:nil];
  707. CGFloat fileSize = [[attrib objectForKey:NSFileSize] floatValue];
  708. weakSelf.allFileSize = weakSelf.allFileSize - fileSize;
  709. [weakSelf.files removeObject:fileAttribute];
  710. [weakSelf.tableView reloadData];
  711. [weakSelf updateButtonState];
  712. };
  713. return cell;
  714. }
  715. KMPDFMergeFileNameTabelViewCell *cell = [tableView makeViewWithIdentifier:@"fileName" owner:self];
  716. if (cell == nil) {
  717. cell = [[KMPDFMergeFileNameTabelViewCell alloc] init];
  718. }
  719. cell.numberLabel.stringValue = [NSString stringWithFormat:@"%ld",(row + 1)];;
  720. cell.fileNameLabel.stringValue = [fileAttribute.filePath lastPathComponent];
  721. cell.pageNumberLabel.stringValue = [NSString stringWithFormat:@"%ld %@",pdf.pageCount,NSLocalizedString(@"Pages", nil)];;
  722. // cell.iconImageView.image = [[pdf pageAtIndex:0] thumbnailWithSize:60*2 forBox:kPDFDisplayBoxCropBox];
  723. cell.iconImageView.image = [NSImage previewForFileWithPath:pdf.documentURL ofSize:NSMakeSize(120, 120) asIcon:YES];
  724. return cell;
  725. }
  726. - (BOOL)tableView:(NSTableView *)tableView shouldSelectTableColumn:(NSTableColumn *)tableColumn
  727. {
  728. return NO;
  729. }
  730. #pragma mark -NSTableViewDelegate
  731. - (BOOL)tableView:(NSTableView *)tv writeRowsWithIndexes:(NSIndexSet *)rowIndexes toPasteboard:(NSPasteboard*)pboard
  732. {
  733. // Copy the row numbers to the pasteboard.
  734. NSData *zNSIndexSetData = [NSKeyedArchiver archivedDataWithRootObject:rowIndexes];
  735. [pboard declareTypes:[NSArray arrayWithObject:MyTableCellViewDataType] owner:self];
  736. [pboard setData:zNSIndexSetData forType:MyTableCellViewDataType];
  737. return YES;
  738. }
  739. - (NSDragOperation)tableView:(NSTableView *)tableView validateDrop:(id <NSDraggingInfo>)info proposedRow:(NSInteger)row proposedDropOperation:(NSTableViewDropOperation)dropOperation
  740. {
  741. if (dropOperation == NSTableViewDropOn) {
  742. return NSDragOperationNone;
  743. }
  744. BOOL isCanDrag = NO;
  745. NSDragOperation result = NSDragOperationNone;
  746. NSPasteboard *pboard = [info draggingPasteboard];
  747. if ([pboard availableTypeFromArray:[NSArray arrayWithObject:NSFilenamesPboardType]])
  748. {
  749. NSArray *fileNames = [pboard propertyListForType:NSFilenamesPboardType];
  750. for (NSString* path in fileNames) {
  751. if ([[KMImageAccessoryController supportedImageTypes] containsObject:path.pathExtension.lowercaseString]) {
  752. isCanDrag = YES;
  753. }else{
  754. isCanDrag = NO;
  755. break;
  756. }
  757. }
  758. }else if ([pboard availableTypeFromArray:[NSArray arrayWithObject:MyTableCellViewDataType]]){
  759. result = NSDragOperationEvery;
  760. }
  761. if (isCanDrag) {
  762. result = NSDragOperationCopy;
  763. }
  764. return result;
  765. }
  766. - (BOOL)tableView:(NSTableView *)tableView acceptDrop:(id <NSDraggingInfo>)info row:(NSInteger)row dropOperation:(NSTableViewDropOperation)dropOperation
  767. {
  768. BOOL result = NO;
  769. NSPasteboard *pboard = [info draggingPasteboard];
  770. _insertRow = row;
  771. if ([pboard availableTypeFromArray:[NSArray arrayWithObject:NSFilenamesPboardType]])
  772. {
  773. NSMutableArray *fileNames = [pboard propertyListForType:NSFilenamesPboardType];
  774. NSMutableArray *array = [NSMutableArray array];
  775. for(NSString *path in fileNames){
  776. if (![self isExistAtFilepath:path]) {
  777. continue;
  778. }
  779. NSDictionary *attrib = [[NSFileManager defaultManager] attributesOfItemAtPath:path error:nil];
  780. CGFloat fileSize = [[attrib objectForKey:NSFileSize] floatValue];
  781. self.allFileSize = self.allFileSize + fileSize;
  782. [array addObject:path];
  783. }
  784. [self addFiles:array];
  785. result = YES;
  786. } else if ([pboard availableTypeFromArray:[NSArray arrayWithObject:MyTableCellViewDataType]]){
  787. NSData* rowData = [pboard dataForType:MyTableCellViewDataType];
  788. NSIndexSet* rowIndexes = [NSKeyedUnarchiver unarchiveObjectWithData:rowData];
  789. NSMutableArray *moveArray = [[NSMutableArray alloc] init];
  790. NSMutableArray *allPhoto = [_files mutableCopy];
  791. NSInteger index = 0;
  792. [rowIndexes enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL * _Nonnull stop) {
  793. KMFileAttribute * file = allPhoto[idx];
  794. [moveArray addObject:file];
  795. [_files removeObject:file];
  796. }];
  797. if (row > 0) {
  798. KMFileAttribute * indexFile = allPhoto[row -1];
  799. while([moveArray containsObject:indexFile]){
  800. row --;
  801. if ((row < 0)) {
  802. indexFile = nil;
  803. break;
  804. } else {
  805. indexFile = allPhoto[row];
  806. }
  807. }
  808. if(indexFile){
  809. index = [_files indexOfObject:indexFile]+1;
  810. }
  811. }
  812. for (NSInteger i = 0; i< moveArray.count; i++) {
  813. [_files insertObject:moveArray[i] atIndex:(index +i)];
  814. }
  815. [_tableView reloadData];
  816. result = YES;
  817. // [moveArray release];
  818. // [allPhoto release];
  819. } else {
  820. result = NO;
  821. }
  822. return result;
  823. }
  824. - (NSTableRowView *)tableView:(NSTableView *)tableView rowViewForRow:(NSInteger)row {
  825. KMBatchTableRowView *rowView = [[KMBatchTableRowView alloc] init];
  826. rowView.backgroundView.layer.backgroundColor = [NSColor colorWithRed:237/255.f green:238/255.f blue:240/255.f alpha:1.f].CGColor;
  827. rowView.backgroundView.layer.cornerRadius = 4.f;
  828. rowView.selectionInset = NSEdgeInsetsMake(0, 16, 0, 16);
  829. rowView.selectionRadius = 4.f;
  830. rowView.selectionBackgroundColorBlock = ^NSColor * _Nonnull{
  831. return [NSColor colorWithRed:206/255.f green:208/255.f blue:212/255.f alpha:0.6f];
  832. };
  833. return rowView;
  834. }
  835. - (void)tableViewSelectionDidChange:(NSNotification *)notification {
  836. }
  837. #pragma mark Buutton Action
  838. - (IBAction)buttonItemClicked_Cancel:(id)sender
  839. {
  840. _isSuccessfully = NO;
  841. [NSApp endSheet:[self window] returnCode:[(NSView *)sender tag]];
  842. [[self window] orderOut:self];
  843. }
  844. - (IBAction)buttonItemClicked_Append:(id)sender
  845. {
  846. if (!self.canMerge) {
  847. return;
  848. }
  849. KMImageTitleButton *customSizeButton = self.customSizeButton.subviews.lastObject;
  850. if (customSizeButton.state == NSControlStateValueOn) {
  851. NSInteger widthFloat = [self.pageSizeWidthTextField.stringValue integerValue];
  852. NSInteger heightFloat = [self.pageSizeHeightTextField.stringValue integerValue];
  853. NSString *widthString = [NSString stringWithFormat:@"%ld",widthFloat];
  854. NSString *heightString = [NSString stringWithFormat:@"%ld",heightFloat];
  855. if (![widthString isEqualToString:self.pageSizeWidthTextField.stringValue] ||![heightString isEqualToString:self.pageSizeHeightTextField.stringValue]||widthFloat <= 0 ||heightFloat <= 0 ) {
  856. NSBeep();
  857. return;
  858. }else{
  859. self.newPageSize = CGSizeMake(widthFloat, heightFloat);
  860. }
  861. }
  862. //初始化数组
  863. if (!self.rootPDFOutlineArray) {
  864. self.rootPDFOutlineArray = [NSMutableArray new];
  865. }else{
  866. [self.rootPDFOutlineArray removeAllObjects];
  867. }
  868. //如果是合并,文件路径不存在,或者文件个数小于等于1 return
  869. if (_editType == KMPDFPageEditMerge) {
  870. if (_files.count <= 1 ) {
  871. NSAlert *alert = [[NSAlert alloc] init];
  872. [alert setAlertStyle:NSAlertStyleCritical];
  873. [alert setMessageText:NSLocalizedString(@"To start merging, please select at least 2 files.", nil)];
  874. [alert runModal];
  875. return;
  876. }
  877. }
  878. _isSuccessfully = NO;
  879. [_progress setHidden:NO];
  880. [self.nCancelVC setEnabled:NO];
  881. self.canMerge = NO;
  882. [_progress startAnimation:nil];
  883. self.loading_backgroundView.hidden = NO;
  884. BOOL allPage = YES;//只有是全部才支持大纲的合并
  885. for (KMFileAttribute *file in _files) {
  886. if (!file.selectPages) {
  887. dispatch_async(dispatch_get_main_queue(), ^{
  888. NSAlert *alert = [[NSAlert alloc] init];
  889. [alert setAlertStyle:NSAlertStyleCritical];
  890. [alert setMessageText:[NSString stringWithFormat:@"%@ %@",[file.filePath lastPathComponent],NSLocalizedString(@"Invalid page range or the page number is out of range. Please try again.", nil)]];
  891. [alert runModal];
  892. [self->_progress setHidden:YES];
  893. [self.nCancelVC setEnabled:YES];
  894. self.canMerge = YES;
  895. self.loading_backgroundView.hidden = YES;
  896. });
  897. return;
  898. }
  899. }
  900. for(KMFileAttribute *file in _files){
  901. //只要有一个文件不是全部page,那么就不合并大纲
  902. if (allPage == YES) {
  903. allPage = file.bAllPage;
  904. }
  905. //通过路径获取文件
  906. PDFDocument *tdocument = file.myPDFDocument;
  907. if (tdocument == nil) {
  908. tdocument = [[PDFDocument alloc] initWithURL:[NSURL fileURLWithPath:[file filePath]]];
  909. file.myPDFDocument = tdocument;
  910. }
  911. //所有的outline数组
  912. NSMutableArray *PDFOutlineArr = [NSMutableArray new];
  913. //需要解锁
  914. if (file.isLocked) {
  915. [tdocument unlockWithPassword:file.password];
  916. }
  917. //如果根存在,那么遍历出所有的outline,添加到数组中,如果不存在,那么就创建一个根
  918. if (tdocument.outlineRoot) {
  919. [self.rootPDFOutlineArray addObject:tdocument.outlineRoot];
  920. [self fetchAllOfChildren:tdocument.outlineRoot containerArray:PDFOutlineArr];
  921. [PDFOutlineArr removeObject:tdocument.outlineRoot];
  922. } else {
  923. PDFOutline *rootOutline = [[PDFOutline alloc] init];
  924. tdocument.outlineRoot = rootOutline;
  925. if (tdocument.outlineRoot) {
  926. [self.rootPDFOutlineArray addObject:tdocument.outlineRoot];
  927. }
  928. }
  929. // for (CPDFOutline *tOutline in PDFOutlineArr) {
  930. //这段代码主要是调用他的getter方法;outline显示正确
  931. // [tOutline.destination.page dataRepresentation];
  932. // }
  933. for (NSNumber *number in file.selectPages) {
  934. PDFPage *page = [tdocument pageAtIndex:[number integerValue]-1];
  935. [self.PDFDocument insertPage:page atIndex:[self.PDFDocument pageCount]];
  936. // [self.PDFDocument insertPageObject:page atIndex:self.PDFDocument.pageCount];
  937. [self.insertIndexSet addIndex:(self.PDFDocument.pageCount - 1)];
  938. }
  939. _isSuccessfully = YES;
  940. }
  941. KMFileAttribute *file = self.files.firstObject;
  942. NSString *fileName = file.filePath.stringByDeletingPathExtension.lastPathComponent;
  943. fileName = [NSString stringWithFormat:@"%@_Merged", fileName];
  944. if (_editType == KMPDFPageEditMerge) {
  945. _isSuccessfully = NO;
  946. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  947. PDFOutline *rootline = [[PDFOutline alloc] init];
  948. self.PDFDocument.outlineRoot = rootline;
  949. if (allPage) {
  950. NSInteger insertIndex = 0;
  951. for (NSUInteger i = 0; i < self.rootPDFOutlineArray.count; i++) {
  952. PDFOutline *rootPDFOutline = self->_rootPDFOutlineArray[i];
  953. for (NSUInteger j = 0; j < rootPDFOutline.numberOfChildren; j ++) {
  954. [self.PDFDocument.outlineRoot insertChild:[rootPDFOutline childAtIndex:j] atIndex: insertIndex];
  955. insertIndex ++;
  956. }
  957. }
  958. }
  959. dispatch_async(dispatch_get_main_queue(), ^{
  960. // [self handerReDraw];
  961. KMSavePanelAccessoryController *savePanelAccessoryViewController = [[KMSavePanelAccessoryController alloc] init];
  962. NSSavePanel *savePanel = [NSSavePanel savePanel];
  963. savePanel.nameFieldStringValue = fileName;
  964. savePanel.allowedFileTypes = @[@"pdf"];
  965. savePanel.accessoryView = savePanelAccessoryViewController.view;
  966. self.savePanelAccessoryViewController = savePanelAccessoryViewController;
  967. [savePanel beginSheetModalForWindow:self.window completionHandler:^(NSInteger result) {
  968. if (result) {
  969. NSString * outputSavePanel = savePanel.URL.path;
  970. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  971. self->_isSuccessfully = [self.PDFDocument writeToFile:outputSavePanel];
  972. if (!self->_isSuccessfully) {
  973. self->_isSuccessfully = [self.PDFDocument.dataRepresentation writeToFile:outputSavePanel atomically:YES];
  974. }
  975. dispatch_async(dispatch_get_main_queue(), ^{
  976. [self->_progress setHidden:YES];
  977. [self.nCancelVC setEnabled:YES];
  978. self.canMerge = YES;
  979. self.loading_backgroundView.hidden = YES;
  980. if (self->_isSuccessfully) {
  981. [NSApp endSheet:[self window] returnCode:[(NSView *)sender tag]];
  982. [[self window] orderOut:self];
  983. if (self.saveAsPDFFilePath) {
  984. BOOL autoOpen = self.savePanelAccessoryViewController.needOpen;
  985. if (autoOpen) {
  986. self.saveAsPDFFilePath(outputSavePanel);
  987. self.saveAsPDFFilePath = nil;
  988. } else {
  989. [self viewFileAtFinder:outputSavePanel];
  990. }
  991. } else {
  992. BOOL autoOpen = self.savePanelAccessoryViewController.needOpen;
  993. if (autoOpen) {
  994. [[NSDocumentController sharedDocumentController] openDocumentWithContentsOfURL:[NSURL fileURLWithPath:outputSavePanel] display:YES completionHandler:^(NSDocument * _Nullable document, BOOL documentWasAlreadyOpen, NSError * _Nullable error) {
  995. }];
  996. } else {
  997. [self viewFileAtFinder:outputSavePanel];
  998. }
  999. }
  1000. } else {
  1001. NSAlert *alert = [[NSAlert alloc] init];
  1002. [alert setAlertStyle:NSAlertStyleCritical];
  1003. [alert setMessageText:[NSString stringWithFormat:NSLocalizedString(@"Failed to merge!", nil)]];
  1004. NSModalResponse response = [alert runModal];
  1005. if (response == 0 ) {
  1006. [NSApp endSheet:[self window] returnCode:[(NSView *)sender tag]];
  1007. [[self window] orderOut:self];
  1008. }
  1009. }
  1010. });
  1011. });
  1012. } else {
  1013. [self->_progress setHidden:YES];
  1014. [self.nCancelVC setEnabled:YES];
  1015. self.canMerge = YES;
  1016. self.loading_backgroundView.hidden = YES;
  1017. }
  1018. }];
  1019. });
  1020. });
  1021. } else {
  1022. [self.nCancelVC setEnabled:YES];
  1023. self.canMerge = YES;
  1024. if (allPage) {
  1025. if (!self.PDFDocument.outlineRoot) {
  1026. PDFOutline *outline = [[PDFOutline alloc] init];
  1027. self.PDFDocument.outlineRoot = outline;
  1028. }
  1029. for (NSInteger i = 0; i < self.rootPDFOutlineArray.count; i++) {
  1030. PDFOutline *rootPDFOutline = _rootPDFOutlineArray[i];
  1031. for (NSInteger j = 0; j < rootPDFOutline.numberOfChildren; j ++) {
  1032. [self.PDFDocument.outlineRoot insertChild:[rootPDFOutline childAtIndex:j] atIndex:self.PDFDocument.outlineRoot.numberOfChildren];
  1033. }
  1034. }
  1035. }
  1036. if (_isSuccessfully) {
  1037. // [self handerReDraw];
  1038. KMSavePanelAccessoryController *savePanelAccessoryViewController = [[KMSavePanelAccessoryController alloc] init];
  1039. NSSavePanel *savePanel = [NSSavePanel savePanel];
  1040. savePanel.nameFieldStringValue = fileName;
  1041. savePanel.allowedFileTypes = @[@"pdf"];
  1042. savePanel.accessoryView = savePanelAccessoryViewController.view;
  1043. self.savePanelAccessoryViewController = savePanelAccessoryViewController;
  1044. [savePanel beginSheetModalForWindow:self.window completionHandler:^(NSInteger result) {
  1045. if (result) {
  1046. NSString * outputSavePanel = savePanel.URL.path;
  1047. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  1048. self->_isSuccessfully = [self.PDFDocument writeToFile:outputSavePanel];
  1049. if (!self->_isSuccessfully) {
  1050. self->_isSuccessfully = [self.PDFDocument.dataRepresentation writeToFile:outputSavePanel atomically:YES];
  1051. }
  1052. dispatch_async(dispatch_get_main_queue(), ^{
  1053. [self->_progress setHidden:YES];
  1054. [self.nCancelVC setEnabled:YES];
  1055. self.canMerge = YES;
  1056. self.loading_backgroundView.hidden = YES;
  1057. if (self->_isSuccessfully) {
  1058. [NSApp endSheet:[self window] returnCode:[(NSView *)sender tag]];
  1059. [[self window] orderOut:self];
  1060. if (self.saveAsPDFFilePath) {
  1061. BOOL autoOpen = self.savePanelAccessoryViewController.needOpen;
  1062. if (autoOpen) {
  1063. self.saveAsPDFFilePath(outputSavePanel);
  1064. self.saveAsPDFFilePath = nil;
  1065. } else {
  1066. [self viewFileAtFinder:outputSavePanel];
  1067. }
  1068. } else {
  1069. BOOL autoOpen = self.savePanelAccessoryViewController.needOpen;
  1070. if (autoOpen) {
  1071. [[NSDocumentController sharedDocumentController] openDocumentWithContentsOfURL:[NSURL fileURLWithPath:outputSavePanel] display:YES completionHandler:^(NSDocument * _Nullable document, BOOL documentWasAlreadyOpen, NSError * _Nullable error) {
  1072. }];
  1073. } else {
  1074. [self viewFileAtFinder:outputSavePanel];
  1075. }
  1076. }
  1077. } else {
  1078. NSAlert *alert = [[NSAlert alloc] init];
  1079. [alert setAlertStyle:NSAlertStyleCritical];
  1080. [alert setMessageText:[NSString stringWithFormat:NSLocalizedString(@"Failed to merge!", nil)]];
  1081. NSModalResponse response = [alert runModal];
  1082. if (response == 0 ) {
  1083. [NSApp endSheet:[self window] returnCode:[(NSView *)sender tag]];
  1084. [[self window] orderOut:self];
  1085. }
  1086. }
  1087. });
  1088. });
  1089. } else {
  1090. [self->_progress setHidden:YES];
  1091. [self.nCancelVC setEnabled:YES];
  1092. self.canMerge = YES;
  1093. self.loading_backgroundView.hidden = YES;
  1094. }
  1095. }];
  1096. } else {
  1097. NSAlert *alert = [[NSAlert alloc] init];
  1098. [alert setAlertStyle:NSAlertStyleCritical];
  1099. [alert setMessageText:[NSString stringWithFormat:NSLocalizedString(@"Failed to merge!", nil)]];
  1100. NSModalResponse response = [alert runModal];
  1101. if (response == 0 ) {
  1102. [NSApp endSheet:[self window] returnCode:[(NSView *)sender tag]];
  1103. [[self window] orderOut:self];
  1104. }
  1105. }
  1106. }
  1107. }
  1108. - (IBAction)buttonItemClicked_Clear:(id)sender
  1109. {
  1110. [_files removeAllObjects];
  1111. [self updateButtonState];
  1112. [self.tableView reloadData];
  1113. self.allFileSize = 0;
  1114. }
  1115. - (IBAction)buttonItemClicked_AddFile:(id)sender
  1116. {
  1117. NSMenu *menu = [[NSMenu alloc] init];
  1118. NSMenuItem *addFileItem = [[NSMenuItem alloc] initWithTitle:NSLocalizedString(@"add files", "") action:@selector(itemAddFileAction) keyEquivalent:@""];
  1119. addFileItem.target = self;
  1120. [menu addItem:addFileItem];
  1121. NSMenuItem *addFolderitem = [[NSMenuItem alloc] initWithTitle:NSLocalizedString(@"add Folder", "") action:@selector(itemAddFolderAction) keyEquivalent:@""];
  1122. addFolderitem.target = self;
  1123. [menu addItem:addFolderitem];
  1124. NSMenuItem *addOpenFileitem = [[NSMenuItem alloc] initWithTitle:NSLocalizedString(@"add Open Files", "") action:@selector(itemAddOpenFileAction) keyEquivalent:@""];
  1125. addOpenFileitem.target = self;
  1126. [menu addItem:addOpenFileitem];
  1127. [menu popUpMenuPositioningItem:nil atLocation:NSMakePoint(100, 10) inView:sender];
  1128. }
  1129. - (void)itemAddFileAction {
  1130. NSOpenPanel *openPanel = [[NSOpenPanel alloc] init];
  1131. [openPanel setAllowedFileTypes:[KMImageAccessoryController supportedImageTypes]];
  1132. [openPanel setAllowsMultipleSelection:YES];
  1133. openPanel.message = NSLocalizedString(@"Press ⌘/⇧ to select multiple files", nil);
  1134. [openPanel beginSheetModalForWindow:self.window completionHandler:^(NSInteger result){
  1135. if (result == NSModalResponseOK) {
  1136. NSMutableArray *array = [NSMutableArray array];
  1137. for (NSURL *fileURL in [openPanel URLs]) {
  1138. if (fileURL) {
  1139. if (![self isExistAtFilepath:fileURL.path]) {
  1140. continue;
  1141. }
  1142. NSDictionary *attrib = [[NSFileManager defaultManager] attributesOfItemAtPath:[fileURL path] error:nil];
  1143. CGFloat fileSize = [[attrib objectForKey:NSFileSize] floatValue];
  1144. self.allFileSize = self.allFileSize + fileSize;
  1145. [array addObject:fileURL.path];
  1146. }
  1147. }
  1148. [self addFiles:array];
  1149. }
  1150. }];
  1151. }
  1152. - (void)itemAddFolderAction {
  1153. NSOpenPanel *openPanel = [[NSOpenPanel alloc] init];
  1154. [openPanel setAllowedFileTypes:[KMImageAccessoryController supportedImageTypes]];
  1155. [openPanel setAllowsMultipleSelection:YES];
  1156. openPanel.canChooseFiles = NO;
  1157. openPanel.canChooseDirectories = YES;
  1158. openPanel.message = NSLocalizedString(@"Press ⌘/⇧ to select multiple files", nil);
  1159. [openPanel beginSheetModalForWindow:self.window completionHandler:^(NSInteger result){
  1160. if (result == NSModalResponseOK) {
  1161. NSMutableArray *result = [NSMutableArray array];
  1162. for (NSURL *url in openPanel.URLs) {
  1163. [self findAllFiles:url result:result];
  1164. }
  1165. NSMutableArray *array = [NSMutableArray array];
  1166. for (NSURL *fileURL in result) {
  1167. if (fileURL) {
  1168. if (![self isExistAtFilepath:fileURL.path]) {
  1169. continue;
  1170. }
  1171. NSDictionary *attrib = [[NSFileManager defaultManager] attributesOfItemAtPath:[fileURL path] error:nil];
  1172. CGFloat fileSize = [[attrib objectForKey:NSFileSize] floatValue];
  1173. self.allFileSize = self.allFileSize + fileSize;
  1174. [array addObject:fileURL.path];
  1175. }
  1176. }
  1177. [self addFiles:array];
  1178. }
  1179. }];
  1180. }
  1181. - (void)itemAddOpenFileAction {
  1182. [_lockFilePathArr removeAllObjects];
  1183. _lockFileIndex = 0;
  1184. for (NSWindow *window in NSApp.windows) {
  1185. NSWindowController *controller = [window windowController];
  1186. if (![controller isKindOfClass:[CTBrowserWindowController class]]) {
  1187. continue;
  1188. }
  1189. CTTabStripModel *model = ((CTBrowserWindowController *)controller).browser.tabStripModel;
  1190. for (int i = 0; i < model.count; i++) {
  1191. KMMainDocument *document = (KMMainDocument *)[model tabContentsAtIndex:i];
  1192. // if (document.windowControllers.count == 0) {
  1193. // break;
  1194. // }
  1195. if (document.isHome) {
  1196. continue;
  1197. }
  1198. KMMainViewController *mainViewController = document.mainViewController;
  1199. if ([self.PDFDocument.documentURL.path isEqualToString:document.fileURL.path]) {
  1200. continue;
  1201. }
  1202. if (![self isExistAtFilepath:document.fileURL.path]) {
  1203. continue;
  1204. }
  1205. CPDFDocument *pdfDocument = mainViewController.listView.document;
  1206. if (pdfDocument.isLocked || (!pdfDocument.allowsCopying || !pdfDocument.allowsPrinting)) {
  1207. [_lockFilePathArr addObject:pdfDocument.documentURL.path];
  1208. } else {
  1209. KMFileAttribute *file = [[KMFileAttribute alloc] init];
  1210. file.filePath = document.fileURL.path;
  1211. file.pdfDocument = mainViewController.document;
  1212. [_files addObject:file];
  1213. }
  1214. }
  1215. }
  1216. [self updateButtonState];
  1217. [_tableView reloadData];
  1218. if ([_lockFilePathArr count]) {
  1219. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  1220. if (self.files.count == 0) {
  1221. self.insertRow = 0;
  1222. } else {
  1223. self.insertRow = self.files.count;
  1224. }
  1225. [self openPasswordWindow];
  1226. });
  1227. }
  1228. }
  1229. - (void)findAllFiles:(NSURL *)folderURL result:(NSMutableArray *)result {
  1230. NSFileManager *fm = [NSFileManager defaultManager];
  1231. BOOL isDirectory = NO;
  1232. [fm fileExistsAtPath:folderURL.path isDirectory:&isDirectory];
  1233. if (!isDirectory) {
  1234. return;
  1235. }
  1236. NSArray *contents = [fm contentsOfDirectoryAtURL:folderURL includingPropertiesForKeys:nil options:0 error:nil];
  1237. if (contents.count == 0) {
  1238. return;
  1239. }
  1240. NSArray *array = @[@"jpg",@"cur",@"bmp",@"jpeg",@"gif",@"png",@"tiff",@"tif",/*@"pic",*/@"ico",@"icns",@"tga",@"psd",@"eps",@"hdr",@"jp2",@"jpc",@"pict",@"sgi",@"heic",@"pdf"];
  1241. for (NSURL *documentURL in contents) {
  1242. BOOL isDirectory = NO;
  1243. [fm fileExistsAtPath:documentURL.path isDirectory:&isDirectory];
  1244. if (isDirectory) {
  1245. [self findAllFiles:documentURL result:result];
  1246. } else {
  1247. if (![array containsObject:[documentURL.pathExtension lowercaseString]]) {
  1248. continue;
  1249. }
  1250. [result addObject:documentURL];
  1251. }
  1252. }
  1253. }
  1254. - (IBAction)buttonClicked_ChooseSize:(KMImageTitleButton *)sender {
  1255. int i = 0;
  1256. for (KMImageTitleButton *button in @[self.originalSizeButton, self.A4SizeButton, self.A3SizeButton,
  1257. self.USLetterSizeButton, self.USLegalButton, self.customSizeButton]) {
  1258. KMImageTitleButton *radio = button.subviews.lastObject;
  1259. radio.state = NSControlStateValueOff;
  1260. radio.imageName = @"btn_radio_unsel_on";
  1261. i += 1;
  1262. }
  1263. sender.state = NSControlStateValueOn;
  1264. sender.imageName = @"btn_radio_sel_on";
  1265. KMImageTitleButton *customSizeButton = self.customSizeButton.subviews.lastObject;
  1266. self.pageSizeHeightTextField.enabled = self.pageSizeWidthTextField.enabled = [sender isEqual:customSizeButton];
  1267. switch (sender.contentButton.tag) {
  1268. case 0:
  1269. break;
  1270. case 1:
  1271. self.newPageSize = CGSizeMake(595, 841);
  1272. break;
  1273. case 2:
  1274. self.newPageSize = CGSizeMake(841, 1190);
  1275. break;
  1276. case 3:
  1277. self.newPageSize = CGSizeMake(612, 792);
  1278. break;
  1279. case 4:
  1280. self.newPageSize = CGSizeMake(612, 1108);
  1281. break;
  1282. case 5:
  1283. self.newPageSize = CGSizeMake(595, 841);
  1284. self.pageSizeWidthTextField.stringValue = [NSString stringWithFormat:@"%.0f",self.newPageSize.width];
  1285. self.pageSizeHeightTextField.stringValue = [NSString stringWithFormat:@"%.0f",self.newPageSize.height];
  1286. break;
  1287. default:
  1288. break;
  1289. }
  1290. }
  1291. #pragma mark - Private Method
  1292. //获取所有的子节点
  1293. - (void)fetchAllOfChildren:(PDFOutline *)aOutline containerArray:(NSMutableArray *)aMArray
  1294. {
  1295. if (![aMArray containsObject:aOutline]) {
  1296. [aMArray addObject:aOutline];
  1297. }
  1298. for (NSInteger i = 0; i < aOutline.numberOfChildren; i++) {
  1299. [aMArray addObject:[aOutline childAtIndex:i]];
  1300. [self fetchAllOfChildren:[aOutline childAtIndex:i] containerArray:aMArray];
  1301. }
  1302. }
  1303. @end