CPDFAnnotationBar.m 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819
  1. //
  2. // CPDFAnnotationBar.m
  3. // compdfkit-tools
  4. //
  5. // Copyright © 2014-2023 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 "CPDFAnnotationBar.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 CPDFAnnotationBar () <UINavigationControllerDelegate, UIImagePickerControllerDelegate, CPDFSignatureViewControllerDelegate, CPDFSignatureEditViewControllerDelegate, CPDFNoteViewControllerDelegate, CPDFShapeCircleViewControllerDelegate, CPDFHighlightViewControllerDelegate, CPDFUnderlineViewControllerDelegate, CPDFStrikeoutViewControllerDelegate, CPDFSquigglyViewControllerDelegate, CPDFInkTopToolBarDelegate, CPDFInkViewControllerDelegate, CPDFShapeArrowViewControllerDelegate, CPDFStampViewControllerDelegate,CPDFLinkViewControllerDelegate>
  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. @end
  48. @implementation CPDFAnnotationBar
  49. #pragma mark - Initializers
  50. - (instancetype)initAnnotationManage:(CAnnotationManage *)annotationManage {
  51. if (self = [super init]) {
  52. self.annotManage = annotationManage;
  53. self.backgroundColor = [CPDFColorUtils CPDFViewControllerBackgroundColor];
  54. UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, 1)];
  55. line.backgroundColor = [UIColor colorWithRed:210.0/255.0 green:210.0/255.0 blue:210.0/255.0 alpha:1.0];
  56. line.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  57. [self addSubview:line];
  58. self.selectedIndex = -1;
  59. self.pdfListView = annotationManage.pdfListView;
  60. [self initSubview];
  61. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(annotationChangedNotification:) name:CPDFListViewActiveAnnotationsChangeNotification object:nil];
  62. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(annotationsOperationChangeNotification:) name:CPDFListViewAnnotationsOperationChangeNotification object:nil];
  63. [[NSNotificationCenter defaultCenter] postNotificationName:CPDFListViewAnnotationsOperationChangeNotification object:annotationManage.pdfListView];
  64. }
  65. return self;
  66. }
  67. - (void)layoutSubviews {
  68. [super layoutSubviews];
  69. self.scrollView.frame = CGRectMake(0, 0, self.frame.size.width-self.propertiesBar.frame.size.width, self.frame.size.height);
  70. }
  71. #pragma mark - Public Methods
  72. - (void)reloadData {
  73. if(CPDFViewAnnotationModeNone == self.pdfListView.annotationMode) {
  74. if (self.selectedIndex >0 &&
  75. self.selectedIndex <= self.annotationBtns.count) {
  76. for (NSInteger i = 0; i< self.annotationBtns.count; i++) {
  77. CPDFAnnotationBarButton *button = [self.annotationBtns objectAtIndex:i];
  78. if(button.tag == self.selectedIndex) {
  79. button.backgroundColor = [UIColor clearColor];
  80. self.selectedIndex = -1;
  81. break;
  82. }
  83. }
  84. }
  85. } else {
  86. for (NSInteger i = 0; i< self.annotationBtns.count; i++) {
  87. UIButton *button = [self.annotationBtns objectAtIndex:i];
  88. if(button.tag == self.pdfListView.annotationMode) {
  89. button.backgroundColor = [CPDFColorUtils CAnnotationBarSelectBackgroundColor];
  90. self.selectedIndex = button.tag;
  91. } else {
  92. button.backgroundColor = [UIColor clearColor];
  93. }
  94. }
  95. }
  96. }
  97. #pragma mark - Private Methods
  98. - (void)initSubview {
  99. _scrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
  100. _scrollView.showsVerticalScrollIndicator = NO;
  101. _scrollView.showsHorizontalScrollIndicator = NO;
  102. [self addSubview:_scrollView];
  103. CGFloat offsetX = 10.0;
  104. CGFloat buttonOffset = 15;
  105. CGFloat buttonSize = 44;
  106. CGFloat topOffset = (46 - buttonSize)/2;
  107. NSArray *images = @[@"CPDFAnnotationBarImageNote",
  108. @"CPDFAnnotationBarImageHighLight",
  109. @"CPDFAnnotationBarImageUnderline",
  110. @"CPDFAnnotationBarImageStrikeout",
  111. @"CPDFAnnotationBarImageSquiggly",
  112. @"CPDFAnnotationBarImageFreehand",
  113. @"CPDFAnnotationBarImagePencilDraw",
  114. @"CPDFAnnotationBarImageShapeCircle",
  115. @"CPDFAnnotationBarImageShapeRectangle",
  116. @"CPDFAnnotationBarImageShapeArrow",
  117. @"CPDFAnnotationBarImageShapeLine",
  118. @"CPDFAnnotationBarImageFreeText",
  119. @"CPDFAnnotationBarImageSignature",
  120. @"CPDFAnnotationBarImageStamp",
  121. @"CPDFAnnotationBarImageImage",
  122. @"CPDFAnnotationBarImageLink",
  123. @"CPDFAnnotationBarImageSound"];
  124. NSArray *types = @[@(CPDFViewAnnotationModeNote),
  125. @(CPDFViewAnnotationModeHighlight),
  126. @(CPDFViewAnnotationModeUnderline),
  127. @(CPDFViewAnnotationModeStrikeout),
  128. @(CPDFViewAnnotationModeSquiggly),
  129. @(CPDFViewAnnotationModeInk),
  130. @(CPDFViewAnnotationModePencilDrawing),
  131. @(CPDFViewAnnotationModeCircle),
  132. @(CPDFViewAnnotationModeSquare),
  133. @(CPDFViewAnnotationModeArrow),
  134. @(CPDFViewAnnotationModeLine),
  135. @(CPDFViewAnnotationModeFreeText),
  136. @(CPDFViewAnnotationModeSignature),
  137. @(CPDFViewAnnotationModeStamp),
  138. @(CPDFViewAnnotationModeImage),
  139. @(CPDFViewAnnotationModeLink),
  140. @(CPDFViewAnnotationModeSound)];
  141. NSMutableArray *annotationBtns = [NSMutableArray array];
  142. for (int i = 0; i < types.count; i++) {
  143. CPDFViewAnnotationMode annotationMode = (CPDFViewAnnotationMode)[types[i] integerValue];
  144. CPDFAnnotationBarButton *button = [CPDFAnnotationBarButton buttonWithType:UIButtonTypeCustom];
  145. button.frame = CGRectMake(offsetX, topOffset, buttonSize, buttonSize);
  146. [button setImage:[UIImage imageNamed:images[i] inBundle:[NSBundle bundleForClass:self.class] compatibleWithTraitCollection:nil] forState:UIControlStateNormal];
  147. button.tag = annotationMode;
  148. if (CPDFViewAnnotationModeHighlight == annotationMode) {
  149. button.lineColor = CAnnotationManage.highlightAnnotationColor;
  150. } else if (CPDFViewAnnotationModeUnderline == annotationMode) {
  151. button.lineColor = CAnnotationManage.underlineAnnotationColor;
  152. } else if (CPDFViewAnnotationModeStrikeout == annotationMode) {
  153. button.lineColor = CAnnotationManage.strikeoutAnnotationColor;
  154. } else if (CPDFViewAnnotationModeSquiggly == annotationMode) {
  155. button.lineColor = CAnnotationManage.squigglyAnnotationColor;
  156. } else if (CPDFViewAnnotationModeInk== annotationMode) {
  157. button.lineColor = CAnnotationManage.freehandAnnotationColor;
  158. }
  159. [button addTarget:self action:@selector(buttonItemClicked_Switch:) forControlEvents:UIControlEventTouchUpInside];
  160. [_scrollView addSubview:button];
  161. [annotationBtns addObject:button];
  162. if(i != types.count -1)
  163. offsetX += button.bounds.size.width + buttonOffset;
  164. else
  165. offsetX += button.bounds.size.width;
  166. }
  167. self.annotationBtns = annotationBtns;
  168. if (UIUserInterfaceIdiomPhone == UI_USER_INTERFACE_IDIOM()) {
  169. _scrollView.contentSize = CGSizeMake(offsetX, _scrollView.bounds.size.height);
  170. } else {
  171. _scrollView.contentSize = CGSizeMake(_scrollView.bounds.size.width, offsetX);
  172. }
  173. [self.scrollView bringSubviewToFront:self.propertiesBar];
  174. _propertiesBar = [[UIView alloc] initWithFrame:CGRectMake(self.bounds.size.width - 150, 0, 140, 44)];
  175. _propertiesBar.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
  176. [self addSubview:self.propertiesBar];
  177. CGFloat offset = 10;
  178. UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(offset, 12, 1, 20)];
  179. if (@available(iOS 13.0, *))
  180. lineView.backgroundColor = [UIColor labelColor];
  181. else
  182. lineView.backgroundColor = [UIColor blackColor];
  183. [self.propertiesBar addSubview:lineView];
  184. offset += lineView.frame.size.width;
  185. _propertiesBtn = [[UIButton alloc] initWithFrame:CGRectMake(offset, topOffset, buttonSize, buttonSize)];
  186. [_propertiesBtn setImage:[UIImage imageNamed:@"CPDFAnnotationBarImageProperties" inBundle:[NSBundle bundleForClass:self.class] compatibleWithTraitCollection:nil] forState:UIControlStateNormal];
  187. [_propertiesBtn addTarget:self action:@selector(buttonItemClicked_open:) forControlEvents:UIControlEventTouchUpInside];
  188. [self.propertiesBar addSubview:self.propertiesBtn];
  189. offset += self.propertiesBtn.frame.size.width;
  190. _undoBtn = [[UIButton alloc] initWithFrame:CGRectMake(offset, topOffset, buttonSize, buttonSize)];
  191. [_undoBtn setImage:[UIImage imageNamed:@"CPDFAnnotationBarImageUndo" inBundle:[NSBundle bundleForClass:self.class] compatibleWithTraitCollection:nil] forState:UIControlStateNormal];
  192. [_undoBtn addTarget:self action:@selector(buttonItemClicked_undo:) forControlEvents:UIControlEventTouchUpInside];
  193. [self.propertiesBar addSubview:self.undoBtn];
  194. offset += self.undoBtn.frame.size.width;
  195. _redoBtn = [[UIButton alloc] initWithFrame:CGRectMake(offset, topOffset, buttonSize, buttonSize)];
  196. [_redoBtn setImage:[UIImage imageNamed:@"CPDFAnnotationBarImageRedo" inBundle:[NSBundle bundleForClass:self.class] compatibleWithTraitCollection:nil] forState:UIControlStateNormal];
  197. [_redoBtn addTarget:self action:@selector(buttonItemClicked_redo:) forControlEvents:UIControlEventTouchUpInside];
  198. [self.propertiesBar addSubview:self.redoBtn];
  199. }
  200. - (void)updatePropertiesButtonState {
  201. if(CPDFViewAnnotationModeNone != self.pdfListView.annotationMode || self.annotManage.pdfListView.activeAnnotations.count > 0) {
  202. self.propertiesBtn.enabled = YES;
  203. } else {
  204. self.propertiesBtn.enabled = NO;
  205. }
  206. }
  207. #pragma mark - Action
  208. - (void)buttonItemClicked_Switch:(UIButton *)button {
  209. self.selectedIndex = button.tag;
  210. BOOL isSelect = YES;
  211. if (self.pdfListView.annotationMode != self.selectedIndex) {
  212. self.propertiesBtn.enabled = YES;
  213. button.backgroundColor = [CPDFColorUtils CAnnotationBarSelectBackgroundColor];
  214. self.pdfListView.annotationMode = self.selectedIndex;
  215. [self.annotManage setAnnotStyleFromMode:self.selectedIndex];
  216. isSelect = YES;
  217. } else {
  218. self.propertiesBtn.enabled = NO;
  219. self.pdfListView.annotationMode = CPDFViewAnnotationModeNone;
  220. self.selectedIndex = -1;
  221. button.backgroundColor = [UIColor clearColor];
  222. isSelect = NO;
  223. }
  224. [self updatePropertiesButtonState];
  225. [self createPropertyViewController];
  226. if([self.delegate respondsToSelector:@selector(annotationBarClick:clickAnnotationMode:forSelected:forButton:)]) {
  227. [self.delegate annotationBarClick:self clickAnnotationMode:(CPDFViewAnnotationMode)button.tag forSelected:isSelect forButton:button];
  228. }
  229. }
  230. - (void)buttonItemClicked_open:(UIButton *)button {
  231. CAnnotStyle *annotStytle = self.annotManage.annotStyle;
  232. AAPLCustomPresentationController *presentationController NS_VALID_UNTIL_END_OF_SCOPE;
  233. switch (annotStytle.annotMode) {
  234. case CPDFToolbarNote:
  235. {
  236. CPDFNoteViewController *noteVC = [[CPDFNoteViewController alloc] initWithStyle:annotStytle];
  237. noteVC.delegate = self;
  238. presentationController = [[AAPLCustomPresentationController alloc] initWithPresentedViewController:noteVC presentingViewController:self.parentVC];
  239. noteVC.transitioningDelegate = presentationController;
  240. [self.parentVC presentViewController:noteVC animated:YES completion:nil];
  241. }
  242. break;
  243. case CPDFToolbarHighlight:
  244. {
  245. CPDFHighlightViewController *highlightVC = [[CPDFHighlightViewController alloc] initWithStyle:annotStytle];
  246. highlightVC.delegate = self;
  247. presentationController = [[AAPLCustomPresentationController alloc] initWithPresentedViewController:highlightVC presentingViewController:self.parentVC];
  248. highlightVC.transitioningDelegate = presentationController;
  249. [self.parentVC presentViewController:highlightVC animated:YES completion:nil];
  250. }
  251. break;
  252. case CPDFToolbarUnderline:
  253. {
  254. CPDFUnderlineViewController *underlineVC = [[CPDFUnderlineViewController alloc] initWithStyle:annotStytle];
  255. underlineVC.delegate = self;
  256. presentationController = [[AAPLCustomPresentationController alloc] initWithPresentedViewController:underlineVC presentingViewController:self.parentVC];
  257. underlineVC.transitioningDelegate = presentationController;
  258. [self.parentVC presentViewController:underlineVC animated:YES completion:nil];
  259. }
  260. break;
  261. case CPDFToolbarStrikeout:
  262. {
  263. CPDFStrikeoutViewController *strikeoutVC = [[CPDFStrikeoutViewController alloc] initWithStyle:annotStytle];
  264. strikeoutVC.delegate = self;
  265. presentationController = [[AAPLCustomPresentationController alloc] initWithPresentedViewController:strikeoutVC presentingViewController:self.parentVC];
  266. strikeoutVC.transitioningDelegate = presentationController;
  267. [self.parentVC presentViewController:strikeoutVC animated:YES completion:nil];
  268. }
  269. break;
  270. case CPDFToolbarSquiggly:
  271. {
  272. CPDFSquigglyViewController *squigglyVC = [[CPDFSquigglyViewController alloc] initWithStyle:annotStytle];
  273. squigglyVC.delegate = self;
  274. presentationController = [[AAPLCustomPresentationController alloc] initWithPresentedViewController:squigglyVC presentingViewController:self.parentVC];
  275. squigglyVC.transitioningDelegate = presentationController;
  276. [self.parentVC presentViewController:squigglyVC animated:YES completion:nil];
  277. }
  278. break;
  279. case CPDFToolbarFreehand:
  280. {
  281. CPDFInkViewController *inkVC = [[CPDFInkViewController alloc] initWithStyle:annotStytle];
  282. inkVC.delegate = self;
  283. presentationController = [[AAPLCustomPresentationController alloc] initWithPresentedViewController:inkVC presentingViewController:self.parentVC];
  284. inkVC.transitioningDelegate = presentationController;
  285. [self.parentVC presentViewController:inkVC animated:YES completion:nil];
  286. }
  287. break;
  288. case CPDFToolbarShapeCircle:
  289. {
  290. CPDFShapeCircleViewController *circleVC = [[CPDFShapeCircleViewController alloc] initWithStyle:annotStytle];
  291. circleVC.delegate = self;
  292. presentationController = [[AAPLCustomPresentationController alloc] initWithPresentedViewController:circleVC presentingViewController:self.parentVC];
  293. circleVC.transitioningDelegate = presentationController;
  294. [self.parentVC presentViewController:circleVC animated:YES completion:nil];
  295. }
  296. break;
  297. case CPDFToolbarShapeRectangle:
  298. {
  299. CPDFShapeCircleViewController *squareVC = [[CPDFShapeCircleViewController alloc] initWithStyle:annotStytle];
  300. squareVC.delegate = self;
  301. presentationController = [[AAPLCustomPresentationController alloc] initWithPresentedViewController:squareVC presentingViewController:self.parentVC];
  302. squareVC.transitioningDelegate = presentationController;
  303. [self.parentVC presentViewController:squareVC animated:YES completion:nil];
  304. }
  305. break;
  306. case CPDFToolbarShapeArrow:
  307. {
  308. CPDFShapeArrowViewController *arrowVC = [[CPDFShapeArrowViewController alloc] initWithStyle:annotStytle];
  309. arrowVC.lineDelegate = self;
  310. presentationController = [[AAPLCustomPresentationController alloc] initWithPresentedViewController:arrowVC presentingViewController:self.parentVC];
  311. arrowVC.transitioningDelegate = presentationController;
  312. [self.parentVC presentViewController:arrowVC animated:YES completion:nil];
  313. }
  314. break;
  315. case CPDFToolbarShapeLine:
  316. {
  317. CPDFShapeArrowViewController *lineVC = [[CPDFShapeArrowViewController alloc] initWithStyle:annotStytle];
  318. lineVC.lineDelegate = self;
  319. presentationController = [[AAPLCustomPresentationController alloc] initWithPresentedViewController:lineVC presentingViewController:self.parentVC];
  320. lineVC.transitioningDelegate = presentationController;
  321. [self.parentVC presentViewController:lineVC animated:YES completion:nil];
  322. }
  323. break;
  324. case CPDFToolbarFreeText:
  325. {
  326. CPDFFreeTextViewController *freeTextVC = [[CPDFFreeTextViewController alloc] initWithStyle:annotStytle];
  327. presentationController = [[AAPLCustomPresentationController alloc] initWithPresentedViewController:freeTextVC presentingViewController:self.parentVC];
  328. freeTextVC.pdfListView = self.annotManage.pdfListView;
  329. freeTextVC.transitioningDelegate = presentationController;
  330. [self.parentVC presentViewController:freeTextVC animated:YES completion:nil];
  331. }
  332. break;
  333. case CPDFToolbarLink:
  334. {
  335. CPDFLinkViewController *linkVC = [[CPDFLinkViewController alloc] initWithStyle:annotStytle];
  336. linkVC.delegate = self;
  337. presentationController = [[AAPLCustomPresentationController alloc] initWithPresentedViewController:linkVC presentingViewController:self.parentVC];
  338. linkVC.transitioningDelegate = presentationController;
  339. [self.parentVC presentViewController:linkVC animated:YES completion:nil];
  340. }
  341. break;
  342. default:
  343. break;
  344. }
  345. }
  346. - (void)buttonItemClicked_undo:(UIButton *)button {
  347. if(self.annotManage.pdfListView.undoPDFManager && [self.annotManage.pdfListView canUndo]) {
  348. [self.annotManage.pdfListView.undoPDFManager undo];
  349. }
  350. }
  351. - (void)buttonItemClicked_redo:(UIButton *)button {
  352. if(self.annotManage.pdfListView.undoPDFManager && [self.annotManage.pdfListView canRedo]) {
  353. [self.annotManage.pdfListView.undoPDFManager redo];
  354. }
  355. }
  356. #pragma mark - Private Methods
  357. - (void)createPropertyViewController {
  358. if (CPDFViewAnnotationModeInk == self.selectedIndex) {
  359. [CPDFKitConfig sharedInstance].enableFreehandPencilKit = NO;
  360. if (@available(iOS 11.0, *)) {
  361. self.topToolBar = [[CPDFInkTopToolBar alloc] initWithFrame:CGRectMake(50, self.window.safeAreaInsets.top, self.pdfListView.frame.size.width-100, 50)];
  362. self.topToolBar.delegate = self;
  363. self.topToolBar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  364. [self.pdfListView addSubview:self.topToolBar];
  365. } else {
  366. self.topToolBar = [[CPDFInkTopToolBar alloc] initWithFrame:CGRectMake(50, 64, self.pdfListView.frame.size.width-100, 50)];
  367. self.topToolBar.delegate = self;
  368. self.topToolBar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  369. [self.pdfListView addSubview:self.topToolBar];
  370. }
  371. } else if (CPDFViewAnnotationModeSignature == self.selectedIndex) {
  372. CAnnotStyle *annotStytle = self.annotManage.annotStyle;
  373. AAPLCustomPresentationController *presentationController NS_VALID_UNTIL_END_OF_SCOPE;
  374. self.signatureVC = [[CPDFSignatureViewController alloc] initWithStyle:annotStytle];
  375. presentationController = [[AAPLCustomPresentationController alloc] initWithPresentedViewController:self.signatureVC presentingViewController:self.parentVC];
  376. self.signatureVC.delegate = self;
  377. self.signatureVC.transitioningDelegate = presentationController;
  378. [self.parentVC presentViewController:self.signatureVC animated:YES completion:nil];
  379. } else if (CPDFViewAnnotationModeStamp == self.selectedIndex) {
  380. AAPLCustomPresentationController *presentationController NS_VALID_UNTIL_END_OF_SCOPE;
  381. CPDFStampViewController *stampVC = [[CPDFStampViewController alloc] init];
  382. presentationController = [[AAPLCustomPresentationController alloc] initWithPresentedViewController:self.signatureVC presentingViewController:self.parentVC];
  383. stampVC.delegate = self;
  384. stampVC.transitioningDelegate = presentationController;
  385. [self.parentVC presentViewController:stampVC animated:YES completion:nil];
  386. } else if (CPDFViewAnnotationModeImage == self.selectedIndex) {
  387. [self createImageAnnotaion];
  388. } else if (CPDFViewAnnotationModeLink == self.selectedIndex) {
  389. self.propertiesBtn.enabled = NO;
  390. } else if (CPDFViewAnnotationModeSound == self.selectedIndex) {
  391. } else if (CPDFViewAnnotationModePencilDrawing == self.selectedIndex) {
  392. if (@available(iOS 13.0, *)) {
  393. [CPDFKitConfig sharedInstance].enableFreehandPencilKit = YES;
  394. } else {
  395. [CPDFKitConfig sharedInstance].enableFreehandPencilKit = NO;
  396. }
  397. }
  398. }
  399. - (void)createImageAnnotaion {
  400. UIViewController *tRootViewControl = [UIApplication sharedApplication].keyWindow.rootViewController;
  401. if ([tRootViewControl presentedViewController]) {
  402. tRootViewControl = [tRootViewControl presentedViewController];
  403. }
  404. UIAlertAction *cameraAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Use Camera", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  405. UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
  406. imagePickerController.delegate = self;
  407. imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
  408. [tRootViewControl presentViewController:imagePickerController animated:YES completion:nil];
  409. }];
  410. UIAlertAction *photoAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Photo Library", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  411. UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
  412. imagePickerController.delegate = self;
  413. imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
  414. imagePickerController.allowsEditing = YES;
  415. imagePickerController.modalPresentationStyle = UIModalPresentationPopover;
  416. [tRootViewControl presentViewController:imagePickerController animated:YES completion:nil];
  417. }];
  418. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Cancel", nil)
  419. style:UIAlertActionStyleCancel
  420. handler:^(UIAlertAction * _Nonnull action) {
  421. self.annotManage.pdfListView.annotationMode = CPDFViewAnnotationModeNone;
  422. [self reloadData];
  423. }];
  424. UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:nil
  425. message:nil
  426. preferredStyle:UIAlertControllerStyleActionSheet];
  427. [actionSheet addAction:cameraAction];
  428. [actionSheet addAction:photoAction];
  429. [actionSheet addAction:cancelAction];
  430. actionSheet.modalPresentationStyle = UIModalPresentationPopover;
  431. [tRootViewControl presentViewController:actionSheet animated:YES completion:nil];
  432. }
  433. #pragma mark - UIImagePickerControllerDelegate
  434. - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
  435. [picker dismissViewControllerAnimated:YES completion:nil];
  436. UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
  437. UIImageOrientation imageOrientation = image.imageOrientation;
  438. if (imageOrientation!=UIImageOrientationUp) {
  439. UIGraphicsBeginImageContext(image.size);
  440. [image drawInRect:CGRectMake(0, 0, image.size.width, image.size.height)];
  441. image = UIGraphicsGetImageFromCurrentImageContext();
  442. UIGraphicsEndImageContext();
  443. }
  444. NSData *imageData = UIImagePNGRepresentation(image);
  445. if (imageData == nil || [imageData length] <= 0) {
  446. return;
  447. }
  448. image = [UIImage imageWithData:imageData];
  449. const CGFloat colorMasking[6] = {222, 255, 222, 255, 222, 255};
  450. CGImageRef imageRef = CGImageCreateWithMaskingColors(image.CGImage, colorMasking);
  451. if (imageRef) {
  452. image = [UIImage imageWithCGImage:imageRef];
  453. CGImageRelease(imageRef);
  454. }
  455. CPDFStampAnnotation *annotation = [[CPDFStampAnnotation alloc] initWithDocument:self.annotManage.pdfListView.document image:image];
  456. self.annotManage.pdfListView.addAnnotation = annotation;
  457. }
  458. - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
  459. [picker dismissViewControllerAnimated:YES completion:nil];
  460. }
  461. #pragma mark - CPDFSignatureViewControllerDelegate
  462. - (void)signatureViewController:(CPDFSignatureViewController *)signatureViewController {
  463. CPDFSignatureEditViewController *editVC = [[CPDFSignatureEditViewController alloc] init];
  464. editVC.delegate = self;
  465. [self.parentVC.navigationController pushViewController:editVC animated:NO];
  466. }
  467. - (void)signatureViewController:(CPDFSignatureViewController *)signatureViewController image:(UIImage *)image {
  468. CPDFSignatureAnnotation *annotation = [[CPDFSignatureAnnotation alloc] initWithDocument:self.annotManage.pdfListView.document];
  469. [annotation setImage:image];
  470. [self.annotManage.pdfListView addAnnotation:annotation];
  471. self.pdfListView.annotationMode = CPDFViewAnnotationModeNone;
  472. self.annotManage.pdfListView.annotationMode = CPDFViewAnnotationModeNone;
  473. [self reloadData];
  474. }
  475. #pragma mark - CPDFSignatureEditViewControllerDelegate
  476. - (void)signatureEditViewController:(CPDFSignatureEditViewController *)signatureEditViewController image:(UIImage *)image{
  477. [[CSignatureManager sharedManager] addImageSignature:image];
  478. self.pdfListView.annotationMode = CPDFViewAnnotationModeNone;
  479. self.annotManage.pdfListView.annotationMode = CPDFViewAnnotationModeNone;
  480. [self reloadData];
  481. [self.signatureVC.tableView reloadData];
  482. }
  483. #pragma mark - CPDFNoteViewControllerDelegate
  484. - (void)noteViewController:(CPDFNoteViewController *)noteViewController annotSytle:(CAnnotStyle *)annotStyle {
  485. if (annotStyle.isSelectAnnot) {
  486. [self.annotManage refreshPageWithAnnotations:annotStyle.annotations];
  487. }
  488. }
  489. #pragma mark - CPDFShapeCircleViewControllerDelegate
  490. - (void)circleViewController:(CPDFShapeCircleViewController *)circleViewController annotStyle:(CAnnotStyle *)annotStyle {
  491. if (annotStyle.isSelectAnnot) {
  492. [self.annotManage refreshPageWithAnnotations:annotStyle.annotations];
  493. }
  494. }
  495. #pragma mark - CPDFHighlightViewControllerDelegate
  496. - (void)highlightViewController:(CPDFHighlightViewController *)highlightViewController annotStyle:(CAnnotStyle *)annotStyle {
  497. if (annotStyle.isSelectAnnot) {
  498. [self.annotManage refreshPageWithAnnotations:annotStyle.annotations];
  499. } else {
  500. for (CPDFAnnotationBarButton *button in self.annotationBtns) {
  501. if(CPDFViewAnnotationModeHighlight == button.tag) {
  502. button.lineColor = CAnnotationManage.highlightAnnotationColor;
  503. [button setNeedsDisplay];
  504. break;
  505. }
  506. }
  507. }
  508. }
  509. #pragma mark - CPDFUnderlineViewControllerDelegate
  510. - (void)underlineViewController:(CPDFUnderlineViewController *)underlineViewController annotStyle:(CAnnotStyle *)annotStyle {
  511. if (annotStyle.isSelectAnnot) {
  512. [self.annotManage refreshPageWithAnnotations:annotStyle.annotations];
  513. } else {
  514. for (CPDFAnnotationBarButton *button in self.annotationBtns) {
  515. if (CPDFViewAnnotationModeUnderline == button.tag) {
  516. button.lineColor = CAnnotationManage.underlineAnnotationColor;
  517. [button setNeedsDisplay];
  518. break;
  519. }
  520. }
  521. }
  522. }
  523. #pragma mark - CPDFStrikeoutViewControllerDelegate
  524. - (void)strikeoutViewController:(CPDFStrikeoutViewController *)strikeoutViewController annotStyle:(CAnnotStyle *)annotStyle {
  525. if (annotStyle.isSelectAnnot) {
  526. [self.annotManage refreshPageWithAnnotations:annotStyle.annotations];
  527. } else {
  528. for (CPDFAnnotationBarButton *button in self.annotationBtns) {
  529. if (CPDFViewAnnotationModeStrikeout == button.tag) {
  530. button.lineColor = CAnnotationManage.strikeoutAnnotationColor;
  531. [button setNeedsDisplay];
  532. break;
  533. }
  534. }
  535. }
  536. }
  537. #pragma mark - CPDFSquigglyViewControllerDelegate
  538. - (void)squigglyViewController:(CPDFSquigglyViewController *)squigglyViewController annotStyle:(CAnnotStyle *)annotStyle {
  539. if (annotStyle.isSelectAnnot) {
  540. [self.annotManage refreshPageWithAnnotations:annotStyle.annotations];
  541. } else {
  542. for (CPDFAnnotationBarButton *button in self.annotationBtns) {
  543. if (CPDFViewAnnotationModeSquiggly == button.tag) {
  544. button.lineColor = CAnnotationManage.squigglyAnnotationColor;
  545. [button setNeedsDisplay];
  546. break;
  547. }
  548. }
  549. }
  550. }
  551. #pragma mark - CPDFAnnotationBarDelegate
  552. - (void)inkTopToolBar:(CPDFInkTopToolBar *)inkTopToolBar tag:(CPDFInkTopToolBarSelect)tag isSelect:(BOOL)isSelect {
  553. UIButton *inkButton = nil;
  554. for (CPDFAnnotationBarButton *button in self.annotationBtns) {
  555. if (CPDFViewAnnotationModeInk == button.tag) {
  556. inkButton = button;
  557. break;
  558. }
  559. }
  560. switch (tag) {
  561. case CPDFInkTopToolBarSetting: {
  562. CAnnotStyle *annotStytle = self.annotManage.annotStyle;
  563. AAPLCustomPresentationController *presentationController NS_VALID_UNTIL_END_OF_SCOPE;
  564. CPDFInkViewController *inkVC = [[CPDFInkViewController alloc] initWithStyle:annotStytle];
  565. inkVC.delegate = self;
  566. presentationController = [[AAPLCustomPresentationController alloc] initWithPresentedViewController:inkVC presentingViewController:self.parentVC];
  567. inkVC.transitioningDelegate = presentationController;
  568. [self.parentVC presentViewController:inkVC animated:YES completion:nil];
  569. }
  570. break;
  571. case CPDFInkTopToolBarErase: {
  572. [self.annotManage.pdfListView setDrawErasing:isSelect];
  573. }
  574. break;
  575. case CPDFInkTopToolBarUndo: {
  576. [self.annotManage.pdfListView drawUndo];
  577. }
  578. break;
  579. case CPDFInkTopToolBarRedo: {
  580. [self.annotManage.pdfListView drawRedo];
  581. }
  582. break;
  583. case CPDFInkTopToolBarClear: {
  584. if([self.delegate respondsToSelector:@selector(annotationBarClick:clickAnnotationMode:forSelected:forButton:)]) {
  585. [self.delegate annotationBarClick:self clickAnnotationMode:CPDFViewAnnotationModeInk forSelected:NO forButton:inkButton];
  586. }
  587. self.annotManage.pdfListView.annotationMode = CPDFViewAnnotationModeNone;
  588. [self reloadData];
  589. }
  590. break;
  591. case CPDFInkTopToolBarSave: {
  592. if([self.delegate respondsToSelector:@selector(annotationBarClick:clickAnnotationMode:forSelected:forButton:)]) {
  593. [self.delegate annotationBarClick:self clickAnnotationMode:CPDFViewAnnotationModeInk forSelected:NO forButton:inkButton];
  594. }
  595. [self.annotManage.pdfListView commitDrawing];
  596. self.annotManage.pdfListView.annotationMode = CPDFViewAnnotationModeNone;
  597. }
  598. [self reloadData];
  599. default:
  600. break;
  601. }
  602. }
  603. #pragma mark - CPDFInkViewControllerDelegate
  604. - (void)inkViewController:(CPDFInkViewController *)inkViewController annotStyle:(CAnnotStyle *)annotStyle {
  605. if (annotStyle.isSelectAnnot) {
  606. [self.annotManage refreshPageWithAnnotations:annotStyle.annotations];
  607. } else {
  608. for (CPDFAnnotationBarButton *button in self.annotationBtns) {
  609. if (CPDFViewAnnotationModeInk == button.tag) {
  610. button.lineColor = CAnnotationManage.freehandAnnotationColor;
  611. [button setNeedsDisplay];
  612. break;
  613. }
  614. }
  615. }
  616. }
  617. #pragma mark - CPDFShapeArrowViewControllerDelegate
  618. - (void)arrowViewController:(CPDFShapeArrowViewController *)arrowViewController annotStyle:(CAnnotStyle *)annotStyle {
  619. if (annotStyle.isSelectAnnot) {
  620. [self.annotManage refreshPageWithAnnotations:annotStyle.annotations];
  621. } else {
  622. }
  623. }
  624. #pragma mark - CPDFLinkViewControllerDelegate
  625. - (void)linkViewController:(CPDFLinkViewController *)linkViewController linkType:(CPDFLinkType)linkType linkString:(NSString *)linkString {
  626. CPDFLinkAnnotation *linkAnnotation = linkViewController.annotStyle.annotations.firstObject;
  627. if (CPDFLinkTypeLink == linkType || CPDFLinkTypeEmail == linkType) {
  628. linkAnnotation.URL = linkString;
  629. } else if (CPDFLinkTypePage == linkType) {
  630. linkAnnotation.destination = [[CPDFDestination alloc] initWithDocument:self.pdfListView.document
  631. pageIndex:[linkString integerValue]-1
  632. atPoint:CGPointZero
  633. zoom:1];
  634. }
  635. [self.pdfListView updateActiveAnnotations:@[]];
  636. [self.pdfListView setNeedsDisplayForPage:linkAnnotation.page];
  637. }
  638. #pragma mark - NSNotification
  639. - (void)annotationsOperationChangeNotification:(NSNotification *)notification {
  640. CPDFListView *pdfListView = notification.object;
  641. if(pdfListView == self.annotManage.pdfListView) {
  642. if([pdfListView canUndo])
  643. self.undoBtn.enabled = YES;
  644. else
  645. self.undoBtn.enabled = NO;
  646. if([pdfListView canRedo])
  647. self.redoBtn.enabled = YES;
  648. else
  649. self.redoBtn.enabled = NO;
  650. }
  651. }
  652. - (void)annotationChangedNotification:(NSNotification *)notification {
  653. if (self.pdfListView.activeAnnotations.firstObject) {
  654. [self.annotManage setAnnotStyleFromAnnotations:self.pdfListView.activeAnnotations];
  655. }
  656. [self updatePropertiesButtonState];
  657. }
  658. #pragma mark - CPDFStampViewControllerDelegate
  659. - (void)stampViewController:(CPDFStampViewController *)stampViewController selectedIndex:(NSInteger)selectedIndex stamp:(NSDictionary *)stamp {
  660. if (selectedIndex == -1) {
  661. self.annotManage.pdfListView.annotationMode = CPDFViewAnnotationModeNone;
  662. [self reloadData];
  663. } else {
  664. if (stamp.count > 0) {
  665. if (stamp[PDFAnnotationStampKeyImagePath]) {
  666. UIImage *image = [UIImage imageWithContentsOfFile:stamp[PDFAnnotationStampKeyImagePath]];
  667. CPDFStampAnnotation *annotation = [[CPDFStampAnnotation alloc] initWithDocument:self.annotManage.pdfListView.document image:image];
  668. self.annotManage.pdfListView.addAnnotation = annotation;
  669. } else {
  670. NSString *stampText = stamp[PDFAnnotationStampKeyText];
  671. BOOL stampShowDate = [stamp[PDFAnnotationStampKeyShowDate] boolValue];
  672. BOOL stampShowTime = [stamp[PDFAnnotationStampKeyShowTime] boolValue];
  673. CPDFStampStyle stampStyle = [stamp[PDFAnnotationStampKeyStyle] integerValue];
  674. CPDFStampShape stampShape = [stamp[PDFAnnotationStampKeyShape] integerValue];
  675. NSString *detailText = nil;
  676. NSTimeZone *timename = [NSTimeZone systemTimeZone];
  677. NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
  678. [outputFormatter setTimeZone:timename];
  679. if (stampShowDate && !stampShowTime){
  680. detailText = [NSDateFormatter localizedStringFromDate:[NSDate date]
  681. dateStyle:NSDateFormatterShortStyle
  682. timeStyle:NSDateFormatterNoStyle];
  683. } else if (stampShowTime && !stampShowDate) {
  684. [outputFormatter setDateFormat:@"HH:mm:ss"];
  685. detailText = [outputFormatter stringFromDate:[NSDate date]];
  686. } else if (stampShowDate && stampShowTime) {
  687. [outputFormatter setDateFormat:@" HH:mm"];
  688. detailText = [NSDateFormatter localizedStringFromDate:[NSDate date]
  689. dateStyle:NSDateFormatterShortStyle
  690. timeStyle:NSDateFormatterNoStyle];
  691. detailText = [detailText stringByAppendingString:[outputFormatter stringFromDate:[NSDate date]]];
  692. }
  693. CPDFStampAnnotation *annotation = [[CPDFStampAnnotation alloc] initWithDocument:self.annotManage.pdfListView.document text:stampText detailText:detailText style:stampStyle shape:stampShape];
  694. self.annotManage.pdfListView.addAnnotation = annotation;
  695. }
  696. } else {
  697. CPDFStampAnnotation *annotation = [[CPDFStampAnnotation alloc] initWithDocument:self.annotManage.pdfListView.document type:selectedIndex+1];
  698. self.annotManage.pdfListView.addAnnotation = annotation;
  699. }
  700. }
  701. }
  702. @end