瀏覽代碼

Merge branch 'compdfkit_demo_ios' of git.kdan.cc:others/kmpdfkit_demo into compdfkit_demo_ios

chenyu 1 年之前
父節點
當前提交
1d9bbe96f1
共有 19 個文件被更改,包括 463 次插入99 次删除
  1. 3 0
      PageEdit-Ctrl-Demo/PageEdit-Ctrl-Demo/Assets.xcassets/CPDFThunbnailImageEnter.imageset/Contents.json
  2. 65 1
      PageEdit-Ctrl-Demo/PageEdit-Ctrl-Demo/CPDFViewController.m
  3. 2 0
      compdfkit-tools/compdfkit-tools/Common/Utils/CPDFColorUtils.h
  4. 8 0
      compdfkit-tools/compdfkit-tools/Common/Utils/CPDFColorUtils.m
  5. 38 0
      compdfkit-tools/compdfkit-tools/Common/Utils/UtilsColor.xcassets/CPageEditToolbarFontColor.colorset/Contents.json
  6. 2 0
      compdfkit-tools/compdfkit-tools/PageEdit/Control/CPDFPDFInsertViewController.h
  7. 75 17
      compdfkit-tools/compdfkit-tools/PageEdit/Control/CPDFPDFInsertViewController.m
  8. 2 0
      compdfkit-tools/compdfkit-tools/PageEdit/Control/CPDFPageEditViewController.h
  9. 116 28
      compdfkit-tools/compdfkit-tools/PageEdit/Control/CPDFPageEditViewController.m
  10. 2 0
      compdfkit-tools/compdfkit-tools/PageEdit/Control/CPDFPageInsertViewController.h
  11. 41 12
      compdfkit-tools/compdfkit-tools/PageEdit/Control/CPDFPageInsertViewController.m
  12. 9 7
      compdfkit-tools/compdfkit-tools/PageEdit/Views/CInsertBlankPageCell.h
  13. 19 5
      compdfkit-tools/compdfkit-tools/PageEdit/Views/CInsertBlankPageCell.m
  14. 1 1
      compdfkit-tools/compdfkit-tools/PageEdit/Views/CPDFPageEditViewCell.h
  15. 3 4
      compdfkit-tools/compdfkit-tools/PageEdit/Views/CPDFPageEditViewCell.m
  16. 2 0
      compdfkit-tools/compdfkit-tools/PageEdit/Views/CPageEditToolBar.h
  17. 72 18
      compdfkit-tools/compdfkit-tools/PageEdit/Views/CPageEditToolBar.m
  18. 3 5
      compdfkit-tools/compdfkit-tools/Viewer/PDFThumbnail/CPDFThumbnailViewCell.m
  19. 0 1
      viewer-ctrl-demo/viewer-ctrl-demo/CPDFViewController.m

+ 3 - 0
PageEdit-Ctrl-Demo/PageEdit-Ctrl-Demo/Assets.xcassets/CPDFThunbnailImageEnter.imageset/Contents.json

@@ -18,5 +18,8 @@
   "info" : {
     "author" : "xcode",
     "version" : 1
+  },
+  "properties" : {
+    "template-rendering-intent" : "original"
   }
 }

+ 65 - 1
PageEdit-Ctrl-Demo/PageEdit-Ctrl-Demo/CPDFViewController.m

@@ -15,7 +15,9 @@
 #import <ComPDFKit/ComPDFKit.h>
 #import <compdfkit_tools/compdfkit_tools.h>
  
-@interface CPDFViewController () <CPDFPageEditViewControllerDelegate>
+@interface CPDFViewController () <CPDFPageEditViewControllerDelegate, CPDFSignatureViewControllerDelegate>
+
+@property (nonatomic, strong) CPDFSignatureWidgetAnnotation * signatureAnnotation;
 
 @end
 
@@ -27,6 +29,19 @@
     self.view.backgroundColor = [CPDFColorUtils CPDFViewControllerBackgroundColor];
 }
 
+- (void)viewWillLayoutSubviews {
+    [super viewWillLayoutSubviews];
+    
+    if([self.popMenu superview]) {
+        if (@available(iOS 11.0, *)) {
+            [self.popMenu showMenuInRect:CGRectMake(self.view.frame.size.width - self.view.safeAreaInsets.right - 250, CGRectGetMaxY(self.navigationController.navigationBar.frame), 250, 200)];
+        } else {
+            // Fallback on earlier versions
+            [self.popMenu showMenuInRect:CGRectMake(self.view.frame.size.width - 250, CGRectGetMaxY(self.navigationController.navigationBar.frame), 250, 200)];
+        }
+    }
+}
+
 #pragma mark - Private Mehtods
 
 - (void)initWitNavigationTitle {
@@ -55,4 +70,53 @@
     }];
 }
 
+- (void)pageEditViewController:(CPDFPageEditViewController *)pageEditViewController pageIndex:(NSInteger)pageIndex {
+    [self.pdfListView goToPageIndex:pageIndex animated:NO];
+    [self.navigationController popViewControllerAnimated:YES];
+}
+
+#pragma mark - CPDFListViewDelegate
+
+- (void)PDFListViewPerformTouchEnded:(CPDFListView *)pdfView {
+    CGFloat tPosY = 0;
+    if (self.navigationController.navigationBarHidden) {
+        [self.navigationController setNavigationBarHidden:NO animated:YES];
+        [UIView animateWithDuration:0.3 animations:^{
+            self.pdfListView.pageSliderView.alpha = 1.0;
+        }];
+        CGRect rectStatus = [[UIApplication sharedApplication] statusBarFrame];
+        tPosY = self.navigationController.navigationBar.frame.size.height + rectStatus.size.height;
+
+    } else {
+        [self.navigationController setNavigationBarHidden:YES animated:YES];
+        [UIView animateWithDuration:0.3 animations:^{
+            self.pdfListView.pageSliderView.alpha = 0.0;
+        }];
+    }
+}
+
+- (void)PDFListViewPerformSignatureWidget:(CPDFListView *)pdfView forAnnotation:(CPDFSignatureWidgetAnnotation *)annotation {
+    self.signatureAnnotation = annotation;
+    AAPLCustomPresentationController *presentationController NS_VALID_UNTIL_END_OF_SCOPE;
+    CPDFSignatureViewController *signatureVC = [[CPDFSignatureViewController alloc] initWithStyle:nil];
+    presentationController = [[AAPLCustomPresentationController alloc] initWithPresentedViewController:signatureVC presentingViewController:self];
+    signatureVC.delegate = self;
+    signatureVC.transitioningDelegate = presentationController;
+    [self presentViewController:signatureVC animated:YES completion:nil];
+}
+
+#pragma mark - CPDFSignatureViewControllerDelegate
+
+- (void)signatureViewControllerDismiss:(CPDFSignatureViewController *)signatureViewController {
+    self.signatureAnnotation = nil;
+}
+
+- (void)signatureViewController:(CPDFSignatureViewController *)signatureViewController image:(UIImage *)image {
+    if(self.signatureAnnotation) {
+        [self.signatureAnnotation signWithImage:image];
+        [self.pdfListView setNeedsDisplayForPage:self.signatureAnnotation.page];
+        self.signatureAnnotation = nil;
+    }
+}
+
 @end

+ 2 - 0
compdfkit-tools/compdfkit-tools/Common/Utils/CPDFColorUtils.h

@@ -34,6 +34,8 @@ NS_ASSUME_NONNULL_BEGIN
 
 + (UIColor *)CViewBackgroundColor;
 
++ (UIColor *)CPageEditToolbarFontColor;
+
 @end
 
 NS_ASSUME_NONNULL_END

+ 8 - 0
compdfkit-tools/compdfkit-tools/Common/Utils/CPDFColorUtils.m

@@ -86,4 +86,12 @@
     }
 }
 
++ (UIColor *)CPageEditToolbarFontColor {
+    if (@available(iOS 13.0, *)) {
+        return [UIColor colorNamed:@"CPageEditToolbarFontColor" inBundle:[NSBundle bundleForClass:self.class] compatibleWithTraitCollection:nil];
+    } else {
+        return [UIColor colorWithRed:61.0/255.0 green:71.0/255.0 blue:77.0/255.0 alpha:1.0];
+    }
+}
+
 @end

+ 38 - 0
compdfkit-tools/compdfkit-tools/Common/Utils/UtilsColor.xcassets/CPageEditToolbarFontColor.colorset/Contents.json

@@ -0,0 +1,38 @@
+{
+  "colors" : [
+    {
+      "color" : {
+        "color-space" : "srgb",
+        "components" : {
+          "alpha" : "1.000",
+          "blue" : "77",
+          "green" : "71",
+          "red" : "18"
+        }
+      },
+      "idiom" : "universal"
+    },
+    {
+      "appearances" : [
+        {
+          "appearance" : "luminosity",
+          "value" : "dark"
+        }
+      ],
+      "color" : {
+        "color-space" : "srgb",
+        "components" : {
+          "alpha" : "1.000",
+          "blue" : "255",
+          "green" : "255",
+          "red" : "255"
+        }
+      },
+      "idiom" : "universal"
+    }
+  ],
+  "info" : {
+    "author" : "xcode",
+    "version" : 1
+  }
+}

+ 2 - 0
compdfkit-tools/compdfkit-tools/PageEdit/Control/CPDFPDFInsertViewController.h

@@ -22,6 +22,8 @@ NS_ASSUME_NONNULL_BEGIN
 
 - (void)pdfInsertViewControllerSave:(CPDFPDFInsertViewController *)PageInsertViewController pageModel:(CBlankPageModel *)pageModel;
 
+- (void)pdfInsertViewControllerCancel:(CPDFPDFInsertViewController *)PageInsertViewController;
+
 @end
 
 @interface CPDFPDFInsertViewController : UIViewController

+ 75 - 17
compdfkit-tools/compdfkit-tools/PageEdit/Control/CPDFPDFInsertViewController.m

@@ -35,8 +35,12 @@
 
 @property (nonatomic, strong) UIButton *selectRangeBtn;
 
+@property (nonatomic, strong) CInsertBlankPageCell *rangePreCell;
+
 @property (nonatomic, strong) UIButton *selectLocationBtn;
 
+@property (nonatomic, strong) CInsertBlankPageCell *locationPreCell;
+
 @property (nonatomic, strong) NSMutableArray *pageLoactionBtns;
 
 @property (nonatomic, strong) NSMutableArray *pageRangeBtns;
@@ -108,7 +112,7 @@
     self.tableView.delegate = self;
     self.tableView.dataSource = self;
     self.tableView.rowHeight = 60.0;
-    self.tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
+    self.tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
     self.tableView.backgroundColor = [CPDFColorUtils CAnnotationSampleBackgoundColor];
     [self.view addSubview:self.tableView];
     
@@ -116,8 +120,13 @@
     self.view.backgroundColor = [CPDFColorUtils CAnnotationPropertyViewControllerBackgoundColor];
     
     self.pageModel = [[CBlankPageModel alloc] init];
-    self.pageModel.pageIndex = 1;
+    self.pageModel.pageIndex = 0;
     self.pageModel.rotation = 0;
+    NSMutableIndexSet *indexSet = [NSMutableIndexSet indexSet];
+    for (int i = 0; i < self.document.pageCount; i++) {
+        [indexSet addIndex:i];
+    }
+    self.pageModel.indexSet = indexSet;
     
     self.pageLoactionBtns = [NSMutableArray array];
     self.pageRangeBtns = [NSMutableArray array];
@@ -157,7 +166,11 @@
 }
 
 - (void)buttonItemClicked_cancel:(id)sender {
-    [self dismissViewControllerAnimated:YES completion:nil];
+    [self dismissViewControllerAnimated:YES completion:^{
+        if (self.delegate && [self.delegate respondsToSelector:@selector(pdfInsertViewControllerCancel:)]) {
+            [self.delegate pdfInsertViewControllerCancel:self];
+        }
+    }];
 }
 
 #pragma mark - UITableViewDataSource
@@ -182,12 +195,26 @@
         case 1:
             [cell setCellStyle:CInsertBlankPageCellLocation label:self.dataArray[indexPath.row]];
             break;
-        case 2 ... 5:
+        case 2:
+        {
+            [cell setCellStyle:CInsertBlankPageCellRangeSelect label:self.dataArray[indexPath.row]];
+            if (![self.pageRangeBtns containsObject:cell.rangeSelectBtn]) {
+                [self.pageRangeBtns addObject:cell.rangeSelectBtn];
+            }
+            
+            self.rangePreCell = cell;
+            cell.rangeSelectBtn.selected = !cell.rangeSelectBtn.selected;
+            cell.rangeSelectLabel.textColor = [CPDFColorUtils CPageEditToolbarFontColor];
+            cell.separatorInset = UIEdgeInsetsMake(0, self.view.bounds.size.width, 0, 0);
+        }
+            break;
+        case 3 ... 5:
         {
             [cell setCellStyle:CInsertBlankPageCellRangeSelect label:self.dataArray[indexPath.row]];
             if (![self.pageRangeBtns containsObject:cell.rangeSelectBtn]) {
                 [self.pageRangeBtns addObject:cell.rangeSelectBtn];
             }
+            cell.separatorInset = UIEdgeInsetsMake(0, self.view.bounds.size.width, 0, 0);
         }
             break;
         case 6:
@@ -196,12 +223,28 @@
         case 7:
             [cell setCellStyle:CInsertBlankPageCellLocation label:self.dataArray[indexPath.row]];
             break;
-        case 8 ... 10:
+        case 8:
+        {
+            [cell setCellStyle:CInsertBlankPageCellLocationSelect label:self.dataArray[indexPath.row]];
+            if (![self.pageLoactionBtns containsObject:cell.locationSelectBtn]) {
+                [self.pageLoactionBtns addObject:cell.locationSelectBtn];
+            }
+            
+            if (!self.currentPageIndex) {
+                self.locationPreCell = cell;
+                cell.locationSelectBtn.selected = !cell.locationSelectBtn.selected;
+                cell.locationSelectLabel.textColor = [CPDFColorUtils CPageEditToolbarFontColor];
+            }
+            cell.separatorInset = UIEdgeInsetsMake(0, self.view.bounds.size.width, 0, 0);
+        }
+            break;
+        case 9 ... 10:
         {
             [cell setCellStyle:CInsertBlankPageCellLocationSelect label:self.dataArray[indexPath.row]];
             if (![self.pageLoactionBtns containsObject:cell.locationSelectBtn]) {
                 [self.pageLoactionBtns addObject:cell.locationSelectBtn];
             }
+            cell.separatorInset = UIEdgeInsetsMake(0, self.view.bounds.size.width, 0, 0);
         }
             break;
         case 11:
@@ -209,8 +252,10 @@
             [cell setCellStyle:CInsertBlankPageCellLocationTextFiled label:self.dataArray[indexPath.row]];
             if (self.currentPageIndex) {
                 cell.locationTextField.text = [NSString stringWithFormat:@"%lu", self.currentPageIndex];
+                self.pageModel.pageIndex = self.currentPageIndex;
             }
             self.locationTextField = cell.locationTextField;
+            cell.separatorInset = UIEdgeInsetsMake(0, self.view.bounds.size.width, 0, 0);
         }
             break;
         case 12:
@@ -219,6 +264,7 @@
             if (![self.pageLoactionBtns containsObject:cell.locationSelectBtn]) {
                 [self.pageLoactionBtns addObject:cell.locationSelectBtn];
             }
+            cell.separatorInset = UIEdgeInsetsMake(0, self.view.bounds.size.width, 0, 0);
         }
             break;
             
@@ -232,17 +278,21 @@
 #pragma mark - CInsertBlankPageCellDelegate
 
 - (void)insertBlankPageCellRange:(CInsertBlankPageCell *)insertBlankPageCell button:(UIButton *)buttom {
-    if (self.selectRangeBtn) {
-        self.selectRangeBtn.selected = !self.selectRangeBtn.selected;
-        if (self.selectRangeBtn != buttom) {
+    if (self.rangePreCell) {
+        self.rangePreCell.rangeSelectBtn.selected = !self.rangePreCell.rangeSelectBtn.selected;
+        self.rangePreCell.rangeSelectLabel.textColor = [UIColor grayColor];
+        if (self.rangePreCell.rangeSelectBtn != buttom) {
             buttom.selected = !buttom.selected;
-            self.selectRangeBtn = buttom;
+            insertBlankPageCell.rangeSelectLabel.textColor = [CPDFColorUtils CPageEditToolbarFontColor];
+            self.rangePreCell = insertBlankPageCell;
         } else {
-            self.selectRangeBtn = nil;
+            self.rangePreCell.rangeSelectLabel.textColor = [UIColor grayColor];
+            self.rangePreCell = nil;
         }
     } else {
-        self.selectRangeBtn = buttom;
+        self.rangePreCell = insertBlankPageCell;
         buttom.selected = !buttom.selected;
+        insertBlankPageCell.rangeSelectLabel.textColor = [CPDFColorUtils CPageEditToolbarFontColor];
     }
     
     NSInteger range = [self.pageRangeBtns indexOfObject:buttom];
@@ -254,6 +304,7 @@
                 [indexSet addIndex:i];
             }
             self.pageModel.indexSet = indexSet;
+            self.rangeTextField.text = @"";
         }
             break;
         case 1:
@@ -263,6 +314,7 @@
                 [indexSet addIndex:i];
             }
             self.pageModel.indexSet = indexSet;
+            self.rangeTextField.text = @"";
         }
             break;
         case 2:
@@ -281,26 +333,32 @@
 }
 
 - (void)insertBlankPageCellLocation:(CInsertBlankPageCell *)insertBlankPageCell button:(UIButton *)buttom {
-    if (self.selectLocationBtn) {
-        self.selectLocationBtn.selected = !self.selectLocationBtn.selected;
-        if (self.selectLocationBtn != buttom) {
+    if (self.locationPreCell) {
+        self.locationPreCell.locationSelectBtn.selected = !self.locationPreCell.locationSelectBtn.selected;
+        self.locationPreCell.locationSelectLabel.textColor = [UIColor grayColor];
+        if (self.locationPreCell.locationSelectBtn != buttom) {
             buttom.selected = !buttom.selected;
-            self.selectLocationBtn = buttom;
+            insertBlankPageCell.locationSelectLabel.textColor = [CPDFColorUtils CPageEditToolbarFontColor];
+            self.locationPreCell = insertBlankPageCell;
         } else {
-            self.selectLocationBtn = nil;
+            self.locationPreCell.locationSelectLabel.textColor = [UIColor grayColor];
+            self.locationPreCell = nil;
         }
     } else {
-        self.selectLocationBtn = buttom;
+        self.locationPreCell = insertBlankPageCell;
         buttom.selected = !buttom.selected;
+        insertBlankPageCell.locationSelectLabel.textColor = [CPDFColorUtils CPageEditToolbarFontColor];
     }
     
     NSInteger location = [self.pageLoactionBtns indexOfObject:buttom];
     switch (location) {
         case 0:
             self.pageModel.pageIndex = 0;
+            self.locationTextField.text = @"";
             break;
         case 1:
             self.pageModel.pageIndex = -2;
+            self.locationTextField.text = @"";
             break;
         case 2:
             self.pageModel.pageIndex = self.pageModel.pageIndex - 1;

+ 2 - 0
compdfkit-tools/compdfkit-tools/PageEdit/Control/CPDFPageEditViewController.h

@@ -20,6 +20,8 @@ NS_ASSUME_NONNULL_BEGIN
 
 - (void)pageEditViewControllerDone:(CPDFPageEditViewController *)pageEditViewController;
 
+- (void)pageEditViewController:(CPDFPageEditViewController *)pageEditViewController pageIndex:(NSInteger)pageIndex;
+
 @end
 
 @interface CPDFPageEditViewController : CPDFThumbnailViewController

+ 116 - 28
compdfkit-tools/compdfkit-tools/PageEdit/Control/CPDFPageEditViewController.m

@@ -39,6 +39,8 @@
 
 @property (nonatomic, strong) UIView *headerView;
 
+@property (nonatomic, assign) BOOL isPageEdit;
+
 @end
 
 @implementation CPDFPageEditViewController
@@ -70,14 +72,28 @@
     self.isEdit = NO;
     
     [self.collectionView registerClass:[CPDFPageEditViewCell class] forCellWithReuseIdentifier:@"pageEditCell"];
-    self.collectionView.allowsMultipleSelection = YES;
     self.collectionView.userInteractionEnabled = YES;
     self.collectionView.backgroundColor = [CPDFColorUtils CAnnotationSampleBackgoundColor];
+        
+    self.doneBtn = [UIButton buttonWithType:UIButtonTypeCustom];
+    [self.doneBtn setTitle:NSLocalizedString(@"Done", nil) forState:UIControlStateNormal];
+    [self.doneBtn addTarget:self action:@selector(buttonItemClicked_done:) forControlEvents:UIControlEventTouchUpInside];
+    [self.doneBtn setTitleColor:[UIColor colorWithRed:20.0/255.0 green:96.0/255.0 blue:243.0/255.0 alpha:1.0] forState:UIControlStateNormal];
+    [self.headerView addSubview:self.doneBtn];
+    
+    self.selectAllBtn = [UIButton buttonWithType:UIButtonTypeCustom];
+    [self.selectAllBtn setImage:[UIImage imageNamed:@"CPDFPageEitImageSelectAll" inBundle:[NSBundle bundleForClass:self.class] compatibleWithTraitCollection:nil] forState:UIControlStateNormal];
+    [self.selectAllBtn addTarget:self action:@selector(buttonItemClicked_selectAll:) forControlEvents:UIControlEventTouchUpInside];
+    [self.headerView addSubview:self.selectAllBtn];
     
     UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressGestureRecognized:)];
     [self.collectionView addGestureRecognizer:longPress];
     
     self.view.backgroundColor = [CPDFColorUtils CAnnotationSampleBackgoundColor];
+    self.pageEditToolBar.hidden = YES;
+    self.doneBtn.hidden = YES;
+    self.selectAllBtn.hidden = YES;
+    self.isPageEdit = NO;
 }
 
 - (void)viewWillLayoutSubviews {
@@ -87,9 +103,9 @@
         self.titleLabel.frame = CGRectMake((self.view.frame.size.width - 120)/2, self.view.safeAreaInsets.top, 120, 50);
         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);
         self.backBtn.frame = CGRectMake(self.view.safeAreaInsets.left, self.view.safeAreaInsets.top, 50, 50);
-        self.editBtn.frame = CGRectMake(self.view.frame.size.width - self.view.safeAreaInsets.right - 50, self.view.safeAreaInsets.top, 50, 50);
-        self.doneBtn.frame = CGRectMake(self.view.frame.size.width - self.view.safeAreaInsets.right - 50, self.view.safeAreaInsets.top, 50, 50);
-        self.selectAllBtn.frame = CGRectMake(self.view.frame.size.width - self.view.safeAreaInsets.right - 110, self.view.safeAreaInsets.top, 50, 50);
+        self.editBtn.frame = CGRectMake(self.view.frame.size.width - self.view.safeAreaInsets.right - 70, self.view.safeAreaInsets.top, 50, 50);
+        self.doneBtn.frame = CGRectMake(self.view.frame.size.width - self.view.safeAreaInsets.right - 70, self.view.safeAreaInsets.top, 50, 50);
+        self.selectAllBtn.frame = CGRectMake(self.view.frame.size.width - self.view.safeAreaInsets.right - 130, self.view.safeAreaInsets.top, 50, 50);
     } else {
         self.headerView.frame = CGRectMake(0, 0, self.view.frame.size.width, 120);
         self.titleLabel.frame = CGRectMake((self.view.frame.size.width - 120)/2, 65, 120, 50);
@@ -104,10 +120,10 @@
 #pragma mark - Action
 
 - (void)buttonItemClicked_edit:(UIButton *)button {
-    self.isEdit = !self.isEdit;
+    self.isEdit = YES;
     self.editBtn.hidden = YES;
     self.isSelecAll = NO;
-
+    
     CGFloat height = 44.0;
     if (@available(iOS 11.0, *))
         height += self.view.safeAreaInsets.bottom;
@@ -116,27 +132,48 @@
     self.pageEditToolBar.pdfView = self.pdfView;
     self.pageEditToolBar.currentPageIndex = -1;
     self.pageEditToolBar.delegate = self;
+    self.pageEditToolBar.currentPageIndex = 1;
     [self.view addSubview:self.pageEditToolBar];
-        
-    self.doneBtn = [UIButton buttonWithType:UIButtonTypeCustom];
-    [self.doneBtn setTitle:NSLocalizedString(@"Done", nil) forState:UIControlStateNormal];
-    [self.doneBtn addTarget:self action:@selector(buttonItemClicked_done:) forControlEvents:UIControlEventTouchUpInside];
-    [self.doneBtn setTitleColor:[UIColor colorWithRed:20.0/255.0 green:96.0/255.0 blue:243.0/255.0 alpha:1.0] forState:UIControlStateNormal];
-    [self.headerView addSubview:self.doneBtn];
     
-    self.selectAllBtn = [UIButton buttonWithType:UIButtonTypeCustom];
-    [self.selectAllBtn setImage:[UIImage imageNamed:@"CPDFPageEitImageSelectNoAll" inBundle:[NSBundle bundleForClass:self.class] compatibleWithTraitCollection:nil] forState:UIControlStateNormal];
-    [self.selectAllBtn addTarget:self action:@selector(buttonItemClicked_selectAll:) forControlEvents:UIControlEventTouchUpInside];
-    [self.headerView addSubview:self.selectAllBtn];
+    self.pageEditToolBar.hidden = NO;
+    self.doneBtn.hidden = NO;
+    self.selectAllBtn.hidden = NO;
+    
+    if (self.isEdit) {
+        self.collectionView.allowsMultipleSelection = YES;
+    } else {
+        self.collectionView.allowsMultipleSelection = NO;
+    }
     
     [self.collectionView reloadData];
+    NSIndexPath *indexPath = [NSIndexPath indexPathForItem:self.pdfView.currentPageIndex inSection:0];
+    [self.collectionView selectItemAtIndexPath:indexPath
+                                      animated:NO
+                                scrollPosition:UICollectionViewScrollPositionCenteredVertically];
+    [self updateTitle];
+
 }
 
 - (void)buttonItemClicked_done:(UIButton *)button {
+    self.isEdit = NO;
+    self.editBtn.hidden = NO;
+    
+    self.pageEditToolBar.hidden = YES;
+    self.doneBtn.hidden = YES;
+    self.selectAllBtn.hidden = YES;
+    
+    [self.collectionView reloadData];
+    
+    [self updateTitle];
+}
+
+- (void)buttonItemClicked_back:(UIButton *)button {
     [self dismissViewControllerAnimated:YES completion:^{
-        [self.pdfView.document writeToURL:self.pdfView.document.documentURL];
-        if (self.pageEditDelegate && [self.pageEditDelegate respondsToSelector:@selector(pageEditViewControllerDone:)]) {
-            [self.pageEditDelegate pageEditViewControllerDone:self];
+        if (self.isPageEdit) {
+            [self.pdfView.document writeToURL:self.pdfView.document.documentURL];
+            if (self.pageEditDelegate && [self.pageEditDelegate respondsToSelector:@selector(pageEditViewControllerDone:)]) {
+                [self.pageEditDelegate pageEditViewControllerDone:self];
+            }
         }
     }];
 }
@@ -144,9 +181,9 @@
 - (void)buttonItemClicked_selectAll:(UIButton *)button {
     self.isSelecAll = !self.isSelecAll;
     if (self.isSelecAll) {
-        [self.selectAllBtn setImage:[UIImage imageNamed:@"CPDFPageEitImageSelectAll" inBundle:[NSBundle bundleForClass:self.class] compatibleWithTraitCollection:nil] forState:UIControlStateNormal];
-    } else {
         [self.selectAllBtn setImage:[UIImage imageNamed:@"CPDFPageEitImageSelectNoAll" inBundle:[NSBundle bundleForClass:self.class] compatibleWithTraitCollection:nil] forState:UIControlStateNormal];
+    } else {
+        [self.selectAllBtn setImage:[UIImage imageNamed:@"CPDFPageEitImageSelectAll" inBundle:[NSBundle bundleForClass:self.class] compatibleWithTraitCollection:nil] forState:UIControlStateNormal];
     }
     
     if (self.isSelecAll) {
@@ -173,6 +210,7 @@
     if (self.isEdit) {
         NSInteger count = [self.collectionView indexPathsForSelectedItems].count;
         self.titleLabel.text = [NSString stringWithFormat:@"%@ %ld",NSLocalizedString(@"Selected:", nil), (long)count];
+        self.pageEditToolBar.isSelect = [self getIsSelect];
     } else {
         self.titleLabel.text = NSLocalizedString(@"Page Edit", nil);
     }
@@ -200,6 +238,14 @@
     return max+1;
 }
 
+- (BOOL)getIsSelect {
+    if ([self.collectionView indexPathsForSelectedItems].count > 0) {
+        return YES;
+    } else {
+        return NO;
+    }
+}
+
 #pragma mark - GestureRecognized
 
 - (void)longPressGestureRecognized:(UILongPressGestureRecognizer *)gestureRecognizer {
@@ -246,11 +292,11 @@
     CGSize pageSize = [self.pdfView.document pageSizeAtIndex:indexPath.item];
     CGFloat multiple = MAX(pageSize.width / 110, pageSize.height / 173);
     
-    cell.editing = self.isEdit;
     cell.imageSize = CGSizeMake(pageSize.width / multiple, pageSize.height / multiple);
     [cell setNeedsLayout];
     cell.imageView.image = [page thumbnailOfSize:CGSizeMake(pageSize.width / multiple, pageSize.height / multiple)];
     cell.textLabel.text = [NSString stringWithFormat:@"%@",@(indexPath.item+1)];
+    [cell setEdit:self.isEdit];
     return cell;
 }
 
@@ -261,6 +307,7 @@
 - (void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath*)destinationIndexPath {
     if (sourceIndexPath.item != destinationIndexPath.item) {
         [self.pdfView.document movePageAtIndex:sourceIndexPath.item withPageAtIndex:destinationIndexPath.item];
+        self.isPageEdit = YES;
     }
 }
 
@@ -271,10 +318,21 @@
 }
 
 - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
-    UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
-    self.pageEditToolBar.currentPageIndex = indexPath.item;
-    [self updateTitle];
-    [cell setSelected:YES];
+    if (self.isEdit) {
+        UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
+        self.pageEditToolBar.currentPageIndex = indexPath.item;
+        self.pageEditToolBar.isSelect = [self getIsSelect];
+        [self updateTitle];
+        [cell setSelected:YES];
+    } else {
+        if([self.pageEditDelegate respondsToSelector:@selector(pageEditViewController:pageIndex:)]) {
+            [self.pageEditDelegate pageEditViewController:self pageIndex:indexPath.item];
+        }
+        
+        [self dismissViewControllerAnimated:YES completion:^{
+                
+        }];
+    }
 }
 
 - (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath {
@@ -299,11 +357,26 @@
     
     [self.pdfView.document insertPage:size atIndex:pageIndex];
     [self.collectionView reloadData];
+    [self.pageEditToolBar reloadData];
+    
+    [self updateTitle];
+    self.isPageEdit = YES;
+    NSIndexPath *indexPath = [NSIndexPath indexPathForItem:pageIndex inSection:0];
+    [self.collectionView selectItemAtIndexPath:indexPath
+                                      animated:NO
+                                scrollPosition:UICollectionViewScrollPositionCenteredVertically];
 }
 
 - (void)pageEditToolBarPDFInsert:(CPageEditToolBar *)pageEditToolBar pageModel:(CBlankPageModel *)pageModel document:(nonnull CPDFDocument *)document {
     [self.pdfView.document importPages:pageModel.indexSet fromDocument:document atIndex:pageModel.pageIndex];
     [self.collectionView reloadData];
+    
+    [pageModel.indexSet enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL * _Nonnull stop) {
+        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:idx+1 inSection:0];
+        [self.collectionView selectItemAtIndexPath:indexPath
+                                          animated:NO
+                                    scrollPosition:UICollectionViewScrollPositionCenteredVertically];
+    }];
 }
 
 - (void)pageEditToolBarExtract:(CPageEditToolBar *)pageEditToolBar {
@@ -329,6 +402,9 @@
     [alert addAction:cancelAction];
     [self presentViewController:alert animated:YES completion:nil];
     [self.pageEditToolBar reloadData];
+    
+    self.isPageEdit = YES;
+    [self updateTitle];
 }
 
 - (void)pageEditToolBarRotate:(CPageEditToolBar *)pageEditToolBar {
@@ -340,8 +416,16 @@
         
         CPDFPage *pPage = [self.pdfView.document pageAtIndex:indexPath.item];
         pPage.rotation += 90;
+        
+        CGSize pageSize = [self.pdfView.document pageSizeAtIndex:indexPath.item];
+        CGFloat multiple = MAX(pageSize.width / 110, pageSize.height / 173);
+        
+        cell.imageSize = CGSizeMake(pageSize.width / multiple, pageSize.height / multiple);
+        [cell setNeedsLayout];
     }
-    [self.pageEditToolBar reloadData];
+    
+    [self updateTitle];
+    self.isPageEdit = YES;
 }
 
 - (void)pageEditToolBarDelete:(CPageEditToolBar *)pageEditToolBar {
@@ -365,8 +449,10 @@
     }
     [self.collectionView deleteItemsAtIndexPaths:[self.collectionView indexPathsForSelectedItems]];
     [self.pdfView.document removePageAtIndexSet:indexSet];
-    [self.collectionView reloadData];
     [self.pageEditToolBar reloadData];
+    
+    [self updateTitle];
+    self.isPageEdit = YES;
 }
 
 - (void)pageEditToolBarReplace:(CPageEditToolBar *)pageEditToolBar document:(CPDFDocument *)document {
@@ -388,6 +474,7 @@
     [self.pageEditToolBar reloadData];
     
     [self updateTitle];
+    self.isPageEdit = YES;
 }
 
 - (void)pageEditToolBarCopy:(CPageEditToolBar *)pageEditToolBar {
@@ -402,6 +489,7 @@
     [self.pageEditToolBar reloadData];
     
     [self updateTitle];
+    self.isPageEdit = YES;
 }
 
 @end

+ 2 - 0
compdfkit-tools/compdfkit-tools/PageEdit/Control/CPDFPageInsertViewController.h

@@ -21,6 +21,8 @@ NS_ASSUME_NONNULL_BEGIN
 
 - (void)pageInsertViewControllerSave:(CPDFPageInsertViewController *)PageInsertViewController pageModel:(CBlankPageModel *)pageModel;
 
+- (void)pageInsertViewControllerCancel:(CPDFPageInsertViewController *)PageInsertViewController;
+
 @end
 
 @interface CPDFPageInsertViewController : UIViewController

+ 41 - 12
compdfkit-tools/compdfkit-tools/PageEdit/Control/CPDFPageInsertViewController.m

@@ -33,10 +33,10 @@
 
 @property (nonatomic, strong) CBlankPageModel *pageModel;
 
-@property (nonatomic, strong) UIButton *selectBtn;
-
 @property (nonatomic, strong) UITextField *locationTextField;
 
+@property (nonatomic, strong) CInsertBlankPageCell *preCell;
+
 @end
 
 @implementation CPDFPageInsertViewController
@@ -88,8 +88,9 @@
     self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 50, self.view.frame.size.width, self.view.frame.size.height-50) style:UITableViewStylePlain];
     self.tableView.delegate = self;
     self.tableView.dataSource = self;
-    self.tableView.rowHeight = 60.0;
-    self.tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
+    self.tableView.rowHeight = UITableViewAutomaticDimension;
+    self.tableView.estimatedRowHeight = 44.0;
+    self.tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
     self.tableView.backgroundColor = [CPDFColorUtils CAnnotationSampleBackgoundColor];
     [self.view addSubview:self.tableView];
     
@@ -97,7 +98,7 @@
     self.view.backgroundColor = [CPDFColorUtils CAnnotationPropertyViewControllerBackgoundColor];
     
     self.pageModel = [[CBlankPageModel alloc] init];
-    self.pageModel.pageIndex = 1;
+    self.pageModel.pageIndex = 0;
     self.pageModel.size = CGSizeMake(210, 297);
     self.pageModel.rotation = 0;
     
@@ -138,7 +139,11 @@
 }
 
 - (void)buttonItemClicked_cancel:(id)sender {
-    [self dismissViewControllerAnimated:YES completion:nil];
+    [self dismissViewControllerAnimated:YES completion:^{
+        if (self.delegate && [self.delegate respondsToSelector:@selector(pageInsertViewControllerCancel:)]) {
+            [self.delegate pageInsertViewControllerCancel:self];
+        }
+    }];
 }
 
 #pragma mark - UITableViewDataSource
@@ -147,6 +152,10 @@
     return self.dataArray.count;
 }
 
+- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
+    return 60.0;
+}
+
 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
     CInsertBlankPageCell *cell = [tableView dequeueReusableCellWithIdentifier:@"pageCell"];
     if (cell == nil) {
@@ -162,6 +171,7 @@
         {
             if (self.isSelect) {
                 [cell setCellStyle:CInsertBlankPageCellSizeSelect label:self.dataArray[indexPath.row]];
+                cell.separatorInset = UIEdgeInsetsMake(0, self.view.bounds.size.width, 0, 0);
             } else {
                 [cell setCellStyle:CInsertBlankPageCellDirection label:self.dataArray[indexPath.row]];
             }
@@ -171,6 +181,7 @@
         {
             if (self.isSelect) {
                 [cell setCellStyle:CInsertBlankPageCellSizeSelect label:self.dataArray[indexPath.row]];
+                cell.separatorInset = UIEdgeInsetsMake(0, self.view.bounds.size.width, 0, 0);
             } else {
                 [cell setCellStyle:CInsertBlankPageCellLocation label:self.dataArray[indexPath.row]];
             }
@@ -182,9 +193,16 @@
                 [cell setCellStyle:CInsertBlankPageCellSizeSelect label:self.dataArray[indexPath.row]];
             } else {
                 [cell setCellStyle:CInsertBlankPageCellLocationSelect label:self.dataArray[indexPath.row]];
+                if (!self.currentPageIndex) {
+                    self.preCell = cell;
+                    cell.locationSelectBtn.selected = !cell.locationSelectBtn.selected;
+                    cell.locationSelectLabel.textColor = [CPDFColorUtils CPageEditToolbarFontColor];
+                }
+                
                 if (![self.pageLoactionBtns containsObject:cell.locationSelectBtn]) {
                     [self.pageLoactionBtns addObject:cell.locationSelectBtn];
                 }
+                cell.separatorInset = UIEdgeInsetsMake(0, self.view.bounds.size.width, 0, 0);
             }
         }
             break;
@@ -194,6 +212,7 @@
             if (![self.pageLoactionBtns containsObject:cell.locationSelectBtn]) {
                 [self.pageLoactionBtns addObject:cell.locationSelectBtn];
             }
+            cell.separatorInset = UIEdgeInsetsMake(0, self.view.bounds.size.width, 0, 0);
         }
             break;
         case 5:
@@ -202,6 +221,7 @@
             if (![self.pageLoactionBtns containsObject:cell.locationSelectBtn]) {
                 [self.pageLoactionBtns addObject:cell.locationSelectBtn];
             }
+            cell.separatorInset = UIEdgeInsetsMake(0, self.view.bounds.size.width, 0, 0);
         }
             break;
         case 6:
@@ -209,8 +229,10 @@
             [cell setCellStyle:CInsertBlankPageCellLocationTextFiled label:self.dataArray[indexPath.row]];
             if (self.currentPageIndex) {
                 cell.locationTextField.text = [NSString stringWithFormat:@"%lu", self.currentPageIndex];
+                self.pageModel.pageIndex = self.currentPageIndex;
             }
             self.locationTextField = cell.locationTextField;
+            cell.separatorInset = UIEdgeInsetsMake(0, self.view.bounds.size.width, 0, 0);
         }
             break;
         case 7:
@@ -219,6 +241,7 @@
             if (![self.pageLoactionBtns containsObject:cell.locationSelectBtn]) {
                 [self.pageLoactionBtns addObject:cell.locationSelectBtn];
             }
+            cell.separatorInset = UIEdgeInsetsMake(0, self.view.bounds.size.width, 0, 0);
         }
             break;
             
@@ -301,26 +324,32 @@
 }
 
 - (void)insertBlankPageCellLocation:(CInsertBlankPageCell *)insertBlankPageCell button:(nonnull UIButton *)buttom {
-    if (self.selectBtn) {
-        self.selectBtn.selected = !self.selectBtn.selected;
-        if (self.selectBtn != buttom) {
+    if (self.preCell) {
+        self.preCell.locationSelectBtn.selected = !self.preCell.locationSelectBtn.selected;
+        self.preCell.locationSelectLabel.textColor = [UIColor grayColor];
+        if (self.preCell.locationSelectBtn != buttom) {
             buttom.selected = !buttom.selected;
-            self.selectBtn = buttom;
+            insertBlankPageCell.locationSelectLabel.textColor = [CPDFColorUtils CPageEditToolbarFontColor];
+            self.preCell = insertBlankPageCell;
         } else {
-            self.selectBtn = nil;
+            self.preCell.locationSelectLabel.textColor = [UIColor grayColor];
+            self.preCell = nil;
         }
     } else {
-        self.selectBtn = buttom;
+        self.preCell = insertBlankPageCell;
         buttom.selected = !buttom.selected;
+        insertBlankPageCell.locationSelectLabel.textColor = [CPDFColorUtils CPageEditToolbarFontColor];
     }
     
     NSInteger location = [self.pageLoactionBtns indexOfObject:buttom];
     switch (location) {
         case 0:
             self.pageModel.pageIndex = 0;
+            self.locationTextField.text = @"";
             break;
         case 1:
             self.pageModel.pageIndex = -2;
+            self.locationTextField.text = @"";
             break;
         case 2:
             self.pageModel.pageIndex = self.pageModel.pageIndex - 1;

+ 9 - 7
compdfkit-tools/compdfkit-tools/PageEdit/Views/CInsertBlankPageCell.h

@@ -51,23 +51,25 @@ typedef NS_ENUM(NSInteger, CInsertBlankPageCellType) {
 
 @property (nonatomic, strong) UILabel *sizeLabel;
 
-@property (nonatomic, strong) UIButton *sizeSelectBtn;
+@property (nullable, nonatomic, strong) UIButton *sizeSelectBtn;
 
 @property (nonatomic, strong) UILabel *sizeSelectLabel;
 
-@property (nonatomic, strong) UIButton *horizontalPageBtn;
+@property (nullable, nonatomic, strong) UIButton *horizontalPageBtn;
 
-@property (nonatomic, strong) UIButton *verticalPageBtn;
+@property (nullable, nonatomic, strong) UIButton *verticalPageBtn;
 
-@property (nonatomic, strong) UIButton *locationSelectBtn;
+@property (nullable, nonatomic, strong) UIButton *locationSelectBtn;
 
 @property (nonatomic, strong) UILabel *locationSelectLabel;
 
-@property (nonatomic, strong) UITextField *locationTextField;
+@property (nonatomic, strong) UILabel *rangeSelectLabel;
 
-@property (nonatomic, strong) UIButton *rangeSelectBtn;
+@property (nullable, nonatomic, strong) UITextField *locationTextField;
 
-@property (nonatomic, strong) UITextField *rangeTextField;
+@property (nullable, nonatomic, strong) UIButton *rangeSelectBtn;
+
+@property (nullable, nonatomic, strong) UITextField *rangeTextField;
 
 - (void)setCellStyle:(CInsertBlankPageCellType)cellType label:(NSString *)label;
 

+ 19 - 5
compdfkit-tools/compdfkit-tools/PageEdit/Views/CInsertBlankPageCell.m

@@ -52,6 +52,13 @@
         self.locationTextField.delegate = nil;
     }
     
+    self.locationSelectBtn = nil;
+    self.locationSelectLabel.text = nil;
+    self.sizeSelectLabel.text = nil;
+    self.textLabel.text = nil;
+    self.sizeSelectLabel.text = nil;
+    self.locationTextField  = nil;
+    
     self.cellType = cellType;
     switch (self.cellType) {
         case CInsertBlankPageCellSize:
@@ -110,8 +117,11 @@
             self.locationSelectBtn.layer.masksToBounds = YES;
             [self.contentView addSubview:self.locationSelectBtn];
             
-            self.locationSelectLabel = [[UILabel alloc] initWithFrame:CGRectMake(80, 5, self.bounds.size.width-80, 50)];
+            if (!self.locationSelectLabel) {
+                self.locationSelectLabel = [[UILabel alloc] initWithFrame:CGRectMake(80, 5, self.bounds.size.width-80, 50)];
+            }
             self.locationSelectLabel.text = label;
+            self.locationSelectLabel.textColor = [UIColor grayColor];
             [self.contentView addSubview:self.locationSelectLabel];
             
             self.backgroundColor = [CPDFColorUtils CAnnotationSampleBackgoundColor];
@@ -129,6 +139,7 @@
             [self.locationTextField addTarget:self action:@selector(textField_location:) forControlEvents:UIControlEventEditingChanged];
             [self.contentView addSubview:self.locationTextField];
             self.backgroundColor = [CPDFColorUtils CAnnotationSampleBackgoundColor];
+            self.separatorInset = UIEdgeInsetsMake(0, self.bounds.size.width, 0, self.bounds.size.width);
         }
             break;
         case CInsertBlankPageCellRangeSelect:
@@ -146,10 +157,13 @@
             self.rangeSelectBtn.layer.masksToBounds = YES;
             [self.contentView addSubview:self.rangeSelectBtn];
             
-            UILabel *locationSelectLabel = [[UILabel alloc] initWithFrame:CGRectMake(80, 5, self.bounds.size.width-80, 50)];
-            locationSelectLabel.text = label;
-            [self.contentView addSubview:locationSelectLabel];
-            
+            if (!self.rangeSelectLabel) {
+                self.rangeSelectLabel = [[UILabel alloc] initWithFrame:CGRectMake(80, 5, self.bounds.size.width-80, 50)];
+            }
+            self.rangeSelectLabel.text = label;
+            self.rangeSelectLabel.textColor = [UIColor grayColor];
+            [self.contentView addSubview:self.rangeSelectLabel];
+        
             self.backgroundColor = [CPDFColorUtils CAnnotationSampleBackgoundColor];
         }
             break;

+ 1 - 1
compdfkit-tools/compdfkit-tools/PageEdit/Views/CPDFPageEditViewCell.h

@@ -16,7 +16,7 @@ NS_ASSUME_NONNULL_BEGIN
 
 @interface CPDFPageEditViewCell : CPDFThumbnailViewCell
 
-@property (nonatomic,assign) BOOL editing;
+- (void)setEdit:(BOOL)editing;
 
 @end
 

+ 3 - 4
compdfkit-tools/compdfkit-tools/PageEdit/Views/CPDFPageEditViewCell.m

@@ -38,12 +38,11 @@
     [super layoutSubviews];
 }
 
-- (void)setEditing:(BOOL)editing {
-    _editing = editing;
-    if (_editing) {
+- (void)setEdit:(BOOL)editing {
+    if (editing) {
         _selectButton.hidden = NO;
     } else {
-//        _selectButton.hidden = YES;
+        _selectButton.hidden = YES;
     }
     [self layoutSubviews];
 }

+ 2 - 0
compdfkit-tools/compdfkit-tools/PageEdit/Views/CPageEditToolBar.h

@@ -54,6 +54,8 @@ typedef NS_ENUM(NSInteger, CPageEditToolBarType) {
 
 @property (nonatomic, assign) NSInteger currentPageIndex;
 
+@property (nonatomic, assign) BOOL isSelect;
+
 - (void)reloadData;
 
 @end

+ 72 - 18
compdfkit-tools/compdfkit-tools/PageEdit/Views/CPageEditToolBar.m

@@ -12,6 +12,8 @@
 #import "CPDFColorUtils.h"
 #import "CPDFPageInsertViewController.h"
 #import "CPDFPDFInsertViewController.h"
+#import "CActivityIndicatorView.h"
+#import "AAPLCustomPresentationController.h"
 
 #import <ComPDFKit/ComPDFKit.h>
 
@@ -27,6 +29,8 @@
 
 @property (nonatomic, strong) CPDFDocument *replaceDocument;
 
+@property(nonatomic, strong) CActivityIndicatorView *loadingView;
+
 @end
 
 @implementation CPageEditToolBar
@@ -100,7 +104,7 @@
         button.frame = CGRectMake(offsetX, topOffset, buttonSize, buttonSize);
         [button setImage:[UIImage imageNamed:images[i] inBundle:[NSBundle bundleForClass:self.class] compatibleWithTraitCollection:nil] forState:UIControlStateNormal];
         [button setTitle:names[i] forState:UIControlStateNormal];
-        [button setTitleColor:[UIColor colorWithRed:61.0/255.0 green:71.0/255.0 blue:77.0/255.0 alpha:1.0] forState:UIControlStateNormal];
+        [button setTitleColor:[CPDFColorUtils CPageEditToolbarFontColor] forState:UIControlStateNormal];
         button.layer.cornerRadius = 5.0;
         button.titleLabel.font = [UIFont systemFontOfSize:12.0];
         button.tag = i;
@@ -158,9 +162,6 @@
 
 - (void)enterPDFAddFile {
     dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
-        if(self.pdfView.document.isModified) {
-            [self.pdfView.document writeToURL:self.pdfView.document.documentURL];
-        }
         dispatch_async(dispatch_get_main_queue(), ^{
             NSArray *documentTypes = @[@"com.adobe.pdf"];
             UIDocumentPickerViewController *documentPickerViewController = [[UIDocumentPickerViewController alloc] initWithDocumentTypes:documentTypes inMode:UIDocumentPickerModeOpen];
@@ -177,6 +178,7 @@
 
 - (void)handleDocument {
     if (self.selectedIndex == CPageEditToolBarInsert) {
+        AAPLCustomPresentationController *presentationController NS_VALID_UNTIL_END_OF_SCOPE;
         CPDFPDFInsertViewController *pdfInsertVC = [[CPDFPDFInsertViewController alloc] initWithDocument:self.insertDocument];
         pdfInsertVC.delegate = self;
         pdfInsertVC.currentPageIndex =  self.currentPageIndex;
@@ -210,6 +212,46 @@
     [tRootViewControl presentViewController:alert animated:YES completion:nil];
 }
 
+- (void)reloadDocumentWithFilePath:(NSString *)filePath password:(nullable NSString *)password completion:(void (^)(BOOL result))completion {
+    if (![self.loadingView superview]) {
+        [self addSubview:self.loadingView];
+    }
+    [self.loadingView startAnimating];
+    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
+        NSURL *url = [NSURL fileURLWithPath:filePath];
+        CPDFDocument *document = [[CPDFDocument alloc] initWithURL:url];
+        if([document isLocked]) {
+            [document unlockWithPassword:password];
+        }
+        dispatch_async(dispatch_get_main_queue(), ^{
+            [self.loadingView stopAnimating];
+            [self.loadingView removeFromSuperview];
+            if (document.error && document.error.code != CPDFDocumentPasswordError) {
+                UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"OK", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
+                }];
+                UIAlertController *alert = [UIAlertController alertControllerWithTitle:@""
+                                                                               message:NSLocalizedString(@"Sorry PDF Reader Can't open this pdf file!", nil)
+                                                                        preferredStyle:UIAlertControllerStyleAlert];
+                [alert addAction:okAction];
+                if (completion) {
+                    completion(NO);
+                }
+            } else {
+                if (self.selectedIndex == CPageEditToolBarInsert) {
+                    self.insertDocument = document;
+                } else if (self.selectedIndex == CPageEditToolBarReplace) {
+                    self.replaceDocument = document;
+                }
+                
+                [self handleDocument];
+                if (completion) {
+                    completion(YES);
+                }
+            }
+        });
+    });
+}
+
 #pragma mark - Action
 
 - (void)buttonItemClicked_switch:(UIButton *)button {
@@ -223,6 +265,10 @@
         self.selectedIndex = button.tag;
     } else {
         self.selectedIndex = -1;
+        if (button.tag == CPageEditToolBarRotate) {
+            self.selectedIndex = button.tag;
+            button.backgroundColor = [CPDFColorUtils CAnnotationBarSelectBackgroundColor];
+        }
     }
     
     switch (button.tag) {
@@ -231,7 +277,7 @@
             break;
         case CPageEditToolBarReplace:
         {
-            if (self.currentPageIndex >= 0) {
+            if (self.isSelect) {
                 [self enterPDFAddFile];
             } else {
                 [self popoverWarning];
@@ -240,7 +286,7 @@
             break;
         case CPageEditToolBarExtract:
         {
-            if (self.currentPageIndex >= 0) {
+            if (self.isSelect) {
                 if (self.delegate && [self.delegate respondsToSelector:@selector(pageEditToolBarExtract:)]) {
                     [self.delegate pageEditToolBarExtract:self];
                 }
@@ -251,7 +297,7 @@
             break;
         case CPageEditToolBarRotate:
         {
-            if (self.currentPageIndex >= 0) {
+            if (self.isSelect) {
                 if (self.delegate && [self.delegate respondsToSelector:@selector(pageEditToolBarRotate:)]) {
                     [self.delegate pageEditToolBarRotate:self];
                 }
@@ -262,7 +308,7 @@
             break;
         case CPageEditToolBarCopy:
         {
-            if (self.currentPageIndex >= 0) {
+            if (self.isSelect) {
                 if (self.delegate && [self.delegate respondsToSelector:@selector(pageEditToolBarCopy:)]) {
                     [self.delegate pageEditToolBarCopy:self];
                 }
@@ -273,7 +319,7 @@
             break;
         case CPageEditToolBarDelete:
         {
-            if (self.currentPageIndex >= 0) {
+            if (self.isSelect) {
                 if (self.delegate && [self.delegate respondsToSelector:@selector(pageEditToolBarDelete:)]) {
                     [self.delegate pageEditToolBarDelete:self];
                 }
@@ -298,6 +344,10 @@
     [self reloadData];
 }
 
+- (void)pageInsertViewControllerCancel:(CPDFPageInsertViewController *)PageInsertViewController {
+    [self reloadData];
+}
+
 #pragma mark - CPDFPDFInsertViewControllerDelegate
 
 - (void)pdfInsertViewControllerSave:(CPDFPageInsertViewController *)PageInsertViewController pageModel:(CBlankPageModel *)pageModel {
@@ -308,9 +358,14 @@
     [self reloadData];
 }
 
+- (void)pdfInsertViewControllerCancel:(CPDFPDFInsertViewController *)PageInsertViewController {
+    [self reloadData];
+}
+
 #pragma mark - UIDocumentPickerDelegate
 
 - (void)documentPicker:(UIDocumentPickerViewController *)controller didPickDocumentsAtURLs:(NSArray<NSURL *> *)urls{
+    __weak typeof(self) weakSelf = self;
     BOOL fileUrlAuthozied = [urls.firstObject startAccessingSecurityScopedResource];
     if(fileUrlAuthozied){
         NSFileCoordinator *fileCoordinator = [[NSFileCoordinator alloc] init];
@@ -327,18 +382,17 @@
                [[NSFileManager defaultManager] copyItemAtPath:newURL.path toPath:documentPath error:NULL];
 
             }
-            NSURL *url = [NSURL fileURLWithPath:documentPath];
-            CPDFDocument *document = [[CPDFDocument alloc] initWithURL:url];
-            if (self.selectedIndex == CPageEditToolBarInsert) {
-                self.insertDocument = document;
-            } else if (self.selectedIndex == CPageEditToolBarReplace) {
-                self.replaceDocument = document;
-            }
+            
+            [weakSelf reloadDocumentWithFilePath:documentPath password:nil completion:^(BOOL result) {
+            }];
+
         }];
         [urls.firstObject stopAccessingSecurityScopedResource];
     }
-    
-    [self handleDocument];
+}
+
+- (void)documentPickerWasCancelled:(UIDocumentPickerViewController *)controller {
+    [self reloadData];
 }
 
 @end

+ 3 - 5
compdfkit-tools/compdfkit-tools/Viewer/PDFThumbnail/CPDFThumbnailViewCell.m

@@ -31,16 +31,14 @@
     return self;
 }
 
-- (void)drawRect:(CGRect)rect {
-    _imageView.frame = CGRectMake((self.frame.size.width - self.imageSize.width)/2, (self.imageSize.height - 14 - self.imageSize.height) / 2, self.imageSize.width, self.imageSize.height);
+- (void)layoutSubviews {
+    _imageView.frame = CGRectMake((self.frame.size.width - self.imageSize.width)/2, (self.frame.size.height - 14 - self.imageSize.height) / 2, self.imageSize.width, self.imageSize.height);
     
     CGFloat startW = [self.textLabel.text sizeWithFont:[UIFont systemFontOfSize:10] constrainedToSize:CGSizeMake(MAXFLOAT, 12)].width;
     //restSize
-    _textLabel.frame = CGRectMake(self.frame.size.width/2 - (startW + 20)/2, self.imageSize.height + (self.imageSize.height - 10 - self.imageSize.height) / 2, startW + 20, 12);
+    _textLabel.frame = CGRectMake(self.frame.size.width/2 - (startW + 20)/2, self.imageSize.height + (self.frame.size.height - 14 - self.imageSize.height) / 2, startW + 20, 12);
 }
 
-
-
 - (void)setPageRef:(CGPDFPageRef)pageRef {
     CGRect boxRect = CGRectZero;
     if (pageRef) {

+ 0 - 1
viewer-ctrl-demo/viewer-ctrl-demo/CPDFViewController.m

@@ -83,5 +83,4 @@
     }
 }
 
-
 @end