CPDFViewController.m 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. //
  2. // CPDFViewController.m
  3. // Edit-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,CPDFEditToolBarDelegate>
  16. @property(nonatomic, strong) NSString *filePath;
  17. @property(nonatomic, strong) CPDFListView *pdfListView;
  18. @property(nonatomic, strong) CActivityIndicatorView *loadingView;
  19. @property(nonatomic, strong) NSString *navigationTitle;
  20. @property(nonatomic, strong) CPDFEditViewController *baseVC;
  21. @property(nonatomic, assign) CPDFEditMode editMode;
  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. [self reloadDocumentWithFilePath:self.filePath completion:^(BOOL result) {
  40. }];
  41. CPDFEditToolBar * toolBar = [[CPDFEditToolBar alloc] initWithPDFView:self.pdfListView];
  42. toolBar.delegate = self;
  43. [self.view addSubview:toolBar];
  44. //default beginEditingMode
  45. [self.pdfListView beginEditingLoadType:CEditingLoadTypeText | CEditingLoadTypeImage];
  46. self.editMode = CPDFEditModeAll;
  47. }
  48. - (void)viewWillLayoutSubviews {
  49. [super viewWillLayoutSubviews];
  50. if (@available(iOS 11.0, *)) {
  51. 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);
  52. } else {
  53. self.pdfListView.frame = self.view.bounds;
  54. }
  55. }
  56. #pragma mark - Accessors
  57. #pragma mark - Public method
  58. - (void)reloadDocumentWithFilePath:(NSString *)filePath completion:(void (^)(BOOL result))completion {
  59. self.title = [[filePath lastPathComponent] stringByDeletingPathExtension];
  60. _navigationTitle = self.title;
  61. [self.navigationController.view setUserInteractionEnabled:NO];
  62. if (![self.loadingView superview]) {
  63. [self.view addSubview:self.loadingView];
  64. }
  65. [self.loadingView startAnimating];
  66. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  67. NSURL *url = [NSURL fileURLWithPath:filePath];
  68. CPDFDocument *document = [[CPDFDocument alloc] initWithURL:url];
  69. dispatch_async(dispatch_get_main_queue(), ^{
  70. [self.navigationController.view setUserInteractionEnabled:YES];
  71. [self.loadingView stopAnimating];
  72. [self.loadingView removeFromSuperview];
  73. if (document.error && document.error.code != CPDFDocumentPasswordError) {
  74. UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"OK", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  75. [self.navigationController popViewControllerAnimated:YES];
  76. }];
  77. UIAlertController *alert = [UIAlertController alertControllerWithTitle:@""
  78. message:NSLocalizedString(@"Sorry PDF Reader Can't open this pdf file!", nil)
  79. preferredStyle:UIAlertControllerStyleAlert];
  80. [alert addAction:okAction];
  81. if (completion) {
  82. completion(NO);
  83. }
  84. } else {
  85. self.pdfListView.document = document;
  86. if (completion) {
  87. completion(YES);
  88. }
  89. }
  90. });
  91. });
  92. }
  93. #pragma mark - Action
  94. #pragma mark - CPDFViewDelegate
  95. - (void)PDFViewDocumentDidLoaded:(CPDFView *)pdfView {
  96. }
  97. - (void)PDFViewCurrentPageDidChanged:(CPDFView *)pdfView {
  98. }
  99. #pragma mark - CPDFEditToolBarDelegate
  100. - (void)editClickInToolBar:(CPDFEditToolBar *)toolBar editMode:(CPDFEditMode)mode{
  101. self.editMode = mode;
  102. switch (mode) {
  103. case CPDFEditModeAll:
  104. [self.pdfListView beginEditingLoadType:CEditingLoadTypeText | CEditingLoadTypeImage];
  105. break;
  106. case CPDFEditModeText:
  107. [self.pdfListView changeEditingLoadType:CEditingLoadTypeText];
  108. break;
  109. case CPDFEditModeImage:
  110. {
  111. [self.pdfListView changeEditingLoadType:CEditingLoadTypeImage];
  112. }
  113. break;
  114. default:
  115. break;
  116. }
  117. }
  118. - (void)undoDidClickInToolBar:(CPDFEditToolBar *)toolBar{
  119. [self.pdfListView editTextUndo];
  120. }
  121. - (void)redoDidClickInToolBar:(CPDFEditToolBar *)toolBar{
  122. [self.pdfListView editTextRedo];
  123. }
  124. - (void)propertyEditDidClickInToolBar:(CPDFEditToolBar *)toolBar{
  125. _baseVC = [[CPDFEditViewController alloc] init];
  126. _baseVC.editMode = self.editMode;
  127. if(self.editMode == CPDFEditModeText || self.editMode == CPDFEditModeImage){
  128. AAPLCustomPresentationController *presentationController NS_VALID_UNTIL_END_OF_SCOPE;
  129. presentationController = [[AAPLCustomPresentationController alloc] initWithPresentedViewController:self.baseVC presentingViewController:self];
  130. self.baseVC.transitioningDelegate = presentationController;
  131. [self presentViewController:self.baseVC animated:YES completion:nil];
  132. }
  133. }
  134. @end