CPDFViewBaseController.m 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852
  1. //
  2. // CPDFViewBaseController.m
  3. // ComPDFKit_Tools
  4. //
  5. // Copyright © 2014-2024 PDF Technologies, Inc. All Rights Reserved.
  6. //
  7. // THIS SOURCE CODE AND ANY ACCOMPANYING DOCUMENTATION ARE PROTECTED BY INTERNATIONAL COPYRIGHT LAW
  8. // AND MAY NOT BE RESOLD OR REDISTRIBUTED. USAGE IS BOUND TO THE ComPDFKit LICENSE AGREEMENT.
  9. // UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES.
  10. // This notice may not be removed from this file.
  11. //
  12. #import "CPDFViewBaseController.h"
  13. #import "CDocumentPasswordViewController.h"
  14. #import "CPDFListView+UndoManager.h"
  15. #import "CPDFConfiguration.h"
  16. #import <ComPDFKit/ComPDFKit.h>
  17. #if __has_include(<ComPDFKit_Tools/ComPDFKit_Tools.h>)
  18. #import <ComPDFKit_Tools/ComPDFKit_Tools.h>
  19. #else
  20. #import "ComPDFKit_Tools.h"
  21. #endif
  22. @interface CPDFViewBaseController ()<UISearchBarDelegate,CPDFViewDelegate,CPDFListViewDelegate, CSearchToolbarDelegate, CPDFDisplayViewDelegate, CPDFBOTAViewControllerDelegate,CPDFSearchResultsDelegate, CPDFThumbnailViewControllerDelegate,CPDFPopMenuViewDelegate,UIDocumentPickerDelegate,CPDFPopMenuDelegate,CDocumentPasswordViewControllerDelegate,CPDFPageEditViewControllerDelegate>
  23. @property(nonatomic, strong) NSString *filePath;
  24. @property(nonatomic, strong) CPDFListView *pdfListView;
  25. @property(nonatomic, strong) CSearchToolbar *searchToolbar;
  26. @property(nonatomic, strong) CPDFPopMenu *popMenu;
  27. @property(nonatomic, strong) NSArray * leftBarButtonItems;
  28. @property(nonatomic, strong) NSArray * rightBarButtonItems;
  29. @property(nonatomic, strong) NSString *password;
  30. @property(nonatomic, strong) CPDFConfiguration *configuration;
  31. @property (nonatomic, strong) UIBarButtonItem *thumbnailBarItem;
  32. @property (nonatomic, strong) UIBarButtonItem *backBarItem;
  33. @end
  34. @implementation CPDFViewBaseController
  35. #pragma mark - Initializers
  36. - (instancetype)initWithFilePath:(NSString *)filePath password:(nullable NSString *)password{
  37. if(self = [super init]) {
  38. self.filePath = filePath;
  39. self.password = password;
  40. _configuration = [[CPDFConfiguration alloc] init];
  41. CNavBarButtonItem *thumbnail = [[CNavBarButtonItem alloc]initWithViewLeftBarButtonItem:CPDFViewBarLeftButtonItem_Thumbnail];
  42. CNavBarButtonItem *search = [[CNavBarButtonItem alloc]initWithViewRightBarButtonItem:CPDFViewBarRightButtonItem_Search];
  43. CNavBarButtonItem *bota = [[CNavBarButtonItem alloc]initWithViewRightBarButtonItem:CPDFViewBarRightButtonItem_Bota];
  44. CNavBarButtonItem *more = [[CNavBarButtonItem alloc]initWithViewRightBarButtonItem:CPDFViewBarRightButtonItem_More];
  45. _configuration.showleftItems = @[thumbnail];
  46. _configuration.showRightItems = @[search,bota,more];
  47. }
  48. return self;
  49. }
  50. - (instancetype)initWithFilePath:(NSString *)filePath password:(nullable NSString *)password configuration:(CPDFConfiguration *)configuration {
  51. if(self = [super init]) {
  52. self.filePath = filePath;
  53. self.password = password;
  54. self.configuration = configuration;
  55. }
  56. return self;
  57. }
  58. - (void)viewDidLoad {
  59. [super viewDidLoad];
  60. self.automaticallyAdjustsScrollViewInsets = NO;
  61. self.view.backgroundColor = [CPDFColorUtils CPDFViewControllerBackgroundColor];
  62. [self initWitPDFListView];
  63. [self initWitNavigation];
  64. [self initWitNavigationTitle];
  65. [self initWithSearchTool];
  66. [self reloadDocumentWithFilePath:self.filePath password:self.password completion:^(BOOL result) {
  67. }];
  68. }
  69. #pragma mark - Private method
  70. -(void)updatePDFViewDocumentView {
  71. UIScrollView * documentView = [self.pdfListView documentView];
  72. if (CPDFDisplayDirectionVertical == [CPDFKitConfig sharedInstance].displayDirection) {
  73. if (self.pdfListView.currentPageIndex != 0) {
  74. if (@available(iOS 11.0, *)) {
  75. documentView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
  76. } else {
  77. self.automaticallyAdjustsScrollViewInsets = NO;
  78. }
  79. } else {
  80. if (@available(iOS 11.0, *)) {
  81. documentView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentAutomatic;
  82. } else {
  83. self.automaticallyAdjustsScrollViewInsets = YES;
  84. }
  85. }
  86. } else {
  87. if (@available(iOS 11.0, *)) {
  88. documentView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
  89. } else {
  90. self.automaticallyAdjustsScrollViewInsets = NO;
  91. }
  92. }
  93. }
  94. - (void)initWitPDFListView {
  95. self.pdfListView = [[CPDFListView alloc] initWithFrame:self.view.bounds];
  96. self.pdfListView.performDelegate = self;
  97. self.pdfListView.delegate = self;
  98. self.pdfListView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  99. [self.view addSubview:self.pdfListView];
  100. }
  101. - (CActivityIndicatorView *)loadingView {
  102. if (!_loadingView) {
  103. _loadingView = [[CActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
  104. _loadingView.center = self.view.center;
  105. _loadingView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
  106. }
  107. return _loadingView;
  108. }
  109. - (void)initWitNavigation {
  110. UIBarButtonItem *thumbnailItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"CPDFThunbnailImageEnter" inBundle:[NSBundle bundleForClass:CPDFViewBaseController.class] compatibleWithTraitCollection:nil] style:UIBarButtonItemStylePlain target:self action:@selector(buttonItemClicked_thumbnail:)];
  111. UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"CPDFViewImageBack" inBundle:[NSBundle bundleForClass:CPDFViewBaseController.class] compatibleWithTraitCollection:nil] style:UIBarButtonItemStylePlain target:self action:@selector(buttonItemClicked_back:)];
  112. self.thumbnailBarItem = thumbnailItem;
  113. self.backBarItem = backItem;
  114. NSMutableArray *leftItems = [NSMutableArray array];
  115. for (CNavBarButtonItem *item in self.configuration.showleftItems) {
  116. if(CPDFViewBarLeftButtonItem_Back == item.leftBarItem)
  117. [leftItems addObject:backItem];
  118. else if (CPDFViewBarLeftButtonItem_Thumbnail == item.leftBarItem)
  119. [leftItems addObject:thumbnailItem];
  120. }
  121. self.navigationItem.leftBarButtonItems = leftItems;
  122. __block typeof(self) blockSelf = self;
  123. NSMutableArray *actions = [NSMutableArray array];
  124. for (CNavBarButtonItem *item in self.configuration.showRightItems) {
  125. if(CPDFViewBarRightButtonItem_Bota == item.rightBarItem) {
  126. UIImage *image = [UIImage imageNamed:@"CNavigationImageNameBota"
  127. inBundle:[NSBundle bundleForClass:CPDFViewBaseController.class]
  128. compatibleWithTraitCollection:nil];
  129. CNavigationRightAction *action = [CNavigationRightAction actionWithImage:image tag:CNavigationRightTypeBota];
  130. [actions addObject:action];
  131. } else if (CPDFViewBarRightButtonItem_Search == item.rightBarItem) {
  132. UIImage *image = [UIImage imageNamed:@"CNavigationImageNameSearch"
  133. inBundle:[NSBundle bundleForClass:CPDFViewBaseController.class]
  134. compatibleWithTraitCollection:nil];
  135. CNavigationRightAction *action = [CNavigationRightAction actionWithImage:image tag:CNavigationRightTypeSearch];
  136. [actions addObject:action];
  137. } else if (CPDFViewBarRightButtonItem_More == item.rightBarItem) {
  138. UIImage *image = [UIImage imageNamed:@"CNavigationImageNameMore"
  139. inBundle:[NSBundle bundleForClass:CPDFViewBaseController.class]
  140. compatibleWithTraitCollection:nil];
  141. CNavigationRightAction *action = [CNavigationRightAction actionWithImage:image tag:CNavigationRightTypeMore];
  142. [actions addObject:action];
  143. }
  144. }
  145. self.rightView = [[CNavigationRightView alloc] initWithRightActions:actions clickBack:^(NSUInteger tag) {
  146. switch (tag) {
  147. case CNavigationRightTypeSearch:
  148. [blockSelf buttonItemClicked_Search:self.rightView];
  149. break;
  150. case CNavigationRightTypeBota:
  151. [blockSelf buttonItemClicked_Bota:self.rightView];
  152. break;
  153. default:
  154. case CNavigationRightTypeMore:
  155. [blockSelf buttonItemClicked_More:self.rightView];
  156. break;
  157. }
  158. }];
  159. }
  160. - (void)initWithSearchTool {
  161. self.searchToolbar = [[CSearchToolbar alloc] initWithPDFView:self.pdfListView];
  162. self.searchToolbar.delegate = self;
  163. }
  164. - (void)enterPDFSetting {
  165. [self.popMenu hideMenu];
  166. CPDFDisplayViewController *displayVc = [[CPDFDisplayViewController alloc] initWithPDFView:self.pdfListView];
  167. displayVc.delegate = self;
  168. AAPLCustomPresentationController *presentationController NS_VALID_UNTIL_END_OF_SCOPE;
  169. presentationController = [[AAPLCustomPresentationController alloc] initWithPresentedViewController:displayVc presentingViewController:self];
  170. displayVc.transitioningDelegate = presentationController;
  171. [self presentViewController:displayVc animated:YES completion:nil];
  172. }
  173. - (void)enterPDFInfo {
  174. [self.popMenu hideMenu];
  175. CPDFInfoViewController * infoVc = [[CPDFInfoViewController alloc] initWithPDFView:self.pdfListView];
  176. AAPLCustomPresentationController *presentationController NS_VALID_UNTIL_END_OF_SCOPE;
  177. presentationController = [[AAPLCustomPresentationController alloc] initWithPresentedViewController:infoVc presentingViewController:self];
  178. infoVc.transitioningDelegate = presentationController;
  179. [self presentViewController:infoVc animated:YES completion:nil];
  180. }
  181. - (void)enterPDFShare {
  182. [self.popMenu hideMenu];
  183. if (self.pdfListView.isEditing && self.pdfListView.isEdited) {
  184. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  185. [self.pdfListView commitEditing];
  186. NSString *documentFolder = [NSHomeDirectory() stringByAppendingFormat:@"/%@/%@", @"Documents", self.pdfListView.document.documentURL.lastPathComponent];
  187. if([[NSFileManager defaultManager] fileExistsAtPath:documentFolder]) {
  188. [[NSFileManager defaultManager] removeItemAtURL:[NSURL fileURLWithPath:documentFolder] error:nil];
  189. }
  190. NSURL *url = [NSURL fileURLWithPath:documentFolder];
  191. [self.pdfListView.document writeToURL:url];
  192. dispatch_async(dispatch_get_main_queue(), ^{
  193. [self shareAction:url];
  194. });
  195. });
  196. } else {
  197. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  198. NSString *documentFolder = [NSHomeDirectory() stringByAppendingFormat:@"/%@/%@", @"Documents", self.pdfListView.document.documentURL.lastPathComponent];
  199. if([[NSFileManager defaultManager] fileExistsAtPath:documentFolder]) {
  200. [[NSFileManager defaultManager] removeItemAtURL:[NSURL fileURLWithPath:documentFolder] error:nil];
  201. }
  202. NSURL *url = [NSURL fileURLWithPath:documentFolder];
  203. [self.pdfListView.document writeToURL:url];
  204. dispatch_async(dispatch_get_main_queue(), ^{
  205. [self shareAction:url];
  206. });
  207. });
  208. }
  209. }
  210. - (void)enterPDFAddFile {
  211. [self.popMenu hideMenu];
  212. NSArray *documentTypes = @[@"com.adobe.pdf"];
  213. self.documentPickerViewController = [[UIDocumentPickerViewController alloc] initWithDocumentTypes:documentTypes inMode:UIDocumentPickerModeOpen];
  214. self.documentPickerViewController.delegate = self;
  215. [self presentViewController:self.documentPickerViewController animated:YES completion:nil];
  216. }
  217. - (void)enterPDFPageEdit{
  218. [self.popMenu hideMenu];
  219. if(self.pdfListView.activeAnnotations.count > 0) {
  220. [self.pdfListView updateActiveAnnotations:@[]];
  221. [self.pdfListView setNeedsDisplayForVisiblePages];
  222. }
  223. if(self.pdfListView.isEditing) {
  224. if(self.pdfListView.isEdited) {
  225. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  226. [self.pdfListView commitEditing];
  227. dispatch_async(dispatch_get_main_queue(), ^{
  228. CPDFPageEditViewController *pageEditViewcontroller = [[CPDFPageEditViewController alloc] initWithPDFView:self.pdfListView];
  229. pageEditViewcontroller.pageEditDelegate = self;
  230. CNavigationController *nav = [[CNavigationController alloc]initWithRootViewController:pageEditViewcontroller];
  231. nav.modalPresentationStyle = UIModalPresentationFullScreen;
  232. [self.navigationController presentViewController:nav animated:YES completion:nil];
  233. [pageEditViewcontroller beginEdit];
  234. });
  235. });
  236. } else {
  237. CPDFPageEditViewController *pageEditViewcontroller = [[CPDFPageEditViewController alloc] initWithPDFView:self.pdfListView];
  238. pageEditViewcontroller.pageEditDelegate = self;
  239. CNavigationController *nav = [[CNavigationController alloc]initWithRootViewController:pageEditViewcontroller];
  240. nav.modalPresentationStyle = UIModalPresentationFullScreen;
  241. [self.navigationController presentViewController:nav animated:YES completion:nil];
  242. [pageEditViewcontroller beginEdit];
  243. }
  244. } else {
  245. CPDFPageEditViewController *pageEditViewcontroller = [[CPDFPageEditViewController alloc] initWithPDFView:self.pdfListView];
  246. pageEditViewcontroller.pageEditDelegate = self;
  247. CNavigationController *nav = [[CNavigationController alloc]initWithRootViewController:pageEditViewcontroller];
  248. nav.modalPresentationStyle = UIModalPresentationFullScreen;
  249. [self.navigationController presentViewController:nav animated:YES completion:nil];
  250. [pageEditViewcontroller beginEdit];
  251. }
  252. }
  253. #pragma mark - CPDFPageEditViewControllerDelegate
  254. - (void)pageEditViewControllerDone:(CPDFPageEditViewController *)pageEditViewController {
  255. __weak typeof(self) weakSelf = self;
  256. [pageEditViewController dismissViewControllerAnimated:YES completion:^{
  257. if (pageEditViewController.isPageEdit) {
  258. [weakSelf reloadDocumentWithFilePath:self.filePath password:weakSelf.pdfListView.document.password completion:^(BOOL result) {
  259. [weakSelf.pdfListView reloadInputViews];
  260. [weakSelf selectDocumentRefresh];
  261. }];
  262. }
  263. }];
  264. }
  265. - (void)pageEditViewController:(CPDFPageEditViewController *)pageEditViewController pageIndex:(NSInteger)pageIndex isPageEdit:(BOOL)isPageEdit {
  266. __weak typeof(self) weakSelf = self;
  267. [pageEditViewController dismissViewControllerAnimated:YES completion:^{
  268. if (isPageEdit) {
  269. [weakSelf reloadDocumentWithFilePath:self.pdfListView.document.documentURL.path password:weakSelf.pdfListView.document.password completion:^(BOOL result) {
  270. [weakSelf.pdfListView reloadInputViews];
  271. [self.pdfListView goToPageIndex:pageIndex animated:NO];
  272. }];
  273. [weakSelf.pdfListView reloadInputViews];
  274. } else {
  275. [weakSelf.pdfListView goToPageIndex:pageIndex animated:NO];
  276. }
  277. }];
  278. }
  279. #pragma mark - Public method
  280. - (void)initWitNavigationTitle {
  281. CNavigationBarTitleButton * navTitleButton = [[CNavigationBarTitleButton alloc] init];
  282. self.titleButton = navTitleButton;
  283. self.navigationTitle = NSLocalizedString(@"View", nil);
  284. [navTitleButton setImage:[UIImage imageNamed:@"syasarrow" inBundle:[NSBundle bundleForClass:CPDFViewBaseController.class]
  285. compatibleWithTraitCollection:nil] forState:UIControlStateNormal];
  286. [navTitleButton addTarget:self action:@selector(titleButtonClickd:) forControlEvents:UIControlEventTouchUpInside];
  287. [navTitleButton setTitle:self.navigationTitle forState:UIControlStateNormal];
  288. [navTitleButton setTitleColor:[CPDFColorUtils CAnyReverseBackgooundColor] forState:UIControlStateNormal];
  289. self.titleButton.frame = CGRectMake(0, 0, 100, 30);
  290. self.navigationItem.titleView = self.titleButton;
  291. }
  292. - (void)reloadDocumentWithFilePath:(NSString *)filePath password:(nullable NSString *)password completion:(void (^)(BOOL result))completion {
  293. [self.navigationController.view setUserInteractionEnabled:NO];
  294. if (![self.loadingView superview]) {
  295. [self.view addSubview:self.loadingView];
  296. }
  297. [self.loadingView startAnimating];
  298. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  299. NSURL *url = [NSURL fileURLWithPath:filePath];
  300. CPDFDocument *document = [[CPDFDocument alloc] initWithURL:url];
  301. if([document isLocked]) {
  302. [document unlockWithPassword:password];
  303. }
  304. dispatch_async(dispatch_get_main_queue(), ^{
  305. [self.navigationController.view setUserInteractionEnabled:YES];
  306. [self.loadingView stopAnimating];
  307. [self.loadingView removeFromSuperview];
  308. if (document.error && document.error.code != CPDFDocumentPasswordError) {
  309. UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"OK", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  310. [self.navigationController popViewControllerAnimated:YES];
  311. }];
  312. UIAlertController *alert = [UIAlertController alertControllerWithTitle:@""
  313. message:NSLocalizedString(@"Sorry PDF Reader Can't open this pdf file!", nil)
  314. preferredStyle:UIAlertControllerStyleAlert];
  315. [alert addAction:okAction];
  316. if (completion) {
  317. completion(NO);
  318. }
  319. } else {
  320. self.pdfListView.document = document;
  321. if (completion) {
  322. completion(YES);
  323. }
  324. }
  325. });
  326. });
  327. }
  328. - (void)setTitleRefresh {
  329. }
  330. - (void)selectDocumentRefresh {
  331. }
  332. - (void)shareRefresh {
  333. }
  334. #pragma mark - Action
  335. - (void)buttonItemClicked_Search:(id)sender {
  336. [self.searchToolbar showInView:self.navigationController.navigationBar];
  337. self.navigationTitle = @"";
  338. self.navigationItem.titleView.hidden = YES;
  339. self.leftBarButtonItems = self.navigationItem.leftBarButtonItems;
  340. self.rightBarButtonItems = self.navigationItem.rightBarButtonItems;
  341. self.navigationItem.rightBarButtonItems = @[];
  342. self.navigationItem.leftBarButtonItems = @[];
  343. }
  344. - (void)buttonItemClicked_Bota:(id)sender {
  345. CPDFBOTAViewController *botaViewController = [[CPDFBOTAViewController alloc] initWithPDFView:self.pdfListView];
  346. botaViewController.delegate = self;
  347. AAPLCustomPresentationController *presentationController NS_VALID_UNTIL_END_OF_SCOPE;
  348. presentationController = [[AAPLCustomPresentationController alloc] initWithPresentedViewController:botaViewController presentingViewController:self];
  349. botaViewController.transitioningDelegate = presentationController;
  350. [self presentViewController:botaViewController animated:YES completion:nil];
  351. }
  352. - (void)buttonItemClicked_More:(id)sender {
  353. CPDFPopMenuView * menuView = [[CPDFPopMenuView alloc] initWithFrame:CGRectMake(0, 0, 200, 250)];
  354. menuView.delegate = self;
  355. self.popMenu = [CPDFPopMenu popMenuWithContentView:menuView];
  356. self.popMenu.dimCoverLayer = YES;
  357. self.popMenu.delegate = self;
  358. if (@available(iOS 11.0, *)) {
  359. [self.popMenu showMenuInRect:CGRectMake(self.view.frame.size.width - self.view.safeAreaInsets.right - 250, CGRectGetMaxY(self.navigationController.navigationBar.frame), 250, 250)];
  360. } else {
  361. // Fallback on earlier versions
  362. [self.popMenu showMenuInRect:CGRectMake(self.view.frame.size.width - 250, CGRectGetMaxY(self.navigationController.navigationBar.frame), 250, 250)];
  363. }
  364. }
  365. - (void)buttonItemClicked_thumbnail:(id)sender {
  366. if(self.pdfListView.activeAnnotations.count > 0) {
  367. [self.pdfListView updateActiveAnnotations:@[]];
  368. [self.pdfListView setNeedsDisplayForVisiblePages];
  369. }
  370. if(self.pdfListView.isEditing) {
  371. if(self.pdfListView.isEdited) {
  372. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  373. [self.pdfListView commitEditing];
  374. dispatch_async(dispatch_get_main_queue(), ^{
  375. [self enterThumbnail];
  376. });
  377. });
  378. } else {
  379. [self enterThumbnail];
  380. }
  381. } else {
  382. [self enterThumbnail];
  383. }
  384. }
  385. - (void)enterThumbnail {
  386. CPDFThumbnailViewController *thumbnailViewController = [[CPDFThumbnailViewController alloc] initWithPDFView:self.pdfListView];
  387. thumbnailViewController.delegate = self;
  388. AAPLCustomPresentationController *presentationController NS_VALID_UNTIL_END_OF_SCOPE;
  389. presentationController = [[AAPLCustomPresentationController alloc] initWithPresentedViewController:thumbnailViewController presentingViewController:self];
  390. thumbnailViewController.transitioningDelegate = presentationController;
  391. UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:thumbnailViewController];
  392. [self presentViewController:nav animated:YES completion:nil];
  393. }
  394. - (void)buttonItemClicked_back:(id)sender {
  395. if (self.pdfListView.isEdited) {
  396. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  397. [self.pdfListView commitEditing];
  398. dispatch_async(dispatch_get_main_queue(), ^{
  399. [self.pdfListView endOfEditing];
  400. if([self.delegate respondsToSelector:@selector(PDFViewBaseControllerDissmiss:)]) {
  401. [self.delegate PDFViewBaseControllerDissmiss:self];
  402. }
  403. });
  404. });
  405. } else {
  406. [self.pdfListView endOfEditing];
  407. if([self.delegate respondsToSelector:@selector(PDFViewBaseControllerDissmiss:)]) {
  408. [self.delegate PDFViewBaseControllerDissmiss:self];
  409. }
  410. }
  411. }
  412. #pragma mark - CPDFViewDelegate
  413. - (void)PDFViewDocumentDidLoaded:(CPDFView *)pdfView {
  414. UIBarButtonItem* rightItem = [[UIBarButtonItem alloc] initWithCustomView:self.rightView];
  415. self.navigationItem.rightBarButtonItems = @[rightItem];
  416. [self updatePDFViewDocumentView];
  417. [self.navigationController.view setUserInteractionEnabled:NO];
  418. if (![self.loadingView superview]) {
  419. [self.view addSubview:self.loadingView];
  420. }
  421. [self.loadingView startAnimating];
  422. NSArray *signatures = [pdfView.document signatures];
  423. NSMutableArray *mSignatures = [NSMutableArray array];
  424. for (CPDFSignature *sign in signatures) {
  425. if (sign.signers.count > 0) {
  426. [mSignatures addObject:sign];
  427. }
  428. }
  429. self.signatures = mSignatures;
  430. [self.navigationController.view setUserInteractionEnabled:YES];
  431. [self.loadingView stopAnimating];
  432. [self.loadingView removeFromSuperview];
  433. }
  434. - (void)PDFViewCurrentPageDidChanged:(CPDFListView *)pdfView {
  435. [self updatePDFViewDocumentView];
  436. }
  437. - (void)PDFViewPerformURL:(CPDFView *)pdfView withContent:(NSString *)content {
  438. if (content) {
  439. [[UIApplication sharedApplication] openURL:[NSURL URLWithString:content]];
  440. } else {
  441. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"OK", nil)
  442. style:UIAlertActionStyleCancel
  443. handler:nil];
  444. UIAlertController *alert = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"Error", nil)
  445. message:NSLocalizedString(@"The hyperlink is invalid.", nil)
  446. preferredStyle:UIAlertControllerStyleAlert];
  447. [alert addAction:cancelAction];
  448. [self presentViewController:alert animated:YES completion:nil];
  449. }
  450. }
  451. - (void)PDFViewPerformReset:(CPDFView *)pdfView {
  452. UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Yes", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
  453. [self.pdfListView.document resetForm];
  454. [self.pdfListView setNeedsDisplayForVisiblePages];
  455. }];
  456. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"No", nil) style:UIAlertActionStyleCancel handler:nil];
  457. UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil
  458. message:[NSString stringWithFormat:NSLocalizedString(@"Do you really want to reset the form?", nil)]
  459. preferredStyle:UIAlertControllerStyleAlert];
  460. [alert addAction:okAction];
  461. [alert addAction:cancelAction];
  462. [self presentViewController:alert animated:YES completion:nil];
  463. }
  464. - (void)PDFViewPerformPrint:(CPDFView *)pdfView {
  465. NSLog(@"Print");
  466. }
  467. #pragma mark - CSearchToolbarDelegate
  468. - (void)searchToolbar:(CSearchToolbar *)searchToolbar onSearchQueryResults:(NSArray *)results {
  469. if ([results count] < 1) {
  470. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"OK", nil)
  471. style:UIAlertActionStyleCancel
  472. handler:nil];
  473. UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil
  474. message:NSLocalizedString(@"your have‘t search result", nil)
  475. preferredStyle:UIAlertControllerStyleAlert];
  476. [alert addAction:cancelAction];
  477. [self presentViewController:alert animated:YES completion:nil];
  478. return;
  479. }
  480. CPDFSearchResultsViewController* searchResultController = [[CPDFSearchResultsViewController alloc] initWithResultArray:results keyword:searchToolbar.searchKeyString document:self.pdfListView.document];
  481. searchResultController.pdfListView = self.pdfListView;
  482. searchResultController.delegate = self;
  483. searchResultController.searchString = searchToolbar.searchKeyString;
  484. CNavigationController *nav = [[CNavigationController alloc]initWithRootViewController:searchResultController];
  485. nav.modalPresentationStyle = UIModalPresentationFullScreen;
  486. [self presentViewController:nav animated:YES completion:nil];
  487. }
  488. - (void)searchToolbarOnExitSearch:(CSearchToolbar *)searchToolbar {
  489. if([searchToolbar superview]) {
  490. [searchToolbar removeFromSuperview];
  491. self.title = self.navigationTitle;
  492. self.navigationItem.leftBarButtonItems = self.leftBarButtonItems;
  493. self.navigationItem.rightBarButtonItems = self.rightBarButtonItems;
  494. }
  495. self.navigationTitle = NSLocalizedString(@"Viewer",nil);
  496. self.navigationItem.titleView.hidden = NO;
  497. [self.pdfListView setHighlightedSelection:nil animated:YES];
  498. [self.pdfListView setNeedsDisplayForVisiblePages];
  499. [self.searchToolbar clearDatas];
  500. }
  501. #pragma mark - CPDFDisplayViewDelegate
  502. - (void)displayViewControllerDismiss:(CPDFDisplayViewController *)displayViewController {
  503. [self.navigationController dismissViewControllerAnimated:YES completion:nil];
  504. }
  505. #pragma mark - CPDFBOTAViewControllerDelegate
  506. - (void)botaViewControllerDismiss:(CPDFBOTAViewController *)botaViewController {
  507. [self.navigationController dismissViewControllerAnimated:YES completion:nil];
  508. }
  509. #pragma mark - CPDFSearchResultsDelegate
  510. - (void)searchResultsView:(CPDFSearchResultsViewController *)resultVC forSelection:(CPDFSelection *)selection indexPath:(NSIndexPath *)indexPath {
  511. NSInteger pageIndex = [self.pdfListView.document indexForPage:selection.page];
  512. [self.pdfListView goToPageIndex:pageIndex animated:NO];
  513. [self.pdfListView setHighlightedSelection:selection animated:YES];
  514. }
  515. - (void)searchResultsViewControllerDismiss:(CPDFSearchResultsViewController *)searchResultsViewController {
  516. [searchResultsViewController dismissViewControllerAnimated:YES completion:nil];
  517. }
  518. - (void)searchResultsViewControllerReplace:(CPDFSearchResultsViewController *)searchResultsViewController {
  519. [searchResultsViewController dismissViewControllerAnimated:YES completion:^{
  520. [self.pdfListView setHighlightedSelection:nil animated:YES];
  521. [self.pdfListView setNeedsDisplayForVisiblePages];
  522. if([self.searchToolbar superview]) {
  523. [self.searchToolbar removeFromSuperview];
  524. self.title = self.navigationTitle;
  525. self.navigationItem.leftBarButtonItems = self.leftBarButtonItems;
  526. self.navigationItem.rightBarButtonItems = self.rightBarButtonItems;
  527. }
  528. self.navigationItem.titleView.hidden = NO;
  529. [self.searchToolbar clearDatas];
  530. self.pdfListView.superview.userInteractionEnabled = YES;
  531. }];
  532. }
  533. #pragma mark - CPDFPopMenuDelegate
  534. - (void)menuDidClosedIn:(CPDFPopMenu *)menu isClosed:(BOOL)isClosed {
  535. }
  536. #pragma mark - CPDFThumbnailViewControllerDelegate
  537. - (void)thumbnailViewController:(CPDFThumbnailViewController *)thumbnailViewController pageIndex:(NSInteger)pageIndex {
  538. [thumbnailViewController dismissViewControllerAnimated:YES completion:^{
  539. [self.pdfListView goToPageIndex:pageIndex animated:NO];
  540. }];
  541. }
  542. - (void)thumbnailViewControllerDismiss:(CPDFThumbnailViewController *)thumbnailViewController {
  543. [thumbnailViewController dismissViewControllerAnimated:YES completion:nil];
  544. }
  545. #pragma mark - CPDFMenuViewdelegate
  546. - (void)menuDidClickAtView:(CPDFPopMenuView *)view clickType:(CPDFPopMenuViewType)viewType {
  547. switch (viewType) {
  548. case CPDFPopMenuViewTypeSetting: {
  549. [self enterPDFSetting];
  550. }
  551. break;
  552. case CPDFPopMenuViewTypePageEdit: {
  553. [self enterPDFPageEdit];
  554. }
  555. break;
  556. case CPDFPopMenuViewTypeInfo:{
  557. [self enterPDFInfo];
  558. }
  559. break;
  560. case CPDFPopMenuViewTypeShare: {
  561. [self enterPDFShare];
  562. }
  563. break;
  564. case CPDFPopMenuViewTypeAddFile: {
  565. [self enterPDFAddFile];
  566. break;
  567. }
  568. default:
  569. break;
  570. }
  571. }
  572. - (void)titleButtonClickd:(UIButton *) button {
  573. }
  574. - (void)shareAction:(NSURL *)url {
  575. UIActivityViewController *activityVC = [[UIActivityViewController alloc]initWithActivityItems:@[url] applicationActivities:nil];
  576. activityVC.definesPresentationContext = YES;
  577. if (UIUserInterfaceIdiomPad == UI_USER_INTERFACE_IDIOM()) {
  578. activityVC.popoverPresentationController.sourceView = self.rightView;
  579. activityVC.popoverPresentationController.sourceRect = CGRectMake(self.rightView.bounds.origin.x + (self.rightView.bounds.size.width)/3*2 + 10, CGRectGetMaxY(self.rightView.bounds), 1, 1);
  580. }
  581. [self presentViewController:activityVC animated:YES completion:nil];
  582. activityVC.completionWithItemsHandler = ^(UIActivityType _Nullable activityType, BOOL completed, NSArray * _Nullable returnedItems, NSError * _Nullable activityError) {
  583. if (completed) {
  584. NSLog(@"Success!");
  585. } else {
  586. NSLog(@"Failed Or Canceled!");
  587. }
  588. };
  589. }
  590. #pragma mark - UIDocumentPickerDelegate
  591. - (void)documentPicker:(UIDocumentPickerViewController *)controller didPickDocumentsAtURLs:(NSArray<NSURL *> *)urls{
  592. BOOL fileUrlAuthozied = [urls.firstObject startAccessingSecurityScopedResource];
  593. if(fileUrlAuthozied){
  594. if (self.pdfListView.isEditing) {
  595. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  596. if(self.pdfListView.isEdited)
  597. [self.pdfListView commitEditing];
  598. dispatch_async(dispatch_get_main_queue(), ^{
  599. [self.pdfListView endOfEditing];
  600. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  601. if(self.pdfListView.document.isModified)
  602. [self.pdfListView.document writeToURL:self.pdfListView.document.documentURL];
  603. dispatch_async(dispatch_get_main_queue(), ^{
  604. [self openFileWithUrls:urls];
  605. });
  606. });
  607. });
  608. });
  609. } else {
  610. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  611. if(self.pdfListView.document.isModified) {
  612. [self.pdfListView.document writeToURL:self.pdfListView.document.documentURL];
  613. }
  614. dispatch_async(dispatch_get_main_queue(), ^{
  615. [self openFileWithUrls:urls];
  616. });
  617. });
  618. }
  619. }
  620. }
  621. - (void)openFileWithUrls:(NSArray<NSURL *> *)urls {
  622. NSFileCoordinator *fileCoordinator = [[NSFileCoordinator alloc] init];
  623. NSError *error;
  624. [fileCoordinator coordinateReadingItemAtURL:urls.firstObject options:0 error:&error byAccessor:^(NSURL *newURL) {
  625. NSString *documentFolder = [NSHomeDirectory() stringByAppendingFormat:@"/%@/%@", @"Documents",@"Files"];
  626. if (![[NSFileManager defaultManager] fileExistsAtPath:documentFolder])
  627. [[NSFileManager defaultManager] createDirectoryAtURL:[NSURL fileURLWithPath:documentFolder] withIntermediateDirectories:YES attributes:nil error:nil];
  628. NSString * documentPath = [documentFolder stringByAppendingPathComponent:[newURL lastPathComponent]];
  629. if (![[NSFileManager defaultManager] fileExistsAtPath:documentPath]) {
  630. [[NSFileManager defaultManager] copyItemAtPath:newURL.path toPath:documentPath error:NULL];
  631. }
  632. NSURL *url = [NSURL fileURLWithPath:documentPath];
  633. CPDFDocument *document = [[CPDFDocument alloc] initWithURL:url];
  634. self.filePath = documentPath;
  635. if (document.error && document.error.code != CPDFDocumentPasswordError) {
  636. UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"OK", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  637. }];
  638. UIAlertController *alert = [UIAlertController alertControllerWithTitle:@""
  639. message:NSLocalizedString(@"Sorry PDF Reader Can't open this pdf file!", nil)
  640. preferredStyle:UIAlertControllerStyleAlert];
  641. [alert addAction:okAction];
  642. UIViewController *tRootViewControl = [UIApplication sharedApplication].keyWindow.rootViewController;
  643. if ([tRootViewControl presentedViewController]) {
  644. tRootViewControl = [tRootViewControl presentedViewController];
  645. }
  646. [tRootViewControl presentViewController:alert animated:YES completion:nil];
  647. } else {
  648. if([document isLocked]) {
  649. CDocumentPasswordViewController *documentPasswordVC = [[CDocumentPasswordViewController alloc] initWithDocument:document];
  650. documentPasswordVC.delegate = self;
  651. documentPasswordVC.modalPresentationStyle = UIModalPresentationFullScreen;
  652. UIViewController *tRootViewControl = [UIApplication sharedApplication].keyWindow.rootViewController;
  653. if ([tRootViewControl presentedViewController]) {
  654. tRootViewControl = [tRootViewControl presentedViewController];
  655. }
  656. [tRootViewControl presentViewController:documentPasswordVC animated:YES completion:nil];
  657. } else {
  658. [self.pdfListView updateActiveAnnotations:[NSMutableArray array]];
  659. self.pdfListView.document = document;
  660. [self.pdfListView registerAsObserver];
  661. [self selectDocumentRefresh];
  662. [self setTitleRefresh];
  663. }
  664. }
  665. }];
  666. [urls.firstObject stopAccessingSecurityScopedResource];
  667. }
  668. - (void)documentPickerWasCancelled:(UIDocumentPickerViewController *)controller {
  669. }
  670. #pragma mark - CDocumentPasswordViewControllerDelegate
  671. - (void)documentPasswordViewControllerOpen:(CDocumentPasswordViewController *)documentPasswordViewController document:(nonnull CPDFDocument *)document {
  672. self.pdfListView.document = document;
  673. [self selectDocumentRefresh];
  674. [self setTitleRefresh];
  675. }
  676. - (void)PDFPageDidFindSearchChangeNotification:(NSNotification *)notification {
  677. [self.pdfListView setHighlightedSelection:nil animated:YES];
  678. [self.pdfListView setNeedsDisplayForVisiblePages];
  679. if([self.searchToolbar superview]) {
  680. [self.searchToolbar removeFromSuperview];
  681. self.title = self.navigationTitle;
  682. self.navigationItem.leftBarButtonItems = self.leftBarButtonItems;
  683. self.navigationItem.rightBarButtonItems = self.rightBarButtonItems;
  684. }
  685. self.navigationItem.titleView.hidden = NO;
  686. [self.searchToolbar clearDatas];
  687. }
  688. @end