// // CPDFViewController.m // Form-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 #import @interface CPDFViewController () @property(nonatomic, strong) CPDFFormBar * formBar; @property(nonatomic, strong) CAnnotationManage *annotationManage; @property(nonatomic, strong) CPDFSignatureWidgetAnnotation * signatureAnnotation; @end @implementation CPDFViewController - (void)viewDidLoad { [super viewDidLoad]; self.pdfListView.toolModel = CToolModelForm; self.pdfListView.delegate = self; self.view.backgroundColor = [CPDFColorUtils CPDFViewControllerBackgroundColor]; [self initFormBar]; [self enterFormMode]; } - (void)initFormBar { self.annotationManage = [[CAnnotationManage alloc] initWithPDFView:self.pdfListView]; self.formBar = [[CPDFFormBar alloc] initAnnotationManage:self.annotationManage]; CGFloat height = 44.0; if (@available(iOS 11.0, *)) height += self.view.safeAreaInsets.bottom; self.formBar.frame = CGRectMake(0, self.view.frame.size.height - height, self.view.frame.size.width, height); self.formBar.delegate = self; self.formBar.parentVC = self; #warning need to set parentVC // [self.formBar setParentVC:self]; [self.view addSubview:self.formBar]; } - (void)initWitNavigationTitle { //titleButton CNavigationBarTitleButton * navTitleButton = [[CNavigationBarTitleButton alloc] init]; self.titleButton = navTitleButton; self.navigationTitle = NSLocalizedString(@"Form", nil); [navTitleButton setImage:[UIImage imageNamed:@"syasarrow"] forState:UIControlStateNormal]; [navTitleButton addTarget:self action:@selector(titleButtonClickd:) forControlEvents:UIControlEventTouchUpInside]; [navTitleButton setTitle:self.navigationTitle forState:UIControlStateNormal]; [navTitleButton setTitleColor:[CPDFColorUtils CAnyReverseBackgooundColor] forState:UIControlStateNormal]; self.titleButton.frame = CGRectMake(0, 0, 50, 30); self.navigationItem.titleView = self.titleButton; } - (void)viewWillLayoutSubviews { [super viewWillLayoutSubviews]; if([self.popMenu superview]) { if (@available(iOS 11.0, *)) { [self.popMenu showMenuInRect:CGRectMake(self.view.frame.size.width - self.view.safeAreaInsets.right - 250, CGRectGetMaxY(self.navigationController.navigationBar.frame), 250, 200)]; } else { // Fallback on earlier versions [self.popMenu showMenuInRect:CGRectMake(self.view.frame.size.width - 250, CGRectGetMaxY(self.navigationController.navigationBar.frame), 250, 200)]; } } CGFloat height = 44.0; if (@available(iOS 11.0, *)) height += self.view.safeAreaInsets.bottom; if(CToolModelForm == self.pdfListView.toolModel) { if (@available(iOS 11.0, *)) { self.formBar.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); } else { self.formBar.frame = CGRectMake(0, self.view.frame.size.height - height, self.view.frame.size.width, height); } } else { self.formBar.frame = CGRectMake(0, self.view.bounds.size.height, self.view.frame.size.width, height); } CGFloat tPosY = 0; CGFloat tBottomY = 0; if(CToolModelForm == self.pdfListView.toolModel) { if (!self.navigationController.navigationBarHidden) { [UIView animateWithDuration:0.3 animations:^{ CGRect frame = self.formBar.frame; frame.origin.y = self.view.bounds.size.height-frame.size.height; self.formBar.frame = frame; }]; CGRect rectStatus = [[UIApplication sharedApplication] statusBarFrame]; tPosY = self.navigationController.navigationBar.frame.size.height + rectStatus.size.height; tBottomY = self.formBar.frame.size.height; } else { [UIView animateWithDuration:0.3 animations:^{ CGRect frame = self.formBar.frame; frame.origin.y = self.view.bounds.size.height; self.formBar.frame = frame; }]; } } else { tPosY = 0; if (!self.navigationController.navigationBarHidden) { CGRect rectStatus = [[UIApplication sharedApplication] statusBarFrame]; tPosY = self.navigationController.navigationBar.frame.size.height + rectStatus.size.height; } } if (@available(iOS 11.0, *)) { 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); } else { self.pdfListView.frame = CGRectMake(0, tPosY, self.view.frame.size.width, self.view.frame.size.height - tBottomY - tPosY); } } #pragma mark - Private - (void)enterFormMode { self.pdfListView.toolModel = CToolModelForm; self.navigationTitle = NSLocalizedString(@"Form", nil); [self.titleButton setTitle:self.navigationTitle forState:UIControlStateNormal]; CGFloat tPosY = 0; CGFloat tBottomY = 0; CGRect frame = self.formBar.frame; frame.origin.y = self.view.bounds.size.height-frame.size.height; self.formBar.frame = frame; CGRect rectStatus = [[UIApplication sharedApplication] statusBarFrame]; tPosY = self.navigationController.navigationBar.frame.size.height + rectStatus.size.height; tBottomY = self.formBar.frame.size.height; if (@available(iOS 11.0, *)) { 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); } else { self.pdfListView.frame = CGRectMake(0, tPosY, self.view.frame.size.width, self.view.frame.size.height - tBottomY - tPosY); } [self.formBar updateStatus]; } - (void)selectDocumentRefresh { [super selectDocumentRefresh]; [self.formBar initUndoRedo]; } - (void)enterViewerMode { self.pdfListView.toolModel = CToolModelViewer; self.navigationTitle = NSLocalizedString(@"Viewer", nil); [self.titleButton setTitle:self.navigationTitle forState:UIControlStateNormal]; CGRect frame = self.formBar.frame; frame.origin.y = self.view.bounds.size.height; self.formBar.frame = frame; CGFloat tPosY = 0; CGFloat tBottomY = 0; if (@available(iOS 11.0, *)) { 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); } else { self.pdfListView.frame = CGRectMake(0, tPosY, self.view.frame.size.width, self.view.frame.size.height - tBottomY - tPosY); } } - (void) titleButtonClickd:(UIButton *) button { CPDFToolsViewController * toolsVc = [[CPDFToolsViewController alloc] initCustomizeWithToolArrays:@[@(CToolModelViewer),@(CToolModelForm)]]; toolsVc.delegate = self; AAPLCustomPresentationController *presentationController NS_VALID_UNTIL_END_OF_SCOPE; presentationController = [[AAPLCustomPresentationController alloc] initWithPresentedViewController:toolsVc presentingViewController:self]; toolsVc.transitioningDelegate = presentationController; [self presentViewController:toolsVc animated:YES completion:nil]; } - (void)navigationRightItemBota { CPDFBOTAViewController *botaViewController = [[CPDFBOTAViewController alloc] initCustomizeWithPDFView:self.pdfListView navArrays:@[@(CPDFBOTATypeStateOutline),@(CPDFBOTATypeStateBookmark),@(CPDFBOTATypeStateAnnotation)]]; botaViewController.delegate = self; AAPLCustomPresentationController *presentationController NS_VALID_UNTIL_END_OF_SCOPE; presentationController = [[AAPLCustomPresentationController alloc] initWithPresentedViewController:botaViewController presentingViewController:self]; botaViewController.transitioningDelegate = presentationController; [self presentViewController:botaViewController animated:YES completion:nil]; } #pragma mark - CPDFListViewDelegate - (void)PDFListViewPerformTouchEnded:(CPDFListView *)pdfView { CGFloat tPosY = 0; CGFloat tBottomY = 0; if(CToolModelForm == self.pdfListView.toolModel) { if (self.navigationController.navigationBarHidden) { [self.navigationController setNavigationBarHidden:NO animated:YES]; [UIView animateWithDuration:0.3 animations:^{ CGRect frame = self.formBar.frame; frame.origin.y = self.view.bounds.size.height-frame.size.height; self.formBar.frame = frame; self.pdfListView.pageSliderView.alpha = 1.0; // self.formBar.topToolBar.alpha = 1.0; // self.formBar.drawPencilFuncView.alpha = 1.0; }]; CGRect rectStatus = [[UIApplication sharedApplication] statusBarFrame]; tPosY = self.navigationController.navigationBar.frame.size.height + rectStatus.size.height; tBottomY = self.formBar.frame.size.height; } else { [self.navigationController setNavigationBarHidden:YES animated:YES]; [UIView animateWithDuration:0.3 animations:^{ CGRect frame = self.formBar.frame; frame.origin.y = self.view.bounds.size.height; self.formBar.frame = frame; self.pdfListView.pageSliderView.alpha = 0.0; }]; } } else { CGFloat tPosY = 0; if (self.navigationController.navigationBarHidden) { [self.navigationController setNavigationBarHidden:NO animated:YES]; [UIView animateWithDuration:0.3 animations:^{ self.pdfListView.pageSliderView.alpha = 1.0; }]; CGRect rectStatus = [[UIApplication sharedApplication] statusBarFrame]; tPosY = self.navigationController.navigationBar.frame.size.height + rectStatus.size.height; } else { [self.navigationController setNavigationBarHidden:YES animated:YES]; [UIView animateWithDuration:0.3 animations:^{ self.pdfListView.pageSliderView.alpha = 0.0; }]; } } if (@available(iOS 11.0, *)) { 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); } else { self.pdfListView.frame = CGRectMake(0, tPosY, self.view.frame.size.width, self.view.frame.size.height - tBottomY - tPosY); } } //- (void)PDFListViewEditNote:(CPDFListView *)pdfListView forAnnotation:(CPDFAnnotation *)annotation { // if([annotation isKindOfClass:[CPDFLinkAnnotation class]]) { // [self.formBar buttonItemClicked_open:annotation]; // } else { // CGRect rect = [self.pdfListView convertRect:annotation.bounds fromPage:annotation.page]; // CPDFNoteOpenViewController *noteVC = [[CPDFNoteOpenViewController alloc]initWithAnnotation:annotation]; // noteVC.delegate = self; // [noteVC showViewController:self inRect:rect]; // } //} // - (void)PDFListViewChangedAnnotationType:(CPDFListView *)pdfListView forAnnotationMode:(CPDFViewAnnotationMode)annotationMode { [self.formBar reloadData]; } - (void)PDFListViewPerformUrl:(CPDFListView *)pdfView withContent:(NSString *)content { NSURL * url = [NSURL URLWithString:content]; [[UIApplication sharedApplication] openURL:url]; } // //- (void)PDFListViewPerformAddStamp:(CPDFListView *)pdfView atPoint:(CGPoint)point forPage:(CPDFPage *)page { // [self.annotationBar addStampAnnotationWithPage:page point:point]; //} // //- (void)PDFListViewPerformAddImage:(CPDFListView *)pdfView atPoint:(CGPoint)point forPage:(CPDFPage *)page { // [self.annotationBar addImageAnnotationWithPage:page point:point]; //} // //- (BOOL)PDFListViewerTouchEndedIsAudioRecordMedia:(CPDFListView *)pdfListView { // if (CPDFMediaStateAudioRecord == [CPDFMediaManager shareManager].mediaState) { // [self PDFListViewPerformTouchEnded:self.pdfListView]; // return YES; // } // return NO; //} // //- (void)PDFListViewPerformCancelMedia:(CPDFListView *)pdfView atPoint:(CGPoint)point forPage:(CPDFPage *)page { // [CPDFMediaManager shareManager].mediaState = CPDFMediaStateStop; // self.pdfListView.annotationMode = CPDFViewAnnotationModeNone; //} // //- (void)PDFListViewPerformRecordMedia:(CPDFListView *)pdfView atPoint:(CGPoint)point forPage:(CPDFPage *)page { // NSInteger pageindex = [self.pdfListView.document indexForPage:page]; // [CPDFMediaManager shareManager].mediaState = CPDFMediaStateAudioRecord; // [CPDFMediaManager shareManager].pageNum = pageindex; // [CPDFMediaManager shareManager].ptInPdf = point; // // if([self.soundPlayBar superview]) { // if(self.soundPlayBar.soundState == CPDFSoundStatePlay) { // [self.soundPlayBar stopAudioPlay]; // [self.soundPlayBar removeFromSuperview]; // } else if (self.soundPlayBar.soundState == CPDFSoundStateRecord) { // [self.soundPlayBar stopRecord]; // [self.soundPlayBar removeFromSuperview]; // } // } // // _soundPlayBar = [[CPDFSoundPlayBar alloc] initWithStyle:self.annotationManage.annotStyle]; // _soundPlayBar.delegate = self; // [_soundPlayBar showInView:self.pdfListView soundState:CPDFSoundStateRecord]; // [_soundPlayBar startAudioRecord]; //} // //- (void)PDFListViewPerformPlay:(CPDFListView *)pdfView forAnnotation:(CPDFSoundAnnotation *)annotation { // NSString *filePath = [annotation mediaPath]; // if (filePath) { // NSURL *URL = [NSURL fileURLWithPath:filePath]; // // _soundPlayBar = [[CPDFSoundPlayBar alloc] initWithStyle:self.annotationManage.annotStyle]; // _soundPlayBar.delegate = self; // [_soundPlayBar showInView:self.pdfListView soundState:CPDFSoundStatePlay]; // [_soundPlayBar setURL:URL]; // [_soundPlayBar startAudioPlay]; // [CPDFMediaManager shareManager].mediaState = CPDFMediaStateVedioPlaying; // } //} // - (void)PDFListViewPerformSignatureWidget:(CPDFListView *)pdfView forAnnotation:(CPDFSignatureWidgetAnnotation *)annotation { if(CToolModelViewer == self.pdfListView.toolModel) { self.signatureAnnotation = annotation; AAPLCustomPresentationController *presentationController NS_VALID_UNTIL_END_OF_SCOPE; CPDFSignatureViewController *signatureVC = [[CPDFSignatureViewController alloc] initWithStyle:nil]; presentationController = [[AAPLCustomPresentationController alloc] initWithPresentedViewController:signatureVC presentingViewController:self]; signatureVC.delegate = self; signatureVC.transitioningDelegate = presentationController; [self presentViewController:signatureVC animated:YES completion:nil]; } } - (void)PDFListViewEditWidget:(CPDFListView *)pdfListView forAnnotation:(CPDFAnnotation *)annotation { [self.formBar buttonItemClicked_openOption:annotation]; } - (void)PDFListViewEditProperties:(CPDFListView *)pdfListView forAnnotation:(CPDFAnnotation *)annotation { [self.formBar buttonItemClicked_open:annotation]; } #pragma mark - CPDFBOTAViewControllerDelegate - (void)botaViewControllerDismiss:(CPDFBOTAViewController *)botaViewController { [self.navigationController dismissViewControllerAnimated:YES completion:nil]; } #pragma mark - CPDFAnnotationBarDelegate - (void)formBarClick:(CPDFFormBar *)pdfFormBar forSelected:(BOOL)isSelected forButton:(UIButton *)button { NSLog(@"form bar Clicked!"); } //- (void)annotationBarClick:(CPDFAnnotationBar *)annotationBar clickAnnotationMode:(CPDFViewAnnotationMode)annotationMode forSelected:(BOOL)isSelected forButton:(UIButton *)button { // if(CPDFViewAnnotationModeInk == annotationMode || CPDFViewAnnotationModePencilDrawing == annotationMode) { // CGFloat tPosY = 0; // if(isSelected) { // [self.navigationController setNavigationBarHidden:YES animated:YES]; // [UIView animateWithDuration:0.3 animations:^{ // CGRect frame = self.annotationBar.frame; // frame.origin.y = self.view.bounds.size.height; // self.annotationBar.frame = frame; // self.pdfListView.pageSliderView.alpha = 0.0; // // if (@available(iOS 11.0, *)) { // 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 - self.view.safeAreaInsets.bottom - tPosY); // } else { // self.pdfListView.frame = CGRectMake(0, tPosY, self.view.frame.size.width, self.view.frame.size.height - tPosY); // } // }]; // } else { // [self.navigationController setNavigationBarHidden:NO animated:YES]; // [UIView animateWithDuration:0.3 animations:^{ // CGRect frame = self.annotationBar.frame; // frame.origin.y = self.view.bounds.size.height-frame.size.height; // self.annotationBar.frame = frame; // self.pdfListView.pageSliderView.alpha = 1.0; // }]; // CGRect rectStatus = [[UIApplication sharedApplication] statusBarFrame]; // tPosY = self.navigationController.navigationBar.frame.size.height + rectStatus.size.height; // // if (@available(iOS 11.0, *)) { // 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- self.annotationBar.frame.size.height); // } else { // self.pdfListView.frame = CGRectMake(0, tPosY, self.view.frame.size.width, self.view.frame.size.height - tPosY- tPosY- self.annotationBar.frame.size.height); // } // } // } else if (CPDFViewAnnotationModeSound == annotationMode && !isSelected) { // if(CPDFSoundStateRecord == self.soundPlayBar.soundState) { // [self.soundPlayBar stopRecord]; // // } else if (CPDFSoundStatePlay== self.soundPlayBar.soundState) { // [self.soundPlayBar stopAudioPlay]; // } // } //} #pragma mark - CPDFNoteOpenViewControllerDelegate //- (void)getNoteOpenViewController:(CPDFNoteOpenViewController *)noteOpenVC content:(NSString *)content isDelete:(BOOL)isDelete { // if (isDelete) { // [noteOpenVC.annotation.page removeAnnotation:noteOpenVC.annotation]; // [self.pdfListView setNeedsDisplayForPage:noteOpenVC.annotation.page]; // if([self.pdfListView.activeAnnotations containsObject:noteOpenVC.annotation]) { // NSMutableArray *activeAnnotations = [NSMutableArray arrayWithArray:self.pdfListView.activeAnnotations]; // [activeAnnotations removeObject:noteOpenVC.annotation]; // [self.pdfListView updateActiveAnnotations:activeAnnotations]; // } // } else { // if([noteOpenVC.annotation isKindOfClass:[CPDFMarkupAnnotation class]]) { // CPDFMarkupAnnotation *markupAnnotation = (CPDFMarkupAnnotation *)noteOpenVC.annotation; // [markupAnnotation setMarkupText:content?:@""]; // } else { // noteOpenVC.annotation.contents = content?:@""; // } // } //} //#pragma mark - CPDFSoundPlayBarDelegate // //- (void)soundPlayBarRecordFinished:(CPDFSoundPlayBar *)soundPlayBar withFile:(NSString *)filePath { // self.pdfListView.annotationMode = CPDFViewAnnotationModeNone; // // CPDFPage *page = [self.pdfListView.document pageAtIndex:[CPDFMediaManager shareManager].pageNum]; // CPDFSoundAnnotation *annotation = [[CPDFSoundAnnotation alloc] initWithDocument:self.pdfListView.document]; // // if ([annotation setMediaPath:filePath]) { // CGRect bounds = annotation.bounds; // bounds.origin.x = [CPDFMediaManager shareManager].ptInPdf.x-bounds.size.width/2.0; // bounds.origin.y = [CPDFMediaManager shareManager].ptInPdf.y-bounds.size.height/2.0; // annotation.bounds = bounds; // [self.pdfListView addAnnotation:annotation forPage:page]; // } // // [CPDFMediaManager shareManager].mediaState = CPDFMediaStateStop; // [self.pdfListView stopRecord]; //} // //- (void)soundPlayBarRecordCancel:(CPDFSoundPlayBar *)soundPlayBar { // if(CPDFSoundStateRecord == self.soundPlayBar.soundState) { // [self.pdfListView stopRecord]; // } // [CPDFMediaManager shareManager].mediaState = CPDFMediaStateStop; // self.pdfListView.annotationMode = CPDFViewAnnotationModeNone; //} // //- (void)soundPlayBarPlayClose:(CPDFSoundPlayBar *)soundPlayBar { // [CPDFMediaManager shareManager].mediaState = CPDFMediaStateStop; //} #pragma mark - CPDFToolsViewControllerDelegate - (void)CPDFToolsViewControllerDismiss:(CPDFToolsViewController *) viewController selectItemAtIndex:(CToolModel)selectIndex { if(CToolModelViewer == selectIndex) { //viewwer [self enterViewerMode]; }else{ //Form [self enterFormMode]; } } #pragma mark - CPDFSignatureViewControllerDelegate - (void)signatureViewControllerDismiss:(CPDFSignatureViewController *)signatureViewController { self.signatureAnnotation = nil; } - (void)signatureViewController:(CPDFSignatureViewController *)signatureViewController image:(UIImage *)image { if(self.signatureAnnotation) { [self.signatureAnnotation signWithImage:image]; [self.pdfListView setNeedsDisplayForPage:self.signatureAnnotation.page]; self.signatureAnnotation = nil; } } @end