Explorar o código

PDFTool(iOS) - 解决Annotation部分冲突

chenyu hai 1 ano
pai
achega
02be201a5a

+ 6 - 6
Annotation-Ctrl-Demo/Annotation-Ctrl-Demo.xcodeproj/project.pbxproj

@@ -7,9 +7,9 @@
 	objects = {
 
 /* Begin PBXBuildFile section */
+		4F06AB5D29F6852C007797E4 /* CPDFViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 4F06AB5C29F6852C007797E4 /* CPDFViewController.m */; };
 		C9AF185129F267DD00EC4979 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = C9AF185029F267DD00EC4979 /* AppDelegate.m */; };
 		C9AF185429F267DD00EC4979 /* SceneDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = C9AF185329F267DD00EC4979 /* SceneDelegate.m */; };
-		C9AF185729F267DD00EC4979 /* CPDFViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = C9AF185629F267DD00EC4979 /* CPDFViewController.m */; };
 		C9AF185A29F267DD00EC4979 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = C9AF185829F267DD00EC4979 /* Main.storyboard */; };
 		C9AF185C29F267E000EC4979 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = C9AF185B29F267E000EC4979 /* Assets.xcassets */; };
 		C9AF185F29F267E000EC4979 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = C9AF185D29F267E000EC4979 /* LaunchScreen.storyboard */; };
@@ -42,13 +42,13 @@
 /* End PBXCopyFilesBuildPhase section */
 
 /* Begin PBXFileReference section */
+		4F06AB5B29F6852C007797E4 /* CPDFViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CPDFViewController.h; sourceTree = "<group>"; };
+		4F06AB5C29F6852C007797E4 /* CPDFViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CPDFViewController.m; sourceTree = "<group>"; };
 		C9AF184C29F267DD00EC4979 /* Annotation-Ctrl-Demo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "Annotation-Ctrl-Demo.app"; sourceTree = BUILT_PRODUCTS_DIR; };
 		C9AF184F29F267DD00EC4979 /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = "<group>"; };
 		C9AF185029F267DD00EC4979 /* AppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = "<group>"; };
 		C9AF185229F267DD00EC4979 /* SceneDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SceneDelegate.h; sourceTree = "<group>"; };
 		C9AF185329F267DD00EC4979 /* SceneDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SceneDelegate.m; sourceTree = "<group>"; };
-		C9AF185529F267DD00EC4979 /* CPDFViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = CPDFViewController.h; path = "../../Edit-Ctrl-Demo/Edit-Ctrl-Demo/CPDFViewController.h"; sourceTree = "<group>"; };
-		C9AF185629F267DD00EC4979 /* CPDFViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; name = CPDFViewController.m; path = "../../Edit-Ctrl-Demo/Edit-Ctrl-Demo/CPDFViewController.m"; sourceTree = "<group>"; };
 		C9AF185929F267DD00EC4979 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = "<group>"; };
 		C9AF185B29F267E000EC4979 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
 		C9AF185E29F267E000EC4979 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = "<group>"; };
@@ -105,8 +105,8 @@
 				C9AF185029F267DD00EC4979 /* AppDelegate.m */,
 				C9AF185229F267DD00EC4979 /* SceneDelegate.h */,
 				C9AF185329F267DD00EC4979 /* SceneDelegate.m */,
-				C9AF185529F267DD00EC4979 /* CPDFViewController.h */,
-				C9AF185629F267DD00EC4979 /* CPDFViewController.m */,
+				4F06AB5B29F6852C007797E4 /* CPDFViewController.h */,
+				4F06AB5C29F6852C007797E4 /* CPDFViewController.m */,
 				C9AF185829F267DD00EC4979 /* Main.storyboard */,
 				C9AF185B29F267E000EC4979 /* Assets.xcassets */,
 				C9AF185D29F267E000EC4979 /* LaunchScreen.storyboard */,
@@ -225,7 +225,7 @@
 			buildActionMask = 2147483647;
 			files = (
 				C9AF187529F26AD200EC4979 /* XPDXMLElement.m in Sources */,
-				C9AF185729F267DD00EC4979 /* CPDFViewController.m in Sources */,
+				4F06AB5D29F6852C007797E4 /* CPDFViewController.m in Sources */,
 				C9AF185129F267DD00EC4979 /* AppDelegate.m in Sources */,
 				C9AF187629F26AD200EC4979 /* XMLParseManager.m in Sources */,
 				C9AF186229F267E000EC4979 /* main.m in Sources */,

+ 27 - 0
Annotation-Ctrl-Demo/Annotation-Ctrl-Demo/CPDFViewController.h

@@ -0,0 +1,27 @@
+//
+//  ViewController.h
+//   ]Edit-Ctrl-Demo
+//
+//  Copyright © 2014-2023 PDF Technologies, Inc. All Rights Reserved.
+//
+//  THIS SOURCE CODE AND ANY ACCOMPANYING DOCUMENTATION ARE PROTECTED BY INTERNATIONAL COPYRIGHT LAW
+//  AND MAY NOT BE RESOLD OR REDISTRIBUTED. USAGE IS BOUND TO THE ComPDFKit LICENSE AGREEMENT.
+//  UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES.
+//  This notice may not be removed from this file.
+//
+
+#import <UIKit/UIKit.h>
+
+NS_ASSUME_NONNULL_BEGIN
+
+@interface CPDFViewController : UIViewController
+
+@property(nonatomic, readonly) NSString *filePath;
+
+- (instancetype)initWithFilePath:(NSString *)filePath;
+
+- (void)reloadDocumentWithFilePath:(NSString *)filePath completion:(void (^)(BOOL result))completion;
+
+@end
+
+NS_ASSUME_NONNULL_END

+ 293 - 0
Annotation-Ctrl-Demo/Annotation-Ctrl-Demo/CPDFViewController.m

@@ -0,0 +1,293 @@
+//
+//  CPDFViewController.m
+//   Edit-Ctrl-Demo
+//
+//  Copyright © 2014-2023 PDF Technologies, Inc. All Rights Reserved.
+//
+//  THIS SOURCE CODE AND ANY ACCOMPANYING DOCUMENTATION ARE PROTECTED BY INTERNATIONAL COPYRIGHT LAW
+//  AND MAY NOT BE RESOLD OR REDISTRIBUTED. USAGE IS BOUND TO THE ComPDFKit LICENSE AGREEMENT.
+//  UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES.
+//  This notice may not be removed from this file.
+//
+
+#import "CPDFViewController.h"
+
+#import <ComPDFKit/ComPDFKit.h>
+#import <compdfkit_tools/compdfkit_tools.h>
+
+@interface CPDFViewController () <UISearchBarDelegate,CPDFViewDelegate,CPDFListViewDelegate, CPDFMoreListViewDelegate,CSearchToolbarDelegate, CPDFDisplayViewDelegate,CPDFBOTAViewControllerDelegate,CPDFSearchResultsDelegate,CPDFThumbnailViewControllerDelegate,CPDFAnnotationBarDelegate>
+
+@property(nonatomic, strong) NSString *filePath;
+
+@property(nonatomic, strong) CPDFListView *pdfListView;
+
+@property(nonatomic, strong) CNavigationRightView *rightView;
+
+@property(nonatomic, strong) CSearchToolbar *searchToolbar;
+
+@property(nonatomic, strong) CActivityIndicatorView *loadingView;
+
+@property(nonatomic, strong) NSString *navigationTitle;
+
+@property(nonatomic, strong) CPDFAnnotationBar *annotatiomBar;
+
+@property(nonatomic, strong) CPDFAnnotationBaseViewController *baseVC;
+
+@end
+
+@implementation CPDFViewController
+
+#pragma mark - Initializers
+
+- (instancetype)initWithFilePath:(NSString *)filePath {
+    if(self = [super init]) {
+        self.filePath = filePath;
+    }
+    return self;
+}
+
+- (void)viewDidLoad {
+    [super viewDidLoad];
+    
+    self.view.backgroundColor = [CPDFColorUtils CPDFViewControllerBackgroundColor];
+    
+    self.pdfListView = [[CPDFListView alloc] initWithFrame:self.view.bounds];
+    self.pdfListView.performDelegate = self;
+    self.pdfListView.delegate = self;
+    self.pdfListView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
+    [self.view addSubview:self.pdfListView];
+    
+    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"CPDFThunbnailImageEnter" inBundle:[NSBundle bundleForClass:self.class] compatibleWithTraitCollection:nil] style:UIBarButtonItemStylePlain target:self action:@selector(buttonItemClicked_thumbnail:)];
+    self.navigationItem.leftBarButtonItem = leftItem;
+    
+    __block typeof(self) blockSelf = self;
+    
+    self.rightView = [[CNavigationRightView alloc] initWithDefaultItemsClickBack:^(NSUInteger tag) {
+        switch (tag) {
+            case CNavigationRightTypeSearch:
+                [blockSelf navigationRightItemSearch];
+                break;
+            case CNavigationRightTypeBota:
+                [blockSelf navigationRightItemBota];
+                break;
+            default:
+            case CNavigationRightTypeMore:
+                [blockSelf navigationRightItemMore];
+                break;
+        }
+        
+    }];
+    
+    self.searchToolbar = [[CSearchToolbar alloc] initWithPDFView:self.pdfListView];
+    self.searchToolbar.delegate = self;
+    
+    self.annotatiomBar = [[CPDFAnnotationBar alloc] init];
+    self.annotatiomBar.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
+    self.annotatiomBar.delegate = self;
+    [self.view addSubview:self.annotatiomBar];
+    
+    [self reloadDocumentWithFilePath:self.filePath completion:^(BOOL result) {
+        
+    }];
+}
+
+- (void)viewWillLayoutSubviews {
+    [super viewWillLayoutSubviews];
+    
+    if (@available(iOS 11.0, *)) {
+        self.pdfListView.frame = CGRectMake(self.view.safeAreaInsets.left, self.view.safeAreaInsets.top, self.view.frame.size.width - self.view.safeAreaInsets.left - self.view.safeAreaInsets.right, self.view.frame.size.height - self.view.safeAreaInsets.bottom- self.view.safeAreaInsets.top);
+        
+        self.annotatiomBar.frame = CGRectMake(0, self.view.frame.size.height - self.view.safeAreaInsets.bottom - 44, self.view.frame.size.width, 44);
+    } else {
+        self.pdfListView.frame = self.view.bounds;
+        self.annotatiomBar.frame = CGRectMake(0, self.view.frame.size.height  - 64, self.view.frame.size.width, 44);
+    }
+}
+
+#pragma mark - Accessors
+
+#pragma mark - Public method
+
+- (void)reloadDocumentWithFilePath:(NSString *)filePath completion:(void (^)(BOOL result))completion {
+    
+    self.title = [[filePath lastPathComponent] stringByDeletingPathExtension];
+    _navigationTitle = self.title;
+    [self.navigationController.view setUserInteractionEnabled:NO];
+    
+    if (![self.loadingView superview]) {
+        [self.view 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];
+        dispatch_async(dispatch_get_main_queue(), ^{
+            [self.navigationController.view setUserInteractionEnabled:YES];
+            [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) {
+                    [self.navigationController popViewControllerAnimated:YES];
+                }];
+                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 {
+                self.pdfListView.document = document;
+                if (completion) {
+                    completion(YES);
+                }
+            }
+        });
+    });
+}
+
+#pragma mark - Action
+
+- (void)navigationRightItemSearch {
+    [self.searchToolbar showInView:self.navigationController.navigationBar];
+    self.title = nil;
+    self.navigationItem.rightBarButtonItem = nil;
+}
+
+- (void)navigationRightItemBota {
+    CPDFBOTAViewController *botaViewController = [[CPDFBOTAViewController alloc] initWithPDFView:self.pdfListView];
+    botaViewController.delegate = self;
+    [self.navigationController pushViewController:botaViewController animated:NO];
+}
+
+- (void)navigationRightItemMore {
+    CPDFMoreListViewController * moreListVc = [[CPDFMoreListViewController alloc] init];
+    moreListVc.delegate = self;
+    [self.navigationController pushViewController:moreListVc animated:YES];
+}
+
+- (void)buttonItemClicked_thumbnail:(id)sender {
+    CPDFThumbnailViewController *thumbnailViewController = [[CPDFThumbnailViewController alloc] initWithPDFView:self.pdfListView];
+    thumbnailViewController.delegate = self;
+    [self.navigationController pushViewController:thumbnailViewController animated:NO];
+}
+
+- (void)menuItemClick_CopyAction:(id)sender {
+    if (self.pdfListView.currentSelection.string)
+        [[UIPasteboard generalPasteboard] setString:self.pdfListView.currentSelection.string];
+    
+    [self.pdfListView clearSelection];
+}
+
+#pragma mark - CPDFViewDelegate
+
+- (void)PDFViewDocumentDidLoaded:(CPDFView *)pdfView {
+    UIBarButtonItem* rightItem = [[UIBarButtonItem alloc] initWithCustomView:self.rightView];
+    self.navigationItem.rightBarButtonItem = rightItem;
+}
+
+- (void)PDFViewCurrentPageDidChanged:(CPDFView *)pdfView {
+}
+
+#pragma mark - CPDFListViewDelegate
+
+- (NSArray<UIMenuItem *> *)PDFListView:(CPDFListView *)pdfListView customizeMenuForPage:(CPDFPage *)page forPagePoint:(CGPoint)pagePoint {
+    if (pdfListView.currentSelection) {
+        UIMenuItem *copyItem = [[UIMenuItem alloc] initWithTitle:NSLocalizedString(@"Copy", nil)
+                                                          action:@selector(menuItemClick_CopyAction:)];
+        return @[copyItem];
+    }
+    return nil;
+}
+
+#pragma mark - CPDFMorelistViewDelegate
+
+- (void)PDFMoreListViewController:(CPDFMoreListViewController *)moreVC didSelectRow:(CPDFMoreListViewType)row {
+    if(CPDFMoreListViewTypeInfo == row){
+        //File Info
+        CPDFInfoViewController * infoVc = [[CPDFInfoViewController alloc] initWithPDFView:self.pdfListView];
+        [self.navigationController pushViewController:infoVc animated:YES];
+    } else {
+        CPDFDisplayViewController *displayVc = [[CPDFDisplayViewController alloc] initWithPDFView:self.pdfListView];
+        displayVc.delegate = self;
+        [self.navigationController pushViewController:displayVc animated:NO];
+    }
+}
+
+#pragma mark - CSearchToolbarDelegate
+
+- (void)searchToolbar:(CSearchToolbar *)searchToolbar onSearchQueryResults:(NSArray *)results {
+    if ([results count] < 1) {
+        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"OK", nil)
+                                                               style:UIAlertActionStyleCancel
+                                                             handler:nil];
+        UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil
+                                                                       message:NSLocalizedString(@"your have‘t search result", nil)
+                                                                preferredStyle:UIAlertControllerStyleAlert];
+        [alert addAction:cancelAction];
+        [self presentViewController:alert animated:YES completion:nil];
+        return;
+    }
+    [self searchToolbarOnExitSearch:searchToolbar];
+    
+    CPDFSearchResultsViewController* searchResultController = [[CPDFSearchResultsViewController alloc] initWithResultArray:results keyword:searchToolbar.searchKeyString document:self.pdfListView.document];
+    searchResultController.delegate = self;
+    [self.navigationController pushViewController:searchResultController animated:YES];
+}
+
+- (void)searchToolbarOnExitSearch:(CSearchToolbar *)searchToolbar {
+    if([searchToolbar superview]) {
+        [searchToolbar removeFromSuperview];
+        UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:self.rightView];
+        self.navigationItem.rightBarButtonItem = rightItem;
+        self.title = self.navigationTitle;
+    }
+}
+
+#pragma mark - CPDFDisplayViewDelegate
+
+- (void)displayViewControllerDismiss:(CPDFDisplayViewController *)displayViewController {
+    [self.navigationController popToRootViewControllerAnimated:YES];
+}
+
+#pragma mark - CPDFBOTAViewControllerDelegate
+
+- (void)botaViewControllerDismiss:(CPDFBOTAViewController *)botaViewController {
+    [self.navigationController popViewControllerAnimated:YES];
+}
+
+#pragma mark - CPDFSearchResultsDelegate
+
+- (void)searchResultsView:(CPDFSearchResultsViewController *)resultVC forSelection:(CPDFSelection *)selection indexPath:(NSIndexPath *)indexPath {
+    [self.navigationController popViewControllerAnimated:YES];
+    
+    NSInteger pageIndex = [self.pdfListView.document indexForPage:selection.page];
+    [self.pdfListView goToPageIndex:pageIndex animated:NO];
+    [self.pdfListView setHighlightedSelection:selection animated:YES];
+}
+
+-(void)searchResultsViewControllerDismiss:(CPDFSearchResultsViewController *)searchResultsViewController {
+    [self.navigationController popViewControllerAnimated:YES];
+}
+
+#pragma mark - CPDFThumbnailViewControllerDelegate
+
+- (void)thumbnailViewController:(CPDFThumbnailViewController *)thumbnailViewController pageIndex:(NSInteger)pageIndex {
+    [self.pdfListView goToPageIndex:pageIndex animated:NO];
+    [self.navigationController popViewControllerAnimated:YES];
+}
+
+#pragma mark - CPDFAnnotationBarDelegate
+
+- (void)clickAnnotationBar:(CPDFAnnotationBar *)annotationBar forIndex:(NSInteger)index forSelected:(BOOL)isSelected forAnnotationBarButton:(UIButton *)buttom {
+    _baseVC = [[CPDFAnnotationBaseViewController alloc] init];
+    AAPLCustomPresentationController *presentationController NS_VALID_UNTIL_END_OF_SCOPE;
+   
+    presentationController = [[AAPLCustomPresentationController alloc] initWithPresentedViewController:self.baseVC presentingViewController:self];
+    self.baseVC.transitioningDelegate = presentationController;
+    [self presentViewController:self.baseVC animated:YES completion:nil];
+}
+
+@end