CPDFViewController.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. //
  2. // CPDFViewController.m
  3. // viewer-ctrl-demo
  4. //
  5. // Copyright © 2014-2023 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 "CPDFViewController.h"
  13. #import <ComPDFKit/ComPDFKit.h>
  14. #import <compdfkit_tools/compdfkit_tools.h>
  15. @interface CPDFViewController ()<UISearchBarDelegate,CPDFViewDelegate,CPDFListViewDelegate, CPDFMoreListViewDelegate, CSearchToolbarDelegate, CPDFDisplayViewDelegate, CPDFBOTAViewControllerDelegate,CPDFSearchResultsDelegate, CPDFThumbnailViewControllerDelegate>
  16. @property(nonatomic, strong) NSString *filePath;
  17. @property(nonatomic, strong) CPDFListView *pdfListView;
  18. @property(nonatomic, strong) CNavigationRightView *rightView;
  19. @property(nonatomic, strong) CSearchToolbar *searchToolbar;
  20. @property(nonatomic, strong) CActivityIndicatorView *loadingView;
  21. @property(nonatomic, strong) NSString *navigationTitle;
  22. @end
  23. @implementation CPDFViewController
  24. #pragma mark - Initializers
  25. - (instancetype)initWithFilePath:(NSString *)filePath {
  26. if(self = [super init]) {
  27. self.filePath = filePath;
  28. }
  29. return self;
  30. }
  31. - (void)viewDidLoad {
  32. [super viewDidLoad];
  33. self.view.backgroundColor = [CPDFColorUtils CPDFViewControllerBackgroundColor];
  34. self.pdfListView = [[CPDFListView alloc] initWithFrame:self.view.bounds];
  35. self.pdfListView.performDelegate = self;
  36. self.pdfListView.delegate = self;
  37. self.pdfListView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  38. [self.view addSubview:self.pdfListView];
  39. UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"CPDFThunbnailImageEnter" inBundle:[NSBundle bundleForClass:self.class] compatibleWithTraitCollection:nil] style:UIBarButtonItemStylePlain target:self action:@selector(buttonItemClicked_thumbnail:)];
  40. self.navigationItem.leftBarButtonItem = leftItem;
  41. __block typeof(self) blockSelf = self;
  42. self.rightView = [[CNavigationRightView alloc] initWithDefaultItemsClickBack:^(NSUInteger tag) {
  43. switch (tag) {
  44. case CNavigationRightTypeSearch:
  45. [blockSelf navigationRightItemSearch];
  46. break;
  47. case CNavigationRightTypeBota:
  48. [blockSelf navigationRightItemBota];
  49. break;
  50. default:
  51. case CNavigationRightTypeMore:
  52. [blockSelf navigationRightItemMore];
  53. break;
  54. }
  55. }];
  56. self.searchToolbar = [[CSearchToolbar alloc] initWithPDFView:self.pdfListView];
  57. self.searchToolbar.delegate = self;
  58. [self reloadDocumentWithFilePath:self.filePath completion:^(BOOL result) {
  59. }];
  60. }
  61. - (void)viewWillLayoutSubviews {
  62. [super viewWillLayoutSubviews];
  63. if (@available(iOS 11.0, *)) {
  64. 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);
  65. } else {
  66. self.pdfListView.frame = self.view.bounds;
  67. }
  68. }
  69. #pragma mark - Accessors
  70. #pragma mark - Public method
  71. - (void)reloadDocumentWithFilePath:(NSString *)filePath completion:(void (^)(BOOL result))completion {
  72. self.title = [[filePath lastPathComponent] stringByDeletingPathExtension];
  73. _navigationTitle = self.title;
  74. [self.navigationController.view setUserInteractionEnabled:NO];
  75. if (![self.loadingView superview]) {
  76. [self.view addSubview:self.loadingView];
  77. }
  78. [self.loadingView startAnimating];
  79. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  80. NSURL *url = [NSURL fileURLWithPath:filePath];
  81. CPDFDocument *document = [[CPDFDocument alloc] initWithURL:url];
  82. dispatch_async(dispatch_get_main_queue(), ^{
  83. [self.navigationController.view setUserInteractionEnabled:YES];
  84. [self.loadingView stopAnimating];
  85. [self.loadingView removeFromSuperview];
  86. if (document.error && document.error.code != CPDFDocumentPasswordError) {
  87. UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"OK", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  88. [self.navigationController popViewControllerAnimated:YES];
  89. }];
  90. UIAlertController *alert = [UIAlertController alertControllerWithTitle:@""
  91. message:NSLocalizedString(@"Sorry PDF Reader Can't open this pdf file!", nil)
  92. preferredStyle:UIAlertControllerStyleAlert];
  93. [alert addAction:okAction];
  94. if (completion) {
  95. completion(NO);
  96. }
  97. } else {
  98. self.pdfListView.document = document;
  99. if (completion) {
  100. completion(YES);
  101. }
  102. }
  103. });
  104. });
  105. }
  106. #pragma mark - Action
  107. - (void)navigationRightItemSearch {
  108. [self.searchToolbar showInView:self.navigationController.navigationBar];
  109. self.title = nil;
  110. self.navigationItem.rightBarButtonItem = nil;
  111. }
  112. - (void)navigationRightItemBota {
  113. CPDFBOTAViewController *botaViewController = [[CPDFBOTAViewController alloc] initWithPDFView:self.pdfListView];
  114. botaViewController.delegate = self;
  115. [self.navigationController pushViewController:botaViewController animated:NO];
  116. }
  117. - (void)navigationRightItemMore {
  118. CPDFMoreListViewController * moreListVc = [[CPDFMoreListViewController alloc] init];
  119. moreListVc.delegate = self;
  120. [self.navigationController pushViewController:moreListVc animated:YES];
  121. }
  122. - (void)buttonItemClicked_thumbnail:(id)sender {
  123. CPDFThumbnailViewController *thumbnailViewController = [[CPDFThumbnailViewController alloc] initWithPDFView:self.pdfListView];
  124. thumbnailViewController.delegate = self;
  125. [self.navigationController pushViewController:thumbnailViewController animated:NO];
  126. }
  127. - (void)menuItemClick_CopyAction:(id)sender {
  128. if (self.pdfListView.currentSelection.string)
  129. [[UIPasteboard generalPasteboard] setString:self.pdfListView.currentSelection.string];
  130. [self.pdfListView clearSelection];
  131. }
  132. #pragma mark - CPDFViewDelegate
  133. - (void)PDFViewDocumentDidLoaded:(CPDFView *)pdfView {
  134. UIBarButtonItem* rightItem = [[UIBarButtonItem alloc] initWithCustomView:self.rightView];
  135. self.navigationItem.rightBarButtonItem = rightItem;
  136. }
  137. - (void)PDFViewCurrentPageDidChanged:(CPDFView *)pdfView {
  138. }
  139. #pragma mark - CPDFListViewDelegate
  140. - (void)PDFListViewPerformTouchEnded:(CPDFListView *)pdfView {
  141. if (CPDFViewAnnotationModeNone != self.pdfListView.annotationMode) {
  142. self.pdfListView.annotationMode = CPDFViewAnnotationModeNone;
  143. return;
  144. }
  145. if (UIUserInterfaceIdiomPhone == UI_USER_INTERFACE_IDIOM()) {
  146. if (self.navigationController.navigationBarHidden) {
  147. [self.navigationController setNavigationBarHidden:NO animated:YES];
  148. [UIView animateWithDuration:0.3 animations:^{
  149. self.pdfListView.pageSliderView.alpha = 1.0;
  150. }];
  151. } else {
  152. [self.navigationController setNavigationBarHidden:YES animated:YES];
  153. [UIView animateWithDuration:0.3 animations:^{
  154. self.pdfListView.pageSliderView.alpha = 0.0;
  155. }];
  156. }
  157. } else {
  158. if (self.navigationController.navigationBarHidden) {
  159. [self.navigationController setNavigationBarHidden:NO animated:YES];
  160. [UIView animateWithDuration:0.3 animations:^{
  161. self.pdfListView.pageSliderView.alpha = 1.0;
  162. }];
  163. } else {
  164. [self.navigationController setNavigationBarHidden:YES animated:YES];
  165. [UIView animateWithDuration:0.3 animations:^{
  166. self.pdfListView.pageSliderView.alpha = 0.0;
  167. }];
  168. }
  169. }
  170. }
  171. #pragma mark - CPDFMorelistViewDelegate
  172. - (void)PDFMoreListViewController:(CPDFMoreListViewController *)moreVC didSelectRow:(CPDFMoreListViewType)row {
  173. if(CPDFMoreListViewTypeInfo == row){
  174. //File Info
  175. CPDFInfoViewController * infoVc = [[CPDFInfoViewController alloc] initWithPDFView:self.pdfListView];
  176. [self.navigationController pushViewController:infoVc animated:YES];
  177. } else {
  178. CPDFDisplayViewController *displayVc = [[CPDFDisplayViewController alloc] initWithPDFView:self.pdfListView];
  179. displayVc.delegate = self;
  180. [self.navigationController pushViewController:displayVc animated:NO];
  181. }
  182. }
  183. #pragma mark - CSearchToolbarDelegate
  184. - (void)searchToolbar:(CSearchToolbar *)searchToolbar onSearchQueryResults:(NSArray *)results {
  185. if ([results count] < 1) {
  186. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"OK", nil)
  187. style:UIAlertActionStyleCancel
  188. handler:nil];
  189. UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil
  190. message:NSLocalizedString(@"your have‘t search result", nil)
  191. preferredStyle:UIAlertControllerStyleAlert];
  192. [alert addAction:cancelAction];
  193. [self presentViewController:alert animated:YES completion:nil];
  194. return;
  195. }
  196. [self searchToolbarOnExitSearch:searchToolbar];
  197. CPDFSearchResultsViewController* searchResultController = [[CPDFSearchResultsViewController alloc] initWithResultArray:results keyword:searchToolbar.searchKeyString document:self.pdfListView.document];
  198. searchResultController.delegate = self;
  199. [self.navigationController pushViewController:searchResultController animated:YES];
  200. }
  201. - (void)searchToolbarOnExitSearch:(CSearchToolbar *)searchToolbar {
  202. if([searchToolbar superview]) {
  203. [searchToolbar removeFromSuperview];
  204. UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:self.rightView];
  205. self.navigationItem.rightBarButtonItem = rightItem;
  206. self.title = self.navigationTitle;
  207. }
  208. }
  209. #pragma mark - CPDFDisplayViewDelegate
  210. - (void)displayViewControllerDismiss:(CPDFDisplayViewController *)displayViewController {
  211. [self.navigationController popToRootViewControllerAnimated:YES];
  212. }
  213. #pragma mark - CPDFBOTAViewControllerDelegate
  214. - (void)botaViewControllerDismiss:(CPDFBOTAViewController *)botaViewController {
  215. [self.navigationController popViewControllerAnimated:YES];
  216. }
  217. #pragma mark - CPDFSearchResultsDelegate
  218. - (void)searchResultsView:(CPDFSearchResultsViewController *)resultVC forSelection:(CPDFSelection *)selection indexPath:(NSIndexPath *)indexPath {
  219. [self.navigationController popViewControllerAnimated:YES];
  220. NSInteger pageIndex = [self.pdfListView.document indexForPage:selection.page];
  221. [self.pdfListView goToPageIndex:pageIndex animated:NO];
  222. [self.pdfListView setHighlightedSelection:selection animated:YES];
  223. }
  224. -(void)searchResultsViewControllerDismiss:(CPDFSearchResultsViewController *)searchResultsViewController {
  225. [self.navigationController popViewControllerAnimated:YES];
  226. }
  227. #pragma mark - CPDFThumbnailViewControllerDelegate
  228. - (void)thumbnailViewController:(CPDFThumbnailViewController *)thumbnailViewController pageIndex:(NSInteger)pageIndex {
  229. [self.pdfListView goToPageIndex:pageIndex animated:NO];
  230. [self.navigationController popViewControllerAnimated:YES];
  231. }
  232. @end