123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323 |
- //
- // CPDFViewController.m
- // Edit-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 <ComPDFKit/ComPDFKit.h>
- #import <compdfkit_tools/compdfkit_tools.h>
- @interface CPDFViewController () <UIDocumentPickerDelegate,CPDFToolsViewControllerDelegate,CPDFEditToolBarDelegate,CPDFSignatureViewControllerDelegate>
- @property(nonatomic, strong) CPDFEditViewController *baseVC;
- @property(nonatomic, assign) CPDFEditMode editMode;
- @property(nonatomic, strong) CPDFEditToolBar * toolBar;
- @property(nonatomic, assign) CGPoint menuPoint;
- @property (nonatomic, strong) CPDFSignatureWidgetAnnotation * signatureAnnotation;
- @end
- @implementation CPDFViewController
- - (void)viewDidLoad {
- [super viewDidLoad];
-
- CPDFEditingConfig *editingConfig = [[CPDFEditingConfig alloc]init];
- editingConfig.editingBorderWidth = 1.0;
- editingConfig.editingOffsetGap = 5;
- self.pdfListView.editingConfig = editingConfig;
- self.pdfListView.toolModel = CToolModelEdit;
-
- [self initWithEditTool];
-
- [self enterEditMode];
- [self.toolBar updateButtonState];
-
- [self enterEditMode];
- }
- - (void)initWithEditTool {
- if(!self.toolBar){
- self.toolBar = [[CPDFEditToolBar alloc] initWithPDFView:self.pdfListView];
- }
-
- self.toolBar.delegate = self;
- [self.view addSubview:self.toolBar];
- }
- - (void)initWitNavigationTitle {
- //titleButton
- CNavigationBarTitleButton * navTitleButton = [[CNavigationBarTitleButton alloc] init];
- self.titleButton = navTitleButton;
- self.navigationTitle = NSLocalizedString(@"Content Edit", 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;
- }
- #pragma mark - Action
- - (void)titleButtonClickd:(UIButton *) button {
- CPDFToolsViewController * toolsVc = [[CPDFToolsViewController alloc] initCustomizeWithToolArrays:@[@(CToolModelViewer),@(CToolModelEdit)]];
- 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)viewWillLayoutSubviews {
- [super viewWillLayoutSubviews];
-
- CGFloat tPosY = 0;
- CGFloat bottomY = 0;
- CGFloat height = 44.0;
-
- if (@available(iOS 11.0, *))
- height += self.view.safeAreaInsets.bottom;
-
- if(CToolModelViewer == self.pdfListView.toolModel) {
- height = 0;
- }
-
- if (!self.navigationController.navigationBarHidden) {
- CGRect rectStatus = [[UIApplication sharedApplication] statusBarFrame];
- tPosY = self.navigationController.navigationBar.frame.size.height + rectStatus.size.height;
- self.toolBar.frame = CGRectMake(0, self.view.frame.size.height - height, self.view.frame.size.width, height);
- bottomY = self.toolBar.frame.size.height;
- } else {
- self.toolBar.frame = CGRectMake(0, self.view.frame.size.height, self.view.frame.size.width, height);
- bottomY = 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 - tPosY- bottomY);
- } else {
- self.pdfListView.frame = CGRectMake(0, tPosY, self.view.frame.size.width, self.view.frame.size.height - tPosY-bottomY);
- }
- }
- #pragma mark - Private
- - (void)enterEditMode {
- self.pdfListView.toolModel = CToolModelEdit;
- self.toolBar.hidden = NO;
- self.navigationTitle = NSLocalizedString(@"Content Edit", nil);
- [self.titleButton setTitle:self.navigationTitle forState:UIControlStateNormal];
- [self.pdfListView beginEditingLoadType:CEditingLoadTypeText | CEditingLoadTypeImage];
- [self viewWillLayoutSubviews];
- }
- - (void)enterViewerMode {
- self.pdfListView.toolModel = CToolModelViewer;
- self.navigationTitle = NSLocalizedString(@"Viewer", nil);
- [self.titleButton setTitle:self.navigationTitle forState:UIControlStateNormal];
- if (self.pdfListView.isEdited) {
- dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
- [self.pdfListView commitEditing];
- dispatch_async(dispatch_get_main_queue(), ^{
- [self.pdfListView endOfEditing];
- });
- });
- } else {
- [self.pdfListView endOfEditing];
- }
-
- CGFloat tPosY = 0;
- CGFloat bottomY = 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 - tPosY- bottomY-self.view.safeAreaInsets.bottom);
- } else {
- self.pdfListView.frame = CGRectMake(0, tPosY, self.view.frame.size.width, self.view.frame.size.height - tPosY-bottomY-20);
- }
- }
- - (void)enterPDFShare {
- if(self.pdfListView.isEditing && self.pdfListView.isEdited) {
- [self.pdfListView commitEditing];
- [self.pdfListView endOfEditing];
- }
-
- [super enterPDFShare];
- }
- - (void)enterPDFAddFile {
- if(self.pdfListView.isEditing && self.pdfListView.isEdited) {
- [self.pdfListView commitEditing];
- [self.pdfListView endOfEditing];
- }
- [super enterPDFAddFile];
- }
- - (void)setTitleRefresh {
- if (CToolModelEdit == self.pdfListView.toolModel) {
- [self enterEditMode];
- } else if (CToolModelViewer == self.pdfListView.toolModel) {
- [self enterViewerMode];
- }
- }
- #pragma mark - CPDFViewDelegate
- - (void)PDFListViewCustomMenuClick:(CPDFListView *)pdfView identifier:(NSString *)menuIdentifier point:(CGPoint)point {
- if([menuIdentifier isEqualToString:@"PropertyItem"]) {
- self.menuPoint = point;
- self.editMode = CPDFEditModeText;
- [self showMenuList];
- [self.toolBar updateButtonState];
- }
- }
- - (void)PDFViewCurrentPageDidChanged:(CPDFView *)pdfView {
- if([pdfView.editingArea isKindOfClass:[CPDFEditImageArea class]]) {
- self.editMode = CPDFEditModeImage;
- }else if([pdfView.editingArea isKindOfClass:[CPDFEditTextArea class]]) {
- self.editMode = CPDFEditModeText;
- }
-
- [self.toolBar updateButtonState];
- [super PDFViewCurrentPageDidChanged:pdfView];
- }
- - (void)PDFListViewPerformTouchEnded:(CPDFListView *)pdfView {
- CGFloat tPosY = 0;
- CGFloat tBottomY = 0;
- if(CToolModelViewer == self.pdfListView.toolModel) {
- 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)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];
- }
- }
- #pragma mark - CPDFViewDelegate
- - (void)PDFViewEditingSelectStateDidChanged:(CPDFView *)pdfView {
- if([pdfView.editingArea isKindOfClass:[CPDFEditImageArea class]]) {
- self.editMode = CPDFEditModeImage;
- }else if([pdfView.editingArea isKindOfClass:[CPDFEditTextArea class]]) {
- self.editMode = CPDFEditModeText;
- }
-
- [self.toolBar updateButtonState];
- }
- #pragma mark - CPDFToolsViewControllerDelegate
- - (void)CPDFToolsViewControllerDismiss:(CPDFToolsViewController *)viewController selectItemAtIndex:(CToolModel)selectIndex {
- if(CToolModelViewer == selectIndex) {
- //viewwer
- [self enterViewerMode];
- }else{
- //edit
- [self enterEditMode];
- }
- }
- #pragma mark - CPDFEditToolBarDelegate
- - (void)editClickInToolBar:(CPDFEditToolBar *)toolBar editMode:(CPDFEditMode)mode{
- self.editMode = mode;
- }
- - (void)undoDidClickInToolBar:(CPDFEditToolBar *)toolBar{
- [self.pdfListView editTextUndo];
- }
- - (void)redoDidClickInToolBar:(CPDFEditToolBar *)toolBar{
- [self.pdfListView editTextRedo];
- }
- - (void)propertyEditDidClickInToolBar:(CPDFEditToolBar *)toolBar{
- [self showMenuList];
- }
- - (void)showMenuList {
- _baseVC = [[CPDFEditViewController alloc] initWithPDFView:self.pdfListView];
- _baseVC.editMode = self.editMode;
-
- if((self.editMode == CPDFEditModeText || self.editMode == CPDFEditModeImage) && self.pdfListView.editStatus != CEditingSelectStateEmpty){
-
- AAPLCustomPresentationController *presentationController NS_VALID_UNTIL_END_OF_SCOPE;
-
- presentationController = [[AAPLCustomPresentationController alloc] initWithPresentedViewController:self.baseVC presentingViewController:self];
- self.baseVC.transitioningDelegate = presentationController;
-
- [self presentViewController:self.baseVC animated:YES completion:nil];
- }
-
- if (self.menuPoint.y < self.pdfListView.documentView.visibleSize.height/2) {
- [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];
- }
- }
- #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;
- }
- }
- #pragma mark - UIDocumentPickerDelegate
- - (void)documentPickerWasCancelled:(UIDocumentPickerViewController *)controller {
- [self enterEditMode];
- }
- @end
|