CPDFAnnotationToolBar.m 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077
  1. //
  2. // CPDFAnnotationToolBar.m
  3. // ComPDFKit_Tools
  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 "CPDFAnnotationToolBar.h"
  13. #import "CPDFAnnotationBarButton.h"
  14. #import "CPDFNoteViewController.h"
  15. #import "CPDFHighlightViewController.h"
  16. #import "CPDFStrikeoutViewController.h"
  17. #import "CPDFSquigglyViewController.h"
  18. #import "CPDFInkViewController.h"
  19. #import "CPDFShapeCircleViewController.h"
  20. #import "CPDFShapeArrowViewController.h"
  21. #import "CPDFFreeTextViewController.h"
  22. #import "CPDFSignatureViewController.h"
  23. #import "CPDFLinkViewController.h"
  24. #import "CPDFInkTopToolBar.h"
  25. #import "CAnnotStyle.h"
  26. #import "CPDFListView.h"
  27. #import "CPDFListView+UndoManager.h"
  28. #import "CPDFListView+Private.h"
  29. #import "CAnnotationManage.h"
  30. #import "CPDFSignatureEditViewController.h"
  31. #import "CPDFStampViewController.h"
  32. #import "CSignatureManager.h"
  33. #import "CPDFColorUtils.h"
  34. #import "AAPLCustomPresentationController.h"
  35. #import <ComPDFKit/ComPDFKit.h>
  36. @interface CPDFAnnotationToolBar () <UINavigationControllerDelegate, UIImagePickerControllerDelegate, CPDFSignatureViewControllerDelegate, CPDFSignatureEditViewControllerDelegate, CPDFNoteViewControllerDelegate, CPDFShapeCircleViewControllerDelegate, CPDFHighlightViewControllerDelegate, CPDFUnderlineViewControllerDelegate, CPDFStrikeoutViewControllerDelegate, CPDFSquigglyViewControllerDelegate, CPDFInkTopToolBarDelegate, CPDFInkViewControllerDelegate, CPDFShapeArrowViewControllerDelegate, CPDFStampViewControllerDelegate,CPDFLinkViewControllerDelegate, CPDFFreeTextViewControllerDelegate,CPDFDrawPencilViewDelegate,AAPLCustomPresentationControllerDelegate>
  37. @property (nonatomic, strong) UIScrollView *scrollView;
  38. @property (nonatomic, strong) NSArray *annotationBtns;
  39. @property (nonatomic, assign) NSInteger selectedIndex;
  40. @property (nonatomic, strong) UIView *propertiesBar;
  41. @property (nonatomic, strong) UIButton *propertiesBtn;
  42. @property (nonatomic, strong) UIButton *undoBtn;
  43. @property (nonatomic, strong) UIButton *redoBtn;
  44. @property (nonatomic, strong) CPDFListView *pdfListView;
  45. @property (nonatomic, strong) CAnnotationManage *annotManage;
  46. @property (nonatomic, strong) CPDFSignatureViewController *signatureVC;
  47. @property (nonatomic, assign) CGPoint menuPoint;
  48. @property (nonatomic, strong) CPDFPage * menuPage;
  49. @property (nonatomic, assign) BOOL isAddAnnotation;
  50. @property (nonatomic, strong) CPDFSignatureWidgetAnnotation * signatureAnnotation;
  51. @property (nonatomic, strong) CPDFLinkViewController *linkVC;
  52. @end
  53. @implementation CPDFAnnotationToolBar
  54. #pragma mark - Initializers
  55. - (instancetype)initAnnotationManage:(CAnnotationManage *)annotationManage {
  56. if (self = [super init]) {
  57. self.annotManage = annotationManage;
  58. self.backgroundColor = [CPDFColorUtils CPDFViewControllerBackgroundColor];
  59. UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, 1)];
  60. line.backgroundColor = [UIColor colorWithRed:210.0/255.0 green:210.0/255.0 blue:210.0/255.0 alpha:1.0];
  61. line.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  62. [self addSubview:line];
  63. self.selectedIndex = 0;
  64. self.pdfListView = annotationManage.pdfListView;
  65. [self initSubview];
  66. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(annotationChangedNotification:) name:CPDFListViewActiveAnnotationsChangeNotification object:nil];
  67. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(annotationsOperationChangeNotification:) name:CPDFListViewAnnotationsOperationChangeNotification object:nil];
  68. [[NSNotificationCenter defaultCenter] postNotificationName:CPDFListViewAnnotationsOperationChangeNotification object:annotationManage.pdfListView];
  69. }
  70. return self;
  71. }
  72. - (void)layoutSubviews {
  73. [super layoutSubviews];
  74. self.scrollView.frame = CGRectMake(0, 0, self.frame.size.width-self.propertiesBar.frame.size.width, self.frame.size.height);
  75. if (@available(iOS 11.0, *)) {
  76. self.topToolBar.frame = CGRectMake(self.pdfListView.frame.size.width-30-self.topToolBar.frame.size.width, self.window.safeAreaInsets.top, self.topToolBar.frame.size.width, 50);
  77. } else {
  78. self.topToolBar.frame = CGRectMake(self.pdfListView.frame.size.width-30-self.topToolBar.frame.size.width, 64, self.topToolBar.frame.size.width, 50);
  79. }
  80. }
  81. #pragma mark - Public Methods
  82. - (void)reloadData {
  83. if(CPDFViewAnnotationModeNone == self.pdfListView.annotationMode) {
  84. if (self.selectedIndex >0) {
  85. for (NSInteger i = 0; i< self.annotationBtns.count; i++) {
  86. CPDFAnnotationBarButton *button = [self.annotationBtns objectAtIndex:i];
  87. if(button.tag == self.selectedIndex) {
  88. button.backgroundColor = [UIColor clearColor];
  89. self.selectedIndex = 0;
  90. break;
  91. }
  92. }
  93. }
  94. } else {
  95. for (NSInteger i = 0; i< self.annotationBtns.count; i++) {
  96. UIButton *button = [self.annotationBtns objectAtIndex:i];
  97. if(button.tag == self.pdfListView.annotationMode) {
  98. button.backgroundColor = [CPDFColorUtils CAnnotationBarSelectBackgroundColor];
  99. self.selectedIndex = button.tag;
  100. } else {
  101. button.backgroundColor = [UIColor clearColor];
  102. }
  103. }
  104. }
  105. }
  106. - (void)openSignatureAnnotation:(CPDFSignatureWidgetAnnotation *)signatureAnnotation {
  107. self.signatureAnnotation = signatureAnnotation;
  108. CAnnotStyle *annotStytle = self.annotManage.annotStyle;
  109. AAPLCustomPresentationController *presentationController NS_VALID_UNTIL_END_OF_SCOPE;
  110. self.signatureVC = [[CPDFSignatureViewController alloc] initWithStyle:annotStytle];
  111. presentationController = [[AAPLCustomPresentationController alloc] initWithPresentedViewController:self.signatureVC presentingViewController:self.parentVC];
  112. self.signatureVC.delegate = self;
  113. self.signatureVC.transitioningDelegate = presentationController;
  114. [self.parentVC presentViewController:self.signatureVC animated:YES completion:nil];
  115. }
  116. - (void)addStampAnnotationWithPage:(CPDFPage *)page point:(CGPoint)point {
  117. self.isAddAnnotation = YES;
  118. self.menuPage = page;
  119. self.menuPoint = point;
  120. AAPLCustomPresentationController *presentationController NS_VALID_UNTIL_END_OF_SCOPE;
  121. CPDFStampViewController *stampVC = [[CPDFStampViewController alloc] init];
  122. presentationController = [[AAPLCustomPresentationController alloc] initWithPresentedViewController:stampVC presentingViewController:self.parentVC];
  123. presentationController.tapDelegate = self;
  124. stampVC.delegate = self;
  125. stampVC.transitioningDelegate = presentationController;
  126. [self.parentVC presentViewController:stampVC animated:YES completion:nil];
  127. }
  128. - (void)addImageAnnotationWithPage:(CPDFPage *)page point:(CGPoint)point {
  129. self.isAddAnnotation = YES;
  130. self.menuPage = page;
  131. self.menuPoint = point;
  132. [self createImageAnnotaion];
  133. }
  134. #pragma mark - Private Methods
  135. - (void)initSubview {
  136. _scrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
  137. _scrollView.showsVerticalScrollIndicator = NO;
  138. _scrollView.showsHorizontalScrollIndicator = NO;
  139. [self addSubview:_scrollView];
  140. CGFloat offsetX = 10.0;
  141. CGFloat buttonOffset = 25;
  142. CGFloat buttonSize = 30;
  143. CGFloat topOffset = (44 - buttonSize)/2;
  144. NSMutableArray *images = [NSMutableArray arrayWithArray:@[@"CPDFAnnotationBarImageNote",
  145. @"CPDFAnnotationBarImageHighLight",
  146. @"CPDFAnnotationBarImageUnderline",
  147. @"CPDFAnnotationBarImageStrikeout",
  148. @"CPDFAnnotationBarImageSquiggly",
  149. @"CPDFAnnotationBarImageFreehand",
  150. @"CPDFAnnotationBarImagePencilDraw",
  151. @"CPDFAnnotationBarImageShapeCircle",
  152. @"CPDFAnnotationBarImageShapeRectangle",
  153. @"CPDFAnnotationBarImageShapeArrow",
  154. @"CPDFAnnotationBarImageShapeLine",
  155. @"CPDFAnnotationBarImageFreeText",
  156. @"CPDFAnnotationBarImageSignature",
  157. @"CPDFAnnotationBarImageStamp",
  158. @"CPDFAnnotationBarImageImage",
  159. @"CPDFAnnotationBarImageLink",
  160. @"CPDFAnnotationBarImageSound"]];
  161. NSMutableArray *types = [NSMutableArray arrayWithArray:@[@(CPDFViewAnnotationModeNote),
  162. @(CPDFViewAnnotationModeHighlight),
  163. @(CPDFViewAnnotationModeUnderline),
  164. @(CPDFViewAnnotationModeStrikeout),
  165. @(CPDFViewAnnotationModeSquiggly),
  166. @(CPDFViewAnnotationModeInk),
  167. @(CPDFViewAnnotationModePencilDrawing),
  168. @(CPDFViewAnnotationModeCircle),
  169. @(CPDFViewAnnotationModeSquare),
  170. @(CPDFViewAnnotationModeArrow),
  171. @(CPDFViewAnnotationModeLine),
  172. @(CPDFViewAnnotationModeFreeText),
  173. @(CPDFViewAnnotationModeSignature),
  174. @(CPDFViewAnnotationModeStamp),
  175. @(CPDFViewAnnotationModeImage),
  176. @(CPDFViewAnnotationModeLink),
  177. @(CPDFViewAnnotationModeSound)]];
  178. if (@available(iOS 13.0, *)) {
  179. } else {
  180. [images removeObject:@"CPDFAnnotationBarImagePencilDraw"];
  181. [types removeObject:@(CPDFViewAnnotationModePencilDrawing)];
  182. }
  183. NSMutableArray *annotationBtns = [NSMutableArray array];
  184. for (int i = 0; i < types.count; i++) {
  185. CPDFViewAnnotationMode annotationMode = (CPDFViewAnnotationMode)[types[i] integerValue];
  186. CPDFAnnotationBarButton *button = [CPDFAnnotationBarButton buttonWithType:UIButtonTypeCustom];
  187. button.frame = CGRectMake(offsetX, topOffset, buttonSize, buttonSize);
  188. [button setImage:[UIImage imageNamed:images[i] inBundle:[NSBundle bundleForClass:self.class] compatibleWithTraitCollection:nil] forState:UIControlStateNormal];
  189. button.layer.cornerRadius = 5.0;
  190. button.tag = annotationMode;
  191. if (CPDFViewAnnotationModeHighlight == annotationMode) {
  192. button.lineColor = CAnnotationManage.highlightAnnotationColor;
  193. } else if (CPDFViewAnnotationModeUnderline == annotationMode) {
  194. button.lineColor = CAnnotationManage.underlineAnnotationColor;
  195. } else if (CPDFViewAnnotationModeStrikeout == annotationMode) {
  196. button.lineColor = CAnnotationManage.strikeoutAnnotationColor;
  197. } else if (CPDFViewAnnotationModeSquiggly == annotationMode) {
  198. button.lineColor = CAnnotationManage.squigglyAnnotationColor;
  199. } else if (CPDFViewAnnotationModeInk== annotationMode) {
  200. button.lineColor = CAnnotationManage.freehandAnnotationColor;
  201. }
  202. [button addTarget:self action:@selector(buttonItemClicked_Switch:) forControlEvents:UIControlEventTouchUpInside];
  203. [_scrollView addSubview:button];
  204. [annotationBtns addObject:button];
  205. if(i != types.count -1)
  206. offsetX += button.bounds.size.width + buttonOffset;
  207. else
  208. offsetX += button.bounds.size.width + 10;
  209. }
  210. self.annotationBtns = annotationBtns;
  211. _scrollView.contentSize = CGSizeMake(offsetX, _scrollView.bounds.size.height);
  212. [self.scrollView bringSubviewToFront:self.propertiesBar];
  213. CGFloat offset = 10;
  214. CGFloat prWidth = buttonSize * 3 + offset;
  215. _propertiesBar = [[UIView alloc] initWithFrame:CGRectMake(self.bounds.size.width - prWidth, 0, prWidth, 44)];
  216. _propertiesBar.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
  217. [self addSubview:self.propertiesBar];
  218. UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(offset, 12, 1, 20)];
  219. if (@available(iOS 13.0, *)){
  220. if([UITraitCollection currentTraitCollection].userInterfaceStyle == UIUserInterfaceStyleDark)
  221. lineView.backgroundColor = [UIColor whiteColor];
  222. else
  223. lineView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.1];
  224. } else
  225. lineView.backgroundColor = [UIColor blackColor];
  226. [self.propertiesBar addSubview:lineView];
  227. offset += lineView.frame.size.width;
  228. _propertiesBtn = [[UIButton alloc] initWithFrame:CGRectMake(offset, topOffset, buttonSize, buttonSize)];
  229. [_propertiesBtn setImage:[UIImage imageNamed:@"CPDFAnnotationBarImageProperties" inBundle:[NSBundle bundleForClass:self.class] compatibleWithTraitCollection:nil] forState:UIControlStateNormal];
  230. [_propertiesBtn addTarget:self action:@selector(buttonItemClicked_openModel:) forControlEvents:UIControlEventTouchUpInside];
  231. [self.propertiesBar addSubview:self.propertiesBtn];
  232. offset += self.propertiesBtn.frame.size.width;
  233. _undoBtn = [[UIButton alloc] initWithFrame:CGRectMake(offset, topOffset, buttonSize, buttonSize)];
  234. [_undoBtn setImage:[UIImage imageNamed:@"CPDFAnnotationBarImageUndo" inBundle:[NSBundle bundleForClass:self.class] compatibleWithTraitCollection:nil] forState:UIControlStateNormal];
  235. [_undoBtn addTarget:self action:@selector(buttonItemClicked_undo:) forControlEvents:UIControlEventTouchUpInside];
  236. [self.propertiesBar addSubview:self.undoBtn];
  237. offset += self.undoBtn.frame.size.width;
  238. _redoBtn = [[UIButton alloc] initWithFrame:CGRectMake(offset, topOffset, buttonSize, buttonSize)];
  239. [_redoBtn setImage:[UIImage imageNamed:@"CPDFAnnotationBarImageRedo" inBundle:[NSBundle bundleForClass:self.class] compatibleWithTraitCollection:nil] forState:UIControlStateNormal];
  240. [_redoBtn addTarget:self action:@selector(buttonItemClicked_redo:) forControlEvents:UIControlEventTouchUpInside];
  241. [self.propertiesBar addSubview:self.redoBtn];
  242. [self updatePropertiesButtonState];
  243. }
  244. - (void)updatePropertiesButtonState {
  245. if(CPDFViewAnnotationModeNone != self.pdfListView.annotationMode) {
  246. CPDFAnnotation *annotation = self.annotManage.pdfListView.activeAnnotations.firstObject;
  247. if([annotation isKindOfClass:[CPDFStampAnnotation class]] || [annotation isKindOfClass:[CPDFSignatureAnnotation class]] || [annotation isKindOfClass:[CPDFSoundAnnotation class]] || (CPDFViewAnnotationModeSound == self.selectedIndex) || [annotation isKindOfClass:[CPDFLinkAnnotation class]] || (CPDFViewAnnotationModeLink == self.selectedIndex)) {
  248. self.propertiesBtn.enabled = NO;
  249. } else {
  250. self.propertiesBtn.enabled = YES;
  251. }
  252. } else {
  253. self.propertiesBtn.enabled = NO;
  254. }
  255. }
  256. - (void)updateUndoRedoState {
  257. self.undoBtn.enabled = NO;
  258. self.redoBtn.enabled = NO;
  259. [self.pdfListView registerAsObserver];
  260. }
  261. #pragma mark - Action
  262. - (void)buttonItemClicked_Switch:(UIButton *)button {
  263. if (CPDFViewAnnotationModeSound == self.selectedIndex) {
  264. [self.pdfListView stopRecord];
  265. }
  266. self.selectedIndex = button.tag;
  267. BOOL isSelect = YES;
  268. if (self.pdfListView.annotationMode != self.selectedIndex) {
  269. self.propertiesBtn.enabled = YES;
  270. button.backgroundColor = [CPDFColorUtils CAnnotationBarSelectBackgroundColor];
  271. if(CPDFViewAnnotationModeInk == self.selectedIndex) {
  272. [CPDFKitConfig sharedInstance].enableFreehandPencilKit = NO;
  273. } else if (CPDFViewAnnotationModePencilDrawing == self.selectedIndex) {
  274. [CPDFKitConfig sharedInstance].enableFreehandPencilKit = YES;
  275. }
  276. self.pdfListView.annotationMode = self.selectedIndex;
  277. isSelect = YES;
  278. } else {
  279. self.propertiesBtn.enabled = NO;
  280. if(CPDFViewAnnotationModeSound == self.selectedIndex) {
  281. [self.pdfListView stopRecord];
  282. } else if (CPDFViewAnnotationModeFreeText == self.selectedIndex) {
  283. [self.pdfListView commitEditAnnotationFreeText];
  284. }
  285. self.pdfListView.annotationMode = CPDFViewAnnotationModeNone;
  286. self.selectedIndex = 0;
  287. button.backgroundColor = [UIColor clearColor];
  288. isSelect = NO;
  289. }
  290. [self updatePropertiesButtonState];
  291. [self createPropertyViewController];
  292. if([self.delegate respondsToSelector:@selector(annotationBarClick:clickAnnotationMode:forSelected:forButton:)]) {
  293. [self.delegate annotationBarClick:self clickAnnotationMode:(CPDFViewAnnotationMode)button.tag forSelected:isSelect forButton:button];
  294. }
  295. }
  296. - (void)buttonItemClicked_openModel:(id)button {
  297. [self.annotManage setAnnotStyleFromMode:self.pdfListView.annotationMode];
  298. [self buttonItemClicked_open:button];
  299. }
  300. - (void)buttonItemClicked_openAnnotation:(id)button {
  301. [self.annotManage setAnnotStyleFromAnnotations:self.pdfListView.activeAnnotations];
  302. [self buttonItemClicked_open:button];
  303. }
  304. - (void)buttonItemClicked_open:(id)button {
  305. CAnnotStyle *annotStytle = self.annotManage.annotStyle;
  306. AAPLCustomPresentationController *presentationController NS_VALID_UNTIL_END_OF_SCOPE;
  307. switch (annotStytle.annotMode) {
  308. case CPDFToolbarNote:
  309. {
  310. CPDFNoteViewController *noteVC = [[CPDFNoteViewController alloc] initWithStyle:annotStytle];
  311. noteVC.delegate = self;
  312. presentationController = [[AAPLCustomPresentationController alloc] initWithPresentedViewController:noteVC presentingViewController:self.parentVC];
  313. noteVC.transitioningDelegate = presentationController;
  314. [self.parentVC presentViewController:noteVC animated:YES completion:nil];
  315. }
  316. break;
  317. case CPDFToolbarHighlight:
  318. {
  319. CPDFHighlightViewController *highlightVC = [[CPDFHighlightViewController alloc] initWithStyle:annotStytle];
  320. highlightVC.delegate = self;
  321. presentationController = [[AAPLCustomPresentationController alloc] initWithPresentedViewController:highlightVC presentingViewController:self.parentVC];
  322. highlightVC.transitioningDelegate = presentationController;
  323. [self.parentVC presentViewController:highlightVC animated:YES completion:nil];
  324. }
  325. break;
  326. case CPDFToolbarUnderline:
  327. {
  328. CPDFUnderlineViewController *underlineVC = [[CPDFUnderlineViewController alloc] initWithStyle:annotStytle];
  329. underlineVC.delegate = self;
  330. presentationController = [[AAPLCustomPresentationController alloc] initWithPresentedViewController:underlineVC presentingViewController:self.parentVC];
  331. underlineVC.transitioningDelegate = presentationController;
  332. [self.parentVC presentViewController:underlineVC animated:YES completion:nil];
  333. }
  334. break;
  335. case CPDFToolbarStrikeout:
  336. {
  337. CPDFStrikeoutViewController *strikeoutVC = [[CPDFStrikeoutViewController alloc] initWithStyle:annotStytle];
  338. strikeoutVC.delegate = self;
  339. presentationController = [[AAPLCustomPresentationController alloc] initWithPresentedViewController:strikeoutVC presentingViewController:self.parentVC];
  340. strikeoutVC.transitioningDelegate = presentationController;
  341. [self.parentVC presentViewController:strikeoutVC animated:YES completion:nil];
  342. }
  343. break;
  344. case CPDFToolbarSquiggly:
  345. {
  346. CPDFSquigglyViewController *squigglyVC = [[CPDFSquigglyViewController alloc] initWithStyle:annotStytle];
  347. squigglyVC.delegate = self;
  348. presentationController = [[AAPLCustomPresentationController alloc] initWithPresentedViewController:squigglyVC presentingViewController:self.parentVC];
  349. squigglyVC.transitioningDelegate = presentationController;
  350. [self.parentVC presentViewController:squigglyVC animated:YES completion:nil];
  351. }
  352. break;
  353. case CPDFToolbarFreehand:
  354. {
  355. CPDFInkViewController *inkVC = [[CPDFInkViewController alloc] initWithStyle:annotStytle];
  356. inkVC.delegate = self;
  357. presentationController = [[AAPLCustomPresentationController alloc] initWithPresentedViewController:inkVC presentingViewController:self.parentVC];
  358. inkVC.transitioningDelegate = presentationController;
  359. [self.parentVC presentViewController:inkVC animated:YES completion:nil];
  360. }
  361. break;
  362. case CPDFToolbarShapeCircle:
  363. {
  364. CPDFShapeCircleViewController *circleVC = [[CPDFShapeCircleViewController alloc] initWithStyle:annotStytle];
  365. circleVC.delegate = self;
  366. presentationController = [[AAPLCustomPresentationController alloc] initWithPresentedViewController:circleVC presentingViewController:self.parentVC];
  367. circleVC.transitioningDelegate = presentationController;
  368. [self.parentVC presentViewController:circleVC animated:YES completion:nil];
  369. }
  370. break;
  371. case CPDFToolbarShapeRectangle:
  372. {
  373. CPDFShapeCircleViewController *squareVC = [[CPDFShapeCircleViewController alloc] initWithStyle:annotStytle];
  374. squareVC.delegate = self;
  375. presentationController = [[AAPLCustomPresentationController alloc] initWithPresentedViewController:squareVC presentingViewController:self.parentVC];
  376. squareVC.transitioningDelegate = presentationController;
  377. [self.parentVC presentViewController:squareVC animated:YES completion:nil];
  378. }
  379. break;
  380. case CPDFToolbarShapeArrow:
  381. {
  382. CPDFShapeArrowViewController *arrowVC = [[CPDFShapeArrowViewController alloc] initWithStyle:annotStytle];
  383. arrowVC.lineDelegate = self;
  384. presentationController = [[AAPLCustomPresentationController alloc] initWithPresentedViewController:arrowVC presentingViewController:self.parentVC];
  385. arrowVC.transitioningDelegate = presentationController;
  386. [self.parentVC presentViewController:arrowVC animated:YES completion:nil];
  387. }
  388. break;
  389. case CPDFToolbarShapeLine:
  390. {
  391. CPDFShapeArrowViewController *lineVC = [[CPDFShapeArrowViewController alloc] initWithStyle:annotStytle];
  392. lineVC.lineDelegate = self;
  393. presentationController = [[AAPLCustomPresentationController alloc] initWithPresentedViewController:lineVC presentingViewController:self.parentVC];
  394. lineVC.transitioningDelegate = presentationController;
  395. [self.parentVC presentViewController:lineVC animated:YES completion:nil];
  396. }
  397. break;
  398. case CPDFToolbarFreeText:
  399. {
  400. CPDFFreeTextViewController *freeTextVC = [[CPDFFreeTextViewController alloc] initWithStyle:annotStytle];
  401. freeTextVC.delegate = self;
  402. presentationController = [[AAPLCustomPresentationController alloc] initWithPresentedViewController:freeTextVC presentingViewController:self.parentVC];
  403. freeTextVC.pdfListView = self.annotManage.pdfListView;
  404. freeTextVC.transitioningDelegate = presentationController;
  405. [self.parentVC presentViewController:freeTextVC animated:YES completion:nil];
  406. }
  407. break;
  408. case CPDFToolbarLink:
  409. {
  410. self.linkVC = [[CPDFLinkViewController alloc] initWithStyle:annotStytle];
  411. self.linkVC.pageCount = self.annotManage.pdfListView.document.pageCount;
  412. self.linkVC.delegate = self;
  413. presentationController = [[AAPLCustomPresentationController alloc] initWithPresentedViewController:self.linkVC presentingViewController:self.parentVC];
  414. presentationController.tapDelegate = self;
  415. self.linkVC.transitioningDelegate = presentationController;
  416. [self.parentVC presentViewController:self.linkVC animated:YES completion:nil];
  417. }
  418. break;
  419. default:
  420. break;
  421. }
  422. }
  423. - (void)buttonItemClicked_undo:(UIButton *)button {
  424. if(self.annotManage.pdfListView.undoPDFManager && [self.annotManage.pdfListView canUndo]) {
  425. [self.annotManage.pdfListView.undoPDFManager undo];
  426. }
  427. }
  428. - (void)buttonItemClicked_redo:(UIButton *)button {
  429. if(self.annotManage.pdfListView.undoPDFManager && [self.annotManage.pdfListView canRedo]) {
  430. [self.annotManage.pdfListView.undoPDFManager redo];
  431. }
  432. }
  433. #pragma mark - Private Methods
  434. - (void)createPropertyViewController {
  435. if (CPDFViewAnnotationModeInk == self.selectedIndex) {
  436. [self.annotManage setAnnotStyleFromMode:self.pdfListView.annotationMode];
  437. self.propertiesBtn.enabled = NO;
  438. if (@available(iOS 11.0, *)) {
  439. self.topToolBar = [[CPDFInkTopToolBar alloc] initWithFrame:CGRectMake(self.pdfListView.frame.size.width-30- 300, self.window.safeAreaInsets.top, 300, 50)];
  440. self.topToolBar.delegate = self;
  441. [self.pdfListView addSubview:self.topToolBar];
  442. } else {
  443. self.topToolBar = [[CPDFInkTopToolBar alloc] initWithFrame:CGRectMake(self.pdfListView.frame.size.width-30-300, 64, 300, 50)];
  444. self.topToolBar.delegate = self;
  445. [self.pdfListView addSubview:self.topToolBar];
  446. }
  447. } else if (CPDFViewAnnotationModeSignature == self.selectedIndex) {
  448. self.propertiesBtn.enabled = NO;
  449. CAnnotStyle *annotStytle = self.annotManage.annotStyle;
  450. AAPLCustomPresentationController *presentationController NS_VALID_UNTIL_END_OF_SCOPE;
  451. self.signatureVC = [[CPDFSignatureViewController alloc] initWithStyle:annotStytle];
  452. presentationController = [[AAPLCustomPresentationController alloc] initWithPresentedViewController:self.signatureVC presentingViewController:self.parentVC];
  453. presentationController.tapDelegate = self;
  454. self.signatureVC.delegate = self;
  455. self.signatureVC.transitioningDelegate = presentationController;
  456. [self.parentVC presentViewController:self.signatureVC animated:YES completion:nil];
  457. } else if (CPDFViewAnnotationModeStamp == self.selectedIndex) {
  458. self.propertiesBtn.enabled = NO;
  459. AAPLCustomPresentationController *presentationController NS_VALID_UNTIL_END_OF_SCOPE;
  460. CPDFStampViewController *stampVC = [[CPDFStampViewController alloc] init];
  461. presentationController = [[AAPLCustomPresentationController alloc] initWithPresentedViewController:stampVC presentingViewController:self.parentVC];
  462. presentationController.tapDelegate = self;
  463. stampVC.delegate = self;
  464. stampVC.transitioningDelegate = presentationController;
  465. [self.parentVC presentViewController:stampVC animated:YES completion:nil];
  466. } else if (CPDFViewAnnotationModeImage == self.selectedIndex) {
  467. self.propertiesBtn.enabled = NO;
  468. [self createImageAnnotaion];
  469. } else if (CPDFViewAnnotationModeLink == self.selectedIndex) {
  470. self.propertiesBtn.enabled = NO;
  471. } else if (CPDFViewAnnotationModeSound == self.selectedIndex) {
  472. } else if (CPDFViewAnnotationModePencilDrawing == self.selectedIndex) {
  473. self.propertiesBtn.enabled = NO;
  474. if (@available(iOS 13.0, *)) {
  475. float tWidth = 412.0;
  476. float tHeight = 60.0;
  477. self.drawPencilFuncView = [[CPDFDrawPencilKitFuncView alloc] initWithFrame:CGRectMake(self.pdfListView.frame.size.width - 30, self.window.safeAreaInsets.top, tWidth, tHeight)];
  478. self.drawPencilFuncView.delegate = self;
  479. self.drawPencilFuncView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin;
  480. [self.pdfListView addSubview:self.drawPencilFuncView];
  481. }
  482. }
  483. }
  484. - (void)createImageAnnotaion {
  485. UIViewController *tRootViewControl = [UIApplication sharedApplication].keyWindow.rootViewController;
  486. if ([tRootViewControl presentedViewController]) {
  487. tRootViewControl = [tRootViewControl presentedViewController];
  488. }
  489. UIAlertAction *cameraAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Camera", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  490. UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
  491. imagePickerController.delegate = self;
  492. imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
  493. [tRootViewControl presentViewController:imagePickerController animated:YES completion:nil];
  494. }];
  495. UIAlertAction *photoAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Choose from Album", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  496. UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
  497. imagePickerController.delegate = self;
  498. imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
  499. imagePickerController.allowsEditing = YES;
  500. imagePickerController.modalPresentationStyle = UIModalPresentationPopover;
  501. if (UIUserInterfaceIdiomPad == UI_USER_INTERFACE_IDIOM()) {
  502. imagePickerController.popoverPresentationController.sourceView = (UIButton *)self.annotationBtns[14];
  503. imagePickerController.popoverPresentationController.sourceRect = ((UIButton *)self.annotationBtns[14]).bounds;
  504. }
  505. [tRootViewControl presentViewController:imagePickerController animated:YES completion:nil];
  506. }];
  507. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Cancel", nil)
  508. style:UIAlertActionStyleCancel
  509. handler:^(UIAlertAction * _Nonnull action) {
  510. self.annotManage.pdfListView.annotationMode = CPDFViewAnnotationModeNone;
  511. [self reloadData];
  512. }];
  513. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil
  514. message:nil
  515. preferredStyle:UIAlertControllerStyleActionSheet];
  516. if (UIUserInterfaceIdiomPad == UI_USER_INTERFACE_IDIOM()) {
  517. alertController.popoverPresentationController.sourceView = (UIButton *)self.annotationBtns[14];
  518. alertController.popoverPresentationController.sourceRect = ((UIButton *)self.annotationBtns[14]).bounds;
  519. }
  520. [alertController addAction:cameraAction];
  521. [alertController addAction:photoAction];
  522. [alertController addAction:cancelAction];
  523. alertController.modalPresentationStyle = UIModalPresentationPopover;
  524. [tRootViewControl presentViewController:alertController animated:YES completion:nil];
  525. }
  526. #pragma mark - AAPLCustomPresentationControllerDelegate
  527. - (void)AAPLCustomPresentationControllerTap:(AAPLCustomPresentationController *)AAPLCustomPresentationController {
  528. CPDFAnnotation *annotation = self.annotManage.pdfListView.activeAnnotations.firstObject;
  529. if([annotation isKindOfClass:[CPDFLinkAnnotation class]]) {
  530. if (!self.linkVC.isLink) {
  531. [((CPDFAnnotation *)self.pdfListView.activeAnnotations.firstObject).page removeAnnotation:self.pdfListView.activeAnnotations.firstObject];
  532. [self.annotManage.pdfListView setNeedsDisplayForPage:self.annotManage.pdfListView.activeAnnotation.page];
  533. [self.annotManage.pdfListView updateActiveAnnotations:@[]];
  534. }
  535. } else if (CPDFViewAnnotationModeSignature == self.selectedIndex || CPDFViewAnnotationModeStamp == self.selectedIndex) {
  536. self.pdfListView.annotationMode = CPDFViewAnnotationModeNone;
  537. }
  538. }
  539. #pragma mark - UIImagePickerControllerDelegate
  540. - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
  541. [picker dismissViewControllerAnimated:YES completion:nil];
  542. UIImage *image;
  543. if ([info objectForKey:UIImagePickerControllerEditedImage]) {
  544. image = [info objectForKey:UIImagePickerControllerEditedImage];
  545. } else if ([info objectForKey:UIImagePickerControllerOriginalImage]) {
  546. image = [info objectForKey:UIImagePickerControllerOriginalImage];
  547. }
  548. UIImageOrientation imageOrientation = image.imageOrientation;
  549. if (imageOrientation!=UIImageOrientationUp) {
  550. UIGraphicsBeginImageContext(image.size);
  551. [image drawInRect:CGRectMake(0, 0, image.size.width, image.size.height)];
  552. image = UIGraphicsGetImageFromCurrentImageContext();
  553. UIGraphicsEndImageContext();
  554. }
  555. NSData *imageData = UIImagePNGRepresentation(image);
  556. if (imageData == nil || [imageData length] <= 0) {
  557. return;
  558. }
  559. image = [UIImage imageWithData:imageData];
  560. const CGFloat colorMasking[6] = {222, 255, 222, 255, 222, 255};
  561. CGImageRef imageRef = CGImageCreateWithMaskingColors(image.CGImage, colorMasking);
  562. if (imageRef) {
  563. image = [UIImage imageWithCGImage:imageRef];
  564. CGImageRelease(imageRef);
  565. }
  566. CPDFStampAnnotation *annotation = [[CPDFStampAnnotation alloc] initWithDocument:self.annotManage.pdfListView.document image:image];
  567. if(self.isAddAnnotation) {
  568. CGRect bounds = annotation.bounds;
  569. bounds.origin.x = self.menuPoint.x-bounds.size.width/2.0;
  570. bounds.origin.y = self.menuPoint.y-bounds.size.height/2.0;
  571. annotation.bounds = bounds;
  572. [self.pdfListView addAnnotation:annotation forPage:self.menuPage];
  573. self.isAddAnnotation = NO;
  574. self.menuPage = nil;
  575. self.menuPoint = CGPointZero;
  576. } else {
  577. self.annotManage.pdfListView.addAnnotation = annotation;
  578. }
  579. }
  580. - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
  581. [picker dismissViewControllerAnimated:YES completion:nil];
  582. }
  583. #pragma mark - CPDFSignatureViewControllerDelegate
  584. - (void)signatureViewControllerDismiss:(CPDFSignatureViewController *)signatureViewController {
  585. self.signatureAnnotation = nil;
  586. self.pdfListView.annotationMode = CPDFViewAnnotationModeNone;
  587. }
  588. - (void)signatureViewController:(CPDFSignatureViewController *)signatureViewController image:(UIImage *)image {
  589. if(self.signatureAnnotation) {
  590. [self.signatureAnnotation signWithImage:image];
  591. [self.pdfListView setNeedsDisplayForPage:self.signatureAnnotation.page];
  592. self.signatureAnnotation = nil;
  593. } else {
  594. CPDFSignatureAnnotation *annotation = [[CPDFSignatureAnnotation alloc] initWithDocument:self.annotManage.pdfListView.document];
  595. [annotation setImage:image];
  596. self.annotManage.pdfListView.addAnnotation = annotation;
  597. }
  598. }
  599. #pragma mark - CPDFNoteViewControllerDelegate
  600. - (void)noteViewController:(CPDFNoteViewController *)noteViewController annotSytle:(CAnnotStyle *)annotStyle {
  601. if (annotStyle.isSelectAnnot) {
  602. [self.annotManage refreshPageWithAnnotations:annotStyle.annotations];
  603. }
  604. }
  605. #pragma mark - CPDFShapeCircleViewControllerDelegate
  606. - (void)circleViewController:(CPDFShapeCircleViewController *)circleViewController annotStyle:(CAnnotStyle *)annotStyle {
  607. if (annotStyle.isSelectAnnot) {
  608. [self.annotManage refreshPageWithAnnotations:annotStyle.annotations];
  609. }
  610. }
  611. #pragma mark - CPDFHighlightViewControllerDelegate
  612. - (void)highlightViewController:(CPDFHighlightViewController *)highlightViewController annotStyle:(CAnnotStyle *)annotStyle {
  613. if (annotStyle.isSelectAnnot) {
  614. [self.annotManage refreshPageWithAnnotations:annotStyle.annotations];
  615. } else {
  616. for (CPDFAnnotationBarButton *button in self.annotationBtns) {
  617. if(CPDFViewAnnotationModeHighlight == button.tag) {
  618. button.lineColor = CAnnotationManage.highlightAnnotationColor;
  619. [button setNeedsDisplay];
  620. break;
  621. }
  622. }
  623. }
  624. }
  625. #pragma mark - CPDFUnderlineViewControllerDelegate
  626. - (void)underlineViewController:(CPDFUnderlineViewController *)underlineViewController annotStyle:(CAnnotStyle *)annotStyle {
  627. if (annotStyle.isSelectAnnot) {
  628. [self.annotManage refreshPageWithAnnotations:annotStyle.annotations];
  629. } else {
  630. for (CPDFAnnotationBarButton *button in self.annotationBtns) {
  631. if (CPDFViewAnnotationModeUnderline == button.tag) {
  632. button.lineColor = CAnnotationManage.underlineAnnotationColor;
  633. [button setNeedsDisplay];
  634. break;
  635. }
  636. }
  637. }
  638. }
  639. #pragma mark - CPDFStrikeoutViewControllerDelegate
  640. - (void)strikeoutViewController:(CPDFStrikeoutViewController *)strikeoutViewController annotStyle:(CAnnotStyle *)annotStyle {
  641. if (annotStyle.isSelectAnnot) {
  642. [self.annotManage refreshPageWithAnnotations:annotStyle.annotations];
  643. } else {
  644. for (CPDFAnnotationBarButton *button in self.annotationBtns) {
  645. if (CPDFViewAnnotationModeStrikeout == button.tag) {
  646. button.lineColor = CAnnotationManage.strikeoutAnnotationColor;
  647. [button setNeedsDisplay];
  648. break;
  649. }
  650. }
  651. }
  652. }
  653. #pragma mark - CPDFSquigglyViewControllerDelegate
  654. - (void)squigglyViewController:(CPDFSquigglyViewController *)squigglyViewController annotStyle:(CAnnotStyle *)annotStyle {
  655. if (annotStyle.isSelectAnnot) {
  656. [self.annotManage refreshPageWithAnnotations:annotStyle.annotations];
  657. } else {
  658. for (CPDFAnnotationBarButton *button in self.annotationBtns) {
  659. if (CPDFViewAnnotationModeSquiggly == button.tag) {
  660. button.lineColor = CAnnotationManage.squigglyAnnotationColor;
  661. [button setNeedsDisplay];
  662. break;
  663. }
  664. }
  665. }
  666. }
  667. #pragma mark - CPDFAnnotationBarDelegate
  668. - (void)inkTopToolBar:(CPDFInkTopToolBar *)inkTopToolBar tag:(CPDFInkTopToolBarSelect)tag isSelect:(BOOL)isSelect {
  669. UIButton *inkButton = nil;
  670. for (CPDFAnnotationBarButton *button in self.annotationBtns) {
  671. if (CPDFViewAnnotationModeInk == button.tag) {
  672. inkButton = button;
  673. break;
  674. }
  675. }
  676. switch (tag) {
  677. case CPDFInkTopToolBarSetting: {
  678. [self.pdfListView commitDrawing];
  679. CAnnotStyle *annotStytle = self.annotManage.annotStyle;
  680. AAPLCustomPresentationController *presentationController NS_VALID_UNTIL_END_OF_SCOPE;
  681. CPDFInkViewController *inkVC = [[CPDFInkViewController alloc] initWithStyle:annotStytle];
  682. inkVC.delegate = self;
  683. presentationController = [[AAPLCustomPresentationController alloc] initWithPresentedViewController:inkVC presentingViewController:self.parentVC];
  684. inkVC.transitioningDelegate = presentationController;
  685. [self.parentVC presentViewController:inkVC animated:YES completion:nil];
  686. }
  687. break;
  688. case CPDFInkTopToolBarErase: {
  689. [self.annotManage.pdfListView setDrawErasing:isSelect];
  690. }
  691. break;
  692. case CPDFInkTopToolBarUndo: {
  693. [self.annotManage.pdfListView drawUndo];
  694. if (((UIButton *)self.topToolBar.buttonArray[1]).selected) {
  695. ((UIButton *)self.topToolBar.buttonArray[1]).backgroundColor = [CPDFColorUtils CAnnotationBarSelectBackgroundColor];
  696. }
  697. }
  698. break;
  699. case CPDFInkTopToolBarRedo: {
  700. [self.annotManage.pdfListView drawRedo];
  701. if (((UIButton *)self.topToolBar.buttonArray[1]).selected) {
  702. ((UIButton *)self.topToolBar.buttonArray[1]).backgroundColor = [CPDFColorUtils CAnnotationBarSelectBackgroundColor];
  703. }
  704. }
  705. break;
  706. case CPDFInkTopToolBarClear: {
  707. if([self.delegate respondsToSelector:@selector(annotationBarClick:clickAnnotationMode:forSelected:forButton:)]) {
  708. [self.delegate annotationBarClick:self clickAnnotationMode:CPDFViewAnnotationModeInk forSelected:NO forButton:inkButton];
  709. }
  710. self.annotManage.pdfListView.annotationMode = CPDFViewAnnotationModeNone;
  711. [self reloadData];
  712. }
  713. break;
  714. case CPDFInkTopToolBarSave: {
  715. if([self.delegate respondsToSelector:@selector(annotationBarClick:clickAnnotationMode:forSelected:forButton:)]) {
  716. [self.delegate annotationBarClick:self clickAnnotationMode:CPDFViewAnnotationModeInk forSelected:NO forButton:inkButton];
  717. }
  718. [self.annotManage.pdfListView commitDrawing];
  719. self.annotManage.pdfListView.annotationMode = CPDFViewAnnotationModeNone;
  720. }
  721. [self reloadData];
  722. default:
  723. break;
  724. }
  725. }
  726. #pragma mark - CPDFInkViewControllerDelegate
  727. - (void)inkViewController:(CPDFInkViewController *)inkViewController annotStyle:(CAnnotStyle *)annotStyle {
  728. if (annotStyle.isSelectAnnot) {
  729. [self.annotManage refreshPageWithAnnotations:annotStyle.annotations];
  730. } else {
  731. for (CPDFAnnotationBarButton *button in self.annotationBtns) {
  732. if (CPDFViewAnnotationModeInk == button.tag) {
  733. button.lineColor = CAnnotationManage.freehandAnnotationColor;
  734. [button setNeedsDisplay];
  735. break;
  736. }
  737. }
  738. }
  739. }
  740. - (void)inkViewControllerDimiss:(CPDFInkViewController *)inkViewController {
  741. if ([self.topToolBar isDescendantOfView:self.pdfListView]) {
  742. ((UIButton *)self.topToolBar.buttonArray[0]).backgroundColor = [CPDFColorUtils CAnnotationPropertyViewControllerBackgoundColor];
  743. if (((UIButton *)self.topToolBar.buttonArray[1]).selected) {
  744. ((UIButton *)self.topToolBar.buttonArray[1]).backgroundColor = [CPDFColorUtils CAnnotationBarSelectBackgroundColor];
  745. }
  746. }
  747. }
  748. #pragma mark - CPDFShapeArrowViewControllerDelegate
  749. - (void)arrowViewController:(CPDFShapeArrowViewController *)arrowViewController annotStyle:(CAnnotStyle *)annotStyle {
  750. if (annotStyle.isSelectAnnot) {
  751. [self.annotManage refreshPageWithAnnotations:annotStyle.annotations];
  752. } else {
  753. }
  754. }
  755. #pragma mark - CPDFFreeTextViewControllerDelegate
  756. - (void)freeTextViewController:(CPDFFreeTextViewController *)freeTextViewController annotStyle:(CAnnotStyle *)annotStyle {
  757. if (annotStyle.isSelectAnnot) {
  758. [self.annotManage refreshPageWithAnnotations:annotStyle.annotations];
  759. } else {
  760. }
  761. }
  762. #pragma mark - CPDFLinkViewControllerDelegate
  763. - (void)linkViewController:(CPDFLinkViewController *)linkViewController linkType:(CPDFLinkType)linkType linkString:(NSString *)linkString {
  764. CPDFLinkAnnotation *linkAnnotation = linkViewController.annotStyle.annotations.firstObject;
  765. if (CPDFLinkTypeLink == linkType || CPDFLinkTypeEmail == linkType) {
  766. linkAnnotation.URL = linkString;
  767. } else if (CPDFLinkTypePage == linkType) {
  768. linkAnnotation.destination = [[CPDFDestination alloc] initWithDocument:self.pdfListView.document
  769. pageIndex:[linkString integerValue]-1
  770. atPoint:CGPointZero
  771. zoom:1];
  772. }
  773. [self.pdfListView updateActiveAnnotations:@[]];
  774. [self.pdfListView setNeedsDisplayForPage:linkAnnotation.page];
  775. }
  776. - (void)linkViewControllerDismiss:(CPDFLinkViewController *)linkViewController isLink:(BOOL)isLink {
  777. if (!isLink) {
  778. [((CPDFAnnotation *)self.pdfListView.activeAnnotations.firstObject).page removeAnnotation:self.pdfListView.activeAnnotations.firstObject];
  779. [self.annotManage.pdfListView setNeedsDisplayForPage:self.annotManage.pdfListView.activeAnnotation.page];
  780. [self.annotManage.pdfListView updateActiveAnnotations:@[]];
  781. }
  782. }
  783. #pragma mark - CPDFDrawPencilViewDelegate
  784. - (void)drawPencilFuncView:(CPDFDrawPencilKitFuncView *)view eraserBtn:(UIButton *)btn {
  785. if (btn.isSelected) {
  786. self.pdfListView.scrollEnabled = YES;
  787. } else {
  788. self.pdfListView.scrollEnabled = NO;
  789. }
  790. }
  791. - (void)drawPencilFuncView:(CPDFDrawPencilKitFuncView *)view saveBtn:(UIButton *)btn {
  792. [self.pdfListView commitDrawing];
  793. [self.pdfListView endDrawing];
  794. self.pdfListView.annotationMode = CPDFViewAnnotationModeNone;
  795. [self.drawPencilFuncView removeFromSuperview];
  796. _drawPencilFuncView.delegate = nil;
  797. _drawPencilFuncView = nil;
  798. }
  799. - (void)drawPencilFuncView:(CPDFDrawPencilKitFuncView *)view cancelBtn:(UIButton *)btn {
  800. self.pdfListView.annotationMode = CPDFViewAnnotationModeNone;
  801. [self.drawPencilFuncView removeFromSuperview];
  802. _drawPencilFuncView.delegate = nil;
  803. _drawPencilFuncView = nil;
  804. }
  805. #pragma mark - NSNotification
  806. - (void)annotationsOperationChangeNotification:(NSNotification *)notification {
  807. CPDFListView *pdfListView = notification.object;
  808. if(pdfListView == self.annotManage.pdfListView) {
  809. if([pdfListView canUndo])
  810. self.undoBtn.enabled = YES;
  811. else
  812. self.undoBtn.enabled = NO;
  813. if([pdfListView canRedo])
  814. self.redoBtn.enabled = YES;
  815. else
  816. self.redoBtn.enabled = NO;
  817. }
  818. }
  819. - (void)annotationChangedNotification:(NSNotification *)notification {
  820. [self updatePropertiesButtonState];
  821. }
  822. #pragma mark - CPDFStampViewControllerDelegate
  823. - (void)stampViewController:(CPDFStampViewController *)stampViewController selectedIndex:(NSInteger)selectedIndex stamp:(NSDictionary *)stamp {
  824. if(self.isAddAnnotation) {
  825. if (selectedIndex == -1) {
  826. } else {
  827. if (stamp.count > 0) {
  828. if (stamp[PDFAnnotationStampKeyImagePath]) {
  829. UIImage *image = [UIImage imageWithContentsOfFile:stamp[PDFAnnotationStampKeyImagePath]];
  830. CPDFStampAnnotation *annotation = [[CPDFStampAnnotation alloc] initWithDocument:self.annotManage.pdfListView.document image:image];
  831. CGRect bounds = annotation.bounds;
  832. bounds.origin.x = self.menuPoint.x-bounds.size.width/2.0;
  833. bounds.origin.y = self.menuPoint.y-bounds.size.height/2.0;
  834. annotation.bounds = bounds;
  835. [self.annotManage.pdfListView addAnnotation:annotation forPage:self.menuPage];
  836. } else {
  837. NSString *stampText = stamp[PDFAnnotationStampKeyText];
  838. BOOL stampShowDate = [stamp[PDFAnnotationStampKeyShowDate] boolValue];
  839. BOOL stampShowTime = [stamp[PDFAnnotationStampKeyShowTime] boolValue];
  840. CPDFStampStyle stampStyle = [stamp[PDFAnnotationStampKeyStyle] integerValue];
  841. CPDFStampShape stampShape = [stamp[PDFAnnotationStampKeyShape] integerValue];
  842. NSString *detailText = nil;
  843. NSTimeZone *timename = [NSTimeZone systemTimeZone];
  844. NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
  845. [outputFormatter setTimeZone:timename];
  846. if (stampShowDate && !stampShowTime){
  847. [outputFormatter setDateFormat:@"yyyy/MM/dd"];
  848. detailText = [outputFormatter stringFromDate:[NSDate date]];
  849. } else if (stampShowTime && !stampShowDate) {
  850. [outputFormatter setDateFormat:@"HH:mm:ss"];
  851. detailText = [outputFormatter stringFromDate:[NSDate date]];
  852. } else if (stampShowDate && stampShowTime) {
  853. [outputFormatter setDateFormat:@"yyyy/MM/dd HH:mm"];
  854. detailText = [outputFormatter stringFromDate:[NSDate date]];
  855. }
  856. CPDFStampAnnotation *annotation = [[CPDFStampAnnotation alloc] initWithDocument:self.annotManage.pdfListView.document text:stampText detailText:detailText style:stampStyle shape:stampShape];
  857. CGRect bounds = annotation.bounds;
  858. bounds.origin.x = self.menuPoint.x-bounds.size.width/2.0;
  859. bounds.origin.y = self.menuPoint.y-bounds.size.height/2.0;
  860. annotation.bounds = bounds;
  861. [self.annotManage.pdfListView addAnnotation:annotation forPage:self.menuPage];
  862. }
  863. } else {
  864. CPDFStampAnnotation *annotation = [[CPDFStampAnnotation alloc] initWithDocument:self.annotManage.pdfListView.document type:selectedIndex+1];
  865. CGRect bounds = annotation.bounds;
  866. bounds.origin.x = self.menuPoint.x-bounds.size.width/2.0;
  867. bounds.origin.y = self.menuPoint.y-bounds.size.height/2.0;
  868. annotation.bounds = bounds;
  869. [self.annotManage.pdfListView addAnnotation:annotation forPage:self.menuPage];
  870. }
  871. }
  872. self.isAddAnnotation = NO;
  873. self.menuPage = nil;
  874. self.menuPoint = CGPointZero;
  875. } else {
  876. if (selectedIndex == -1) {
  877. self.annotManage.pdfListView.annotationMode = CPDFViewAnnotationModeNone;
  878. [self reloadData];
  879. } else {
  880. if (stamp.count > 0) {
  881. if (stamp[PDFAnnotationStampKeyImagePath]) {
  882. UIImage *image = [UIImage imageWithContentsOfFile:stamp[PDFAnnotationStampKeyImagePath]];
  883. CPDFStampAnnotation *annotation = [[CPDFStampAnnotation alloc] initWithDocument:self.annotManage.pdfListView.document image:image];
  884. self.annotManage.pdfListView.addAnnotation = annotation;
  885. } else {
  886. NSString *stampText = stamp[PDFAnnotationStampKeyText];
  887. BOOL stampShowDate = [stamp[PDFAnnotationStampKeyShowDate] boolValue];
  888. BOOL stampShowTime = [stamp[PDFAnnotationStampKeyShowTime] boolValue];
  889. CPDFStampStyle stampStyle = [stamp[PDFAnnotationStampKeyStyle] integerValue];
  890. CPDFStampShape stampShape = [stamp[PDFAnnotationStampKeyShape] integerValue];
  891. NSString *detailText = nil;
  892. NSTimeZone *timename = [NSTimeZone systemTimeZone];
  893. NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
  894. [outputFormatter setTimeZone:timename];
  895. if (stampShowDate && !stampShowTime){
  896. [outputFormatter setDateFormat:@"yyyy/MM/dd"];
  897. detailText = [outputFormatter stringFromDate:[NSDate date]];
  898. } else if (stampShowTime && !stampShowDate) {
  899. [outputFormatter setDateFormat:@"HH:mm:ss"];
  900. detailText = [outputFormatter stringFromDate:[NSDate date]];
  901. } else if (stampShowDate && stampShowTime) {
  902. [outputFormatter setDateFormat:@"yyyy/MM/dd HH:mm"];
  903. detailText = [outputFormatter stringFromDate:[NSDate date]];
  904. }
  905. CPDFStampAnnotation *annotation = [[CPDFStampAnnotation alloc] initWithDocument:self.annotManage.pdfListView.document text:stampText detailText:detailText style:stampStyle shape:stampShape];
  906. self.annotManage.pdfListView.addAnnotation = annotation;
  907. }
  908. } else {
  909. CPDFStampAnnotation *annotation = [[CPDFStampAnnotation alloc] initWithDocument:self.annotManage.pdfListView.document type:selectedIndex+1];
  910. self.annotManage.pdfListView.addAnnotation = annotation;
  911. }
  912. }
  913. }
  914. }
  915. - (void)stampViewControllerDismiss:(CPDFStampViewController *)stampViewController {
  916. self.pdfListView.annotationMode = CPDFViewAnnotationModeNone;
  917. }
  918. @end