123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294 |
- //
- // CPDFViewController.m
- // viewer-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>
- @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;
- @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 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);
- } else {
- self.pdfListView.frame = self.view.bounds;
- }
- }
- #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
- - (void)PDFListViewPerformTouchEnded:(CPDFListView *)pdfView {
- if (CPDFViewAnnotationModeNone != self.pdfListView.annotationMode) {
- self.pdfListView.annotationMode = CPDFViewAnnotationModeNone;
- return;
- }
- if (UIUserInterfaceIdiomPhone == UI_USER_INTERFACE_IDIOM()) {
- if (self.navigationController.navigationBarHidden) {
- [self.navigationController setNavigationBarHidden:NO animated:YES];
- [UIView animateWithDuration:0.3 animations:^{
- self.pdfListView.pageSliderView.alpha = 1.0;
- }];
- } else {
- [self.navigationController setNavigationBarHidden:YES animated:YES];
- [UIView animateWithDuration:0.3 animations:^{
- self.pdfListView.pageSliderView.alpha = 0.0;
- }];
- }
- } else {
- if (self.navigationController.navigationBarHidden) {
- [self.navigationController setNavigationBarHidden:NO animated:YES];
- [UIView animateWithDuration:0.3 animations:^{
- self.pdfListView.pageSliderView.alpha = 1.0;
- }];
- } else {
- [self.navigationController setNavigationBarHidden:YES animated:YES];
- [UIView animateWithDuration:0.3 animations:^{
- self.pdfListView.pageSliderView.alpha = 0.0;
- }];
- }
- }
- }
- #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];
- }
- @end
|