CPDFViewController.m 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  1. //
  2. // CPDFViewController.m
  3. // ContentEditor
  4. //
  5. // Copyright © 2014-2024 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 <AVFAudio/AVFAudio.h>
  15. #import <AVFoundation/AVFoundation.h>
  16. @interface CPDFViewController () <CPDFSoundPlayBarDelegate,CPDFAnnotationBarDelegate,CPDFToolsViewControllerDelegate,CPDFNoteOpenViewControllerDelegate,CPDFBOTAViewControllerDelegate,CPDFKeyboardToolbarDelegate>
  17. @property(nonatomic, strong) CPDFAnnotationToolBar *annotationBar;
  18. @property(nonatomic, strong) CPDFSoundPlayBar *soundPlayBar;
  19. @property(nonatomic, strong) CAnnotationManage *annotationManage;
  20. @end
  21. @implementation CPDFViewController
  22. - (void)viewDidLoad {
  23. [super viewDidLoad];
  24. self.pdfListView.toolModel = CToolModelAnnotation;
  25. self.view.backgroundColor = [CPDFColorUtils CPDFViewControllerBackgroundColor];
  26. [self initAnnotationBar];
  27. if (self.configuration.enterToolModel == CPDFToolFunctionTypeStateViewer) {
  28. [self enterViewerMode];
  29. } else {
  30. [self enterAnnotationMode];
  31. }
  32. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(PDFPageDidRemoveAnnotationNotification:) name:CPDFPageDidRemoveAnnotationNotification object:nil];
  33. }
  34. - (void)initAnnotationBar {
  35. self.annotationManage = [[CAnnotationManage alloc] initWithPDFView:self.pdfListView];
  36. self.annotationBar = [[CPDFAnnotationToolBar alloc] initAnnotationManage:self.annotationManage];
  37. CGFloat height = 44.0;
  38. if (@available(iOS 11.0, *))
  39. height += self.view.safeAreaInsets.bottom;
  40. self.annotationBar.frame = CGRectMake(0, self.view.frame.size.height - height, self.view.frame.size.width, height);
  41. self.annotationBar.delegate = self;
  42. [self.annotationBar setParentVC:self];
  43. [self.view addSubview:self.annotationBar];
  44. }
  45. - (void)initWitNavigationTitle {
  46. //titleButton
  47. [super initWitNavigationTitle];
  48. self.navigationTitle = NSLocalizedString(@"Annotation", nil);
  49. }
  50. - (void)viewWillLayoutSubviews {
  51. [super viewWillLayoutSubviews];
  52. if([self.popMenu superview]) {
  53. if (@available(iOS 11.0, *)) {
  54. [self.popMenu showMenuInRect:CGRectMake(self.view.frame.size.width - self.view.safeAreaInsets.right - 250, CGRectGetMaxY(self.navigationController.navigationBar.frame), 250, 250)];
  55. } else {
  56. // Fallback on earlier versions
  57. [self.popMenu showMenuInRect:CGRectMake(self.view.frame.size.width - 250, CGRectGetMaxY(self.navigationController.navigationBar.frame), 250, 250)];
  58. }
  59. }
  60. CGFloat height = 44.0;
  61. if (@available(iOS 11.0, *))
  62. height += self.view.safeAreaInsets.bottom;
  63. if(CToolModelAnnotation == self.pdfListView.toolModel) {
  64. if (@available(iOS 11.0, *)) {
  65. self.annotationBar.frame = CGRectMake(self.view.safeAreaInsets.left, self.view.frame.size.height - height, self.view.frame.size.width- self.view.safeAreaInsets.left - self.view.safeAreaInsets.right, height);
  66. } else {
  67. self.annotationBar.frame = CGRectMake(0, self.view.frame.size.height - height, self.view.frame.size.width, height);
  68. }
  69. } else {
  70. self.annotationBar.frame = CGRectMake(0, self.view.bounds.size.height, self.view.frame.size.width, height);
  71. }
  72. CGFloat tPosY = 0;
  73. CGFloat tBottomY = 0;
  74. if(CToolModelAnnotation == self.pdfListView.toolModel) {
  75. if (!self.navigationController.navigationBarHidden) {
  76. [UIView animateWithDuration:0.3 animations:^{
  77. CGRect frame = self.annotationBar.frame;
  78. frame.origin.y = self.view.bounds.size.height-frame.size.height;
  79. self.annotationBar.frame = frame;
  80. }];
  81. CGRect rectStatus = [[UIApplication sharedApplication] statusBarFrame];
  82. tPosY = self.navigationController.navigationBar.frame.size.height + rectStatus.size.height;
  83. tBottomY = self.annotationBar.frame.size.height;
  84. } else {
  85. [UIView animateWithDuration:0.3 animations:^{
  86. CGRect frame = self.annotationBar.frame;
  87. frame.origin.y = self.view.bounds.size.height;
  88. self.annotationBar.frame = frame;
  89. }];
  90. }
  91. } else {
  92. tPosY = 0;
  93. if (!self.navigationController.navigationBarHidden) {
  94. CGRect rectStatus = [[UIApplication sharedApplication] statusBarFrame];
  95. tPosY = self.navigationController.navigationBar.frame.size.height + rectStatus.size.height;
  96. }
  97. }
  98. if (CPDFDisplayDirectionVertical == [CPDFKitConfig sharedInstance].displayDirection) {
  99. UIEdgeInsets inset = self.pdfListView.documentView.contentInset;
  100. inset.bottom = 10 + self.annotationBar.frame.size.height;
  101. self.pdfListView.documentView.contentInset = inset;
  102. } else{
  103. UIEdgeInsets inset = self.pdfListView.documentView.contentInset;
  104. inset.bottom = 0;
  105. self.pdfListView.documentView.contentInset = inset;
  106. }
  107. }
  108. #pragma mark - Public Methods
  109. - (void)selectDocumentRefresh {
  110. self.pdfListView.annotationMode = CPDFViewAnnotationModeNone;
  111. [self.annotationBar updatePropertiesButtonState];
  112. [self.annotationBar reloadData];
  113. [self.annotationBar updateUndoRedoState];
  114. }
  115. #pragma mark - Private Methods
  116. - (void)enterAnnotationMode {
  117. self.functionTypeState = CPDFToolFunctionTypeStateAnnotation;
  118. self.pdfListView.toolModel = CToolModelAnnotation;
  119. self.navigationTitle = NSLocalizedString(@"Annotation", nil);
  120. [self.titleButton setTitle:self.navigationTitle forState:UIControlStateNormal];
  121. CGFloat tPosY = 0;
  122. CGFloat tBottomY = 0;
  123. CGRect frame = self.annotationBar.frame;
  124. frame.origin.y = self.view.bounds.size.height-frame.size.height;
  125. self.annotationBar.frame = frame;
  126. CGRect rectStatus = [[UIApplication sharedApplication] statusBarFrame];
  127. tPosY = self.navigationController.navigationBar.frame.size.height + rectStatus.size.height;
  128. tBottomY = self.annotationBar.frame.size.height;
  129. if (CPDFDisplayDirectionVertical == [CPDFKitConfig sharedInstance].displayDirection) {
  130. UIEdgeInsets inset = self.pdfListView.documentView.contentInset;
  131. inset.bottom = 10 + self.annotationBar.frame.size.height;
  132. self.pdfListView.documentView.contentInset = inset;
  133. } else{
  134. UIEdgeInsets inset = self.pdfListView.documentView.contentInset;
  135. inset.bottom = 0;
  136. self.pdfListView.documentView.contentInset = inset;
  137. }
  138. }
  139. - (void)enterViewerMode {
  140. self.functionTypeState = CPDFToolFunctionTypeStateViewer;
  141. if (self.pdfListView.isEdited) {
  142. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  143. [self.pdfListView commitEditing];
  144. dispatch_async(dispatch_get_main_queue(), ^{
  145. [self.pdfListView endOfEditing];
  146. });
  147. });
  148. } else {
  149. [self.pdfListView endOfEditing];
  150. }
  151. self.pdfListView.toolModel = CToolModelViewer;
  152. self.navigationTitle = NSLocalizedString(@"Viewer", nil);
  153. [self.titleButton setTitle:self.navigationTitle forState:UIControlStateNormal];
  154. CGRect frame = self.annotationBar.frame;
  155. frame.origin.y = self.view.bounds.size.height;
  156. self.annotationBar.frame = frame;
  157. if (CPDFDisplayDirectionVertical == [CPDFKitConfig sharedInstance].displayDirection) {
  158. UIEdgeInsets inset = self.pdfListView.documentView.contentInset;
  159. inset.bottom = 10 + self.annotationBar.frame.size.height;
  160. self.pdfListView.documentView.contentInset = inset;
  161. } else{
  162. UIEdgeInsets inset = self.pdfListView.documentView.contentInset;
  163. inset.bottom = 0;
  164. self.pdfListView.documentView.contentInset = inset;
  165. }
  166. }
  167. - (void)titleButtonClickd:(UIButton *) button {
  168. CPDFToolsViewController * toolsVc = [[CPDFToolsViewController alloc] initCustomizeWithToolArrays:@[@(CPDFToolFunctionTypeStateViewer),@(CPDFToolFunctionTypeStateAnnotation)]];
  169. toolsVc.delegate = self;
  170. AAPLCustomPresentationController *presentationController NS_VALID_UNTIL_END_OF_SCOPE;
  171. presentationController = [[AAPLCustomPresentationController alloc] initWithPresentedViewController:toolsVc presentingViewController:self];
  172. toolsVc.transitioningDelegate = presentationController;
  173. [self presentViewController:toolsVc animated:YES completion:nil];
  174. }
  175. - (void)buttonItemClicked_Bota:(id)sender {
  176. CPDFBOTAViewController *botaViewController = [[CPDFBOTAViewController alloc] initCustomizeWithPDFView:self.pdfListView navArrays:@[@(CPDFBOTATypeStateOutline),@(CPDFBOTATypeStateBookmark),@(CPDFBOTATypeStateAnnotation)]];
  177. botaViewController.delegate = self;
  178. AAPLCustomPresentationController *presentationController NS_VALID_UNTIL_END_OF_SCOPE;
  179. presentationController = [[AAPLCustomPresentationController alloc] initWithPresentedViewController:botaViewController presentingViewController:self];
  180. botaViewController.transitioningDelegate = presentationController;
  181. [self presentViewController:botaViewController animated:YES completion:nil];
  182. }
  183. - (void)setTitleRefresh {
  184. if (CPDFToolFunctionTypeStateAnnotation == self.functionTypeState) {
  185. [self enterAnnotationMode];
  186. } else {
  187. [self enterViewerMode];
  188. }
  189. }
  190. #pragma mark - CPDFViewDelegate
  191. - (void)PDFViewShouldBeginEditing:(CPDFView *)pdfView textView:(UITextView *)textView forAnnotation:(CPDFFreeTextAnnotation *)annotation {
  192. CPDFKeyboardToolbar *keyBoadrdToolbar = [[CPDFKeyboardToolbar alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 50)];
  193. keyBoadrdToolbar.delegate = self;
  194. [keyBoadrdToolbar bindToTextView:textView];
  195. }
  196. #pragma mark - CPDFListViewDelegate
  197. - (void)PDFListViewPerformTouchEnded:(CPDFListView *)pdfView {
  198. CGFloat tPosY = 0;
  199. CGFloat tBottomY = 0;
  200. if(CToolModelAnnotation == self.pdfListView.toolModel) {
  201. if (self.navigationController.navigationBarHidden) {
  202. [self.navigationController setNavigationBarHidden:NO animated:YES];
  203. [UIView animateWithDuration:0.3 animations:^{
  204. CGRect frame = self.annotationBar.frame;
  205. frame.origin.y = self.view.bounds.size.height-frame.size.height;
  206. self.annotationBar.frame = frame;
  207. self.pdfListView.pageSliderView.alpha = 1.0;
  208. self.annotationBar.topToolBar.alpha = 1.0;
  209. self.annotationBar.drawPencilFuncView.alpha = 1.0;
  210. }];
  211. CGRect rectStatus = [[UIApplication sharedApplication] statusBarFrame];
  212. tPosY = self.navigationController.navigationBar.frame.size.height + rectStatus.size.height;
  213. tBottomY = self.annotationBar.frame.size.height;
  214. } else {
  215. [self.navigationController setNavigationBarHidden:YES animated:YES];
  216. [UIView animateWithDuration:0.3 animations:^{
  217. CGRect frame = self.annotationBar.frame;
  218. frame.origin.y = self.view.bounds.size.height;
  219. self.annotationBar.frame = frame;
  220. self.pdfListView.pageSliderView.alpha = 0.0;
  221. self.annotationBar.topToolBar.alpha = 0.0;
  222. self.annotationBar.drawPencilFuncView.alpha = 0.0;
  223. }];
  224. }
  225. } else {
  226. CGFloat tPosY = 0;
  227. if (self.navigationController.navigationBarHidden) {
  228. [self.navigationController setNavigationBarHidden:NO animated:YES];
  229. [UIView animateWithDuration:0.3 animations:^{
  230. self.pdfListView.pageSliderView.alpha = 1.0;
  231. }];
  232. CGRect rectStatus = [[UIApplication sharedApplication] statusBarFrame];
  233. tPosY = self.navigationController.navigationBar.frame.size.height + rectStatus.size.height;
  234. } else {
  235. [self.navigationController setNavigationBarHidden:YES animated:YES];
  236. [UIView animateWithDuration:0.3 animations:^{
  237. self.pdfListView.pageSliderView.alpha = 0.0;
  238. }];
  239. }
  240. }
  241. if (CPDFDisplayDirectionVertical == [CPDFKitConfig sharedInstance].displayDirection) {
  242. UIEdgeInsets inset = self.pdfListView.documentView.contentInset;
  243. inset.bottom = 10 + self.annotationBar.frame.size.height;
  244. self.pdfListView.documentView.contentInset = inset;
  245. } else{
  246. UIEdgeInsets inset = self.pdfListView.documentView.contentInset;
  247. inset.bottom = 0;
  248. self.pdfListView.documentView.contentInset = inset;
  249. }
  250. }
  251. - (void)PDFListViewEditNote:(CPDFListView *)pdfListView forAnnotation:(CPDFAnnotation *)annotation {
  252. if([annotation isKindOfClass:[CPDFLinkAnnotation class]]) {
  253. [self.annotationBar buttonItemClicked_openAnnotation:self.titleButton];
  254. } else {
  255. CGRect rect = [self.pdfListView convertRect:annotation.bounds fromPage:annotation.page];
  256. CPDFNoteOpenViewController *noteVC = [[CPDFNoteOpenViewController alloc]initWithAnnotation:annotation];
  257. noteVC.delegate = self;
  258. [noteVC showViewController:self inRect:rect];
  259. }
  260. }
  261. - (void)PDFListViewChangedAnnotationType:(CPDFListView *)pdfListView forAnnotationMode:(CPDFViewAnnotationMode)annotationMode {
  262. [self.annotationBar reloadData];
  263. }
  264. - (void)PDFListViewPerformAddStamp:(CPDFListView *)pdfView atPoint:(CGPoint)point forPage:(CPDFPage *)page {
  265. [self.annotationBar addStampAnnotationWithPage:page point:point];
  266. }
  267. - (void)PDFListViewPerformAddImage:(CPDFListView *)pdfView atPoint:(CGPoint)point forPage:(CPDFPage *)page {
  268. [self.annotationBar addImageAnnotationWithPage:page point:point];
  269. }
  270. - (BOOL)PDFListViewerTouchEndedIsAudioRecordMedia:(CPDFListView *)pdfListView {
  271. if (CPDFMediaStateAudioRecord == [CPDFMediaManager shareManager].mediaState) {
  272. [self PDFListViewPerformTouchEnded:self.pdfListView];
  273. return YES;
  274. }
  275. return NO;
  276. }
  277. - (void)PDFListViewPerformCancelMedia:(CPDFListView *)pdfView atPoint:(CGPoint)point forPage:(CPDFPage *)page {
  278. [CPDFMediaManager shareManager].mediaState = CPDFMediaStateStop;
  279. }
  280. - (void)PDFListViewPerformRecordMedia:(CPDFListView *)pdfView atPoint:(CGPoint)point forPage:(CPDFPage *)page {
  281. if([self.soundPlayBar superview]) {
  282. if(self.soundPlayBar.soundState == CPDFSoundStatePlay) {
  283. [self.soundPlayBar stopAudioPlay];
  284. [self.soundPlayBar removeFromSuperview];
  285. } else if (self.soundPlayBar.soundState == CPDFSoundStateRecord) {
  286. [self.soundPlayBar stopRecord];
  287. [self.soundPlayBar removeFromSuperview];
  288. }
  289. }
  290. AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio];
  291. if (authStatus == AVAuthorizationStatusNotDetermined || authStatus == AVAuthorizationStatusDenied) {
  292. [AVCaptureDevice requestAccessForMediaType:AVMediaTypeAudio completionHandler:^(BOOL granted) {
  293. if (granted) {
  294. } else {
  295. NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
  296. if ([[UIApplication sharedApplication] canOpenURL:url]) {
  297. [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
  298. }
  299. }
  300. }];
  301. }
  302. if (authStatus == AVAuthorizationStatusAuthorized) {
  303. NSInteger pageindex = [self.pdfListView.document indexForPage:page];
  304. [CPDFMediaManager shareManager].mediaState = CPDFMediaStateAudioRecord;
  305. [CPDFMediaManager shareManager].pageNum = pageindex;
  306. [CPDFMediaManager shareManager].ptInPdf = point;
  307. _soundPlayBar = [[CPDFSoundPlayBar alloc] initWithStyle:self.annotationManage.annotStyle];
  308. _soundPlayBar.delegate = self;
  309. [_soundPlayBar showInView:self.pdfListView soundState:CPDFSoundStateRecord];
  310. [_soundPlayBar startAudioRecord];
  311. } else {
  312. return;
  313. }
  314. }
  315. - (void)PDFListViewPerformPlay:(CPDFListView *)pdfView forAnnotation:(CPDFSoundAnnotation *)annotation {
  316. NSString *filePath = [annotation mediaPath];
  317. if (filePath) {
  318. NSURL *URL = [NSURL fileURLWithPath:filePath];
  319. _soundPlayBar = [[CPDFSoundPlayBar alloc] initWithStyle:self.annotationManage.annotStyle];
  320. _soundPlayBar.delegate = self;
  321. [_soundPlayBar showInView:self.pdfListView soundState:CPDFSoundStatePlay];
  322. [_soundPlayBar setURL:URL];
  323. [_soundPlayBar startAudioPlay];
  324. [CPDFMediaManager shareManager].mediaState = CPDFMediaStateVedioPlaying;
  325. }
  326. }
  327. - (void)PDFListViewPerformSignatureWidget:(CPDFListView *)pdfView forAnnotation:(CPDFSignatureWidgetAnnotation *)annotation {
  328. [self.annotationBar openSignatureAnnotation:annotation];
  329. }
  330. - (void)PDFListViewEditProperties:(CPDFListView *)pdfListView forAnnotation:(CPDFAnnotation *)annotation {
  331. [self.annotationBar buttonItemClicked_openAnnotation:self.titleButton];
  332. }
  333. #pragma mark - CPDFKeyboardToolbarDelegate
  334. - (void)keyboardShouldDissmiss:(CPDFKeyboardToolbar *)toolbar {
  335. [self.pdfListView commitEditAnnotationFreeText];
  336. self.pdfListView.annotationMode = CPDFViewAnnotationModeNone;
  337. [self.annotationBar reloadData];
  338. }
  339. #pragma mark - CPDFBOTAViewControllerDelegate
  340. - (void)botaViewControllerDismiss:(CPDFBOTAViewController *)botaViewController {
  341. [self.navigationController dismissViewControllerAnimated:YES completion:nil];
  342. }
  343. #pragma mark - CPDFAnnotationBarDelegate
  344. - (void)annotationBarClick:(CPDFAnnotationToolBar *)annotationBar clickAnnotationMode:(CPDFViewAnnotationMode)annotationMode forSelected:(BOOL)isSelected forButton:(UIButton *)button {
  345. if(CPDFViewAnnotationModeInk == annotationMode || CPDFViewAnnotationModePencilDrawing == annotationMode) {
  346. CGFloat tPosY = 0;
  347. if(isSelected) {
  348. [self.navigationController setNavigationBarHidden:YES animated:YES];
  349. [UIView animateWithDuration:0.3 animations:^{
  350. CGRect frame = self.annotationBar.frame;
  351. frame.origin.y = self.view.bounds.size.height;
  352. self.annotationBar.frame = frame;
  353. self.pdfListView.pageSliderView.alpha = 0.0;
  354. UIEdgeInsets inset = self.pdfListView.documentView.contentInset;
  355. inset.bottom = 0;
  356. self.pdfListView.documentView.contentInset = inset;
  357. }];
  358. } else {
  359. [self.navigationController setNavigationBarHidden:NO animated:YES];
  360. [UIView animateWithDuration:0.3 animations:^{
  361. CGRect frame = self.annotationBar.frame;
  362. frame.origin.y = self.view.bounds.size.height-frame.size.height;
  363. self.annotationBar.frame = frame;
  364. self.pdfListView.pageSliderView.alpha = 1.0;
  365. }];
  366. CGRect rectStatus = [[UIApplication sharedApplication] statusBarFrame];
  367. tPosY = self.navigationController.navigationBar.frame.size.height + rectStatus.size.height;
  368. UIEdgeInsets inset = self.pdfListView.documentView.contentInset;
  369. inset.bottom = self.annotationBar.frame.size.height;
  370. self.pdfListView.documentView.contentInset = inset;
  371. }
  372. } else if (CPDFViewAnnotationModeSound == annotationMode && !isSelected) {
  373. if(CPDFSoundStateRecord == self.soundPlayBar.soundState) {
  374. [self.soundPlayBar stopRecord];
  375. } else if (CPDFSoundStatePlay== self.soundPlayBar.soundState) {
  376. [self.soundPlayBar stopAudioPlay];
  377. }
  378. }
  379. }
  380. #pragma mark - CPDFNoteOpenViewControllerDelegate
  381. - (void)getNoteOpenViewController:(CPDFNoteOpenViewController *)noteOpenVC content:(NSString *)content isDelete:(BOOL)isDelete {
  382. if (isDelete) {
  383. [noteOpenVC.annotation.page removeAnnotation:noteOpenVC.annotation];
  384. [self.pdfListView setNeedsDisplayForPage:noteOpenVC.annotation.page];
  385. if([self.pdfListView.activeAnnotations containsObject:noteOpenVC.annotation]) {
  386. NSMutableArray *activeAnnotations = [NSMutableArray arrayWithArray:self.pdfListView.activeAnnotations];
  387. [activeAnnotations removeObject:noteOpenVC.annotation];
  388. [self.pdfListView updateActiveAnnotations:activeAnnotations];
  389. }
  390. } else {
  391. if([noteOpenVC.annotation isKindOfClass:[CPDFMarkupAnnotation class]]) {
  392. CPDFMarkupAnnotation *markupAnnotation = (CPDFMarkupAnnotation *)noteOpenVC.annotation;
  393. [markupAnnotation setContents:content?:@""];
  394. } else if(([noteOpenVC.annotation isKindOfClass:[CPDFTextAnnotation class]])){
  395. if(content && content.length > 0) {
  396. noteOpenVC.annotation.contents = content?:@"";
  397. } else {
  398. if([self.pdfListView.activeAnnotations containsObject:noteOpenVC.annotation]) {
  399. [self.pdfListView updateActiveAnnotations:@[]];
  400. }
  401. [noteOpenVC.annotation.page removeAnnotation:noteOpenVC.annotation];
  402. [self.pdfListView setNeedsDisplayForPage:noteOpenVC.annotation.page];
  403. }
  404. } else {
  405. noteOpenVC.annotation.contents = content?:@"";
  406. }
  407. }
  408. }
  409. #pragma mark - CPDFSoundPlayBarDelegate
  410. - (void)soundPlayBarRecordFinished:(CPDFSoundPlayBar *)soundPlayBar withFile:(NSString *)filePath {
  411. CPDFPage *page = [self.pdfListView.document pageAtIndex:[CPDFMediaManager shareManager].pageNum];
  412. CPDFSoundAnnotation *annotation = [[CPDFSoundAnnotation alloc] initWithDocument:self.pdfListView.document];
  413. if ([annotation setMediaPath:filePath]) {
  414. CGRect bounds = annotation.bounds;
  415. bounds.origin.x = [CPDFMediaManager shareManager].ptInPdf.x-bounds.size.width/2.0;
  416. bounds.origin.y = [CPDFMediaManager shareManager].ptInPdf.y-bounds.size.height/2.0;
  417. annotation.bounds = bounds;
  418. [self.pdfListView addAnnotation:annotation forPage:page];
  419. }
  420. [CPDFMediaManager shareManager].mediaState = CPDFMediaStateStop;
  421. [self.pdfListView stopRecord];
  422. }
  423. - (void)soundPlayBarRecordCancel:(CPDFSoundPlayBar *)soundPlayBar {
  424. if(CPDFSoundStateRecord == self.soundPlayBar.soundState) {
  425. [self.pdfListView stopRecord];
  426. }
  427. [CPDFMediaManager shareManager].mediaState = CPDFMediaStateStop;
  428. }
  429. - (void)soundPlayBarPlayClose:(CPDFSoundPlayBar *)soundPlayBar {
  430. [CPDFMediaManager shareManager].mediaState = CPDFMediaStateStop;
  431. }
  432. #pragma mark - Notification
  433. - (void)PDFPageDidRemoveAnnotationNotification:(NSNotification *)notification {
  434. CPDFAnnotation *annotation = [notification object];
  435. if ([annotation isKindOfClass:[CPDFSoundAnnotation class]]) {
  436. [self.soundPlayBar stopAudioPlay];
  437. if ([self.soundPlayBar isDescendantOfView:self.view]) {
  438. [self.soundPlayBar removeFromSuperview];
  439. }
  440. }
  441. }
  442. #pragma mark - CPDFToolsViewControllerDelegate
  443. - (void)CPDFToolsViewControllerDismiss:(CPDFToolsViewController *) viewController selectItemAtIndex:(CPDFToolFunctionTypeState)selectIndex {
  444. if(CPDFToolFunctionTypeStateViewer == selectIndex) {
  445. //viewwer
  446. [self enterViewerMode];
  447. }else{
  448. //Annotation
  449. [self enterAnnotationMode];
  450. }
  451. }
  452. @end