CPDFViewController.m 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808
  1. //
  2. // CPDFViewController.m
  3. // PDFView_RN
  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. #import <AVFAudio/AVFAudio.h>
  16. #import <AVFoundation/AVFoundation.h>
  17. @interface CPDFViewController () <CPDFSoundPlayBarDelegate,CPDFAnnotationBarDelegate,CPDFToolsViewControllerDelegate,CPDFNoteOpenViewControllerDelegate,CPDFBOTAViewControllerDelegate,CPDFEditToolBarDelegate,CPDFFormBarDelegate,CPDFListViewDelegate,CPDFSignatureViewControllerDelegate,CPDFPageEditViewControllerDelegate,CPDFKeyboardToolbarDelegate>
  18. @property(nonatomic, strong) CPDFAnnotationToolBar *annotationBar;
  19. @property (nonatomic, strong) CPDFFormToolBar *formBar;
  20. @property(nonatomic, strong) CPDFSoundPlayBar *soundPlayBar;
  21. @property(nonatomic, strong) CAnnotationManage *annotationManage;
  22. @property(nonatomic, strong) CPDFEditToolBar * toolBar;
  23. @property(nonatomic, strong) CPDFEditViewController *baseVC;
  24. @property(nonatomic, assign) CPDFEditMode editMode;
  25. @property(nonatomic, strong) CPDFSignatureWidgetAnnotation * signatureAnnotation;
  26. @property(nonatomic, strong) CPDFConfiguration *configuration;
  27. @end
  28. @implementation CPDFViewController
  29. - (void)viewDidLoad {
  30. [super viewDidLoad];
  31. self.pdfListView.toolModel = CToolModelViewer;
  32. CPDFEditingConfig *editingConfig = [[CPDFEditingConfig alloc]init];
  33. editingConfig.editingBorderWidth = 1.0;
  34. editingConfig.editingOffsetGap = 5;
  35. self.pdfListView.editingConfig = editingConfig;
  36. [self initAnnotationBar];
  37. [self initWithEditTool];
  38. [self initWithFormTool];
  39. [self enterViewerMode];
  40. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(PDFPageDidRemoveAnnotationNotification:) name:CPDFPageDidRemoveAnnotationNotification object:nil];
  41. }
  42. - (void)initAnnotationBar {
  43. self.annotationManage = [[CAnnotationManage alloc] initWithPDFView:self.pdfListView];
  44. self.annotationBar = [[CPDFAnnotationToolBar alloc] initAnnotationManage:self.annotationManage];
  45. CGFloat height = 44.0;
  46. if (@available(iOS 11.0, *))
  47. height += self.view.safeAreaInsets.bottom;
  48. self.annotationBar.frame = CGRectMake(0, self.view.frame.size.height - height, self.view.frame.size.width, height);
  49. self.annotationBar.delegate = self;
  50. [self.annotationBar setParentVC:self];
  51. [self.view addSubview:self.annotationBar];
  52. }
  53. - (void)initWithEditTool {
  54. if(!self.toolBar){
  55. self.toolBar = [[CPDFEditToolBar alloc] initWithPDFView:self.pdfListView];
  56. }
  57. self.toolBar.delegate = self;
  58. [self.view addSubview:self.toolBar];
  59. }
  60. - (void)initWithFormTool {
  61. if(!self.formBar){
  62. self.formBar = [[CPDFFormToolBar alloc] initAnnotationManage:self.annotationManage];
  63. }
  64. self.formBar.delegate = self;
  65. self.formBar.parentVC = self;
  66. [self.view addSubview:self.formBar];
  67. }
  68. - (void)viewWillLayoutSubviews {
  69. [super viewWillLayoutSubviews];
  70. if([self.popMenu superview]) {
  71. if (@available(iOS 11.0, *)) {
  72. [self.popMenu showMenuInRect:CGRectMake(self.view.frame.size.width - self.view.safeAreaInsets.right - 250, CGRectGetMaxY(self.navigationController.navigationBar.frame), 250, 250)];
  73. } else {
  74. // Fallback on earlier versions
  75. [self.popMenu showMenuInRect:CGRectMake(self.view.frame.size.width - 250, CGRectGetMaxY(self.navigationController.navigationBar.frame), 250, 250)];
  76. }
  77. }
  78. CGFloat height = 44.0;
  79. if (@available(iOS 11.0, *))
  80. height += self.view.safeAreaInsets.bottom;
  81. CGFloat bottomHeight = 0;
  82. if(CToolModelAnnotation == self.pdfListView.toolModel) {
  83. self.annotationBar.frame = CGRectMake(0, self.view.frame.size.height - height, self.view.frame.size.width, height);
  84. bottomHeight = self.self.annotationBar.frame.size.height;
  85. } else if(CToolModelEdit == self.pdfListView.toolModel){
  86. self.toolBar.frame = CGRectMake(0, self.view.frame.size.height - height, self.view.frame.size.width, height);
  87. bottomHeight = self.self.toolBar.frame.size.height;
  88. } else if(CToolModelForm == self.pdfListView.toolModel){
  89. self.formBar.frame = CGRectMake(0, self.view.frame.size.height - height, self.view.frame.size.width, height);
  90. bottomHeight = self.self.formBar.frame.size.height;
  91. }
  92. CGFloat tPosY = 0;
  93. CGFloat tBottomY = 0;
  94. if(CToolModelAnnotation == self.pdfListView.toolModel) {
  95. if (!self.navigationController.navigationBarHidden) {
  96. [UIView animateWithDuration:0.3 animations:^{
  97. CGRect frame = self.annotationBar.frame;
  98. frame.origin.y = self.view.bounds.size.height-frame.size.height;
  99. self.annotationBar.frame = frame;
  100. }];
  101. CGRect rectStatus = [[UIApplication sharedApplication] statusBarFrame];
  102. tPosY = self.navigationController.navigationBar.frame.size.height + rectStatus.size.height;
  103. tBottomY = self.annotationBar.frame.size.height;
  104. } else {
  105. [UIView animateWithDuration:0.3 animations:^{
  106. CGRect frame = self.annotationBar.frame;
  107. frame.origin.y = self.view.bounds.size.height;
  108. self.annotationBar.frame = frame;
  109. }];
  110. }
  111. } else {
  112. tPosY = 0;
  113. if (!self.navigationController.navigationBarHidden) {
  114. CGRect rectStatus = [[UIApplication sharedApplication] statusBarFrame];
  115. tPosY = self.navigationController.navigationBar.frame.size.height + rectStatus.size.height;
  116. }
  117. }
  118. if (CPDFDisplayDirectionVertical == [CPDFKitConfig sharedInstance].displayDirection) {
  119. UIEdgeInsets inset = self.pdfListView.documentView.contentInset;
  120. inset.bottom = 10 + bottomHeight;
  121. self.pdfListView.documentView.contentInset = inset;
  122. } else{
  123. UIEdgeInsets inset = self.pdfListView.documentView.contentInset;
  124. inset.bottom = 0;
  125. self.pdfListView.documentView.contentInset = inset;
  126. }
  127. }
  128. #pragma mark - Public Methods
  129. - (void)selectDocumentRefresh {
  130. if(CToolModelAnnotation == self.pdfListView.toolModel) {
  131. self.pdfListView.annotationMode = CPDFViewAnnotationModeNone;
  132. [self.annotationBar updatePropertiesButtonState];
  133. [self.annotationBar reloadData];
  134. [self.annotationBar updateUndoRedoState];
  135. }else if(CToolModelForm == self.pdfListView.toolModel) {
  136. [self.formBar initUndoRedo];
  137. }
  138. }
  139. #pragma mark - Private
  140. - (void)enterEditMode {
  141. [self selectDocumentRefresh];
  142. self.toolBar.hidden = NO;
  143. self.annotationBar.hidden = YES;
  144. self.formBar.hidden = YES;
  145. self.pdfListView.toolModel = CToolModelEdit;
  146. [self.pdfListView beginEditingLoadType:CEditingLoadTypeText | CEditingLoadTypeImage];
  147. self.navigationTitle = NSLocalizedString(@"Content Edit", nil);
  148. [self.titleButton setTitle:self.navigationTitle forState:UIControlStateNormal];
  149. [self.toolBar updateButtonState];
  150. CGRect frame = self.toolBar.frame;
  151. frame.origin.y = self.view.bounds.size.height-frame.size.height;
  152. self.toolBar.frame = frame;
  153. [self viewWillLayoutSubviews];
  154. }
  155. - (void)enterAnnotationMode {
  156. self.toolBar.hidden = YES;
  157. self.annotationBar.hidden = NO;
  158. self.formBar.hidden = YES;
  159. if (self.pdfListView.isEdited) {
  160. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  161. [self.pdfListView commitEditing];
  162. dispatch_async(dispatch_get_main_queue(), ^{
  163. [self.pdfListView endOfEditing];
  164. });
  165. });
  166. } else {
  167. [self.pdfListView endOfEditing];
  168. }
  169. self.pdfListView.toolModel = CToolModelAnnotation;
  170. self.navigationTitle = NSLocalizedString(@"Annotation", nil);
  171. [self.titleButton setTitle:self.navigationTitle forState:UIControlStateNormal];
  172. CGFloat tPosY = 0;
  173. CGFloat tBottomY = 0;
  174. CGRect frame = self.annotationBar.frame;
  175. frame.origin.y = self.view.bounds.size.height-frame.size.height;
  176. self.annotationBar.frame = frame;
  177. CGRect rectStatus = [[UIApplication sharedApplication] statusBarFrame];
  178. tPosY = self.navigationController.navigationBar.frame.size.height + rectStatus.size.height;
  179. tBottomY = self.annotationBar.frame.size.height;
  180. if (CPDFDisplayDirectionVertical == [CPDFKitConfig sharedInstance].displayDirection) {
  181. UIEdgeInsets inset = self.pdfListView.documentView.contentInset;
  182. inset.bottom = 10 + self.annotationBar.frame.size.height;
  183. self.pdfListView.documentView.contentInset = inset;
  184. } else{
  185. UIEdgeInsets inset = self.pdfListView.documentView.contentInset;
  186. inset.bottom = 0;
  187. self.pdfListView.documentView.contentInset = inset;
  188. }
  189. [self viewWillLayoutSubviews];
  190. }
  191. - (void)enterViewerMode {
  192. self.toolBar.hidden = YES;
  193. self.formBar.hidden = YES;
  194. self.annotationBar.hidden = YES;
  195. if (self.pdfListView.isEdited) {
  196. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  197. [self.pdfListView commitEditing];
  198. dispatch_async(dispatch_get_main_queue(), ^{
  199. [self.pdfListView endOfEditing];
  200. });
  201. });
  202. } else {
  203. [self.pdfListView endOfEditing];
  204. }
  205. self.pdfListView.toolModel = CToolModelViewer;
  206. self.navigationTitle = NSLocalizedString(@"Viewer", nil);
  207. [self.titleButton setTitle:self.navigationTitle forState:UIControlStateNormal];
  208. CGRect frame = self.annotationBar.frame;
  209. frame.origin.y = self.view.bounds.size.height;
  210. self.annotationBar.frame = frame;
  211. if (CPDFDisplayDirectionVertical == [CPDFKitConfig sharedInstance].displayDirection) {
  212. UIEdgeInsets inset = self.pdfListView.documentView.contentInset;
  213. inset.bottom = 0;
  214. self.pdfListView.documentView.contentInset = inset;
  215. } else{
  216. UIEdgeInsets inset = self.pdfListView.documentView.contentInset;
  217. inset.bottom = 0;
  218. self.pdfListView.documentView.contentInset = inset;
  219. }
  220. }
  221. - (void)enterFormMode {
  222. self.toolBar.hidden = YES;
  223. self.annotationBar.hidden = YES;
  224. self.formBar.hidden = NO;
  225. if (self.pdfListView.isEdited) {
  226. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  227. [self.pdfListView commitEditing];
  228. dispatch_async(dispatch_get_main_queue(), ^{
  229. [self.pdfListView endOfEditing];
  230. });
  231. });
  232. } else {
  233. [self.pdfListView endOfEditing];
  234. }
  235. self.pdfListView.toolModel = CToolModelForm;
  236. self.navigationTitle = NSLocalizedString(@"Form", nil);
  237. [self.titleButton setTitle:self.navigationTitle forState:UIControlStateNormal];
  238. CGFloat tPosY = 0;
  239. CGFloat tBottomY = 0;
  240. CGRect frame = self.formBar.frame;
  241. frame.origin.y = self.view.bounds.size.height-frame.size.height;
  242. self.formBar.frame = frame;
  243. CGRect rectStatus = [[UIApplication sharedApplication] statusBarFrame];
  244. tPosY = self.navigationController.navigationBar.frame.size.height + rectStatus.size.height;
  245. tBottomY = self.formBar.frame.size.height;
  246. if (CPDFDisplayDirectionVertical == [CPDFKitConfig sharedInstance].displayDirection) {
  247. UIEdgeInsets inset = self.pdfListView.documentView.contentInset;
  248. inset.bottom = 10 + self.formBar.frame.size.height;
  249. self.pdfListView.documentView.contentInset = inset;
  250. } else{
  251. UIEdgeInsets inset = self.pdfListView.documentView.contentInset;
  252. inset.bottom = 0;
  253. self.pdfListView.documentView.contentInset = inset;
  254. }
  255. [self viewWillLayoutSubviews];
  256. }
  257. - (void)setTitleRefresh {
  258. if (CToolModelEdit == self.pdfListView.toolModel) {
  259. [self enterEditMode];
  260. } else if (CToolModelViewer == self.pdfListView.toolModel) {
  261. [self enterViewerMode];
  262. } else if (CToolModelAnnotation == self.pdfListView.toolModel) {
  263. [self enterAnnotationMode];
  264. } else if(CToolModelForm == self.pdfListView.toolModel) {
  265. [self enterFormMode];
  266. }
  267. }
  268. #pragma mark - Action
  269. - (void)navigationRightItemBota {
  270. CPDFBOTAViewController *botaViewController = [[CPDFBOTAViewController alloc] initCustomizeWithPDFView:self.pdfListView navArrays:@[@(CPDFBOTATypeStateOutline),@(CPDFBOTATypeStateBookmark),@(CPDFBOTATypeStateAnnotation)]];
  271. botaViewController.delegate = self;
  272. AAPLCustomPresentationController *presentationController NS_VALID_UNTIL_END_OF_SCOPE;
  273. presentationController = [[AAPLCustomPresentationController alloc] initWithPresentedViewController:botaViewController presentingViewController:self];
  274. botaViewController.transitioningDelegate = presentationController;
  275. [self presentViewController:botaViewController animated:YES completion:nil];
  276. }
  277. - (void) titleButtonClickd:(UIButton *) button {
  278. CPDFToolsViewController * toolsVc = [[CPDFToolsViewController alloc] initCustomizeWithToolArrays:@[@(CToolModelViewer),@(CToolModelEdit),@(CToolModelAnnotation),@(CToolModelForm)]];
  279. toolsVc.delegate = self;
  280. AAPLCustomPresentationController *presentationController NS_VALID_UNTIL_END_OF_SCOPE;
  281. presentationController = [[AAPLCustomPresentationController alloc] initWithPresentedViewController:toolsVc presentingViewController:self];
  282. toolsVc.transitioningDelegate = presentationController;
  283. [self presentViewController:toolsVc animated:YES completion:nil];
  284. }
  285. #pragma - CPDFEditToolBarDelegate
  286. - (void)undoDidClickInToolBar:(CPDFEditToolBar *)toolBar{
  287. [self.pdfListView editTextUndo];
  288. }
  289. - (void)redoDidClickInToolBar:(CPDFEditToolBar *)toolBar{
  290. [self.pdfListView editTextRedo];
  291. }
  292. - (void)propertyEditDidClickInToolBar:(CPDFEditToolBar *)toolBar{
  293. [self showMenuList];
  294. }
  295. - (void)showMenuList {
  296. _baseVC = [[CPDFEditViewController alloc] initWithPDFView:self.pdfListView];
  297. _baseVC.editMode = self.editMode;
  298. if((self.editMode == CPDFEditModeText || self.editMode == CPDFEditModeImage) && self.pdfListView.editStatus != CEditingSelectStateEmpty){
  299. AAPLCustomPresentationController *presentationController NS_VALID_UNTIL_END_OF_SCOPE;
  300. presentationController = [[AAPLCustomPresentationController alloc] initWithPresentedViewController:self.baseVC presentingViewController:self];
  301. self.baseVC.transitioningDelegate = presentationController;
  302. [self presentViewController:self.baseVC animated:YES completion:nil];
  303. }
  304. }
  305. #pragma mark - CPDFViewDelegate
  306. - (void)PDFViewEditingSelectStateDidChanged:(CPDFView *)pdfView {
  307. if([pdfView.editingArea isKindOfClass:[CPDFEditImageArea class]]) {
  308. self.editMode = CPDFEditModeImage;
  309. }else if([pdfView.editingArea isKindOfClass:[CPDFEditTextArea class]]) {
  310. self.editMode = CPDFEditModeText;
  311. }
  312. [self.toolBar updateButtonState];
  313. }
  314. - (void)PDFViewShouldBeginEditing:(CPDFView *)pdfView textView:(UITextView *)textView forAnnotation:(CPDFFreeTextAnnotation *)annotation {
  315. CPDFKeyboardToolbar *keyBoadrdToolbar = [[CPDFKeyboardToolbar alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 50)];
  316. keyBoadrdToolbar.delegate = self;
  317. [keyBoadrdToolbar bindToTextView:textView];
  318. }
  319. #pragma mark - CPDFListViewDelegate
  320. - (void)PDFListViewPerformTouchEnded:(CPDFListView *)pdfView {
  321. CGFloat tPosY = 0;
  322. CGFloat tBottomY = 0;
  323. if(CToolModelAnnotation == self.pdfListView.toolModel) {
  324. if (self.navigationController.navigationBarHidden) {
  325. [self.navigationController setNavigationBarHidden:NO animated:YES];
  326. [UIView animateWithDuration:0.3 animations:^{
  327. CGRect frame = self.annotationBar.frame;
  328. frame.origin.y = self.view.bounds.size.height-frame.size.height;
  329. self.annotationBar.frame = frame;
  330. self.pdfListView.pageSliderView.alpha = 1.0;
  331. self.annotationBar.topToolBar.alpha = 1.0;
  332. self.annotationBar.drawPencilFuncView.alpha = 1.0;
  333. }];
  334. CGRect rectStatus = [[UIApplication sharedApplication] statusBarFrame];
  335. tPosY = self.navigationController.navigationBar.frame.size.height + rectStatus.size.height;
  336. tBottomY = self.annotationBar.frame.size.height;
  337. } else {
  338. [self.navigationController setNavigationBarHidden:YES animated:YES];
  339. [UIView animateWithDuration:0.3 animations:^{
  340. CGRect frame = self.annotationBar.frame;
  341. frame.origin.y = self.view.bounds.size.height;
  342. self.annotationBar.frame = frame;
  343. self.pdfListView.pageSliderView.alpha = 0.0;
  344. self.annotationBar.topToolBar.alpha = 0.0;
  345. self.annotationBar.drawPencilFuncView.alpha = 0.0;
  346. }];
  347. }
  348. } else {
  349. CGFloat tPosY = 0;
  350. if (self.navigationController.navigationBarHidden) {
  351. [self.navigationController setNavigationBarHidden:NO animated:YES];
  352. [UIView animateWithDuration:0.3 animations:^{
  353. self.pdfListView.pageSliderView.alpha = 1.0;
  354. }];
  355. CGRect rectStatus = [[UIApplication sharedApplication] statusBarFrame];
  356. tPosY = self.navigationController.navigationBar.frame.size.height + rectStatus.size.height;
  357. } else {
  358. [self.navigationController setNavigationBarHidden:YES animated:YES];
  359. [UIView animateWithDuration:0.3 animations:^{
  360. self.pdfListView.pageSliderView.alpha = 0.0;
  361. }];
  362. }
  363. }
  364. if (CPDFDisplayDirectionVertical == [CPDFKitConfig sharedInstance].displayDirection) {
  365. UIEdgeInsets inset = self.pdfListView.documentView.contentInset;
  366. inset.bottom = 10 + self.annotationBar.frame.size.height;
  367. self.pdfListView.documentView.contentInset = inset;
  368. } else{
  369. UIEdgeInsets inset = self.pdfListView.documentView.contentInset;
  370. inset.bottom = 0;
  371. self.pdfListView.documentView.contentInset = inset;
  372. }
  373. }
  374. - (void)PDFListViewEditNote:(CPDFListView *)pdfListView forAnnotation:(CPDFAnnotation *)annotation {
  375. if([annotation isKindOfClass:[CPDFLinkAnnotation class]]) {
  376. [self.annotationBar buttonItemClicked_openAnnotation:self.titleButton];
  377. } else if ([annotation isKindOfClass:[CPDFWidgetAnnotation class]]) {
  378. [self.formBar buttonItemClicked_openOption:annotation];
  379. } else {
  380. CGRect rect = [self.pdfListView convertRect:annotation.bounds fromPage:annotation.page];
  381. CPDFNoteOpenViewController *noteVC = [[CPDFNoteOpenViewController alloc]initWithAnnotation:annotation];
  382. noteVC.delegate = self;
  383. [noteVC showViewController:self inRect:rect];
  384. }
  385. }
  386. - (void)PDFListViewChangedAnnotationType:(CPDFListView *)pdfListView forAnnotationMode:(CPDFViewAnnotationMode)annotationMode {
  387. if(CToolModelAnnotation == self.pdfListView.toolModel) {
  388. [self.annotationBar reloadData];
  389. }else if(CToolModelForm == self.pdfListView.toolModel) {
  390. [self.formBar reloadData];
  391. }
  392. }
  393. - (void)PDFListViewPerformUrl:(CPDFListView *)pdfView withContent:(NSString *)content {
  394. NSURL * url = [NSURL URLWithString:content];
  395. [[UIApplication sharedApplication] openURL:url];
  396. }
  397. - (void)PDFListViewPerformAddStamp:(CPDFListView *)pdfView atPoint:(CGPoint)point forPage:(CPDFPage *)page {
  398. [self.annotationBar addStampAnnotationWithPage:page point:point];
  399. }
  400. - (void)PDFListViewPerformAddImage:(CPDFListView *)pdfView atPoint:(CGPoint)point forPage:(CPDFPage *)page {
  401. [self.annotationBar addImageAnnotationWithPage:page point:point];
  402. }
  403. - (BOOL)PDFListViewerTouchEndedIsAudioRecordMedia:(CPDFListView *)pdfListView {
  404. if (CPDFMediaStateAudioRecord == [CPDFMediaManager shareManager].mediaState) {
  405. [self PDFListViewPerformTouchEnded:self.pdfListView];
  406. return YES;
  407. }
  408. return NO;
  409. }
  410. - (void)PDFListViewPerformCancelMedia:(CPDFListView *)pdfView atPoint:(CGPoint)point forPage:(CPDFPage *)page {
  411. [CPDFMediaManager shareManager].mediaState = CPDFMediaStateStop;
  412. }
  413. - (void)PDFListViewPerformRecordMedia:(CPDFListView *)pdfView atPoint:(CGPoint)point forPage:(CPDFPage *)page {
  414. if([self.soundPlayBar superview]) {
  415. if(self.soundPlayBar.soundState == CPDFSoundStatePlay) {
  416. [self.soundPlayBar stopAudioPlay];
  417. [self.soundPlayBar removeFromSuperview];
  418. } else if (self.soundPlayBar.soundState == CPDFSoundStateRecord) {
  419. [self.soundPlayBar stopRecord];
  420. [self.soundPlayBar removeFromSuperview];
  421. }
  422. }
  423. AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio];
  424. if (authStatus == AVAuthorizationStatusNotDetermined || authStatus == AVAuthorizationStatusDenied) {
  425. [AVCaptureDevice requestAccessForMediaType:AVMediaTypeAudio completionHandler:^(BOOL granted) {
  426. if (granted) {
  427. } else {
  428. NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
  429. if ([[UIApplication sharedApplication] canOpenURL:url]) {
  430. [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
  431. }
  432. }
  433. }];
  434. }
  435. if (authStatus == AVAuthorizationStatusAuthorized) {
  436. NSInteger pageindex = [self.pdfListView.document indexForPage:page];
  437. [CPDFMediaManager shareManager].mediaState = CPDFMediaStateAudioRecord;
  438. [CPDFMediaManager shareManager].pageNum = pageindex;
  439. [CPDFMediaManager shareManager].ptInPdf = point;
  440. _soundPlayBar = [[CPDFSoundPlayBar alloc] initWithStyle:self.annotationManage.annotStyle];
  441. _soundPlayBar.delegate = self;
  442. [_soundPlayBar showInView:self.pdfListView soundState:CPDFSoundStateRecord];
  443. [_soundPlayBar startAudioRecord];
  444. } else {
  445. return;
  446. }
  447. }
  448. - (void)PDFListViewPerformPlay:(CPDFListView *)pdfView forAnnotation:(CPDFSoundAnnotation *)annotation {
  449. NSString *filePath = [annotation mediaPath];
  450. if (filePath) {
  451. NSURL *URL = [NSURL fileURLWithPath:filePath];
  452. _soundPlayBar = [[CPDFSoundPlayBar alloc] initWithStyle:self.annotationManage.annotStyle];
  453. _soundPlayBar.delegate = self;
  454. [_soundPlayBar showInView:self.pdfListView soundState:CPDFSoundStatePlay];
  455. [_soundPlayBar setURL:URL];
  456. [_soundPlayBar startAudioPlay];
  457. [CPDFMediaManager shareManager].mediaState = CPDFMediaStateVedioPlaying;
  458. }
  459. }
  460. - (void)PDFListViewPerformSignatureWidget:(CPDFListView *)pdfView forAnnotation:(CPDFSignatureWidgetAnnotation *)annotation {
  461. if(CToolModelAnnotation == self.pdfListView.toolModel) {
  462. [self.annotationBar openSignatureAnnotation:annotation];
  463. }else if(CToolModelViewer == self.pdfListView.toolModel) {
  464. self.signatureAnnotation = annotation;
  465. AAPLCustomPresentationController *presentationController NS_VALID_UNTIL_END_OF_SCOPE;
  466. CPDFSignatureViewController *signatureVC = [[CPDFSignatureViewController alloc] initWithStyle:nil];
  467. presentationController = [[AAPLCustomPresentationController alloc] initWithPresentedViewController:signatureVC presentingViewController:self];
  468. signatureVC.delegate = self;
  469. signatureVC.transitioningDelegate = presentationController;
  470. [self presentViewController:signatureVC animated:YES completion:nil];
  471. }
  472. }
  473. - (void)PDFListViewEditProperties:(CPDFListView *)pdfListView forAnnotation:(CPDFAnnotation *)annotation {
  474. if(CToolModelAnnotation == self.pdfListView.toolModel){
  475. [self.annotationBar buttonItemClicked_openAnnotation:self.titleButton];
  476. }else if(CToolModelForm == self.pdfListView.toolModel) {
  477. [self.formBar buttonItemClicked_open:annotation];
  478. }
  479. }
  480. - (void)PDFListViewContentEditProperty:(CPDFListView *)pdfView point:(CGPoint)point {
  481. if([pdfView.editingArea isKindOfClass:[CPDFEditImageArea class]]) {
  482. self.editMode = CPDFEditModeImage;
  483. } else if([pdfView.editingArea isKindOfClass:[CPDFEditTextArea class]]) {
  484. self.editMode = CPDFEditModeText;
  485. }
  486. [self showMenuList];
  487. [self.toolBar updateButtonState];
  488. }
  489. - (void)PDFViewCurrentPageDidChanged:(CPDFView *)pdfView {
  490. if([pdfView.editingArea isKindOfClass:[CPDFEditImageArea class]]) {
  491. self.editMode = CPDFEditModeImage;
  492. }else if([pdfView.editingArea isKindOfClass:[CPDFEditTextArea class]]) {
  493. self.editMode = CPDFEditModeText;
  494. }
  495. [self.toolBar updateButtonState];
  496. [super PDFViewCurrentPageDidChanged:pdfView];
  497. }
  498. #pragma mark - CPDFKeyboardToolbarDelegate
  499. - (void)keyboardShouldDissmiss:(CPDFKeyboardToolbar *)toolbar {
  500. [self.pdfListView commitEditAnnotationFreeText];
  501. self.pdfListView.annotationMode = CPDFViewAnnotationModeNone;
  502. [self.annotationBar reloadData];
  503. }
  504. #pragma mark - CPDFAnnotationBarDelegate
  505. - (void)annotationBarClick:(CPDFAnnotationToolBar *)annotationBar clickAnnotationMode:(CPDFViewAnnotationMode)annotationMode forSelected:(BOOL)isSelected forButton:(UIButton *)button {
  506. if(CPDFViewAnnotationModeInk == annotationMode || CPDFViewAnnotationModePencilDrawing == annotationMode) {
  507. CGFloat tPosY = 0;
  508. if(isSelected) {
  509. [self.navigationController setNavigationBarHidden:YES animated:YES];
  510. [UIView animateWithDuration:0.3 animations:^{
  511. CGRect frame = self.annotationBar.frame;
  512. frame.origin.y = self.view.bounds.size.height;
  513. self.annotationBar.frame = frame;
  514. self.pdfListView.pageSliderView.alpha = 0.0;
  515. UIEdgeInsets inset = self.pdfListView.documentView.contentInset;
  516. inset.bottom = 0;
  517. self.pdfListView.documentView.contentInset = inset;
  518. }];
  519. } else {
  520. [self.navigationController setNavigationBarHidden:NO animated:YES];
  521. [UIView animateWithDuration:0.3 animations:^{
  522. CGRect frame = self.annotationBar.frame;
  523. frame.origin.y = self.view.bounds.size.height-frame.size.height;
  524. self.annotationBar.frame = frame;
  525. self.pdfListView.pageSliderView.alpha = 1.0;
  526. }];
  527. CGRect rectStatus = [[UIApplication sharedApplication] statusBarFrame];
  528. tPosY = self.navigationController.navigationBar.frame.size.height + rectStatus.size.height;
  529. UIEdgeInsets inset = self.pdfListView.documentView.contentInset;
  530. inset.bottom = self.annotationBar.frame.size.height;
  531. self.pdfListView.documentView.contentInset = inset;
  532. }
  533. } else if (CPDFViewAnnotationModeSound == annotationMode && !isSelected) {
  534. if(CPDFSoundStateRecord == self.soundPlayBar.soundState) {
  535. [self.soundPlayBar stopRecord];
  536. } else if (CPDFSoundStatePlay== self.soundPlayBar.soundState) {
  537. [self.soundPlayBar stopAudioPlay];
  538. }
  539. }
  540. }
  541. #pragma mark - CPDFNoteOpenViewControllerDelegate
  542. - (void)getNoteOpenViewController:(CPDFNoteOpenViewController *)noteOpenVC content:(NSString *)content isDelete:(BOOL)isDelete {
  543. if (isDelete) {
  544. [noteOpenVC.annotation.page removeAnnotation:noteOpenVC.annotation];
  545. [self.pdfListView setNeedsDisplayForPage:noteOpenVC.annotation.page];
  546. if([self.pdfListView.activeAnnotations containsObject:noteOpenVC.annotation]) {
  547. NSMutableArray *activeAnnotations = [NSMutableArray arrayWithArray:self.pdfListView.activeAnnotations];
  548. [activeAnnotations removeObject:noteOpenVC.annotation];
  549. [self.pdfListView updateActiveAnnotations:activeAnnotations];
  550. }
  551. } else {
  552. if([noteOpenVC.annotation isKindOfClass:[CPDFMarkupAnnotation class]]) {
  553. CPDFMarkupAnnotation *markupAnnotation = (CPDFMarkupAnnotation *)noteOpenVC.annotation;
  554. [markupAnnotation setContents:content?:@""];
  555. } else if(([noteOpenVC.annotation isKindOfClass:[CPDFTextAnnotation class]])){
  556. if(content && content.length > 0) {
  557. noteOpenVC.annotation.contents = content?:@"";
  558. } else {
  559. if([self.pdfListView.activeAnnotations containsObject:noteOpenVC.annotation]) {
  560. [self.pdfListView updateActiveAnnotations:@[]];
  561. }
  562. [noteOpenVC.annotation.page removeAnnotation:noteOpenVC.annotation];
  563. [self.pdfListView setNeedsDisplayForPage:noteOpenVC.annotation.page];
  564. }
  565. } else {
  566. noteOpenVC.annotation.contents = content?:@"";
  567. }
  568. }
  569. }
  570. #pragma mark - CPDFSoundPlayBarDelegate
  571. - (void)soundPlayBarRecordFinished:(CPDFSoundPlayBar *)soundPlayBar withFile:(NSString *)filePath {
  572. CPDFPage *page = [self.pdfListView.document pageAtIndex:[CPDFMediaManager shareManager].pageNum];
  573. CPDFSoundAnnotation *annotation = [[CPDFSoundAnnotation alloc] initWithDocument:self.pdfListView.document];
  574. if ([annotation setMediaPath:filePath]) {
  575. CGRect bounds = annotation.bounds;
  576. bounds.origin.x = [CPDFMediaManager shareManager].ptInPdf.x-bounds.size.width/2.0;
  577. bounds.origin.y = [CPDFMediaManager shareManager].ptInPdf.y-bounds.size.height/2.0;
  578. annotation.bounds = bounds;
  579. [self.pdfListView addAnnotation:annotation forPage:page];
  580. }
  581. [CPDFMediaManager shareManager].mediaState = CPDFMediaStateStop;
  582. [self.pdfListView stopRecord];
  583. }
  584. - (void)soundPlayBarRecordCancel:(CPDFSoundPlayBar *)soundPlayBar {
  585. if(CPDFSoundStateRecord == self.soundPlayBar.soundState) {
  586. [self.pdfListView stopRecord];
  587. }
  588. [CPDFMediaManager shareManager].mediaState = CPDFMediaStateStop;
  589. }
  590. - (void)soundPlayBarPlayClose:(CPDFSoundPlayBar *)soundPlayBar {
  591. [CPDFMediaManager shareManager].mediaState = CPDFMediaStateStop;
  592. }
  593. #pragma mark - Notification
  594. - (void)PDFPageDidRemoveAnnotationNotification:(NSNotification *)notification {
  595. CPDFAnnotation *annotation = [notification object];
  596. if ([annotation isKindOfClass:[CPDFSoundAnnotation class]]) {
  597. [self.soundPlayBar stopAudioPlay];
  598. if ([self.soundPlayBar isDescendantOfView:self.view]) {
  599. [self.soundPlayBar removeFromSuperview];
  600. }
  601. }
  602. }
  603. #pragma mark - CPDFToolsViewControllerDelegate
  604. - (void)CPDFToolsViewControllerDismiss:(CPDFToolsViewController *) viewController selectItemAtIndex:(CToolModel)selectIndex {
  605. if(CToolModelViewer == selectIndex) {
  606. //viewwer
  607. [self enterViewerMode];
  608. }else if(CToolModelEdit == selectIndex) {
  609. [self enterEditMode];
  610. }else if(CToolModelAnnotation == selectIndex){
  611. //Annotation
  612. [self enterAnnotationMode];
  613. }else if(CToolModelForm == selectIndex) {
  614. [self.formBar updateStatus];
  615. [self enterFormMode];
  616. }
  617. }
  618. #pragma mark - CPDFBOTAViewControllerDelegate
  619. - (void)botaViewControllerDismiss:(CPDFBOTAViewController *)botaViewController {
  620. [self.navigationController dismissViewControllerAnimated:YES completion:nil];
  621. }
  622. #pragma mark - CPDFSignatureViewControllerDelegate
  623. - (void)signatureViewControllerDismiss:(CPDFSignatureViewController *)signatureViewController {
  624. self.signatureAnnotation = nil;
  625. }
  626. - (void)signatureViewController:(CPDFSignatureViewController *)signatureViewController image:(UIImage *)image {
  627. if(self.signatureAnnotation) {
  628. [self.signatureAnnotation signWithImage:image];
  629. [self.pdfListView setNeedsDisplayForPage:self.signatureAnnotation.page];
  630. self.signatureAnnotation = nil;
  631. }
  632. }
  633. #pragma mark - Action
  634. - (void)buttonItemClicked_thumbnail:(id)sender {
  635. if(self.pdfListView.activeAnnotations.count > 0) {
  636. [self.pdfListView updateActiveAnnotations:@[]];
  637. [self.pdfListView setNeedsDisplayForVisiblePages];
  638. }
  639. CPDFPageEditViewController *pageEditViewcontroller = [[CPDFPageEditViewController alloc] initWithPDFView:self.pdfListView];
  640. pageEditViewcontroller.pageEditDelegate = self;
  641. pageEditViewcontroller.modalPresentationStyle = UIModalPresentationFullScreen;
  642. [self.navigationController presentViewController:pageEditViewcontroller animated:YES completion:nil];
  643. }
  644. #pragma mark - CPDFPageEditViewControllerDelegate
  645. - (void)pageEditViewControllerDone:(CPDFPageEditViewController *)pageEditViewController {
  646. if (pageEditViewController.isPageEdit) {
  647. __weak typeof(self) weakSelf = self;
  648. [weakSelf reloadDocumentWithFilePath:self.filePath password:self.pdfListView.document.password completion:^(BOOL result) {
  649. [weakSelf.pdfListView reloadInputViews];
  650. [weakSelf selectDocumentRefresh];
  651. }];
  652. [weakSelf.pdfListView reloadInputViews];
  653. }
  654. }
  655. - (void)pageEditViewController:(CPDFPageEditViewController *)pageEditViewController pageIndex:(NSInteger)pageIndex isPageEdit:(BOOL)isPageEdit {
  656. if (isPageEdit) {
  657. __weak typeof(self) weakSelf = self;
  658. [weakSelf reloadDocumentWithFilePath:self.filePath password:self.pdfListView.document.password completion:^(BOOL result) {
  659. [weakSelf.pdfListView reloadInputViews];
  660. [weakSelf selectDocumentRefresh];
  661. }];
  662. [weakSelf.pdfListView reloadInputViews];
  663. }
  664. [self.pdfListView goToPageIndex:pageIndex animated:NO];
  665. }
  666. @end