123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179 |
- //
- // CPDFViewController.m
- // Edit-Ctrl-Demo
- //
- // Copyright © 2014-2023 PDF Technologies, Inc. All Rights Reserved.
- //
- // THIS SOURCE CODE AND ANY ACCOMPANYING DOCUMENTATION ARE PROTECTED BY INTERNATIONAL COPYRIGHT LAW
- // AND MAY NOT BE RESOLD OR REDISTRIBUTED. USAGE IS BOUND TO THE ComPDFKit LICENSE AGREEMENT.
- // UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES.
- // This notice may not be removed from this file.
- //
- #import "CPDFViewController.h"
- #import <ComPDFKit/ComPDFKit.h>
- #import <compdfkit_tools/compdfkit_tools.h>
- @interface CPDFViewController () <UISearchBarDelegate,CPDFViewDelegate,CPDFListViewDelegate,CPDFEditToolBarDelegate>
- @property(nonatomic, strong) NSString *filePath;
- @property(nonatomic, strong) CPDFListView *pdfListView;
- @property(nonatomic, strong) CActivityIndicatorView *loadingView;
-
- @property(nonatomic, strong) NSString *navigationTitle;
- @property(nonatomic, strong) CPDFEditViewController *baseVC;
- @property(nonatomic, assign) CPDFEditMode editMode;
- @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];
-
- [self reloadDocumentWithFilePath:self.filePath completion:^(BOOL result) {
-
- }];
-
- CPDFEditToolBar * toolBar = [[CPDFEditToolBar alloc] initWithPDFView:self.pdfListView];
- toolBar.delegate = self;
- [self.view addSubview:toolBar];
-
- //default beginEditingMode
- [self.pdfListView beginEditingLoadType:CEditingLoadTypeText | CEditingLoadTypeImage];
- self.editMode = CPDFEditModeAll;
- }
- - (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
- #pragma mark - CPDFViewDelegate
- - (void)PDFViewDocumentDidLoaded:(CPDFView *)pdfView {
- }
- - (void)PDFViewCurrentPageDidChanged:(CPDFView *)pdfView {
- }
- #pragma mark - CPDFEditToolBarDelegate
- - (void)editClickInToolBar:(CPDFEditToolBar *)toolBar editMode:(CPDFEditMode)mode{
- self.editMode = mode;
- switch (mode) {
- case CPDFEditModeAll:
- [self.pdfListView beginEditingLoadType:CEditingLoadTypeText | CEditingLoadTypeImage];
- break;
-
- case CPDFEditModeText:
- [self.pdfListView changeEditingLoadType:CEditingLoadTypeText];
- break;
-
- case CPDFEditModeImage:
- {
-
- [self.pdfListView changeEditingLoadType:CEditingLoadTypeImage];
- }
- break;
-
- default:
- break;
- }
- }
- - (void)undoDidClickInToolBar:(CPDFEditToolBar *)toolBar{
- [self.pdfListView editTextUndo];
- }
- - (void)redoDidClickInToolBar:(CPDFEditToolBar *)toolBar{
- [self.pdfListView editTextRedo];
- }
- - (void)propertyEditDidClickInToolBar:(CPDFEditToolBar *)toolBar{
- _baseVC = [[CPDFEditViewController alloc] init];
- _baseVC.editMode = self.editMode;
- if(self.editMode == CPDFEditModeText || self.editMode == CPDFEditModeImage){
-
- AAPLCustomPresentationController *presentationController NS_VALID_UNTIL_END_OF_SCOPE;
-
- presentationController = [[AAPLCustomPresentationController alloc] initWithPresentedViewController:self.baseVC presentingViewController:self];
- self.baseVC.transitioningDelegate = presentationController;
-
- [self presentViewController:self.baseVC animated:YES completion:nil];
- }
- }
- @end
|