123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852 |
- //
- // CPDFViewBaseController.m
- // ComPDFKit_Tools
- //
- // Copyright © 2014-2024 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 "CPDFViewBaseController.h"
- #import "CDocumentPasswordViewController.h"
- #import "CPDFListView+UndoManager.h"
- #import "CPDFConfiguration.h"
- #import <ComPDFKit/ComPDFKit.h>
- #if __has_include(<ComPDFKit_Tools/ComPDFKit_Tools.h>)
- #import <ComPDFKit_Tools/ComPDFKit_Tools.h>
- #else
- #import "ComPDFKit_Tools.h"
- #endif
- @interface CPDFViewBaseController ()<UISearchBarDelegate,CPDFViewDelegate,CPDFListViewDelegate, CSearchToolbarDelegate, CPDFDisplayViewDelegate, CPDFBOTAViewControllerDelegate,CPDFSearchResultsDelegate, CPDFThumbnailViewControllerDelegate,CPDFPopMenuViewDelegate,UIDocumentPickerDelegate,CPDFPopMenuDelegate,CDocumentPasswordViewControllerDelegate,CPDFPageEditViewControllerDelegate>
- @property(nonatomic, strong) NSString *filePath;
- @property(nonatomic, strong) CPDFListView *pdfListView;
- @property(nonatomic, strong) CSearchToolbar *searchToolbar;
- @property(nonatomic, strong) CPDFPopMenu *popMenu;
- @property(nonatomic, strong) NSArray * leftBarButtonItems;
- @property(nonatomic, strong) NSArray * rightBarButtonItems;
- @property(nonatomic, strong) NSString *password;
- @property(nonatomic, strong) CPDFConfiguration *configuration;
- @property (nonatomic, strong) UIBarButtonItem *thumbnailBarItem;
- @property (nonatomic, strong) UIBarButtonItem *backBarItem;
- @end
- @implementation CPDFViewBaseController
- #pragma mark - Initializers
- - (instancetype)initWithFilePath:(NSString *)filePath password:(nullable NSString *)password{
- if(self = [super init]) {
- self.filePath = filePath;
- self.password = password;
-
- _configuration = [[CPDFConfiguration alloc] init];
- CNavBarButtonItem *thumbnail = [[CNavBarButtonItem alloc]initWithViewLeftBarButtonItem:CPDFViewBarLeftButtonItem_Thumbnail];
-
- CNavBarButtonItem *search = [[CNavBarButtonItem alloc]initWithViewRightBarButtonItem:CPDFViewBarRightButtonItem_Search];
- CNavBarButtonItem *bota = [[CNavBarButtonItem alloc]initWithViewRightBarButtonItem:CPDFViewBarRightButtonItem_Bota];
- CNavBarButtonItem *more = [[CNavBarButtonItem alloc]initWithViewRightBarButtonItem:CPDFViewBarRightButtonItem_More];
-
- _configuration.showleftItems = @[thumbnail];
- _configuration.showRightItems = @[search,bota,more];
- }
- return self;
- }
- - (instancetype)initWithFilePath:(NSString *)filePath password:(nullable NSString *)password configuration:(CPDFConfiguration *)configuration {
- if(self = [super init]) {
- self.filePath = filePath;
- self.password = password;
-
- self.configuration = configuration;
- }
- return self;
- }
- - (void)viewDidLoad {
- [super viewDidLoad];
-
- self.automaticallyAdjustsScrollViewInsets = NO;
-
- self.view.backgroundColor = [CPDFColorUtils CPDFViewControllerBackgroundColor];
-
- [self initWitPDFListView];
-
- [self initWitNavigation];
-
- [self initWitNavigationTitle];
-
- [self initWithSearchTool];
-
- [self reloadDocumentWithFilePath:self.filePath password:self.password completion:^(BOOL result) {
-
- }];
- }
- #pragma mark - Private method
- -(void)updatePDFViewDocumentView {
- UIScrollView * documentView = [self.pdfListView documentView];
- if (CPDFDisplayDirectionVertical == [CPDFKitConfig sharedInstance].displayDirection) {
- if (self.pdfListView.currentPageIndex != 0) {
- if (@available(iOS 11.0, *)) {
- documentView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
- } else {
- self.automaticallyAdjustsScrollViewInsets = NO;
- }
- } else {
- if (@available(iOS 11.0, *)) {
- documentView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentAutomatic;
- } else {
- self.automaticallyAdjustsScrollViewInsets = YES;
- }
- }
- } else {
- if (@available(iOS 11.0, *)) {
- documentView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
- } else {
- self.automaticallyAdjustsScrollViewInsets = NO;
- }
- }
- }
- - (void)initWitPDFListView {
- 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];
- }
- - (CActivityIndicatorView *)loadingView {
- if (!_loadingView) {
- _loadingView = [[CActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
- _loadingView.center = self.view.center;
- _loadingView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
- }
- return _loadingView;
- }
- - (void)initWitNavigation {
- UIBarButtonItem *thumbnailItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"CPDFThunbnailImageEnter" inBundle:[NSBundle bundleForClass:CPDFViewBaseController.class] compatibleWithTraitCollection:nil] style:UIBarButtonItemStylePlain target:self action:@selector(buttonItemClicked_thumbnail:)];
-
- UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"CPDFViewImageBack" inBundle:[NSBundle bundleForClass:CPDFViewBaseController.class] compatibleWithTraitCollection:nil] style:UIBarButtonItemStylePlain target:self action:@selector(buttonItemClicked_back:)];
-
- self.thumbnailBarItem = thumbnailItem;
- self.backBarItem = backItem;
-
- NSMutableArray *leftItems = [NSMutableArray array];
- for (CNavBarButtonItem *item in self.configuration.showleftItems) {
- if(CPDFViewBarLeftButtonItem_Back == item.leftBarItem)
- [leftItems addObject:backItem];
- else if (CPDFViewBarLeftButtonItem_Thumbnail == item.leftBarItem)
- [leftItems addObject:thumbnailItem];
- }
-
- self.navigationItem.leftBarButtonItems = leftItems;
-
- __block typeof(self) blockSelf = self;
- NSMutableArray *actions = [NSMutableArray array];
-
- for (CNavBarButtonItem *item in self.configuration.showRightItems) {
- if(CPDFViewBarRightButtonItem_Bota == item.rightBarItem) {
- UIImage *image = [UIImage imageNamed:@"CNavigationImageNameBota"
- inBundle:[NSBundle bundleForClass:CPDFViewBaseController.class]
- compatibleWithTraitCollection:nil];
- CNavigationRightAction *action = [CNavigationRightAction actionWithImage:image tag:CNavigationRightTypeBota];
- [actions addObject:action];
-
- } else if (CPDFViewBarRightButtonItem_Search == item.rightBarItem) {
- UIImage *image = [UIImage imageNamed:@"CNavigationImageNameSearch"
- inBundle:[NSBundle bundleForClass:CPDFViewBaseController.class]
- compatibleWithTraitCollection:nil];
- CNavigationRightAction *action = [CNavigationRightAction actionWithImage:image tag:CNavigationRightTypeSearch];
- [actions addObject:action];
-
- } else if (CPDFViewBarRightButtonItem_More == item.rightBarItem) {
- UIImage *image = [UIImage imageNamed:@"CNavigationImageNameMore"
- inBundle:[NSBundle bundleForClass:CPDFViewBaseController.class]
- compatibleWithTraitCollection:nil];
- CNavigationRightAction *action = [CNavigationRightAction actionWithImage:image tag:CNavigationRightTypeMore];
- [actions addObject:action];
- }
- }
-
- self.rightView = [[CNavigationRightView alloc] initWithRightActions:actions clickBack:^(NSUInteger tag) {
- switch (tag) {
- case CNavigationRightTypeSearch:
- [blockSelf buttonItemClicked_Search:self.rightView];
- break;
- case CNavigationRightTypeBota:
- [blockSelf buttonItemClicked_Bota:self.rightView];
- break;
- default:
- case CNavigationRightTypeMore:
- [blockSelf buttonItemClicked_More:self.rightView];
- break;
- }
- }];
- }
- - (void)initWithSearchTool {
- self.searchToolbar = [[CSearchToolbar alloc] initWithPDFView:self.pdfListView];
- self.searchToolbar.delegate = self;
- }
- - (void)enterPDFSetting {
- [self.popMenu hideMenu];
- CPDFDisplayViewController *displayVc = [[CPDFDisplayViewController alloc] initWithPDFView:self.pdfListView];
- displayVc.delegate = self;
-
-
- AAPLCustomPresentationController *presentationController NS_VALID_UNTIL_END_OF_SCOPE;
- presentationController = [[AAPLCustomPresentationController alloc] initWithPresentedViewController:displayVc presentingViewController:self];
- displayVc.transitioningDelegate = presentationController;
- [self presentViewController:displayVc animated:YES completion:nil];
- }
- - (void)enterPDFInfo {
- [self.popMenu hideMenu];
- CPDFInfoViewController * infoVc = [[CPDFInfoViewController alloc] initWithPDFView:self.pdfListView];
-
- AAPLCustomPresentationController *presentationController NS_VALID_UNTIL_END_OF_SCOPE;
- presentationController = [[AAPLCustomPresentationController alloc] initWithPresentedViewController:infoVc presentingViewController:self];
- infoVc.transitioningDelegate = presentationController;
- [self presentViewController:infoVc animated:YES completion:nil];
- }
- - (void)enterPDFShare {
- [self.popMenu hideMenu];
-
- if (self.pdfListView.isEditing && self.pdfListView.isEdited) {
- dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
- [self.pdfListView commitEditing];
-
- NSString *documentFolder = [NSHomeDirectory() stringByAppendingFormat:@"/%@/%@", @"Documents", self.pdfListView.document.documentURL.lastPathComponent];
- if([[NSFileManager defaultManager] fileExistsAtPath:documentFolder]) {
- [[NSFileManager defaultManager] removeItemAtURL:[NSURL fileURLWithPath:documentFolder] error:nil];
- }
- NSURL *url = [NSURL fileURLWithPath:documentFolder];
-
- [self.pdfListView.document writeToURL:url];
- dispatch_async(dispatch_get_main_queue(), ^{
- [self shareAction:url];
- });
- });
- } else {
- dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
- NSString *documentFolder = [NSHomeDirectory() stringByAppendingFormat:@"/%@/%@", @"Documents", self.pdfListView.document.documentURL.lastPathComponent];
- if([[NSFileManager defaultManager] fileExistsAtPath:documentFolder]) {
- [[NSFileManager defaultManager] removeItemAtURL:[NSURL fileURLWithPath:documentFolder] error:nil];
- }
- NSURL *url = [NSURL fileURLWithPath:documentFolder];
-
- [self.pdfListView.document writeToURL:url];
- dispatch_async(dispatch_get_main_queue(), ^{
- [self shareAction:url];
- });
- });
- }
-
- }
- - (void)enterPDFAddFile {
- [self.popMenu hideMenu];
-
- NSArray *documentTypes = @[@"com.adobe.pdf"];
- self.documentPickerViewController = [[UIDocumentPickerViewController alloc] initWithDocumentTypes:documentTypes inMode:UIDocumentPickerModeOpen];
- self.documentPickerViewController.delegate = self;
- [self presentViewController:self.documentPickerViewController animated:YES completion:nil];
- }
- - (void)enterPDFPageEdit{
- [self.popMenu hideMenu];
- if(self.pdfListView.activeAnnotations.count > 0) {
- [self.pdfListView updateActiveAnnotations:@[]];
- [self.pdfListView setNeedsDisplayForVisiblePages];
- }
-
- if(self.pdfListView.isEditing) {
- if(self.pdfListView.isEdited) {
- dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
- [self.pdfListView commitEditing];
- dispatch_async(dispatch_get_main_queue(), ^{
- CPDFPageEditViewController *pageEditViewcontroller = [[CPDFPageEditViewController alloc] initWithPDFView:self.pdfListView];
- pageEditViewcontroller.pageEditDelegate = self;
- CNavigationController *nav = [[CNavigationController alloc]initWithRootViewController:pageEditViewcontroller];
- nav.modalPresentationStyle = UIModalPresentationFullScreen;
- [self.navigationController presentViewController:nav animated:YES completion:nil];
- [pageEditViewcontroller beginEdit];
- });
- });
- } else {
- CPDFPageEditViewController *pageEditViewcontroller = [[CPDFPageEditViewController alloc] initWithPDFView:self.pdfListView];
- pageEditViewcontroller.pageEditDelegate = self;
- CNavigationController *nav = [[CNavigationController alloc]initWithRootViewController:pageEditViewcontroller];
- nav.modalPresentationStyle = UIModalPresentationFullScreen;
- [self.navigationController presentViewController:nav animated:YES completion:nil];
- [pageEditViewcontroller beginEdit];
- }
- } else {
- CPDFPageEditViewController *pageEditViewcontroller = [[CPDFPageEditViewController alloc] initWithPDFView:self.pdfListView];
- pageEditViewcontroller.pageEditDelegate = self;
- CNavigationController *nav = [[CNavigationController alloc]initWithRootViewController:pageEditViewcontroller];
- nav.modalPresentationStyle = UIModalPresentationFullScreen;
- [self.navigationController presentViewController:nav animated:YES completion:nil];
- [pageEditViewcontroller beginEdit];
- }
- }
- #pragma mark - CPDFPageEditViewControllerDelegate
- - (void)pageEditViewControllerDone:(CPDFPageEditViewController *)pageEditViewController {
- __weak typeof(self) weakSelf = self;
- [pageEditViewController dismissViewControllerAnimated:YES completion:^{
- if (pageEditViewController.isPageEdit) {
- [weakSelf reloadDocumentWithFilePath:self.filePath password:weakSelf.pdfListView.document.password completion:^(BOOL result) {
- [weakSelf.pdfListView reloadInputViews];
- [weakSelf selectDocumentRefresh];
- }];
- }
- }];
- }
- - (void)pageEditViewController:(CPDFPageEditViewController *)pageEditViewController pageIndex:(NSInteger)pageIndex isPageEdit:(BOOL)isPageEdit {
- __weak typeof(self) weakSelf = self;
- [pageEditViewController dismissViewControllerAnimated:YES completion:^{
- if (isPageEdit) {
- [weakSelf reloadDocumentWithFilePath:self.pdfListView.document.documentURL.path password:weakSelf.pdfListView.document.password completion:^(BOOL result) {
- [weakSelf.pdfListView reloadInputViews];
- [self.pdfListView goToPageIndex:pageIndex animated:NO];
- }];
-
- [weakSelf.pdfListView reloadInputViews];
- } else {
- [weakSelf.pdfListView goToPageIndex:pageIndex animated:NO];
- }
- }];
- }
- #pragma mark - Public method
- - (void)initWitNavigationTitle {
- CNavigationBarTitleButton * navTitleButton = [[CNavigationBarTitleButton alloc] init];
- self.titleButton = navTitleButton;
- self.navigationTitle = NSLocalizedString(@"View", nil);
- [navTitleButton setImage:[UIImage imageNamed:@"syasarrow" inBundle:[NSBundle bundleForClass:CPDFViewBaseController.class]
- compatibleWithTraitCollection:nil] forState:UIControlStateNormal];
- [navTitleButton addTarget:self action:@selector(titleButtonClickd:) forControlEvents:UIControlEventTouchUpInside];
- [navTitleButton setTitle:self.navigationTitle forState:UIControlStateNormal];
- [navTitleButton setTitleColor:[CPDFColorUtils CAnyReverseBackgooundColor] forState:UIControlStateNormal];
- self.titleButton.frame = CGRectMake(0, 0, 100, 30);
- self.navigationItem.titleView = self.titleButton;
- }
- - (void)reloadDocumentWithFilePath:(NSString *)filePath password:(nullable NSString *)password completion:(void (^)(BOOL result))completion {
-
- [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];
- if([document isLocked]) {
- [document unlockWithPassword:password];
- }
- 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);
- }
- }
- });
- });
- }
- - (void)setTitleRefresh {
-
- }
- - (void)selectDocumentRefresh {
-
- }
- - (void)shareRefresh {
-
- }
- #pragma mark - Action
- - (void)buttonItemClicked_Search:(id)sender {
- [self.searchToolbar showInView:self.navigationController.navigationBar];
- self.navigationTitle = @"";
- self.navigationItem.titleView.hidden = YES;
- self.leftBarButtonItems = self.navigationItem.leftBarButtonItems;
- self.rightBarButtonItems = self.navigationItem.rightBarButtonItems;
- self.navigationItem.rightBarButtonItems = @[];
- self.navigationItem.leftBarButtonItems = @[];
- }
- - (void)buttonItemClicked_Bota:(id)sender {
- CPDFBOTAViewController *botaViewController = [[CPDFBOTAViewController alloc] initWithPDFView:self.pdfListView];
- botaViewController.delegate = self;
-
- AAPLCustomPresentationController *presentationController NS_VALID_UNTIL_END_OF_SCOPE;
-
- presentationController = [[AAPLCustomPresentationController alloc] initWithPresentedViewController:botaViewController presentingViewController:self];
- botaViewController.transitioningDelegate = presentationController;
-
- [self presentViewController:botaViewController animated:YES completion:nil];
- }
- - (void)buttonItemClicked_More:(id)sender {
- CPDFPopMenuView * menuView = [[CPDFPopMenuView alloc] initWithFrame:CGRectMake(0, 0, 200, 250)];
- menuView.delegate = self;
- self.popMenu = [CPDFPopMenu popMenuWithContentView:menuView];
- self.popMenu.dimCoverLayer = YES;
- self.popMenu.delegate = self;
-
- 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, 250)];
- } else {
- // Fallback on earlier versions
- [self.popMenu showMenuInRect:CGRectMake(self.view.frame.size.width - 250, CGRectGetMaxY(self.navigationController.navigationBar.frame), 250, 250)];
- }
- }
- - (void)buttonItemClicked_thumbnail:(id)sender {
- if(self.pdfListView.activeAnnotations.count > 0) {
- [self.pdfListView updateActiveAnnotations:@[]];
- [self.pdfListView setNeedsDisplayForVisiblePages];
- }
-
- if(self.pdfListView.isEditing) {
- if(self.pdfListView.isEdited) {
- dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
- [self.pdfListView commitEditing];
- dispatch_async(dispatch_get_main_queue(), ^{
- [self enterThumbnail];
- });
- });
- } else {
- [self enterThumbnail];
- }
- } else {
- [self enterThumbnail];
- }
- }
- - (void)enterThumbnail {
- CPDFThumbnailViewController *thumbnailViewController = [[CPDFThumbnailViewController alloc] initWithPDFView:self.pdfListView];
- thumbnailViewController.delegate = self;
-
- AAPLCustomPresentationController *presentationController NS_VALID_UNTIL_END_OF_SCOPE;
- presentationController = [[AAPLCustomPresentationController alloc] initWithPresentedViewController:thumbnailViewController presentingViewController:self];
- thumbnailViewController.transitioningDelegate = presentationController;
- UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:thumbnailViewController];
- [self presentViewController:nav animated:YES completion:nil];
- }
- - (void)buttonItemClicked_back:(id)sender {
- if (self.pdfListView.isEdited) {
- dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
- [self.pdfListView commitEditing];
- dispatch_async(dispatch_get_main_queue(), ^{
- [self.pdfListView endOfEditing];
- if([self.delegate respondsToSelector:@selector(PDFViewBaseControllerDissmiss:)]) {
- [self.delegate PDFViewBaseControllerDissmiss:self];
- }
- });
- });
- } else {
- [self.pdfListView endOfEditing];
- if([self.delegate respondsToSelector:@selector(PDFViewBaseControllerDissmiss:)]) {
- [self.delegate PDFViewBaseControllerDissmiss:self];
- }
- }
- }
- #pragma mark - CPDFViewDelegate
- - (void)PDFViewDocumentDidLoaded:(CPDFView *)pdfView {
- UIBarButtonItem* rightItem = [[UIBarButtonItem alloc] initWithCustomView:self.rightView];
- self.navigationItem.rightBarButtonItems = @[rightItem];
-
- [self updatePDFViewDocumentView];
-
- [self.navigationController.view setUserInteractionEnabled:NO];
-
- if (![self.loadingView superview]) {
- [self.view addSubview:self.loadingView];
- }
- [self.loadingView startAnimating];
- NSArray *signatures = [pdfView.document signatures];
- NSMutableArray *mSignatures = [NSMutableArray array];
- for (CPDFSignature *sign in signatures) {
- if (sign.signers.count > 0) {
- [mSignatures addObject:sign];
- }
- }
- self.signatures = mSignatures;
- [self.navigationController.view setUserInteractionEnabled:YES];
- [self.loadingView stopAnimating];
- [self.loadingView removeFromSuperview];
- }
- - (void)PDFViewCurrentPageDidChanged:(CPDFListView *)pdfView {
- [self updatePDFViewDocumentView];
- }
- - (void)PDFViewPerformURL:(CPDFView *)pdfView withContent:(NSString *)content {
- if (content) {
- [[UIApplication sharedApplication] openURL:[NSURL URLWithString:content]];
- } else {
- UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"OK", nil)
- style:UIAlertActionStyleCancel
- handler:nil];
- UIAlertController *alert = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"Error", nil)
- message:NSLocalizedString(@"The hyperlink is invalid.", nil)
- preferredStyle:UIAlertControllerStyleAlert];
- [alert addAction:cancelAction];
- [self presentViewController:alert animated:YES completion:nil];
- }
- }
- - (void)PDFViewPerformReset:(CPDFView *)pdfView {
- UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Yes", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
- [self.pdfListView.document resetForm];
- [self.pdfListView setNeedsDisplayForVisiblePages];
- }];
- UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"No", nil) style:UIAlertActionStyleCancel handler:nil];
- UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil
- message:[NSString stringWithFormat:NSLocalizedString(@"Do you really want to reset the form?", nil)]
- preferredStyle:UIAlertControllerStyleAlert];
- [alert addAction:okAction];
- [alert addAction:cancelAction];
- [self presentViewController:alert animated:YES completion:nil];
- }
- - (void)PDFViewPerformPrint:(CPDFView *)pdfView {
- NSLog(@"Print");
- }
- #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;
- }
-
- CPDFSearchResultsViewController* searchResultController = [[CPDFSearchResultsViewController alloc] initWithResultArray:results keyword:searchToolbar.searchKeyString document:self.pdfListView.document];
- searchResultController.pdfListView = self.pdfListView;
- searchResultController.delegate = self;
- searchResultController.searchString = searchToolbar.searchKeyString;
- CNavigationController *nav = [[CNavigationController alloc]initWithRootViewController:searchResultController];
- nav.modalPresentationStyle = UIModalPresentationFullScreen;
- [self presentViewController:nav animated:YES completion:nil];
- }
- - (void)searchToolbarOnExitSearch:(CSearchToolbar *)searchToolbar {
- if([searchToolbar superview]) {
- [searchToolbar removeFromSuperview];
- self.title = self.navigationTitle;
-
- self.navigationItem.leftBarButtonItems = self.leftBarButtonItems;
- self.navigationItem.rightBarButtonItems = self.rightBarButtonItems;
- }
- self.navigationTitle = NSLocalizedString(@"Viewer",nil);
- self.navigationItem.titleView.hidden = NO;
-
- [self.pdfListView setHighlightedSelection:nil animated:YES];
- [self.pdfListView setNeedsDisplayForVisiblePages];
- [self.searchToolbar clearDatas];
- }
- #pragma mark - CPDFDisplayViewDelegate
- - (void)displayViewControllerDismiss:(CPDFDisplayViewController *)displayViewController {
- [self.navigationController dismissViewControllerAnimated:YES completion:nil];
- }
- #pragma mark - CPDFBOTAViewControllerDelegate
- - (void)botaViewControllerDismiss:(CPDFBOTAViewController *)botaViewController {
- [self.navigationController dismissViewControllerAnimated:YES completion:nil];
- }
- #pragma mark - CPDFSearchResultsDelegate
- - (void)searchResultsView:(CPDFSearchResultsViewController *)resultVC forSelection:(CPDFSelection *)selection indexPath:(NSIndexPath *)indexPath {
-
- NSInteger pageIndex = [self.pdfListView.document indexForPage:selection.page];
- [self.pdfListView goToPageIndex:pageIndex animated:NO];
- [self.pdfListView setHighlightedSelection:selection animated:YES];
- }
- - (void)searchResultsViewControllerDismiss:(CPDFSearchResultsViewController *)searchResultsViewController {
-
- [searchResultsViewController dismissViewControllerAnimated:YES completion:nil];
- }
- - (void)searchResultsViewControllerReplace:(CPDFSearchResultsViewController *)searchResultsViewController {
-
- [searchResultsViewController dismissViewControllerAnimated:YES completion:^{
- [self.pdfListView setHighlightedSelection:nil animated:YES];
- [self.pdfListView setNeedsDisplayForVisiblePages];
- if([self.searchToolbar superview]) {
- [self.searchToolbar removeFromSuperview];
- self.title = self.navigationTitle;
-
- self.navigationItem.leftBarButtonItems = self.leftBarButtonItems;
- self.navigationItem.rightBarButtonItems = self.rightBarButtonItems;
- }
- self.navigationItem.titleView.hidden = NO;
- [self.searchToolbar clearDatas];
- self.pdfListView.superview.userInteractionEnabled = YES;
- }];
- }
- #pragma mark - CPDFPopMenuDelegate
- - (void)menuDidClosedIn:(CPDFPopMenu *)menu isClosed:(BOOL)isClosed {
-
- }
- #pragma mark - CPDFThumbnailViewControllerDelegate
- - (void)thumbnailViewController:(CPDFThumbnailViewController *)thumbnailViewController pageIndex:(NSInteger)pageIndex {
- [thumbnailViewController dismissViewControllerAnimated:YES completion:^{
- [self.pdfListView goToPageIndex:pageIndex animated:NO];
- }];
- }
- - (void)thumbnailViewControllerDismiss:(CPDFThumbnailViewController *)thumbnailViewController {
- [thumbnailViewController dismissViewControllerAnimated:YES completion:nil];
- }
- #pragma mark - CPDFMenuViewdelegate
- - (void)menuDidClickAtView:(CPDFPopMenuView *)view clickType:(CPDFPopMenuViewType)viewType {
- switch (viewType) {
- case CPDFPopMenuViewTypeSetting: {
- [self enterPDFSetting];
- }
- break;
-
- case CPDFPopMenuViewTypePageEdit: {
- [self enterPDFPageEdit];
- }
- break;
-
- case CPDFPopMenuViewTypeInfo:{
- [self enterPDFInfo];
- }
- break;
-
- case CPDFPopMenuViewTypeShare: {
- [self enterPDFShare];
- }
- break;
-
- case CPDFPopMenuViewTypeAddFile: {
- [self enterPDFAddFile];
- break;
- }
-
- default:
- break;
- }
- }
- - (void)titleButtonClickd:(UIButton *) button {
-
- }
- - (void)shareAction:(NSURL *)url {
- UIActivityViewController *activityVC = [[UIActivityViewController alloc]initWithActivityItems:@[url] applicationActivities:nil];
- activityVC.definesPresentationContext = YES;
- if (UIUserInterfaceIdiomPad == UI_USER_INTERFACE_IDIOM()) {
- activityVC.popoverPresentationController.sourceView = self.rightView;
- activityVC.popoverPresentationController.sourceRect = CGRectMake(self.rightView.bounds.origin.x + (self.rightView.bounds.size.width)/3*2 + 10, CGRectGetMaxY(self.rightView.bounds), 1, 1);
- }
- [self presentViewController:activityVC animated:YES completion:nil];
- activityVC.completionWithItemsHandler = ^(UIActivityType _Nullable activityType, BOOL completed, NSArray * _Nullable returnedItems, NSError * _Nullable activityError) {
- if (completed) {
- NSLog(@"Success!");
- } else {
- NSLog(@"Failed Or Canceled!");
- }
- };
- }
- #pragma mark - UIDocumentPickerDelegate
- - (void)documentPicker:(UIDocumentPickerViewController *)controller didPickDocumentsAtURLs:(NSArray<NSURL *> *)urls{
- BOOL fileUrlAuthozied = [urls.firstObject startAccessingSecurityScopedResource];
- if(fileUrlAuthozied){
- if (self.pdfListView.isEditing) {
- dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
- if(self.pdfListView.isEdited)
- [self.pdfListView commitEditing];
-
- dispatch_async(dispatch_get_main_queue(), ^{
- [self.pdfListView endOfEditing];
- dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
- if(self.pdfListView.document.isModified)
- [self.pdfListView.document writeToURL:self.pdfListView.document.documentURL];
-
- dispatch_async(dispatch_get_main_queue(), ^{
- [self openFileWithUrls:urls];
- });
- });
- });
- });
- } else {
- dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
- if(self.pdfListView.document.isModified) {
- [self.pdfListView.document writeToURL:self.pdfListView.document.documentURL];
- }
- dispatch_async(dispatch_get_main_queue(), ^{
- [self openFileWithUrls:urls];
- });
-
- });
- }
-
- }
- }
- - (void)openFileWithUrls:(NSArray<NSURL *> *)urls {
- NSFileCoordinator *fileCoordinator = [[NSFileCoordinator alloc] init];
- NSError *error;
- [fileCoordinator coordinateReadingItemAtURL:urls.firstObject options:0 error:&error byAccessor:^(NSURL *newURL) {
-
- NSString *documentFolder = [NSHomeDirectory() stringByAppendingFormat:@"/%@/%@", @"Documents",@"Files"];
- if (![[NSFileManager defaultManager] fileExistsAtPath:documentFolder])
- [[NSFileManager defaultManager] createDirectoryAtURL:[NSURL fileURLWithPath:documentFolder] withIntermediateDirectories:YES attributes:nil error:nil];
-
- NSString * documentPath = [documentFolder stringByAppendingPathComponent:[newURL lastPathComponent]];
- if (![[NSFileManager defaultManager] fileExistsAtPath:documentPath]) {
- [[NSFileManager defaultManager] copyItemAtPath:newURL.path toPath:documentPath error:NULL];
- }
- NSURL *url = [NSURL fileURLWithPath:documentPath];
- CPDFDocument *document = [[CPDFDocument alloc] initWithURL:url];
- self.filePath = documentPath;
-
- 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];
- UIViewController *tRootViewControl = [UIApplication sharedApplication].keyWindow.rootViewController;
- if ([tRootViewControl presentedViewController]) {
- tRootViewControl = [tRootViewControl presentedViewController];
- }
- [tRootViewControl presentViewController:alert animated:YES completion:nil];
- } else {
- if([document isLocked]) {
- CDocumentPasswordViewController *documentPasswordVC = [[CDocumentPasswordViewController alloc] initWithDocument:document];
- documentPasswordVC.delegate = self;
- documentPasswordVC.modalPresentationStyle = UIModalPresentationFullScreen;
-
- UIViewController *tRootViewControl = [UIApplication sharedApplication].keyWindow.rootViewController;
- if ([tRootViewControl presentedViewController]) {
- tRootViewControl = [tRootViewControl presentedViewController];
- }
- [tRootViewControl presentViewController:documentPasswordVC animated:YES completion:nil];
- } else {
- [self.pdfListView updateActiveAnnotations:[NSMutableArray array]];
- self.pdfListView.document = document;
- [self.pdfListView registerAsObserver];
- [self selectDocumentRefresh];
- [self setTitleRefresh];
- }
-
- }
-
- }];
- [urls.firstObject stopAccessingSecurityScopedResource];
- }
- - (void)documentPickerWasCancelled:(UIDocumentPickerViewController *)controller {
- }
- #pragma mark - CDocumentPasswordViewControllerDelegate
- - (void)documentPasswordViewControllerOpen:(CDocumentPasswordViewController *)documentPasswordViewController document:(nonnull CPDFDocument *)document {
- self.pdfListView.document = document;
- [self selectDocumentRefresh];
- [self setTitleRefresh];
- }
- - (void)PDFPageDidFindSearchChangeNotification:(NSNotification *)notification {
- [self.pdfListView setHighlightedSelection:nil animated:YES];
- [self.pdfListView setNeedsDisplayForVisiblePages];
- if([self.searchToolbar superview]) {
- [self.searchToolbar removeFromSuperview];
- self.title = self.navigationTitle;
-
- self.navigationItem.leftBarButtonItems = self.leftBarButtonItems;
- self.navigationItem.rightBarButtonItems = self.rightBarButtonItems;
- }
- self.navigationItem.titleView.hidden = NO;
- [self.searchToolbar clearDatas];
- }
- @end
|