CPDFPageEditViewController.m 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. //
  2. // CPDFPageEditViewController.m
  3. // compdfkit-tools
  4. //
  5. // THIS SOURCE CODE AND ANY ACCOMPANYING DOCUMENTATION ARE PROTECTED BY INTERNATIONAL COPYRIGHT LAW
  6. // AND MAY NOT BE RESOLD OR REDISTRIBUTED. USAGE IS BOUND TO THE ComPDFKit LICENSE AGREEMENT.
  7. // UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES.
  8. // This notice may not be removed from this file.
  9. //
  10. #import "CPDFPageEditViewController.h"
  11. #import "UIViewController+LeftItem.h"
  12. #import "CPDFColorUtils.h"
  13. #import "CPDFPageEditViewCell.h"
  14. #import "CPageEditToolBar.h"
  15. #import "CBlankPageModel.h"
  16. #import <ComPDFKit/ComPDFKit.h>
  17. @interface CPDFPageEditViewController () <CPageEditToolBarDelegate>
  18. @property (nonatomic, strong) UICollectionView *collectionView;
  19. @property (nonatomic, strong) UILabel *titleLabel;
  20. @property (nonatomic, strong) UIButton *backBtn;
  21. @property (nonatomic, strong) UIButton *doneBtn;
  22. @property (nonatomic, strong) UIButton *selectAllBtn;
  23. @property (nonatomic, strong) UIButton *editBtn;
  24. @property (nonatomic, strong) CPageEditToolBar *pageEditToolBar;
  25. @property (nonatomic, assign) BOOL isSelecAll;
  26. @property (nonatomic, assign) BOOL isEdit;
  27. @property (nonatomic, strong) UIView *headerView;
  28. @property (nonatomic, assign) BOOL isPageEdit;
  29. @end
  30. @implementation CPDFPageEditViewController
  31. #pragma mark - UIViewController Methods
  32. - (void)viewDidLoad {
  33. [super viewDidLoad];
  34. // Do any additional setup after loading the view.
  35. self.headerView = [[UIView alloc] init];
  36. self.headerView.layer.borderColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.1].CGColor;
  37. self.headerView.layer.borderWidth = 1.0;
  38. self.headerView.backgroundColor = [CPDFColorUtils CAnnotationPropertyViewControllerBackgoundColor];
  39. [self.view addSubview:self.headerView];
  40. self.titleLabel.text = NSLocalizedString(@"Page Edit", nil);
  41. self.titleLabel.textAlignment = NSTextAlignmentCenter;
  42. [self.backBtn setImage:[UIImage imageNamed:@"CPDFPageEitImageBack" inBundle:[NSBundle bundleForClass:self.class] compatibleWithTraitCollection:nil] forState:UIControlStateNormal];
  43. [self.headerView addSubview:self.titleLabel];
  44. [self.headerView addSubview:self.backBtn];
  45. self.editBtn = [[UIButton alloc] init];
  46. self.editBtn.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
  47. [self.editBtn setImage:[UIImage imageNamed:@"CPDFPageEitImageEdit" inBundle:[NSBundle bundleForClass:self.class] compatibleWithTraitCollection:nil] forState:UIControlStateNormal];
  48. [self.editBtn addTarget:self action:@selector(buttonItemClicked_edit:) forControlEvents:UIControlEventTouchUpInside];
  49. [self.headerView addSubview:self.editBtn];
  50. self.isEdit = NO;
  51. [self.collectionView registerClass:[CPDFPageEditViewCell class] forCellWithReuseIdentifier:@"pageEditCell"];
  52. self.collectionView.userInteractionEnabled = YES;
  53. self.collectionView.backgroundColor = [CPDFColorUtils CAnnotationSampleBackgoundColor];
  54. self.doneBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  55. [self.doneBtn setTitle:NSLocalizedString(@"Done", nil) forState:UIControlStateNormal];
  56. [self.doneBtn addTarget:self action:@selector(buttonItemClicked_done:) forControlEvents:UIControlEventTouchUpInside];
  57. [self.doneBtn setTitleColor:[UIColor colorWithRed:20.0/255.0 green:96.0/255.0 blue:243.0/255.0 alpha:1.0] forState:UIControlStateNormal];
  58. [self.headerView addSubview:self.doneBtn];
  59. self.selectAllBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  60. [self.selectAllBtn setImage:[UIImage imageNamed:@"CPDFPageEitImageSelectAll" inBundle:[NSBundle bundleForClass:self.class] compatibleWithTraitCollection:nil] forState:UIControlStateNormal];
  61. [self.selectAllBtn addTarget:self action:@selector(buttonItemClicked_selectAll:) forControlEvents:UIControlEventTouchUpInside];
  62. [self.headerView addSubview:self.selectAllBtn];
  63. UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressGestureRecognized:)];
  64. [self.collectionView addGestureRecognizer:longPress];
  65. self.view.backgroundColor = [CPDFColorUtils CAnnotationSampleBackgoundColor];
  66. self.pageEditToolBar.hidden = YES;
  67. self.doneBtn.hidden = YES;
  68. self.selectAllBtn.hidden = YES;
  69. self.isPageEdit = NO;
  70. }
  71. - (void)viewWillLayoutSubviews {
  72. if (@available(iOS 11.0, *)) {
  73. self.headerView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.safeAreaInsets.top + 50);
  74. self.titleLabel.frame = CGRectMake((self.view.frame.size.width - 120)/2, self.view.safeAreaInsets.top, 120, 50);
  75. self.collectionView.frame = CGRectMake(self.view.safeAreaInsets.left, self.view.safeAreaInsets.top + 60, self.view.frame.size.width - self.view.safeAreaInsets.left - self.view.safeAreaInsets.right, self.view.frame.size.height - 110 - self.view.safeAreaInsets.top);
  76. self.backBtn.frame = CGRectMake(self.view.safeAreaInsets.left, self.view.safeAreaInsets.top, 50, 50);
  77. self.editBtn.frame = CGRectMake(self.view.frame.size.width - self.view.safeAreaInsets.right - 70, self.view.safeAreaInsets.top, 50, 50);
  78. self.doneBtn.frame = CGRectMake(self.view.frame.size.width - self.view.safeAreaInsets.right - 70, self.view.safeAreaInsets.top, 50, 50);
  79. self.selectAllBtn.frame = CGRectMake(self.view.frame.size.width - self.view.safeAreaInsets.right - 130, self.view.safeAreaInsets.top, 50, 50);
  80. } else {
  81. self.headerView.frame = CGRectMake(0, 0, self.view.frame.size.width, 120);
  82. self.titleLabel.frame = CGRectMake((self.view.frame.size.width - 120)/2, 65, 120, 50);
  83. self.collectionView.frame = CGRectMake(0, 50, self.view.frame.size.width, self.view.frame.size.height - 110);
  84. self.backBtn.frame = CGRectMake(self.view.frame.size.width - 60, 65, 50, 50);
  85. self.editBtn.frame = CGRectMake(self.view.frame.size.width - 70, 65, 50, 50);
  86. self.doneBtn.frame = CGRectMake(self.view.frame.size.width - 70, 65, 50, 50);
  87. self.selectAllBtn.frame = CGRectMake(self.view.frame.size.width - 120, 65, 50, 50);
  88. }
  89. }
  90. #pragma mark - Action
  91. - (void)buttonItemClicked_edit:(UIButton *)button {
  92. self.isEdit = YES;
  93. self.editBtn.hidden = YES;
  94. self.isSelecAll = NO;
  95. CGFloat height = 44.0;
  96. if (@available(iOS 11.0, *))
  97. height += self.view.safeAreaInsets.bottom;
  98. self.pageEditToolBar = [[CPageEditToolBar alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - height, self.view.frame.size.width, height)];
  99. self.pageEditToolBar.pdfView = self.pdfView;
  100. self.pageEditToolBar.currentPageIndex = -1;
  101. self.pageEditToolBar.delegate = self;
  102. self.pageEditToolBar.currentPageIndex = 1;
  103. [self.view addSubview:self.pageEditToolBar];
  104. self.pageEditToolBar.hidden = NO;
  105. self.doneBtn.hidden = NO;
  106. self.selectAllBtn.hidden = NO;
  107. if (self.isEdit) {
  108. self.collectionView.allowsMultipleSelection = YES;
  109. } else {
  110. self.collectionView.allowsMultipleSelection = NO;
  111. }
  112. [self.collectionView reloadData];
  113. NSIndexPath *indexPath = [NSIndexPath indexPathForItem:self.pdfView.currentPageIndex inSection:0];
  114. [self.collectionView selectItemAtIndexPath:indexPath
  115. animated:NO
  116. scrollPosition:UICollectionViewScrollPositionCenteredVertically];
  117. [self updateTitle];
  118. }
  119. - (void)buttonItemClicked_done:(UIButton *)button {
  120. self.isEdit = NO;
  121. self.editBtn.hidden = NO;
  122. self.pageEditToolBar.hidden = YES;
  123. self.doneBtn.hidden = YES;
  124. self.selectAllBtn.hidden = YES;
  125. [self.collectionView reloadData];
  126. [self updateTitle];
  127. }
  128. - (void)buttonItemClicked_back:(UIButton *)button {
  129. [self dismissViewControllerAnimated:YES completion:^{
  130. if (self.isPageEdit) {
  131. [self.pdfView.document writeToURL:self.pdfView.document.documentURL];
  132. if (self.pageEditDelegate && [self.pageEditDelegate respondsToSelector:@selector(pageEditViewControllerDone:)]) {
  133. [self.pageEditDelegate pageEditViewControllerDone:self];
  134. }
  135. }
  136. }];
  137. }
  138. - (void)buttonItemClicked_selectAll:(UIButton *)button {
  139. self.isSelecAll = !self.isSelecAll;
  140. if (self.isSelecAll) {
  141. [self.selectAllBtn setImage:[UIImage imageNamed:@"CPDFPageEitImageSelectNoAll" inBundle:[NSBundle bundleForClass:self.class] compatibleWithTraitCollection:nil] forState:UIControlStateNormal];
  142. } else {
  143. [self.selectAllBtn setImage:[UIImage imageNamed:@"CPDFPageEitImageSelectAll" inBundle:[NSBundle bundleForClass:self.class] compatibleWithTraitCollection:nil] forState:UIControlStateNormal];
  144. }
  145. if (self.isSelecAll) {
  146. for (NSInteger section = 0; section < self.collectionView.numberOfSections; section++) {
  147. for (NSInteger item = 0; item < [self.collectionView numberOfItemsInSection:section]; item++) {
  148. NSIndexPath *indexPath = [NSIndexPath indexPathForItem:item inSection:section];
  149. [self.collectionView selectItemAtIndexPath:indexPath animated:NO scrollPosition:UICollectionViewScrollPositionNone];
  150. }
  151. }
  152. } else {
  153. for (NSInteger section = 0; section < self.collectionView.numberOfSections; section++) {
  154. for (NSInteger item = 0; item < [self.collectionView numberOfItemsInSection:section]; item++) {
  155. NSIndexPath *indexPath = [NSIndexPath indexPathForItem:item inSection:section];
  156. [self.collectionView deselectItemAtIndexPath:indexPath animated:YES];
  157. }
  158. }
  159. }
  160. [self updateTitle];
  161. }
  162. #pragma mark - Private Methods
  163. - (void)updateTitle {
  164. if (self.isEdit) {
  165. NSInteger count = [self.collectionView indexPathsForSelectedItems].count;
  166. self.titleLabel.text = [NSString stringWithFormat:@"%@ %ld",NSLocalizedString(@"Selected:", nil), (long)count];
  167. self.pageEditToolBar.isSelect = [self getIsSelect];
  168. } else {
  169. self.titleLabel.text = NSLocalizedString(@"Page Edit", nil);
  170. }
  171. }
  172. - (NSInteger)getMinSelectIndex {
  173. NSInteger min = self.pdfView.document.pageCount;
  174. for (NSIndexPath *indexPath in [self.collectionView indexPathsForSelectedItems]) {
  175. if (indexPath.item < min) {
  176. min = indexPath.item;
  177. }
  178. }
  179. return min;
  180. }
  181. - (NSInteger)getMaxSelectIndex {
  182. NSInteger max = -1;
  183. for (NSIndexPath *indexPath in [self.collectionView indexPathsForSelectedItems]) {
  184. if (indexPath.item > max) {
  185. max = indexPath.item;
  186. }
  187. }
  188. return max+1;
  189. }
  190. - (BOOL)getIsSelect {
  191. if ([self.collectionView indexPathsForSelectedItems].count > 0) {
  192. return YES;
  193. } else {
  194. return NO;
  195. }
  196. }
  197. #pragma mark - GestureRecognized
  198. - (void)longPressGestureRecognized:(UILongPressGestureRecognizer *)gestureRecognizer {
  199. switch (gestureRecognizer.state) {
  200. case UIGestureRecognizerStateBegan:
  201. {
  202. NSIndexPath *indexPath = [self.collectionView indexPathForItemAtPoint:[gestureRecognizer locationInView:self.collectionView]];
  203. if (indexPath == nil) {
  204. break;
  205. }
  206. [self.collectionView beginInteractiveMovementForItemAtIndexPath:indexPath];
  207. [UIView animateWithDuration:0.2 animations:^{
  208. [self.collectionView updateInteractiveMovementTargetPosition:[gestureRecognizer locationInView:self.collectionView]];
  209. }];
  210. }
  211. break;
  212. case UIGestureRecognizerStateChanged:
  213. {
  214. [self.collectionView updateInteractiveMovementTargetPosition:[gestureRecognizer locationInView:self.collectionView]];
  215. }
  216. break;
  217. case UIGestureRecognizerStateEnded:
  218. {
  219. [self.collectionView endInteractiveMovement];
  220. }
  221. break;
  222. default:
  223. {
  224. [self.collectionView cancelInteractiveMovement];
  225. }
  226. break;
  227. }
  228. }
  229. #pragma mark - UICollectionViewDataSource
  230. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  231. return self.pdfView.document.pageCount;
  232. }
  233. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  234. CPDFPageEditViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"pageEditCell" forIndexPath:indexPath];
  235. CPDFPage *page = [self.pdfView.document pageAtIndex:indexPath.item];
  236. CGSize pageSize = [self.pdfView.document pageSizeAtIndex:indexPath.item];
  237. CGFloat multiple = MAX(pageSize.width / 110, pageSize.height / 173);
  238. cell.imageSize = CGSizeMake(pageSize.width / multiple, pageSize.height / multiple);
  239. [cell setNeedsLayout];
  240. cell.imageView.image = [page thumbnailOfSize:CGSizeMake(pageSize.width / multiple, pageSize.height / multiple)];
  241. cell.textLabel.text = [NSString stringWithFormat:@"%@",@(indexPath.item+1)];
  242. [cell setEdit:self.isEdit];
  243. return cell;
  244. }
  245. - (BOOL)collectionView:(UICollectionView *)collectionView canMoveItemAtIndexPath:(NSIndexPath *)indexPath {
  246. return YES;
  247. }
  248. - (void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath*)destinationIndexPath {
  249. if (sourceIndexPath.item != destinationIndexPath.item) {
  250. [self.pdfView.document movePageAtIndex:sourceIndexPath.item withPageAtIndex:destinationIndexPath.item];
  251. self.isPageEdit = YES;
  252. }
  253. }
  254. #pragma mark - UICollectionViewDelegate
  255. - (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  256. return YES;
  257. }
  258. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  259. if (self.isEdit) {
  260. UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
  261. self.pageEditToolBar.currentPageIndex = indexPath.item;
  262. self.pageEditToolBar.isSelect = [self getIsSelect];
  263. [self updateTitle];
  264. [cell setSelected:YES];
  265. } else {
  266. if([self.pageEditDelegate respondsToSelector:@selector(pageEditViewController:pageIndex:)]) {
  267. [self.pageEditDelegate pageEditViewController:self pageIndex:indexPath.item];
  268. }
  269. [self dismissViewControllerAnimated:YES completion:^{
  270. }];
  271. }
  272. }
  273. - (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath {
  274. UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
  275. [self updateTitle];
  276. [cell setSelected:NO];
  277. }
  278. #pragma mark - CPageEditToolBarDelegate
  279. - (void)pageEditToolBarBlankPageInsert:(CPageEditToolBar *)pageEditToolBar pageModel:(CBlankPageModel *)pageModel {
  280. CGSize size = pageModel.size;
  281. if (pageModel.rotation == 1) {
  282. size = CGSizeMake(pageModel.size.height, pageModel.size.width);
  283. }
  284. NSInteger pageIndex = pageModel.pageIndex;
  285. if (pageModel.pageIndex == -2) {
  286. pageIndex = self.pdfView.document.pageCount;
  287. }
  288. [self.pdfView.document insertPage:size atIndex:pageIndex];
  289. [self.collectionView reloadData];
  290. [self.pageEditToolBar reloadData];
  291. [self updateTitle];
  292. self.isPageEdit = YES;
  293. NSIndexPath *indexPath = [NSIndexPath indexPathForItem:pageIndex inSection:0];
  294. [self.collectionView selectItemAtIndexPath:indexPath
  295. animated:NO
  296. scrollPosition:UICollectionViewScrollPositionCenteredVertically];
  297. }
  298. - (void)pageEditToolBarPDFInsert:(CPageEditToolBar *)pageEditToolBar pageModel:(CBlankPageModel *)pageModel document:(nonnull CPDFDocument *)document {
  299. [self.pdfView.document importPages:pageModel.indexSet fromDocument:document atIndex:pageModel.pageIndex];
  300. [self.collectionView reloadData];
  301. [pageModel.indexSet enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL * _Nonnull stop) {
  302. NSIndexPath *indexPath = [NSIndexPath indexPathForItem:idx+1 inSection:0];
  303. [self.collectionView selectItemAtIndexPath:indexPath
  304. animated:NO
  305. scrollPosition:UICollectionViewScrollPositionCenteredVertically];
  306. }];
  307. }
  308. - (void)pageEditToolBarExtract:(CPageEditToolBar *)pageEditToolBar {
  309. NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
  310. NSString *fileName = self.pdfView.document.documentURL.lastPathComponent.stringByDeletingPathExtension;
  311. NSString *filePath = [NSString stringWithFormat:@"%@/%@_Pages.pdf",path,fileName];
  312. NSMutableIndexSet *indexSet = [NSMutableIndexSet indexSet];
  313. for (NSIndexPath *indexPath in [self.collectionView indexPathsForSelectedItems]) {
  314. [indexSet addIndex:indexPath.item];
  315. }
  316. CPDFDocument *document = [[CPDFDocument alloc] init];
  317. [document importPages:indexSet fromDocument:self.pdfView.document atIndex:0];
  318. [document writeToURL:[NSURL fileURLWithPath:filePath]];
  319. NSString *message = [NSString stringWithFormat:NSLocalizedString(@"This file has been saved in:'Documents/'", nil)];
  320. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"OK", nil)
  321. style:UIAlertActionStyleCancel
  322. handler:nil];
  323. UIAlertController *alert = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"Successfully!", nil)
  324. message:message
  325. preferredStyle:UIAlertControllerStyleAlert];
  326. [alert addAction:cancelAction];
  327. [self presentViewController:alert animated:YES completion:nil];
  328. [self.pageEditToolBar reloadData];
  329. self.isPageEdit = YES;
  330. [self updateTitle];
  331. }
  332. - (void)pageEditToolBarRotate:(CPageEditToolBar *)pageEditToolBar {
  333. NSMutableIndexSet *indexSet = [NSMutableIndexSet indexSet];
  334. for (NSIndexPath *indexPath in [self.collectionView indexPathsForSelectedItems]) {
  335. CPDFPageEditViewCell *cell = (CPDFPageEditViewCell *)[self.collectionView cellForItemAtIndexPath:indexPath];
  336. cell.imageView.transform = CGAffineTransformRotate(cell.imageView.transform, M_PI/2);
  337. [indexSet addIndex:indexPath.item];
  338. CPDFPage *pPage = [self.pdfView.document pageAtIndex:indexPath.item];
  339. pPage.rotation += 90;
  340. CGSize pageSize = [self.pdfView.document pageSizeAtIndex:indexPath.item];
  341. CGFloat multiple = MAX(pageSize.width / 110, pageSize.height / 173);
  342. cell.imageSize = CGSizeMake(pageSize.width / multiple, pageSize.height / multiple);
  343. [cell setNeedsLayout];
  344. }
  345. [self updateTitle];
  346. self.isPageEdit = YES;
  347. }
  348. - (void)pageEditToolBarDelete:(CPageEditToolBar *)pageEditToolBar {
  349. NSInteger selectedCount = [self.collectionView indexPathsForSelectedItems].count;
  350. NSInteger totalCount = [self.collectionView numberOfItemsInSection:0];
  351. if (selectedCount == totalCount) {
  352. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"OK", nil)
  353. style:UIAlertActionStyleCancel
  354. handler:nil];
  355. UIAlertController *alert = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"Warning", nil)
  356. message:NSLocalizedString(@"Can not delete all pages.", nil)
  357. preferredStyle:UIAlertControllerStyleAlert];
  358. [alert addAction:cancelAction];
  359. [self presentViewController:alert animated:YES completion:nil];
  360. return;
  361. }
  362. NSMutableIndexSet *indexSet = [NSMutableIndexSet indexSet];
  363. for (NSIndexPath *indexPath in [self.collectionView indexPathsForSelectedItems]) {
  364. [indexSet addIndex:indexPath.item];
  365. }
  366. [self.collectionView deleteItemsAtIndexPaths:[self.collectionView indexPathsForSelectedItems]];
  367. [self.pdfView.document removePageAtIndexSet:indexSet];
  368. [self.pageEditToolBar reloadData];
  369. [self updateTitle];
  370. self.isPageEdit = YES;
  371. }
  372. - (void)pageEditToolBarReplace:(CPageEditToolBar *)pageEditToolBar document:(CPDFDocument *)document {
  373. NSInteger min = [self getMinSelectIndex];
  374. NSMutableIndexSet *indexSet = [NSMutableIndexSet indexSet];
  375. for (int i = 0; i < document.pageCount; i++) {
  376. [indexSet addIndex:i];
  377. }
  378. NSMutableIndexSet *deleteIndexSet = [NSMutableIndexSet indexSet];
  379. for (NSIndexPath *indexPath in [self.collectionView indexPathsForSelectedItems]) {
  380. [deleteIndexSet addIndex:indexPath.item];
  381. }
  382. [self.collectionView deleteItemsAtIndexPaths:[self.collectionView indexPathsForSelectedItems]];
  383. [self.pdfView.document removePageAtIndexSet:deleteIndexSet];
  384. [self.pdfView.document importPages:indexSet fromDocument:document atIndex:min];
  385. [self.collectionView reloadData];
  386. [self.pageEditToolBar reloadData];
  387. [self updateTitle];
  388. self.isPageEdit = YES;
  389. }
  390. - (void)pageEditToolBarCopy:(CPageEditToolBar *)pageEditToolBar {
  391. NSInteger max = [self getMaxSelectIndex];
  392. NSMutableIndexSet *indexSet = [NSMutableIndexSet indexSet];
  393. for (NSIndexPath *indexPath in [self.collectionView indexPathsForSelectedItems]) {
  394. [indexSet addIndex:indexPath.item];
  395. }
  396. [self.pdfView.document importPages:indexSet fromDocument:self.pdfView.document atIndex:max];
  397. [self.collectionView reloadData];
  398. [self.pageEditToolBar reloadData];
  399. [self updateTitle];
  400. self.isPageEdit = YES;
  401. }
  402. @end