CPDFViewController.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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 () <UIDocumentPickerDelegate,CPDFToolsViewControllerDelegate,CPDFEditToolBarDelegate,CPDFSignatureViewControllerDelegate>
  16. @property(nonatomic, strong) CPDFEditViewController *baseVC;
  17. @property(nonatomic, assign) CPDFEditMode editMode;
  18. @property(nonatomic, strong) CPDFEditToolBar * toolBar;
  19. @property(nonatomic, assign) CGPoint menuPoint;
  20. @property (nonatomic, strong) CPDFSignatureWidgetAnnotation * signatureAnnotation;
  21. @end
  22. @implementation CPDFViewController
  23. - (void)viewDidLoad {
  24. [super viewDidLoad];
  25. CPDFEditingConfig *editingConfig = [[CPDFEditingConfig alloc]init];
  26. editingConfig.editingBorderWidth = 1.0;
  27. editingConfig.editingOffsetGap = 5;
  28. self.pdfListView.editingConfig = editingConfig;
  29. self.pdfListView.toolModel = CToolModelEdit;
  30. [self initWithEditTool];
  31. [self enterEditMode];
  32. [self.toolBar updateButtonState];
  33. [self enterEditMode];
  34. }
  35. - (void)initWithEditTool {
  36. if(!self.toolBar){
  37. self.toolBar = [[CPDFEditToolBar alloc] initWithPDFView:self.pdfListView];
  38. }
  39. self.toolBar.delegate = self;
  40. [self.view addSubview:self.toolBar];
  41. }
  42. - (void)initWitNavigationTitle {
  43. //titleButton
  44. CNavigationBarTitleButton * navTitleButton = [[CNavigationBarTitleButton alloc] init];
  45. self.titleButton = navTitleButton;
  46. self.navigationTitle = NSLocalizedString(@"Content Edit", nil);
  47. [navTitleButton setImage:[UIImage imageNamed:@"syasarrow"] forState:UIControlStateNormal];
  48. [navTitleButton addTarget:self action:@selector(titleButtonClickd:) forControlEvents:UIControlEventTouchUpInside];
  49. [navTitleButton setTitle:self.navigationTitle forState:UIControlStateNormal];
  50. [navTitleButton setTitleColor:[CPDFColorUtils CAnyReverseBackgooundColor] forState:UIControlStateNormal];
  51. self.titleButton.frame = CGRectMake(0, 0, 50, 30);
  52. self.navigationItem.titleView = self.titleButton;
  53. }
  54. #pragma mark - Action
  55. - (void)titleButtonClickd:(UIButton *) button {
  56. CPDFToolsViewController * toolsVc = [[CPDFToolsViewController alloc] initCustomizeWithToolArrays:@[@(CToolModelViewer),@(CToolModelEdit)]];
  57. toolsVc.delegate = self;
  58. AAPLCustomPresentationController *presentationController NS_VALID_UNTIL_END_OF_SCOPE;
  59. presentationController = [[AAPLCustomPresentationController alloc] initWithPresentedViewController:toolsVc presentingViewController:self];
  60. toolsVc.transitioningDelegate = presentationController;
  61. [self presentViewController:toolsVc animated:YES completion:nil];
  62. }
  63. - (void)viewWillLayoutSubviews {
  64. [super viewWillLayoutSubviews];
  65. CGFloat tPosY = 0;
  66. CGFloat bottomY = 0;
  67. CGFloat height = 44.0;
  68. if (@available(iOS 11.0, *))
  69. height += self.view.safeAreaInsets.bottom;
  70. if(CToolModelViewer == self.pdfListView.toolModel) {
  71. height = 0;
  72. }
  73. if (!self.navigationController.navigationBarHidden) {
  74. CGRect rectStatus = [[UIApplication sharedApplication] statusBarFrame];
  75. tPosY = self.navigationController.navigationBar.frame.size.height + rectStatus.size.height;
  76. self.toolBar.frame = CGRectMake(0, self.view.frame.size.height - height, self.view.frame.size.width, height);
  77. bottomY = self.toolBar.frame.size.height;
  78. } else {
  79. self.toolBar.frame = CGRectMake(0, self.view.frame.size.height, self.view.frame.size.width, height);
  80. bottomY = 0;
  81. }
  82. if (@available(iOS 11.0, *)) {
  83. self.pdfListView.frame = CGRectMake(self.view.safeAreaInsets.left, tPosY, self.view.frame.size.width - self.view.safeAreaInsets.left - self.view.safeAreaInsets.right, self.view.frame.size.height - tPosY- bottomY);
  84. } else {
  85. self.pdfListView.frame = CGRectMake(0, tPosY, self.view.frame.size.width, self.view.frame.size.height - tPosY-bottomY);
  86. }
  87. }
  88. #pragma mark - Private
  89. - (void)enterEditMode {
  90. self.pdfListView.toolModel = CToolModelEdit;
  91. self.toolBar.hidden = NO;
  92. self.navigationTitle = NSLocalizedString(@"Content Edit", nil);
  93. [self.titleButton setTitle:self.navigationTitle forState:UIControlStateNormal];
  94. [self.pdfListView beginEditingLoadType:CEditingLoadTypeText | CEditingLoadTypeImage];
  95. [self viewWillLayoutSubviews];
  96. }
  97. - (void)enterViewerMode {
  98. self.pdfListView.toolModel = CToolModelViewer;
  99. self.navigationTitle = NSLocalizedString(@"Viewer", nil);
  100. [self.titleButton setTitle:self.navigationTitle forState:UIControlStateNormal];
  101. if (self.pdfListView.isEdited) {
  102. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  103. [self.pdfListView commitEditing];
  104. dispatch_async(dispatch_get_main_queue(), ^{
  105. [self.pdfListView endOfEditing];
  106. });
  107. });
  108. } else {
  109. [self.pdfListView endOfEditing];
  110. }
  111. CGFloat tPosY = 0;
  112. CGFloat bottomY = 0;
  113. if (@available(iOS 11.0, *)) {
  114. self.pdfListView.frame = CGRectMake(self.view.safeAreaInsets.left, tPosY, self.view.frame.size.width - self.view.safeAreaInsets.left - self.view.safeAreaInsets.right, self.view.frame.size.height - tPosY- bottomY-self.view.safeAreaInsets.bottom);
  115. } else {
  116. self.pdfListView.frame = CGRectMake(0, tPosY, self.view.frame.size.width, self.view.frame.size.height - tPosY-bottomY-20);
  117. }
  118. }
  119. - (void)enterPDFShare {
  120. if(self.pdfListView.isEditing && self.pdfListView.isEdited) {
  121. [self.pdfListView commitEditing];
  122. [self.pdfListView endOfEditing];
  123. }
  124. [super enterPDFShare];
  125. }
  126. - (void)enterPDFAddFile {
  127. if(self.pdfListView.isEditing && self.pdfListView.isEdited) {
  128. [self.pdfListView commitEditing];
  129. [self.pdfListView endOfEditing];
  130. }
  131. [super enterPDFAddFile];
  132. }
  133. - (void)setTitleRefresh {
  134. if (CToolModelEdit == self.pdfListView.toolModel) {
  135. [self enterEditMode];
  136. } else if (CToolModelViewer == self.pdfListView.toolModel) {
  137. [self enterViewerMode];
  138. }
  139. }
  140. #pragma mark - CPDFViewDelegate
  141. - (void)PDFListViewCustomMenuClick:(CPDFListView *)pdfView identifier:(NSString *)menuIdentifier point:(CGPoint)point {
  142. if([menuIdentifier isEqualToString:@"PropertyItem"]) {
  143. self.menuPoint = point;
  144. self.editMode = CPDFEditModeText;
  145. [self showMenuList];
  146. [self.toolBar updateButtonState];
  147. }
  148. }
  149. - (void)PDFViewCurrentPageDidChanged:(CPDFView *)pdfView {
  150. if([pdfView.editingArea isKindOfClass:[CPDFEditImageArea class]]) {
  151. self.editMode = CPDFEditModeImage;
  152. }else if([pdfView.editingArea isKindOfClass:[CPDFEditTextArea class]]) {
  153. self.editMode = CPDFEditModeText;
  154. }
  155. [self.toolBar updateButtonState];
  156. [super PDFViewCurrentPageDidChanged:pdfView];
  157. }
  158. - (void)PDFListViewPerformTouchEnded:(CPDFListView *)pdfView {
  159. CGFloat tPosY = 0;
  160. CGFloat tBottomY = 0;
  161. if(CToolModelViewer == self.pdfListView.toolModel) {
  162. CGFloat tPosY = 0;
  163. if (self.navigationController.navigationBarHidden) {
  164. [self.navigationController setNavigationBarHidden:NO animated:YES];
  165. [UIView animateWithDuration:0.3 animations:^{
  166. self.pdfListView.pageSliderView.alpha = 1.0;
  167. }];
  168. CGRect rectStatus = [[UIApplication sharedApplication] statusBarFrame];
  169. tPosY = self.navigationController.navigationBar.frame.size.height + rectStatus.size.height;
  170. } else {
  171. [self.navigationController setNavigationBarHidden:YES animated:YES];
  172. [UIView animateWithDuration:0.3 animations:^{
  173. self.pdfListView.pageSliderView.alpha = 0.0;
  174. }];
  175. }
  176. }
  177. if (@available(iOS 11.0, *)) {
  178. self.pdfListView.frame = CGRectMake(self.view.safeAreaInsets.left, tPosY, self.view.frame.size.width - self.view.safeAreaInsets.left - self.view.safeAreaInsets.right, self.view.frame.size.height - tBottomY - tPosY);
  179. } else {
  180. self.pdfListView.frame = CGRectMake(0, tPosY, self.view.frame.size.width, self.view.frame.size.height - tBottomY - tPosY);
  181. }
  182. }
  183. - (void)PDFListViewPerformSignatureWidget:(CPDFListView *)pdfView forAnnotation:(CPDFSignatureWidgetAnnotation *)annotation {
  184. if(CToolModelViewer == self.pdfListView.toolModel) {
  185. self.signatureAnnotation = annotation;
  186. AAPLCustomPresentationController *presentationController NS_VALID_UNTIL_END_OF_SCOPE;
  187. CPDFSignatureViewController *signatureVC = [[CPDFSignatureViewController alloc] initWithStyle:nil];
  188. presentationController = [[AAPLCustomPresentationController alloc] initWithPresentedViewController:signatureVC presentingViewController:self];
  189. signatureVC.delegate = self;
  190. signatureVC.transitioningDelegate = presentationController;
  191. [self presentViewController:signatureVC animated:YES completion:nil];
  192. }
  193. }
  194. #pragma mark - CPDFViewDelegate
  195. - (void)PDFViewEditingSelectStateDidChanged:(CPDFView *)pdfView {
  196. if([pdfView.editingArea isKindOfClass:[CPDFEditImageArea class]]) {
  197. self.editMode = CPDFEditModeImage;
  198. }else if([pdfView.editingArea isKindOfClass:[CPDFEditTextArea class]]) {
  199. self.editMode = CPDFEditModeText;
  200. }
  201. [self.toolBar updateButtonState];
  202. }
  203. #pragma mark - CPDFToolsViewControllerDelegate
  204. - (void)CPDFToolsViewControllerDismiss:(CPDFToolsViewController *)viewController selectItemAtIndex:(CToolModel)selectIndex {
  205. if(CToolModelViewer == selectIndex) {
  206. //viewwer
  207. [self enterViewerMode];
  208. }else{
  209. //edit
  210. [self enterEditMode];
  211. }
  212. }
  213. #pragma mark - CPDFEditToolBarDelegate
  214. - (void)editClickInToolBar:(CPDFEditToolBar *)toolBar editMode:(CPDFEditMode)mode{
  215. self.editMode = mode;
  216. }
  217. - (void)undoDidClickInToolBar:(CPDFEditToolBar *)toolBar{
  218. [self.pdfListView editTextUndo];
  219. }
  220. - (void)redoDidClickInToolBar:(CPDFEditToolBar *)toolBar{
  221. [self.pdfListView editTextRedo];
  222. }
  223. - (void)propertyEditDidClickInToolBar:(CPDFEditToolBar *)toolBar{
  224. [self showMenuList];
  225. }
  226. - (void)showMenuList {
  227. _baseVC = [[CPDFEditViewController alloc] initWithPDFView:self.pdfListView];
  228. _baseVC.editMode = self.editMode;
  229. if((self.editMode == CPDFEditModeText || self.editMode == CPDFEditModeImage) && self.pdfListView.editStatus != CEditingSelectStateEmpty){
  230. AAPLCustomPresentationController *presentationController NS_VALID_UNTIL_END_OF_SCOPE;
  231. presentationController = [[AAPLCustomPresentationController alloc] initWithPresentedViewController:self.baseVC presentingViewController:self];
  232. self.baseVC.transitioningDelegate = presentationController;
  233. [self presentViewController:self.baseVC animated:YES completion:nil];
  234. }
  235. if (self.menuPoint.y < self.pdfListView.documentView.visibleSize.height/2) {
  236. [self.pdfListView goToRect:CGRectMake(self.menuPoint.x, self.pdfListView.documentView.visibleSize.height/2-50, 1, 1) onPage:[self.pdfListView.document pageAtIndex:self.pdfListView.currentPageIndex] animated:YES];
  237. }
  238. }
  239. #pragma mark - CPDFSignatureViewControllerDelegate
  240. - (void)signatureViewControllerDismiss:(CPDFSignatureViewController *)signatureViewController {
  241. self.signatureAnnotation = nil;
  242. }
  243. - (void)signatureViewController:(CPDFSignatureViewController *)signatureViewController image:(UIImage *)image {
  244. if(self.signatureAnnotation) {
  245. [self.signatureAnnotation signWithImage:image];
  246. [self.pdfListView setNeedsDisplayForPage:self.signatureAnnotation.page];
  247. self.signatureAnnotation = nil;
  248. }
  249. }
  250. #pragma mark - UIDocumentPickerDelegate
  251. - (void)documentPickerWasCancelled:(UIDocumentPickerViewController *)controller {
  252. [self enterEditMode];
  253. }
  254. @end