123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819 |
- //
- // CPDFAnnotationBar.m
- // compdfkit-tools
- //
- // 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 "CPDFAnnotationBar.h"
- #import "CPDFAnnotationBarButton.h"
- #import "CPDFNoteViewController.h"
- #import "CPDFHighlightViewController.h"
- #import "CPDFStrikeoutViewController.h"
- #import "CPDFSquigglyViewController.h"
- #import "CPDFInkViewController.h"
- #import "CPDFShapeCircleViewController.h"
- #import "CPDFShapeArrowViewController.h"
- #import "CPDFFreeTextViewController.h"
- #import "CPDFSignatureViewController.h"
- #import "CPDFLinkViewController.h"
- #import "CPDFInkTopToolBar.h"
- #import "CAnnotStyle.h"
- #import "CPDFListView.h"
- #import "CPDFListView+UndoManager.h"
- #import "CPDFListView+Private.h"
- #import "CAnnotationManage.h"
- #import "CPDFSignatureEditViewController.h"
- #import "CPDFStampViewController.h"
- #import "CSignatureManager.h"
- #import "CPDFColorUtils.h"
- #import "AAPLCustomPresentationController.h"
- #import <ComPDFKit/ComPDFKit.h>
- @interface CPDFAnnotationBar () <UINavigationControllerDelegate, UIImagePickerControllerDelegate, CPDFSignatureViewControllerDelegate, CPDFSignatureEditViewControllerDelegate, CPDFNoteViewControllerDelegate, CPDFShapeCircleViewControllerDelegate, CPDFHighlightViewControllerDelegate, CPDFUnderlineViewControllerDelegate, CPDFStrikeoutViewControllerDelegate, CPDFSquigglyViewControllerDelegate, CPDFInkTopToolBarDelegate, CPDFInkViewControllerDelegate, CPDFShapeArrowViewControllerDelegate, CPDFStampViewControllerDelegate,CPDFLinkViewControllerDelegate>
- @property (nonatomic, strong) UIScrollView *scrollView;
- @property (nonatomic, strong) NSArray *annotationBtns;
- @property (nonatomic, assign) NSInteger selectedIndex;
- @property (nonatomic, strong) UIView *propertiesBar;
- @property (nonatomic, strong) UIButton *propertiesBtn;
- @property (nonatomic, strong) UIButton *undoBtn;
- @property (nonatomic, strong) UIButton *redoBtn;
- @property (nonatomic, strong) CPDFListView *pdfListView;
- @property (nonatomic, strong) CAnnotationManage *annotManage;
- @property (nonatomic, strong) CPDFSignatureViewController *signatureVC;
- @end
- @implementation CPDFAnnotationBar
- #pragma mark - Initializers
- - (instancetype)initAnnotationManage:(CAnnotationManage *)annotationManage {
- if (self = [super init]) {
- self.annotManage = annotationManage;
-
- self.backgroundColor = [CPDFColorUtils CPDFViewControllerBackgroundColor];
-
- UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, 1)];
- line.backgroundColor = [UIColor colorWithRed:210.0/255.0 green:210.0/255.0 blue:210.0/255.0 alpha:1.0];
- line.autoresizingMask = UIViewAutoresizingFlexibleWidth;
- [self addSubview:line];
-
- self.selectedIndex = -1;
-
- self.pdfListView = annotationManage.pdfListView;
- [self initSubview];
-
- [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(annotationChangedNotification:) name:CPDFListViewActiveAnnotationsChangeNotification object:nil];
- [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(annotationsOperationChangeNotification:) name:CPDFListViewAnnotationsOperationChangeNotification object:nil];
-
- [[NSNotificationCenter defaultCenter] postNotificationName:CPDFListViewAnnotationsOperationChangeNotification object:annotationManage.pdfListView];
- }
- return self;
- }
- - (void)layoutSubviews {
- [super layoutSubviews];
- self.scrollView.frame = CGRectMake(0, 0, self.frame.size.width-self.propertiesBar.frame.size.width, self.frame.size.height);
- }
- #pragma mark - Public Methods
- - (void)reloadData {
- if(CPDFViewAnnotationModeNone == self.pdfListView.annotationMode) {
- if (self.selectedIndex >0 &&
- self.selectedIndex <= self.annotationBtns.count) {
- for (NSInteger i = 0; i< self.annotationBtns.count; i++) {
- CPDFAnnotationBarButton *button = [self.annotationBtns objectAtIndex:i];
- if(button.tag == self.selectedIndex) {
- button.backgroundColor = [UIColor clearColor];
- self.selectedIndex = -1;
- break;
- }
- }
- }
- } else {
- for (NSInteger i = 0; i< self.annotationBtns.count; i++) {
- UIButton *button = [self.annotationBtns objectAtIndex:i];
- if(button.tag == self.pdfListView.annotationMode) {
- button.backgroundColor = [CPDFColorUtils CAnnotationBarSelectBackgroundColor];
- self.selectedIndex = button.tag;
- } else {
- button.backgroundColor = [UIColor clearColor];
- }
- }
- }
- }
- #pragma mark - Private Methods
- - (void)initSubview {
- _scrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
- _scrollView.showsVerticalScrollIndicator = NO;
- _scrollView.showsHorizontalScrollIndicator = NO;
- [self addSubview:_scrollView];
-
- CGFloat offsetX = 10.0;
- CGFloat buttonOffset = 15;
- CGFloat buttonSize = 44;
- CGFloat topOffset = (46 - buttonSize)/2;
- NSArray *images = @[@"CPDFAnnotationBarImageNote",
- @"CPDFAnnotationBarImageHighLight",
- @"CPDFAnnotationBarImageUnderline",
- @"CPDFAnnotationBarImageStrikeout",
- @"CPDFAnnotationBarImageSquiggly",
- @"CPDFAnnotationBarImageFreehand",
- @"CPDFAnnotationBarImagePencilDraw",
- @"CPDFAnnotationBarImageShapeCircle",
- @"CPDFAnnotationBarImageShapeRectangle",
- @"CPDFAnnotationBarImageShapeArrow",
- @"CPDFAnnotationBarImageShapeLine",
- @"CPDFAnnotationBarImageFreeText",
- @"CPDFAnnotationBarImageSignature",
- @"CPDFAnnotationBarImageStamp",
- @"CPDFAnnotationBarImageImage",
- @"CPDFAnnotationBarImageLink",
- @"CPDFAnnotationBarImageSound"];
-
- NSArray *types = @[@(CPDFViewAnnotationModeNote),
- @(CPDFViewAnnotationModeHighlight),
- @(CPDFViewAnnotationModeUnderline),
- @(CPDFViewAnnotationModeStrikeout),
- @(CPDFViewAnnotationModeSquiggly),
- @(CPDFViewAnnotationModeInk),
- @(CPDFViewAnnotationModePencilDrawing),
- @(CPDFViewAnnotationModeCircle),
- @(CPDFViewAnnotationModeSquare),
- @(CPDFViewAnnotationModeArrow),
- @(CPDFViewAnnotationModeLine),
- @(CPDFViewAnnotationModeFreeText),
- @(CPDFViewAnnotationModeSignature),
- @(CPDFViewAnnotationModeStamp),
- @(CPDFViewAnnotationModeImage),
- @(CPDFViewAnnotationModeLink),
- @(CPDFViewAnnotationModeSound)];
-
- NSMutableArray *annotationBtns = [NSMutableArray array];
- for (int i = 0; i < types.count; i++) {
- CPDFViewAnnotationMode annotationMode = (CPDFViewAnnotationMode)[types[i] integerValue];
- CPDFAnnotationBarButton *button = [CPDFAnnotationBarButton buttonWithType:UIButtonTypeCustom];
- button.frame = CGRectMake(offsetX, topOffset, buttonSize, buttonSize);
- [button setImage:[UIImage imageNamed:images[i] inBundle:[NSBundle bundleForClass:self.class] compatibleWithTraitCollection:nil] forState:UIControlStateNormal];
- button.tag = annotationMode;
- if (CPDFViewAnnotationModeHighlight == annotationMode) {
- button.lineColor = CAnnotationManage.highlightAnnotationColor;
- } else if (CPDFViewAnnotationModeUnderline == annotationMode) {
- button.lineColor = CAnnotationManage.underlineAnnotationColor;
- } else if (CPDFViewAnnotationModeStrikeout == annotationMode) {
- button.lineColor = CAnnotationManage.strikeoutAnnotationColor;
- } else if (CPDFViewAnnotationModeSquiggly == annotationMode) {
- button.lineColor = CAnnotationManage.squigglyAnnotationColor;
- } else if (CPDFViewAnnotationModeInk== annotationMode) {
- button.lineColor = CAnnotationManage.freehandAnnotationColor;
- }
- [button addTarget:self action:@selector(buttonItemClicked_Switch:) forControlEvents:UIControlEventTouchUpInside];
- [_scrollView addSubview:button];
- [annotationBtns addObject:button];
- if(i != types.count -1)
- offsetX += button.bounds.size.width + buttonOffset;
- else
- offsetX += button.bounds.size.width;
- }
- self.annotationBtns = annotationBtns;
-
- if (UIUserInterfaceIdiomPhone == UI_USER_INTERFACE_IDIOM()) {
- _scrollView.contentSize = CGSizeMake(offsetX, _scrollView.bounds.size.height);
- } else {
- _scrollView.contentSize = CGSizeMake(_scrollView.bounds.size.width, offsetX);
- }
- [self.scrollView bringSubviewToFront:self.propertiesBar];
-
- _propertiesBar = [[UIView alloc] initWithFrame:CGRectMake(self.bounds.size.width - 150, 0, 140, 44)];
- _propertiesBar.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
- [self addSubview:self.propertiesBar];
-
- CGFloat offset = 10;
- UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(offset, 12, 1, 20)];
- if (@available(iOS 13.0, *))
- lineView.backgroundColor = [UIColor labelColor];
- else
- lineView.backgroundColor = [UIColor blackColor];
-
- [self.propertiesBar addSubview:lineView];
- offset += lineView.frame.size.width;
-
- _propertiesBtn = [[UIButton alloc] initWithFrame:CGRectMake(offset, topOffset, buttonSize, buttonSize)];
- [_propertiesBtn setImage:[UIImage imageNamed:@"CPDFAnnotationBarImageProperties" inBundle:[NSBundle bundleForClass:self.class] compatibleWithTraitCollection:nil] forState:UIControlStateNormal];
- [_propertiesBtn addTarget:self action:@selector(buttonItemClicked_open:) forControlEvents:UIControlEventTouchUpInside];
- [self.propertiesBar addSubview:self.propertiesBtn];
- offset += self.propertiesBtn.frame.size.width;
- _undoBtn = [[UIButton alloc] initWithFrame:CGRectMake(offset, topOffset, buttonSize, buttonSize)];
- [_undoBtn setImage:[UIImage imageNamed:@"CPDFAnnotationBarImageUndo" inBundle:[NSBundle bundleForClass:self.class] compatibleWithTraitCollection:nil] forState:UIControlStateNormal];
- [_undoBtn addTarget:self action:@selector(buttonItemClicked_undo:) forControlEvents:UIControlEventTouchUpInside];
- [self.propertiesBar addSubview:self.undoBtn];
- offset += self.undoBtn.frame.size.width;
- _redoBtn = [[UIButton alloc] initWithFrame:CGRectMake(offset, topOffset, buttonSize, buttonSize)];
- [_redoBtn setImage:[UIImage imageNamed:@"CPDFAnnotationBarImageRedo" inBundle:[NSBundle bundleForClass:self.class] compatibleWithTraitCollection:nil] forState:UIControlStateNormal];
- [_redoBtn addTarget:self action:@selector(buttonItemClicked_redo:) forControlEvents:UIControlEventTouchUpInside];
- [self.propertiesBar addSubview:self.redoBtn];
- }
- - (void)updatePropertiesButtonState {
- if(CPDFViewAnnotationModeNone != self.pdfListView.annotationMode || self.annotManage.pdfListView.activeAnnotations.count > 0) {
- self.propertiesBtn.enabled = YES;
- } else {
- self.propertiesBtn.enabled = NO;
- }
- }
- #pragma mark - Action
- - (void)buttonItemClicked_Switch:(UIButton *)button {
- self.selectedIndex = button.tag;
- BOOL isSelect = YES;
- if (self.pdfListView.annotationMode != self.selectedIndex) {
- self.propertiesBtn.enabled = YES;
- button.backgroundColor = [CPDFColorUtils CAnnotationBarSelectBackgroundColor];
- self.pdfListView.annotationMode = self.selectedIndex;
-
- [self.annotManage setAnnotStyleFromMode:self.selectedIndex];
- isSelect = YES;
- } else {
- self.propertiesBtn.enabled = NO;
- self.pdfListView.annotationMode = CPDFViewAnnotationModeNone;
- self.selectedIndex = -1;
- button.backgroundColor = [UIColor clearColor];
- isSelect = NO;
- }
-
- [self updatePropertiesButtonState];
-
- [self createPropertyViewController];
-
- if([self.delegate respondsToSelector:@selector(annotationBarClick:clickAnnotationMode:forSelected:forButton:)]) {
- [self.delegate annotationBarClick:self clickAnnotationMode:(CPDFViewAnnotationMode)button.tag forSelected:isSelect forButton:button];
- }
- }
- - (void)buttonItemClicked_open:(UIButton *)button {
- CAnnotStyle *annotStytle = self.annotManage.annotStyle;
- AAPLCustomPresentationController *presentationController NS_VALID_UNTIL_END_OF_SCOPE;
- switch (annotStytle.annotMode) {
- case CPDFToolbarNote:
- {
- CPDFNoteViewController *noteVC = [[CPDFNoteViewController alloc] initWithStyle:annotStytle];
- noteVC.delegate = self;
- presentationController = [[AAPLCustomPresentationController alloc] initWithPresentedViewController:noteVC presentingViewController:self.parentVC];
- noteVC.transitioningDelegate = presentationController;
- [self.parentVC presentViewController:noteVC animated:YES completion:nil];
- }
- break;
- case CPDFToolbarHighlight:
- {
- CPDFHighlightViewController *highlightVC = [[CPDFHighlightViewController alloc] initWithStyle:annotStytle];
- highlightVC.delegate = self;
- presentationController = [[AAPLCustomPresentationController alloc] initWithPresentedViewController:highlightVC presentingViewController:self.parentVC];
- highlightVC.transitioningDelegate = presentationController;
- [self.parentVC presentViewController:highlightVC animated:YES completion:nil];
- }
- break;
- case CPDFToolbarUnderline:
- {
- CPDFUnderlineViewController *underlineVC = [[CPDFUnderlineViewController alloc] initWithStyle:annotStytle];
- underlineVC.delegate = self;
- presentationController = [[AAPLCustomPresentationController alloc] initWithPresentedViewController:underlineVC presentingViewController:self.parentVC];
- underlineVC.transitioningDelegate = presentationController;
- [self.parentVC presentViewController:underlineVC animated:YES completion:nil];
- }
- break;
- case CPDFToolbarStrikeout:
- {
- CPDFStrikeoutViewController *strikeoutVC = [[CPDFStrikeoutViewController alloc] initWithStyle:annotStytle];
- strikeoutVC.delegate = self;
- presentationController = [[AAPLCustomPresentationController alloc] initWithPresentedViewController:strikeoutVC presentingViewController:self.parentVC];
- strikeoutVC.transitioningDelegate = presentationController;
- [self.parentVC presentViewController:strikeoutVC animated:YES completion:nil];
- }
- break;
- case CPDFToolbarSquiggly:
- {
- CPDFSquigglyViewController *squigglyVC = [[CPDFSquigglyViewController alloc] initWithStyle:annotStytle];
- squigglyVC.delegate = self;
- presentationController = [[AAPLCustomPresentationController alloc] initWithPresentedViewController:squigglyVC presentingViewController:self.parentVC];
- squigglyVC.transitioningDelegate = presentationController;
- [self.parentVC presentViewController:squigglyVC animated:YES completion:nil];
- }
- break;
- case CPDFToolbarFreehand:
- {
- CPDFInkViewController *inkVC = [[CPDFInkViewController alloc] initWithStyle:annotStytle];
- inkVC.delegate = self;
- presentationController = [[AAPLCustomPresentationController alloc] initWithPresentedViewController:inkVC presentingViewController:self.parentVC];
- inkVC.transitioningDelegate = presentationController;
- [self.parentVC presentViewController:inkVC animated:YES completion:nil];
- }
- break;
- case CPDFToolbarShapeCircle:
- {
- CPDFShapeCircleViewController *circleVC = [[CPDFShapeCircleViewController alloc] initWithStyle:annotStytle];
- circleVC.delegate = self;
- presentationController = [[AAPLCustomPresentationController alloc] initWithPresentedViewController:circleVC presentingViewController:self.parentVC];
- circleVC.transitioningDelegate = presentationController;
- [self.parentVC presentViewController:circleVC animated:YES completion:nil];
- }
- break;
- case CPDFToolbarShapeRectangle:
- {
- CPDFShapeCircleViewController *squareVC = [[CPDFShapeCircleViewController alloc] initWithStyle:annotStytle];
- squareVC.delegate = self;
-
- presentationController = [[AAPLCustomPresentationController alloc] initWithPresentedViewController:squareVC presentingViewController:self.parentVC];
- squareVC.transitioningDelegate = presentationController;
- [self.parentVC presentViewController:squareVC animated:YES completion:nil];
- }
- break;
- case CPDFToolbarShapeArrow:
- {
- CPDFShapeArrowViewController *arrowVC = [[CPDFShapeArrowViewController alloc] initWithStyle:annotStytle];
- arrowVC.lineDelegate = self;
- presentationController = [[AAPLCustomPresentationController alloc] initWithPresentedViewController:arrowVC presentingViewController:self.parentVC];
- arrowVC.transitioningDelegate = presentationController;
- [self.parentVC presentViewController:arrowVC animated:YES completion:nil];
- }
- break;
- case CPDFToolbarShapeLine:
- {
- CPDFShapeArrowViewController *lineVC = [[CPDFShapeArrowViewController alloc] initWithStyle:annotStytle];
- lineVC.lineDelegate = self;
- presentationController = [[AAPLCustomPresentationController alloc] initWithPresentedViewController:lineVC presentingViewController:self.parentVC];
- lineVC.transitioningDelegate = presentationController;
- [self.parentVC presentViewController:lineVC animated:YES completion:nil];
- }
- break;
- case CPDFToolbarFreeText:
- {
- CPDFFreeTextViewController *freeTextVC = [[CPDFFreeTextViewController alloc] initWithStyle:annotStytle];
- presentationController = [[AAPLCustomPresentationController alloc] initWithPresentedViewController:freeTextVC presentingViewController:self.parentVC];
- freeTextVC.pdfListView = self.annotManage.pdfListView;
- freeTextVC.transitioningDelegate = presentationController;
- [self.parentVC presentViewController:freeTextVC animated:YES completion:nil];
- }
- break;
- case CPDFToolbarLink:
- {
- CPDFLinkViewController *linkVC = [[CPDFLinkViewController alloc] initWithStyle:annotStytle];
- linkVC.delegate = self;
- presentationController = [[AAPLCustomPresentationController alloc] initWithPresentedViewController:linkVC presentingViewController:self.parentVC];
- linkVC.transitioningDelegate = presentationController;
- [self.parentVC presentViewController:linkVC animated:YES completion:nil];
- }
- break;
- default:
- break;
- }
- }
- - (void)buttonItemClicked_undo:(UIButton *)button {
- if(self.annotManage.pdfListView.undoPDFManager && [self.annotManage.pdfListView canUndo]) {
- [self.annotManage.pdfListView.undoPDFManager undo];
- }
- }
- - (void)buttonItemClicked_redo:(UIButton *)button {
- if(self.annotManage.pdfListView.undoPDFManager && [self.annotManage.pdfListView canRedo]) {
- [self.annotManage.pdfListView.undoPDFManager redo];
- }
- }
- #pragma mark - Private Methods
- - (void)createPropertyViewController {
- if (CPDFViewAnnotationModeInk == self.selectedIndex) {
- [CPDFKitConfig sharedInstance].enableFreehandPencilKit = NO;
- if (@available(iOS 11.0, *)) {
- self.topToolBar = [[CPDFInkTopToolBar alloc] initWithFrame:CGRectMake(50, self.window.safeAreaInsets.top, self.pdfListView.frame.size.width-100, 50)];
- self.topToolBar.delegate = self;
-
- self.topToolBar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
- [self.pdfListView addSubview:self.topToolBar];
- } else {
- self.topToolBar = [[CPDFInkTopToolBar alloc] initWithFrame:CGRectMake(50, 64, self.pdfListView.frame.size.width-100, 50)];
- self.topToolBar.delegate = self;
- self.topToolBar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
- [self.pdfListView addSubview:self.topToolBar];
- }
- } else if (CPDFViewAnnotationModeSignature == self.selectedIndex) {
- CAnnotStyle *annotStytle = self.annotManage.annotStyle;
- AAPLCustomPresentationController *presentationController NS_VALID_UNTIL_END_OF_SCOPE;
- self.signatureVC = [[CPDFSignatureViewController alloc] initWithStyle:annotStytle];
- presentationController = [[AAPLCustomPresentationController alloc] initWithPresentedViewController:self.signatureVC presentingViewController:self.parentVC];
- self.signatureVC.delegate = self;
- self.signatureVC.transitioningDelegate = presentationController;
- [self.parentVC presentViewController:self.signatureVC animated:YES completion:nil];
- } else if (CPDFViewAnnotationModeStamp == self.selectedIndex) {
- AAPLCustomPresentationController *presentationController NS_VALID_UNTIL_END_OF_SCOPE;
- CPDFStampViewController *stampVC = [[CPDFStampViewController alloc] init];
- presentationController = [[AAPLCustomPresentationController alloc] initWithPresentedViewController:self.signatureVC presentingViewController:self.parentVC];
- stampVC.delegate = self;
- stampVC.transitioningDelegate = presentationController;
- [self.parentVC presentViewController:stampVC animated:YES completion:nil];
- } else if (CPDFViewAnnotationModeImage == self.selectedIndex) {
- [self createImageAnnotaion];
- } else if (CPDFViewAnnotationModeLink == self.selectedIndex) {
- self.propertiesBtn.enabled = NO;
- } else if (CPDFViewAnnotationModeSound == self.selectedIndex) {
-
- } else if (CPDFViewAnnotationModePencilDrawing == self.selectedIndex) {
- if (@available(iOS 13.0, *)) {
- [CPDFKitConfig sharedInstance].enableFreehandPencilKit = YES;
- } else {
- [CPDFKitConfig sharedInstance].enableFreehandPencilKit = NO;
- }
- }
- }
- - (void)createImageAnnotaion {
- UIViewController *tRootViewControl = [UIApplication sharedApplication].keyWindow.rootViewController;
- if ([tRootViewControl presentedViewController]) {
- tRootViewControl = [tRootViewControl presentedViewController];
- }
- UIAlertAction *cameraAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Use Camera", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
- UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
- imagePickerController.delegate = self;
- imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
- [tRootViewControl presentViewController:imagePickerController animated:YES completion:nil];
- }];
-
- UIAlertAction *photoAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Photo Library", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
- UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
- imagePickerController.delegate = self;
- imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
- imagePickerController.allowsEditing = YES;
- imagePickerController.modalPresentationStyle = UIModalPresentationPopover;
- [tRootViewControl presentViewController:imagePickerController animated:YES completion:nil];
- }];
-
- UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Cancel", nil)
- style:UIAlertActionStyleCancel
- handler:^(UIAlertAction * _Nonnull action) {
- self.annotManage.pdfListView.annotationMode = CPDFViewAnnotationModeNone;
- [self reloadData];
- }];
-
- UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:nil
- message:nil
- preferredStyle:UIAlertControllerStyleActionSheet];
-
- [actionSheet addAction:cameraAction];
- [actionSheet addAction:photoAction];
- [actionSheet addAction:cancelAction];
- actionSheet.modalPresentationStyle = UIModalPresentationPopover;
- [tRootViewControl presentViewController:actionSheet animated:YES completion:nil];
- }
- #pragma mark - UIImagePickerControllerDelegate
- - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
- [picker dismissViewControllerAnimated:YES completion:nil];
- UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
-
- UIImageOrientation imageOrientation = image.imageOrientation;
- if (imageOrientation!=UIImageOrientationUp) {
- UIGraphicsBeginImageContext(image.size);
- [image drawInRect:CGRectMake(0, 0, image.size.width, image.size.height)];
- image = UIGraphicsGetImageFromCurrentImageContext();
- UIGraphicsEndImageContext();
- }
-
- NSData *imageData = UIImagePNGRepresentation(image);
- if (imageData == nil || [imageData length] <= 0) {
- return;
- }
- image = [UIImage imageWithData:imageData];
-
- const CGFloat colorMasking[6] = {222, 255, 222, 255, 222, 255};
- CGImageRef imageRef = CGImageCreateWithMaskingColors(image.CGImage, colorMasking);
- if (imageRef) {
- image = [UIImage imageWithCGImage:imageRef];
- CGImageRelease(imageRef);
- }
-
- CPDFStampAnnotation *annotation = [[CPDFStampAnnotation alloc] initWithDocument:self.annotManage.pdfListView.document image:image];
- self.annotManage.pdfListView.addAnnotation = annotation;
- }
- - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
- [picker dismissViewControllerAnimated:YES completion:nil];
- }
- #pragma mark - CPDFSignatureViewControllerDelegate
- - (void)signatureViewController:(CPDFSignatureViewController *)signatureViewController {
- CPDFSignatureEditViewController *editVC = [[CPDFSignatureEditViewController alloc] init];
- editVC.delegate = self;
- [self.parentVC.navigationController pushViewController:editVC animated:NO];
- }
- - (void)signatureViewController:(CPDFSignatureViewController *)signatureViewController image:(UIImage *)image {
- CPDFSignatureAnnotation *annotation = [[CPDFSignatureAnnotation alloc] initWithDocument:self.annotManage.pdfListView.document];
- [annotation setImage:image];
- [self.annotManage.pdfListView addAnnotation:annotation];
-
- self.pdfListView.annotationMode = CPDFViewAnnotationModeNone;
- self.annotManage.pdfListView.annotationMode = CPDFViewAnnotationModeNone;
- [self reloadData];
- }
- #pragma mark - CPDFSignatureEditViewControllerDelegate
- - (void)signatureEditViewController:(CPDFSignatureEditViewController *)signatureEditViewController image:(UIImage *)image{
- [[CSignatureManager sharedManager] addImageSignature:image];
- self.pdfListView.annotationMode = CPDFViewAnnotationModeNone;
- self.annotManage.pdfListView.annotationMode = CPDFViewAnnotationModeNone;
- [self reloadData];
- [self.signatureVC.tableView reloadData];
- }
- #pragma mark - CPDFNoteViewControllerDelegate
- - (void)noteViewController:(CPDFNoteViewController *)noteViewController annotSytle:(CAnnotStyle *)annotStyle {
- if (annotStyle.isSelectAnnot) {
- [self.annotManage refreshPageWithAnnotations:annotStyle.annotations];
- }
- }
- #pragma mark - CPDFShapeCircleViewControllerDelegate
- - (void)circleViewController:(CPDFShapeCircleViewController *)circleViewController annotStyle:(CAnnotStyle *)annotStyle {
- if (annotStyle.isSelectAnnot) {
- [self.annotManage refreshPageWithAnnotations:annotStyle.annotations];
- }
- }
- #pragma mark - CPDFHighlightViewControllerDelegate
- - (void)highlightViewController:(CPDFHighlightViewController *)highlightViewController annotStyle:(CAnnotStyle *)annotStyle {
- if (annotStyle.isSelectAnnot) {
- [self.annotManage refreshPageWithAnnotations:annotStyle.annotations];
- } else {
- for (CPDFAnnotationBarButton *button in self.annotationBtns) {
- if(CPDFViewAnnotationModeHighlight == button.tag) {
- button.lineColor = CAnnotationManage.highlightAnnotationColor;
- [button setNeedsDisplay];
- break;
- }
- }
- }
- }
- #pragma mark - CPDFUnderlineViewControllerDelegate
- - (void)underlineViewController:(CPDFUnderlineViewController *)underlineViewController annotStyle:(CAnnotStyle *)annotStyle {
- if (annotStyle.isSelectAnnot) {
- [self.annotManage refreshPageWithAnnotations:annotStyle.annotations];
- } else {
- for (CPDFAnnotationBarButton *button in self.annotationBtns) {
- if (CPDFViewAnnotationModeUnderline == button.tag) {
- button.lineColor = CAnnotationManage.underlineAnnotationColor;
- [button setNeedsDisplay];
- break;
- }
- }
- }
- }
- #pragma mark - CPDFStrikeoutViewControllerDelegate
- - (void)strikeoutViewController:(CPDFStrikeoutViewController *)strikeoutViewController annotStyle:(CAnnotStyle *)annotStyle {
- if (annotStyle.isSelectAnnot) {
- [self.annotManage refreshPageWithAnnotations:annotStyle.annotations];
- } else {
- for (CPDFAnnotationBarButton *button in self.annotationBtns) {
- if (CPDFViewAnnotationModeStrikeout == button.tag) {
- button.lineColor = CAnnotationManage.strikeoutAnnotationColor;
- [button setNeedsDisplay];
- break;
- }
- }
- }
- }
- #pragma mark - CPDFSquigglyViewControllerDelegate
- - (void)squigglyViewController:(CPDFSquigglyViewController *)squigglyViewController annotStyle:(CAnnotStyle *)annotStyle {
- if (annotStyle.isSelectAnnot) {
- [self.annotManage refreshPageWithAnnotations:annotStyle.annotations];
- } else {
- for (CPDFAnnotationBarButton *button in self.annotationBtns) {
- if (CPDFViewAnnotationModeSquiggly == button.tag) {
- button.lineColor = CAnnotationManage.squigglyAnnotationColor;
- [button setNeedsDisplay];
- break;
- }
- }
- }
- }
- #pragma mark - CPDFAnnotationBarDelegate
- - (void)inkTopToolBar:(CPDFInkTopToolBar *)inkTopToolBar tag:(CPDFInkTopToolBarSelect)tag isSelect:(BOOL)isSelect {
- UIButton *inkButton = nil;
- for (CPDFAnnotationBarButton *button in self.annotationBtns) {
- if (CPDFViewAnnotationModeInk == button.tag) {
- inkButton = button;
- break;
- }
- }
- switch (tag) {
- case CPDFInkTopToolBarSetting: {
- CAnnotStyle *annotStytle = self.annotManage.annotStyle;
- AAPLCustomPresentationController *presentationController NS_VALID_UNTIL_END_OF_SCOPE;
- CPDFInkViewController *inkVC = [[CPDFInkViewController alloc] initWithStyle:annotStytle];
- inkVC.delegate = self;
- presentationController = [[AAPLCustomPresentationController alloc] initWithPresentedViewController:inkVC presentingViewController:self.parentVC];
- inkVC.transitioningDelegate = presentationController;
- [self.parentVC presentViewController:inkVC animated:YES completion:nil];
- }
- break;
- case CPDFInkTopToolBarErase: {
- [self.annotManage.pdfListView setDrawErasing:isSelect];
- }
- break;
- case CPDFInkTopToolBarUndo: {
- [self.annotManage.pdfListView drawUndo];
- }
- break;
- case CPDFInkTopToolBarRedo: {
- [self.annotManage.pdfListView drawRedo];
- }
- break;
- case CPDFInkTopToolBarClear: {
- if([self.delegate respondsToSelector:@selector(annotationBarClick:clickAnnotationMode:forSelected:forButton:)]) {
- [self.delegate annotationBarClick:self clickAnnotationMode:CPDFViewAnnotationModeInk forSelected:NO forButton:inkButton];
- }
- self.annotManage.pdfListView.annotationMode = CPDFViewAnnotationModeNone;
- [self reloadData];
- }
- break;
- case CPDFInkTopToolBarSave: {
- if([self.delegate respondsToSelector:@selector(annotationBarClick:clickAnnotationMode:forSelected:forButton:)]) {
- [self.delegate annotationBarClick:self clickAnnotationMode:CPDFViewAnnotationModeInk forSelected:NO forButton:inkButton];
- }
- [self.annotManage.pdfListView commitDrawing];
- self.annotManage.pdfListView.annotationMode = CPDFViewAnnotationModeNone;
- }
- [self reloadData];
-
- default:
- break;
- }
- }
- #pragma mark - CPDFInkViewControllerDelegate
- - (void)inkViewController:(CPDFInkViewController *)inkViewController annotStyle:(CAnnotStyle *)annotStyle {
- if (annotStyle.isSelectAnnot) {
- [self.annotManage refreshPageWithAnnotations:annotStyle.annotations];
- } else {
- for (CPDFAnnotationBarButton *button in self.annotationBtns) {
- if (CPDFViewAnnotationModeInk == button.tag) {
- button.lineColor = CAnnotationManage.freehandAnnotationColor;
- [button setNeedsDisplay];
- break;
- }
- }
- }
- }
- #pragma mark - CPDFShapeArrowViewControllerDelegate
- - (void)arrowViewController:(CPDFShapeArrowViewController *)arrowViewController annotStyle:(CAnnotStyle *)annotStyle {
- if (annotStyle.isSelectAnnot) {
- [self.annotManage refreshPageWithAnnotations:annotStyle.annotations];
- } else {
-
- }
- }
- #pragma mark - CPDFLinkViewControllerDelegate
- - (void)linkViewController:(CPDFLinkViewController *)linkViewController linkType:(CPDFLinkType)linkType linkString:(NSString *)linkString {
- CPDFLinkAnnotation *linkAnnotation = linkViewController.annotStyle.annotations.firstObject;
-
- if (CPDFLinkTypeLink == linkType || CPDFLinkTypeEmail == linkType) {
- linkAnnotation.URL = linkString;
- } else if (CPDFLinkTypePage == linkType) {
- linkAnnotation.destination = [[CPDFDestination alloc] initWithDocument:self.pdfListView.document
- pageIndex:[linkString integerValue]-1
- atPoint:CGPointZero
- zoom:1];
- }
- [self.pdfListView updateActiveAnnotations:@[]];
- [self.pdfListView setNeedsDisplayForPage:linkAnnotation.page];
- }
- #pragma mark - NSNotification
- - (void)annotationsOperationChangeNotification:(NSNotification *)notification {
- CPDFListView *pdfListView = notification.object;
- if(pdfListView == self.annotManage.pdfListView) {
- if([pdfListView canUndo])
- self.undoBtn.enabled = YES;
- else
- self.undoBtn.enabled = NO;
-
- if([pdfListView canRedo])
- self.redoBtn.enabled = YES;
- else
- self.redoBtn.enabled = NO;
- }
- }
- - (void)annotationChangedNotification:(NSNotification *)notification {
- if (self.pdfListView.activeAnnotations.firstObject) {
- [self.annotManage setAnnotStyleFromAnnotations:self.pdfListView.activeAnnotations];
- }
- [self updatePropertiesButtonState];
- }
- #pragma mark - CPDFStampViewControllerDelegate
- - (void)stampViewController:(CPDFStampViewController *)stampViewController selectedIndex:(NSInteger)selectedIndex stamp:(NSDictionary *)stamp {
- if (selectedIndex == -1) {
- self.annotManage.pdfListView.annotationMode = CPDFViewAnnotationModeNone;
- [self reloadData];
- } else {
- if (stamp.count > 0) {
- if (stamp[PDFAnnotationStampKeyImagePath]) {
- UIImage *image = [UIImage imageWithContentsOfFile:stamp[PDFAnnotationStampKeyImagePath]];
- CPDFStampAnnotation *annotation = [[CPDFStampAnnotation alloc] initWithDocument:self.annotManage.pdfListView.document image:image];
- self.annotManage.pdfListView.addAnnotation = annotation;
- } else {
- NSString *stampText = stamp[PDFAnnotationStampKeyText];
- BOOL stampShowDate = [stamp[PDFAnnotationStampKeyShowDate] boolValue];
- BOOL stampShowTime = [stamp[PDFAnnotationStampKeyShowTime] boolValue];
- CPDFStampStyle stampStyle = [stamp[PDFAnnotationStampKeyStyle] integerValue];
- CPDFStampShape stampShape = [stamp[PDFAnnotationStampKeyShape] integerValue];
-
- NSString *detailText = nil;
- NSTimeZone *timename = [NSTimeZone systemTimeZone];
- NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
- [outputFormatter setTimeZone:timename];
- if (stampShowDate && !stampShowTime){
- detailText = [NSDateFormatter localizedStringFromDate:[NSDate date]
- dateStyle:NSDateFormatterShortStyle
- timeStyle:NSDateFormatterNoStyle];
- } else if (stampShowTime && !stampShowDate) {
- [outputFormatter setDateFormat:@"HH:mm:ss"];
- detailText = [outputFormatter stringFromDate:[NSDate date]];
- } else if (stampShowDate && stampShowTime) {
- [outputFormatter setDateFormat:@" HH:mm"];
- detailText = [NSDateFormatter localizedStringFromDate:[NSDate date]
- dateStyle:NSDateFormatterShortStyle
- timeStyle:NSDateFormatterNoStyle];
- detailText = [detailText stringByAppendingString:[outputFormatter stringFromDate:[NSDate date]]];
- }
-
- CPDFStampAnnotation *annotation = [[CPDFStampAnnotation alloc] initWithDocument:self.annotManage.pdfListView.document text:stampText detailText:detailText style:stampStyle shape:stampShape];
- self.annotManage.pdfListView.addAnnotation = annotation;
- }
- } else {
- CPDFStampAnnotation *annotation = [[CPDFStampAnnotation alloc] initWithDocument:self.annotManage.pdfListView.document type:selectedIndex+1];
- self.annotManage.pdfListView.addAnnotation = annotation;
- }
- }
- }
- @end
|