CPDFListView.m 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372
  1. //
  2. // CPDFListView.m
  3. // ComPDFKit
  4. //
  5. // Copyright © 2014-2022 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 "CPDFListView.h"
  13. #import "CPDFAnnotationModel.h"
  14. #import "CSelfSignAnnotation.h"
  15. #import "CSelfSignAnnotationFreeText.h"
  16. #import "CPDFListView+Private.h"
  17. #import "CPDFListView+Event.h"
  18. #import "CPDFListView+Tool.h"
  19. #import "CPDFListView+Extension.h"
  20. #import "CPDFListView+UndoManager.h"
  21. #import "NSImage+PDFListView.h"
  22. #import "NSCursor+PDFListView.h"
  23. #import "CPDFMarkupAnnotation+PDFListView.h"
  24. #import "CPDFListView+Extension.h"
  25. #import <PDF_Master-Swift.h>
  26. NSNotificationName const CPDFListViewSelectionChangedNotification = @"CPDFListViewSelectionChangedNotification";
  27. NSNotificationName const CPDFListViewToolModeChangeNotification = @"CPDFListViewToolModeChangeNotification";
  28. NSNotificationName const CPDFListViewAnnotationTypeChangeNotification = @"CPDFListViewAnnotationTypeChangeNotification";
  29. NSNotificationName const CPDFListViewMagnificationChangedNotification = @"CPDFListViewMagnificationChangedNotification";
  30. NSNotificationName const CPDFListViewDidAddAnnotationNotification = @"CPDFListViewDidAddAnnotationNotification";
  31. NSNotificationName const CPDFListViewDidRemoveAnnotationNotification = @"CPDFListViewDidRemoveAnnotationNotification";
  32. NSNotificationName const CPDFListViewActiveAnnotationsChangeNotification = @"CPDFListViewActiveAnnotationsChangeNotification";
  33. NSNotificationName const CPDFListViewAnnotationsAttributeHasChangeNotification = @"CPDFListViewAnnotationsAttributeHasChangeNotification";
  34. @implementation CPDFListView
  35. #pragma mark - Init
  36. - (id)init {
  37. self = [super init];
  38. if (self) {
  39. [self commonInitialization];
  40. }
  41. return self;
  42. }
  43. - (id)initWithFrame:(NSRect)frameRect {
  44. self = [super initWithFrame:frameRect];
  45. if (self) {
  46. [self commonInitialization];
  47. }
  48. return self;
  49. }
  50. - (id)initWithCoder:(NSCoder *)decoder {
  51. self = [super initWithCoder:decoder];
  52. if (self) {
  53. [self commonInitialization];
  54. }
  55. return self;
  56. }
  57. - (void)updateLayer {
  58. [super updateLayer];
  59. if (@available(macOS 10.14, *)) {
  60. if ([self.pdfListViewDelegate respondsToSelector:@selector(PDFListViewBackgroundColor)]) {
  61. NSColor *color = [self.pdfListViewDelegate PDFListViewBackgroundColor];
  62. self.backgroundColor = color;
  63. }
  64. }
  65. }
  66. - (void)commonInitialization {
  67. self.selectionPageIndex = NSNotFound;
  68. self.clickLineAnnotation = nil;
  69. self.isClickDoubleCreatLine = NO;
  70. self.activeAnnotations = [NSMutableArray array];
  71. self.selectAnnotations = [NSMutableArray array];
  72. self.dragHoverPoints = [NSMutableArray array];
  73. self.aCopyAnnotations = [NSMutableArray array];
  74. [self registerAsObserver];
  75. [self addTrackingArea];
  76. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handlePageChangedNotification:)
  77. name:CPDFViewPageChangedNotification object:self];
  78. }
  79. - (void)addTrackingArea {
  80. NSTrackingArea *trackingArea = [[NSTrackingArea alloc] initWithRect:self.bounds options:NSTrackingMouseEnteredAndExited | NSTrackingInVisibleRect | NSTrackingActiveInKeyWindow | NSTrackingMouseMoved owner:self userInfo:nil];
  81. [self addTrackingArea:trackingArea];
  82. }
  83. - (void)goToPageIndex:(NSInteger)pageIndex animated:(BOOL)animated {
  84. if (self.currentPageIndex != pageIndex) {
  85. [self recordForwardBackWithPageIndex:self.currentPageIndex];
  86. }
  87. [super goToPageIndex:pageIndex animated:animated];
  88. }
  89. - (void)goToSelection:(CPDFSelection *)selection animated:(BOOL)animated {
  90. if (self.currentPageIndex != selection.page.pageIndex) {
  91. [self recordForwardBackWithPageIndex:self.currentPageIndex];
  92. }
  93. [super goToSelection:selection animated:animated];
  94. }
  95. - (void)goToDestination:(CPDFDestination *)destination {
  96. [super goToDestination:destination];
  97. }
  98. - (void)goToRect:(CGRect)rect onPage:(CPDFPage *)page animated:(BOOL)animated {
  99. if (self.currentPageIndex != page.pageIndex) {
  100. [self recordForwardBackWithPageIndex:self.currentPageIndex];
  101. }
  102. [super goToRect:rect onPage:page animated:animated];
  103. }
  104. - (void)goToTargetPoint:(NSPoint)point onPage:(CPDFPage *)page atScrollPosition:(CScrollPosition)scrollPosition {
  105. if (self.currentPageIndex != page.pageIndex) {
  106. [self recordForwardBackWithPageIndex:self.currentPageIndex];
  107. }
  108. [super goToTargetPoint:point onPage:page atScrollPosition:scrollPosition];
  109. }
  110. #pragma mark - Setter & Get
  111. - (void)setDocument:(CPDFDocument *)document {
  112. @synchronized (self) {
  113. self.selectionRect = NSZeroRect;
  114. self.selectionPageIndex = NSNotFound;
  115. }
  116. // self.toolMode = CFormToolMode;
  117. // self.annotationType = CAnnotationTypeRadioButton;
  118. // CStampSignatureObject *objc = [[CStampSignatureObject alloc] initImageStampWithFilePath:[[NSBundle mainBundle] pathForResource:@"Quick Start Guide" ofType:@"pdf"]];
  119. // [self setAddStampObject:objc keepToolModel:YES];
  120. //
  121. [super setDocument:document];
  122. [self.window makeFirstResponder:self];
  123. }
  124. -(void)setToolMode:(CToolMode)newToolMode {
  125. if(newToolMode != _toolMode) {
  126. BOOL isDisplayAnnotationView = NO;
  127. if(self.isSetLinkDestinationArea)
  128. self.isSetLinkDestinationArea = NO;
  129. if ((CTextToolMode == _toolMode || CNoteToolMode == _toolMode) && CTextToolMode != newToolMode) {
  130. if (newToolMode != CNoteToolMode && self.activeAnnotation) {
  131. if([self.activeAnnotation isKindOfClass:[CPDFFreeTextAnnotation class]]) {
  132. CPDFFreeTextAnnotation *freeTextAn = (CPDFFreeTextAnnotation *)self.activeAnnotation;
  133. if ([self isEditWithCurrentFreeText:freeTextAn]) {
  134. [self commitEditAnnotationFreeText:freeTextAn];
  135. }
  136. }
  137. [self updateActiveAnnotations:@[]];
  138. }
  139. if ([self currentSelection])
  140. [self setCurrentSelection:nil];
  141. } else if (_toolMode == CSelectToolMode && NSEqualRects(self.selectionRect, NSZeroRect) == NO) {
  142. self.selectionRect = NSZeroRect;
  143. self.selectionPageIndex = NSNotFound;
  144. dispatch_async(dispatch_get_main_queue(), ^{
  145. [[NSNotificationCenter defaultCenter] postNotificationName:CPDFViewSelectionChangedNotification object:self];
  146. });
  147. if([self.pdfListViewDelegate respondsToSelector:@selector(PDFListViewChangedSelectionOrMagnification:)])
  148. [self.pdfListViewDelegate PDFListViewChangedSelectionOrMagnification:self];
  149. isDisplayAnnotationView = YES;
  150. }
  151. BOOL highlightFormFiled = [[NSUserDefaults standardUserDefaults] boolForKey:@"kPDFViewHighlightFormFiledKey"];
  152. if (CFormToolMode == _toolMode && !highlightFormFiled) {
  153. [[CPDFKitConfig sharedInstance] setEnableFormFieldHighlight:highlightFormFiled];
  154. isDisplayAnnotationView = YES;
  155. } else if (CFormToolMode == newToolMode && !highlightFormFiled) {
  156. [[CPDFKitConfig sharedInstance] setEnableFormFieldHighlight:YES];
  157. isDisplayAnnotationView = YES;
  158. }
  159. if(CEditPDFToolMode == newToolMode)
  160. [self beginEditingLoadType:CEditingLoadTypeText|CEditingLoadTypeImage];
  161. if(_toolMode == CEditPDFToolMode) {
  162. if(self.isEdited) {
  163. [self commitEditing];
  164. }
  165. [self endOfEditing];
  166. }
  167. _toolMode = newToolMode;
  168. self.selectZoomToolModeZoomOut = NO;
  169. if([self.pdfListViewDelegate respondsToSelector:@selector(PDFListViewChangedToolMode:forToolMode:)])
  170. [self.pdfListViewDelegate PDFListViewChangedToolMode:self forToolMode:newToolMode];
  171. dispatch_async(dispatch_get_main_queue(), ^{
  172. [[NSNotificationCenter defaultCenter] postNotificationName:CPDFListViewToolModeChangeNotification object:self];
  173. });
  174. [self resetPDFViewAnnotation];
  175. if(isDisplayAnnotationView)
  176. [self setNeedsDisplayAnnotationViewForVisiblePages];
  177. }
  178. }
  179. -(void)setShowHopnumber:(BOOL)showHopnumber {
  180. _showHopnumber = showHopnumber;
  181. [self setNeedsDisplayAnnotationViewForVisiblePages];
  182. }
  183. - (void)setAnnotationType:(CAnnotationType)annotationType {
  184. if (_annotationType != annotationType) {
  185. if(CAnnotationTypeLink == annotationType || CAnnotationTypeLink == _annotationType)
  186. [self setNeedsDisplayAnnotationViewForVisiblePages];
  187. _annotationType = annotationType;
  188. if([self.pdfListViewDelegate respondsToSelector:@selector(PDFListViewChangedAnnotationType:forAnnotationType:)])
  189. [self.pdfListViewDelegate PDFListViewChangedAnnotationType:self forAnnotationType:annotationType];
  190. dispatch_async(dispatch_get_main_queue(), ^{
  191. [[NSNotificationCenter defaultCenter] postNotificationName:CPDFListViewAnnotationTypeChangeNotification object:self];
  192. });
  193. }
  194. if(self.activeAnnotations.count > 0) {
  195. if([self.activeAnnotation isKindOfClass:[CPDFFreeTextAnnotation class]]) {
  196. CPDFFreeTextAnnotation *freeTextAn = (CPDFFreeTextAnnotation *)self.activeAnnotation;
  197. if ([self isEditWithCurrentFreeText:freeTextAn]) {
  198. [self commitEditAnnotationFreeText:freeTextAn];
  199. }
  200. }
  201. [self updateActiveAnnotations:@[]];
  202. [self setNeedsDisplayAnnotationViewForVisiblePages];
  203. }
  204. if(self.isSetLinkDestinationArea)
  205. self.isSetLinkDestinationArea = NO;
  206. [self resetPDFViewAnnotation];
  207. }
  208. - (NSRect)currentSelectionRect {
  209. if (CSelectToolMode == self.toolMode)
  210. return self.selectionRect;
  211. return NSZeroRect;
  212. }
  213. - (CPDFAnnotation *)activeAnnotation {
  214. return self.activeAnnotations.lastObject;
  215. }
  216. - (void)setHideNotes:(BOOL)hideNotes {
  217. _hideNotes = hideNotes;
  218. for(NSInteger i = 0;i<self.document.pageCount;i++) {
  219. CPDFPage *page = [self.document pageAtIndex:i];
  220. for (CPDFAnnotation *annotation in page.annotations) {
  221. [annotation setAnnotationShouldDisplay:!self.hideNotes];
  222. [annotation setHidden: self.hideNotes];
  223. }
  224. }
  225. [self setNeedsDisplayAnnotationViewForVisiblePages];
  226. }
  227. -(void)setIsSetLinkDestinationArea:(BOOL)isSetLinkDestinationArea {
  228. _isSetLinkDestinationArea = isSetLinkDestinationArea;
  229. if (_isSetLinkDestinationArea) {
  230. if([self.pdfListViewDelegate respondsToSelector:@selector(PDFListViewLinkDestinationStart:withActiveAnnotation:)]) {
  231. [self.pdfListViewDelegate PDFListViewLinkDestinationStart:self withActiveAnnotation:self.activeAnnotation];
  232. }
  233. } else {
  234. if([self.pdfListViewDelegate respondsToSelector:@selector(PDFListViewLinkDestinationEnd:withActiveAnnotation:)]) {
  235. [self.pdfListViewDelegate PDFListViewLinkDestinationEnd:self withActiveAnnotation:self.activeAnnotation];
  236. }
  237. }
  238. }
  239. #pragma mark - Public
  240. - (CPDFAnnotation *)addAnnotationWithType:(CAnnotationType)annotationType selection:(CPDFSelection *)selection page:(CPDFPage *)page bounds:(NSRect)bounds {
  241. CPDFAnnotation *annotation = nil;
  242. NSString *text = [selection PDFListViewCleanedString];
  243. if([CPDFListView isMarkupAnnotationType:annotationType]) {
  244. if(selection){
  245. annotation = [self addPDFSelection:selection annotationTyoe:annotationType];
  246. [annotation setContents:text?:@""];
  247. } else NSBeep();
  248. } else {
  249. BOOL isInitial = NO;
  250. if(selection) {
  251. CGRect noteRect = selection.bounds;
  252. isInitial = NSEqualSizes(noteRect.size, NSZeroSize);
  253. } else {
  254. isInitial = NSEqualSizes(bounds.size, NSZeroSize);
  255. }
  256. CPDFAnnotationModel *annotationModel = [[CPDFAnnotationModel alloc]initWithAnnotationType:annotationType];
  257. CGSize size = CGSizeMake(annotationModel.noteWidth, annotationModel.noteHeight);
  258. if(annotationType == CAnnotationTypeAnchored)
  259. bounds = CGRectMake(bounds.origin.x, bounds.origin.y, size.width, size.height);
  260. if (annotationType == CAnnotationTypeCircle || annotationType == CAnnotationTypeSquare || annotationType == CAnnotationTypeLine || annotationType == CAnnotationTypeArrow || annotationType == CAnnotationTypeAnchored) {
  261. bounds = KMConstrainRect(bounds, [page boundsForBox:[self displayBox]]);
  262. }
  263. if (isInitial)
  264. bounds = annotationType == CAnnotationTypeAnchored ? CPDFListViewRectFromCenterAndSize(bounds.origin, size) : CPDFListViewRectFromCenterAndSquareSize(bounds.origin, MIN_NOTE_SIZE);
  265. switch (annotationType) {
  266. case CAnnotationTypeFreeText:
  267. annotation = [[CPDFFreeTextAnnotation alloc]initWithPDFListViewNoteWith:self.document];
  268. annotation.bounds = bounds;
  269. break;
  270. case CAnnotationTypeAnchored:
  271. annotation = [[CPDFTextAnnotation alloc]initWithPDFListViewNoteWith:self.document];
  272. annotation.bounds = bounds;
  273. break;
  274. case CAnnotationTypeCircle:
  275. annotation = [[CPDFCircleAnnotation alloc]initWithPDFListViewNoteWith:self.document];
  276. annotation.bounds = bounds;
  277. break;
  278. case CAnnotationTypeSquare:
  279. annotation = [[CPDFSquareAnnotation alloc]initWithPDFListViewNoteWith:self.document];
  280. annotation.bounds = bounds;
  281. break;
  282. case CAnnotationTypeLine:
  283. annotation = [[CPDFLineAnnotation alloc]initWithPDFListViewNoteWith:self.document annotationType:CAnnotationTypeLine];
  284. [(CPDFLineAnnotation *)annotation setObservedStartPoint:CGPointMake(bounds.origin.x, bounds.origin.y)];
  285. [(CPDFLineAnnotation *)annotation setObservedEndPoint:CGPointMake(bounds.origin.x + bounds.size.width,bounds.origin.y+ bounds.size.height)];
  286. [(CPDFLineAnnotation *)annotation setStartLineStyle:CPDFLineStyleNone];
  287. [(CPDFLineAnnotation *)annotation setEndLineStyle:CPDFLineStyleNone];
  288. break;
  289. case CAnnotationTypeArrow: {
  290. annotation = [[CPDFLineAnnotation alloc]initWithPDFListViewNoteWith:self.document annotationType:CAnnotationTypeArrow];
  291. [(CPDFLineAnnotation *)annotation setObservedStartPoint:CGPointMake(bounds.origin.x, bounds.origin.y)];
  292. [(CPDFLineAnnotation *)annotation setObservedEndPoint:CGPointMake(bounds.origin.x + bounds.size.width,bounds.origin.y+ bounds.size.height)];
  293. }
  294. break;
  295. case CAnnotationTypeLink:
  296. annotation = [[CPDFLinkAnnotation alloc] initWithPDFListViewNoteWith:self.document];
  297. annotation.bounds = bounds;
  298. break;
  299. case CAnnotationTypeTextField:
  300. annotation = [[CPDFTextWidgetAnnotation alloc] initWithPDFListViewNoteWith:self.document];
  301. annotation.bounds = bounds;
  302. break;
  303. case CAnnotationTypeCheckBox:
  304. annotation = [[CPDFButtonWidgetAnnotation alloc] initWithPDFListViewNoteWith:self.document controlType:CPDFWidgetCheckBoxControl];
  305. annotation.bounds = bounds;
  306. break;
  307. case CAnnotationTypeRadioButton:
  308. annotation = [[CPDFButtonWidgetAnnotation alloc] initWithPDFListViewNoteWith:self.document controlType:CPDFWidgetRadioButtonControl];
  309. annotation.bounds = bounds;
  310. break;
  311. case CAnnotationTypeListMenu:
  312. annotation = [[CPDFChoiceWidgetAnnotation alloc] initWithPDFListViewNoteWith:self.document listChoice:YES];
  313. annotation.bounds = bounds;
  314. break;
  315. case CAnnotationTypeComboBox:
  316. annotation = [[CPDFChoiceWidgetAnnotation alloc] initWithPDFListViewNoteWith:self.document listChoice:NO];
  317. annotation.bounds = bounds;
  318. break;
  319. case CAnnotationTypeSignature:
  320. annotation = [[CPDFSignatureWidgetAnnotation alloc] initWithPDFListViewNoteWith:self.document];
  321. annotation.bounds = bounds;
  322. break;
  323. case CAnnotationTypeActionButton:
  324. annotation = [[CPDFButtonWidgetAnnotation alloc] initWithPDFListViewNoteWith:self.document controlType:CPDFWidgetPushButtonControl];
  325. annotation.bounds = bounds;
  326. break;
  327. case CAnnotationTypeSignFalse:
  328. case CAnnotationTypeSignTure:
  329. case CAnnotationTypeSignCircle:
  330. case CAnnotationTypeSignLine:
  331. case CAnnotationTypeSignDot:
  332. annotation = [[CSelfSignAnnotation alloc]initPDFListViewNoteWithDocument:self.document type:self.annotationType];
  333. annotation.bounds = bounds;
  334. [(CSelfSignAnnotation *)annotation updateAppearanceStream];
  335. break;
  336. case CAnnotationTypeSignText:
  337. case CAnnotationTypeSignConfig:
  338. case CAnnotationTypeSignDate:
  339. annotation = [[CSelfSignAnnotationFreeText alloc]initPDFListViewNoteWithDocument:self.document subType:self.annotationType string:@"" bounds:bounds];
  340. break;
  341. case CAnnotationTypeRedact:
  342. annotation = [self addRedactPDFSelection:selection];
  343. break;
  344. default:
  345. break;
  346. }
  347. }
  348. if (annotation) {
  349. if (annotationType != CAnnotationTypeLine && annotationType != CAnnotationTypeArrow && annotationType != CAnnotationTypeInk && [text length] > 0)
  350. [annotation setString:text];
  351. [self addAnnotation:annotation toPage:page];
  352. }
  353. return annotation;
  354. }
  355. - (void)drawAnnotation:(CPDFAnnotation *)annotation toContext:(CGContextRef)context {
  356. [super drawAnnotation:annotation toContext:context];
  357. if ([self.pdfListViewDelegate respondsToSelector:@selector(PDFListView:needDrawAnnotation:)]) {
  358. if ([self.pdfListViewDelegate PDFListView:self needDrawAnnotation:annotation] == false) {
  359. return;
  360. }
  361. }
  362. if (CFormToolMode == _toolMode &&
  363. [annotation isKindOfClass:[CPDFWidgetAnnotation class]] && self.showFormFieldName && !annotation.isHidden) {
  364. CPDFWidgetAnnotation *annotationWidget = (CPDFWidgetAnnotation*)annotation;
  365. NSString *fieldName = annotationWidget.fieldName;
  366. if(self.showHopnumber) {
  367. NSInteger hopNumen = annotationWidget.widgetHopNumber;
  368. if(fieldName) {
  369. fieldName = [NSString stringWithFormat:@"%ld %@",hopNumen,fieldName];
  370. } else {
  371. fieldName = [NSString stringWithFormat:@"%ld",hopNumen];
  372. }
  373. }
  374. if (fieldName.length > 0) {
  375. CGContextSaveGState(context);
  376. NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
  377. paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
  378. paragraphStyle.alignment = NSTextAlignmentLeft;
  379. NSColor *backgroundColor = [NSColor blackColor];
  380. if ([self.activeAnnotations containsObject:annotation]) {
  381. backgroundColor = [NSColor colorWithRed:82.0/255.0 green:102.0/255.0 blue:204.0/255.0 alpha:1.0];
  382. }
  383. if([CPDFKitConfig sharedInstance].isShowFormRequiredFlagColor)
  384. backgroundColor = [CPDFKitConfig sharedInstance].formRequiredFlagColor?:[NSColor redColor];
  385. NSDictionary *attributes = @{NSFontAttributeName : [NSFont systemFontOfSize:12],
  386. NSForegroundColorAttributeName : [NSColor whiteColor],
  387. NSBackgroundColorAttributeName : backgroundColor,
  388. NSParagraphStyleAttributeName : paragraphStyle};
  389. NSString* drawString = fieldName;
  390. drawString = [drawString getMaxStringWithBounds:annotationWidget.bounds attributes:attributes];
  391. CGSize size = [drawString sizeWithAttributes:attributes];
  392. CGRect drawRect = annotationWidget.bounds;
  393. drawRect.origin.y += (drawRect.size.height - size.height)/2.0;
  394. drawRect.origin.x += (drawRect.size.width - size.width)/2.0;
  395. drawRect.size.height = size.height;
  396. drawRect.size.width = size.width;
  397. [NSGraphicsContext saveGraphicsState];
  398. [NSGraphicsContext setCurrentContext:[NSGraphicsContext graphicsContextWithGraphicsPort:context flipped:NO]];
  399. [drawString drawInRect:drawRect withAttributes:attributes];
  400. [NSGraphicsContext restoreGraphicsState];
  401. CGContextRestoreGState(context);
  402. NSRect rect = [self integralRect:[annotationWidget bounds] onPage:[annotationWidget page]];
  403. CGFloat lineWidth = [self unitWidthOnPage:[annotationWidget page]];
  404. CGContextSaveGState(context);
  405. CGColorRef color = [NSColor blackColor].CGColor;
  406. CGContextSetStrokeColorWithColor(context, color);
  407. CGContextStrokeRectWithWidth(context, CGRectInset(NSRectToCGRect(rect), 0,0), lineWidth);
  408. }
  409. } else if(CNoteToolMode== _toolMode && _annotationType == CAnnotationTypeLink && [annotation isKindOfClass:[CPDFLinkAnnotation class]]) {
  410. CGContextSaveGState(context);
  411. NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
  412. paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
  413. paragraphStyle.alignment = NSTextAlignmentLeft;
  414. NSColor *backgroundColor = [NSColor clearColor];
  415. NSDictionary *attributes = @{NSFontAttributeName : [NSFont systemFontOfSize:12],
  416. NSForegroundColorAttributeName : [NSColor whiteColor],
  417. NSBackgroundColorAttributeName : backgroundColor,
  418. NSParagraphStyleAttributeName : paragraphStyle};
  419. NSString *drawString = NSLocalizedString(@"No Destination", nil);
  420. CPDFDestination *destination = [(CPDFLinkAnnotation *)annotation destination];
  421. if(destination) {
  422. CPDFPage *page = destination.page;
  423. NSString * url = [(CPDFLinkAnnotation *)annotation URL];
  424. if(destination.page) {
  425. NSUInteger index = [self.document indexForPage:page] + 1;
  426. drawString = [NSString stringWithFormat:@"%@ %@",NSLocalizedString(@"to Page",nil),@(index)];
  427. } else if(url.length > 0) {
  428. drawString = url;
  429. }
  430. }
  431. drawString = [drawString getMaxStringWithBounds:annotation.bounds attributes:attributes];
  432. CGSize size = [drawString sizeWithAttributes:attributes];
  433. CGRect drawRect = annotation.bounds;
  434. drawRect.origin.y += (drawRect.size.height - size.height)/2.0;
  435. drawRect.origin.x += (drawRect.size.width - size.width)/2.0;
  436. drawRect.size.height = size.height;
  437. drawRect.size.width = size.width;
  438. CGContextSaveGState(context);
  439. CGContextSetFillColorWithColor(context, [NSColor colorWithRed:23.0/255.0 green:112.0/255.0 blue:244.0/255.0 alpha:0.5].CGColor);
  440. CGContextFillRect(context,annotation.bounds);
  441. CGContextRestoreGState(context);
  442. [NSGraphicsContext saveGraphicsState];
  443. CGRect frameRect=CGRectMake(annotation.bounds.origin.x, annotation.bounds.origin.y + (annotation.bounds.size.height - 20)/2, annotation.bounds.size.width, 20);
  444. float fontSize = 9;
  445. NSColor *textColor = [NSColor whiteColor];
  446. CTTextAlignment alignMent = kCTTextAlignmentCenter;
  447. CFStringRef stringRef = (__bridge CFStringRef)drawString;
  448. NSMutableAttributedString* attString = [[NSMutableAttributedString alloc] initWithString:drawString];
  449. NSInteger _stringLength=[drawString length];
  450. CTTextAlignment theAlignment = alignMent;
  451. CTParagraphStyleSetting theSettings[1] =
  452. {
  453. { kCTParagraphStyleSpecifierAlignment, sizeof(CTTextAlignment),&theAlignment
  454. }
  455. };
  456. CTParagraphStyleRef paragraphStyleRef = CTParagraphStyleCreate(theSettings, 1);
  457. [attString addAttribute:(id)kCTParagraphStyleAttributeName value:(id)paragraphStyleRef range:NSMakeRange(0, _stringLength)];
  458. [attString addAttribute:(id)kCTForegroundColorAttributeName value:(id)textColor.CGColor range:NSMakeRange(0, _stringLength)];
  459. [attString addAttribute:(id)kCTFontNameAttribute value:@"Courier" range:NSMakeRange(0, _stringLength)];
  460. CFAttributedStringRef attrString =(__bridge CFAttributedStringRef) attString;
  461. CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(attrString);
  462. CGMutablePathRef framePath = CGPathCreateMutable();
  463. CGPathAddRect(framePath, NULL, frameRect);
  464. CFRange currentRange = CFRangeMake(0, 0);
  465. CTFrameRef frameRef = CTFramesetterCreateFrame(framesetter, currentRange, framePath, NULL);
  466. CGPathRelease(framePath);
  467. CTFrameDraw(frameRef, context);
  468. CFRelease(frameRef);
  469. CFRelease(framesetter);
  470. [NSGraphicsContext restoreGraphicsState];
  471. CGContextRestoreGState(context);
  472. }
  473. }
  474. - (void)setAddStampObject:(CStampSignatureObject *)stampObject keepToolModel:(BOOL)isToolModel {
  475. if (CAnnotationTypeStamp == self.annotationType ||
  476. CAnnotationTypeSignSignature == self.annotationType) {
  477. self.stampObject = stampObject;
  478. self.isStampModel = isToolModel;
  479. [NSCursor clearStampCursor];
  480. }
  481. }
  482. - (void)addAnnotationWithImage:(NSImage *)image page:(CPDFPage *)page point:(NSPoint)point {
  483. NSRect bounds = NSZeroRect;
  484. CPDFSelection *selection = [self currentSelection];
  485. CGFloat defaultWidth = 360;
  486. CGFloat defaultHeight = 90;
  487. NSSize defaultSize = ([page rotation] % 180 == 0) ? NSMakeSize(defaultWidth, defaultHeight) : NSMakeSize(defaultHeight, defaultWidth);
  488. bounds = CPDFListViewRectFromCenterAndSize(point, defaultSize);
  489. // Make sure it fits in the page
  490. bounds = CPDFListViewConstrainRect(bounds, [page boundsForBox:[self displayBox]],[CPDFListViewConfig defaultManager].annotationBorderOffset.floatValue);
  491. // bounds = CPDFListViewConstrainRect(bounds, [page boundsForBox:[self displayBox]]);
  492. if (page != nil) {
  493. BOOL isInitial = NSEqualSizes(bounds.size, NSZeroSize) && selection == nil;
  494. // new note added by note tool mode, don't add actual zero sized notes
  495. if (isInitial)
  496. bounds = CPDFListViewRectFromCenterAndSquareSize(bounds.origin, 8.0);
  497. CGFloat borderDefaultWidth = image.size.width;
  498. CGFloat borderScale = borderDefaultWidth/image.size.height;
  499. if (bounds.size.width/bounds.size.height > borderScale) {
  500. bounds.size.height = bounds.size.height;
  501. bounds.size.width = bounds.size.height*borderScale;
  502. } else {
  503. bounds.size.width = bounds.size.width;
  504. bounds.size.height = bounds.size.width/borderScale;
  505. }
  506. CPDFListStampAnnotation *newAnnotation = [[CPDFListStampAnnotation alloc] initWithDocument:self.document image:image];
  507. newAnnotation.bounds = bounds;
  508. [newAnnotation setBorderBoundsWithImage:image];
  509. [self addAnnotation:newAnnotation toPage:page];
  510. [[self undoManager] setActionName:NSLocalizedString(@"Add Note", @"Undo action name")];
  511. [self updateActiveAnnotations:@[newAnnotation]];
  512. [self setNeedsDisplayAnnotation:newAnnotation];
  513. }
  514. else NSBeep();
  515. }
  516. - (void)updateIsRightActiveAnnotations:(NSArray<CPDFAnnotation *> *)activeAnnotations {
  517. [self refreshActiveAnnotations:activeAnnotations isRight:YES];
  518. }
  519. - (void)updateActiveAnnotations:(NSArray<CPDFAnnotation *> *)activeAnnotations {
  520. [self refreshActiveAnnotations:activeAnnotations isRight:NO];
  521. }
  522. -(void)refreshActiveAnnotations:(NSArray<CPDFAnnotation *> *)activeAnnotations isRight:(BOOL)isRight {
  523. // for (CPDFAnnotation *annotation in self.activeAnnotations) {
  524. // if([annotation isKindOfClass:[CPDFLinkAnnotation class]]) {
  525. // CPDFLinkAnnotation *linkAnnotation = (CPDFLinkAnnotation *)annotation;
  526. // if((linkAnnotation.destination || linkAnnotation.URL.length > 0)) {
  527. // } else {
  528. // [self removeAnnotation:linkAnnotation];
  529. // }
  530. // }
  531. // }
  532. if(activeAnnotations) {
  533. NSArray *selectTure = [activeAnnotations filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"SELF in %@", self.activeAnnotations]];
  534. BOOL isSame = NO;
  535. if(self.activeAnnotations.count == activeAnnotations.count && self.activeAnnotations.count == selectTure.count) isSame = YES;
  536. if(!isSame) {
  537. self.activeAnnotations = [NSMutableArray arrayWithArray:activeAnnotations];
  538. if([self.pdfListViewDelegate respondsToSelector:@selector(PDFListViewChangeatioActiveAnnotations:forActiveAnnotations:isRightMenu:)])
  539. [self.pdfListViewDelegate PDFListViewChangeatioActiveAnnotations:self forActiveAnnotations:self.activeAnnotations isRightMenu:isRight];
  540. dispatch_async(dispatch_get_main_queue(), ^{
  541. [[NSNotificationCenter defaultCenter] postNotificationName:CPDFListViewActiveAnnotationsChangeNotification object:self];
  542. });
  543. }
  544. } else if (!activeAnnotations && self.activeAnnotations.count > 0) {
  545. [self.activeAnnotations removeAllObjects];
  546. if([self.pdfListViewDelegate respondsToSelector:@selector(PDFListViewChangeatioActiveAnnotations:forActiveAnnotations:isRightMenu:)])
  547. [self.pdfListViewDelegate PDFListViewChangeatioActiveAnnotations:self forActiveAnnotations:self.activeAnnotations isRightMenu:isRight];
  548. dispatch_async(dispatch_get_main_queue(), ^{
  549. [[NSNotificationCenter defaultCenter] postNotificationName:CPDFListViewActiveAnnotationsChangeNotification object:self];
  550. });
  551. }
  552. }
  553. - (void)addActiveAnnotations:(NSArray<CPDFAnnotation *> *)activeAnnotations {
  554. BOOL isContains = YES;
  555. for (CPDFAnnotation *annotation in activeAnnotations) {
  556. if(![self.activeAnnotations containsObject:annotation]) {
  557. [self.activeAnnotations addObject:annotation];
  558. isContains = NO;
  559. }
  560. }
  561. if(!isContains) {
  562. if([self.pdfListViewDelegate respondsToSelector:@selector(PDFListViewChangeatioActiveAnnotations:forActiveAnnotations:isRightMenu:)])
  563. [self.pdfListViewDelegate PDFListViewChangeatioActiveAnnotations:self forActiveAnnotations:self.activeAnnotations isRightMenu:NO];
  564. dispatch_async(dispatch_get_main_queue(), ^{
  565. [[NSNotificationCenter defaultCenter] postNotificationName:CPDFListViewActiveAnnotationsChangeNotification object:self];
  566. });
  567. }
  568. }
  569. - (void)removeActiveAnnotations:(NSArray<CPDFAnnotation *> *)removeAnnotations {
  570. BOOL isContains = NO;
  571. NSMutableArray *tarr = [NSMutableArray arrayWithArray:self.activeAnnotations];
  572. [tarr removeObjectsInArray:removeAnnotations];
  573. self.activeAnnotations = tarr;
  574. isContains = YES;
  575. if(isContains) {
  576. if([self.pdfListViewDelegate respondsToSelector:@selector(PDFListViewChangeatioActiveAnnotations:forActiveAnnotations:isRightMenu:)])
  577. [self.pdfListViewDelegate PDFListViewChangeatioActiveAnnotations:self forActiveAnnotations:self.activeAnnotations isRightMenu:NO];
  578. dispatch_async(dispatch_get_main_queue(), ^{
  579. [[NSNotificationCenter defaultCenter] postNotificationName:CPDFListViewActiveAnnotationsChangeNotification object:self];
  580. });
  581. }
  582. }
  583. - (void)editAnnotation:(CPDFAnnotation *)annotation {
  584. if ([annotation isKindOfClass:[CPDFFreeTextAnnotation class]]) {
  585. [self editAnnotationFreeText:(CPDFFreeTextAnnotation *)annotation];
  586. [[self window] makeFirstResponder:self];
  587. [self setNeedsDisplayAnnotation:annotation];
  588. } else {
  589. if([self.pdfListViewDelegate respondsToSelector:@selector(PDFListViewEditAnnotation:forAnnotation:)]) {
  590. [self.pdfListViewDelegate PDFListViewEditAnnotation:self forAnnotation:annotation];
  591. } else {
  592. [CPDFListView cancelPreviousPerformRequestsWithTarget:self selector:@selector(showHUDHint:) object:self.hoverAnnotation];
  593. if (!self.popOver) {
  594. _popOver = [[NSPopover alloc] init];
  595. }
  596. if([annotation isKindOfClass:[CPDFTextAnnotation class]]) {
  597. __block typeof(self) blockSelf = self;
  598. _popOver.delegate = self;
  599. CPDFListEditAnnotationViewController *vc = [[CPDFListEditAnnotationViewController alloc] initWithNibName:@"CPDFListEditAnnotationViewController" bundle:[NSBundle mainBundle] annotation:annotation];
  600. vc.pdflistView = self;
  601. _popOver.contentViewController = vc;
  602. vc.closeCallBack = ^{
  603. [blockSelf.popOver close];
  604. };
  605. NSBox *box = (NSBox *)vc.view;
  606. NSView *popoverView = [[[self.popOver contentViewController] view] superview];
  607. [popoverView setWantsLayer:YES];
  608. vc.changeColorCallBack = ^{
  609. NSColor *borColor = annotation.color?:[NSColor whiteColor];
  610. [[popoverView layer] setBackgroundColor:borColor.CGColor];
  611. [blockSelf setNeedsDisplayAnnotationViewForPage:annotation.page];
  612. };
  613. NSColor *borColor = annotation.color?:[NSColor whiteColor];
  614. [[popoverView layer] setBackgroundColor:borColor.CGColor];
  615. _popOver.animates = YES;
  616. _popOver.behavior = NSPopoverBehaviorTransient;
  617. [_popOver showRelativeToRect:[self convertRect:annotation.bounds fromPage:annotation.page] ofView:self preferredEdge:NSMaxXEdge];
  618. } else if(![annotation isKindOfClass:[CPDFLinkAnnotation class]]){
  619. CPDFListAnnotationNoteWindowController *wc = [CPDFListAnnotationNoteWindowController sharedInstance];
  620. CGRect rect = wc.window.frame;
  621. CGRect anBounds = [self convertRect:annotation.bounds fromPage:annotation.page];
  622. anBounds = [self convertRectToScreen:anBounds];
  623. rect.origin.x = anBounds.origin.x;
  624. rect.origin.y = anBounds.origin.y;
  625. if (@available(macOS 10.13, *))
  626. [wc.window makeKeyAndOrderFront:nil];
  627. else
  628. [wc showWindow:nil];
  629. [wc updateAnnotation:annotation];
  630. [wc.window setFrame:rect display:YES animate:NO];
  631. wc.window.title = @"";
  632. }
  633. }
  634. }
  635. }
  636. - (void)rotateStampAnnotation:(CPDFListStampAnnotation *)annotation rotateAngle:(NSInteger)angle {
  637. if([annotation isKindOfClass:[CPDFListStampAnnotation class]]) {
  638. CPDFListStampAnnotation *stampAnnotation = (CPDFListStampAnnotation *)annotation;
  639. NSInteger rotation = stampAnnotation.rotation;
  640. rotation = rotation - angle;
  641. if (rotation < 0) {
  642. rotation += 360;
  643. } else if (rotation >= 360) {
  644. rotation-=360;
  645. }
  646. [annotation setRotation:rotation];
  647. rotation = [annotation appearanceStreamRotation];
  648. [self setNeedsDisplayAnnotationViewForPage:annotation.page];
  649. [[[self undoManager] prepareWithInvocationTarget:self] rotateStampAnnotation:annotation rotateAngle:-angle];
  650. }
  651. }
  652. - (void)rotateSignatureAnnotation:(CPDFListSignatureAnnotation *)annotation rotateAngle:(NSInteger)angle {
  653. if([annotation isKindOfClass:[CPDFListSignatureAnnotation class]]) {
  654. CPDFListSignatureAnnotation *signatureAnnotation = (CPDFListSignatureAnnotation *)annotation;
  655. NSInteger rotation = signatureAnnotation.rotation;
  656. rotation = rotation - angle;
  657. if (rotation < 0) {
  658. rotation += 360;
  659. } else if (rotation >= 360) {
  660. rotation-=360;
  661. }
  662. [signatureAnnotation setRotation:rotation];
  663. rotation = [signatureAnnotation appearanceStreamRotation];
  664. [self setNeedsDisplayAnnotationViewForPage:annotation.page];
  665. [[[self undoManager] prepareWithInvocationTarget:self] rotateSignatureAnnotation:annotation rotateAngle:-angle];
  666. }
  667. }
  668. - (void)addAnnotation:(CPDFAnnotation *)annotation toPage:(CPDFPage *)page {
  669. [[[self undoManager] prepareWithInvocationTarget:self] removeAnnotation:annotation];
  670. [annotation setModificationDate:[NSDate date]];
  671. //widget设置作者与fieldName冲突
  672. if (![annotation isKindOfClass:[CPDFWidgetAnnotation class]]) {
  673. [annotation setUserName:CPDFKitShareConfig.annotationAuthor?:NSFullUserName()];
  674. }
  675. [page addAnnotation:annotation];
  676. [self setNeedsDisplayAnnotation:annotation];
  677. [[self undoManager] setActionName:NSLocalizedString(@"Add Note", @"Undo action name")];
  678. dispatch_async(dispatch_get_main_queue(), ^{
  679. [[NSNotificationCenter defaultCenter] postNotificationName:CPDFListViewDidAddAnnotationNotification object:self userInfo:[NSDictionary dictionaryWithObjectsAndKeys:page, CPDFListViewPageKey, annotation, CPDFListViewAnnotationKey, nil]];
  680. });
  681. if([self.pdfListViewDelegate respondsToSelector:@selector(PDFListViewAddAnnotations:forAddAnnotations:inPage:)])
  682. [self.pdfListViewDelegate PDFListViewAddAnnotations:self forAddAnnotations:@[annotation] inPage:page];
  683. }
  684. - (void)removeAnnotation:(CPDFAnnotation *)annotation {
  685. if ([annotation isKindOfClass:[CPDFTextAnnotation class]] && self.popOver.isShown) {
  686. // if (annotation.contents.length <= 0) {
  687. CPDFAnnotation *wasAnnotation = annotation;
  688. CPDFPage *page = wasAnnotation.page;
  689. [[[self undoManager] prepareWithInvocationTarget:self] addAnnotation:wasAnnotation toPage:page];
  690. [page removeAnnotation:wasAnnotation];
  691. [self annotationsChangedOnPage:page];
  692. [self setNeedsDisplayAnnotation:wasAnnotation];
  693. dispatch_async(dispatch_get_main_queue(), ^{
  694. [[NSNotificationCenter defaultCenter] postNotificationName:CPDFListViewDidRemoveAnnotationNotification object:self
  695. userInfo:[NSDictionary dictionaryWithObjectsAndKeys:wasAnnotation, CPDFListViewAnnotationKey, page, CPDFListViewPageKey, nil]];
  696. });
  697. if([self.pdfListViewDelegate respondsToSelector:@selector(PDFListViewRemoveAnnotations:forRemoveAnnotations:inPage:)])
  698. [self.pdfListViewDelegate PDFListViewRemoveAnnotations:self forRemoveAnnotations:@[annotation] inPage:page];
  699. // }
  700. } else {
  701. CPDFAnnotation *wasAnnotation = annotation;
  702. CPDFPage *page = wasAnnotation.page;
  703. [[[self undoManager] prepareWithInvocationTarget:self] addAnnotation:wasAnnotation toPage:page];
  704. [page removeAnnotation:wasAnnotation];
  705. [self annotationsChangedOnPage:page];
  706. [self setNeedsDisplayAnnotation:wasAnnotation];
  707. dispatch_async(dispatch_get_main_queue(), ^{
  708. [[NSNotificationCenter defaultCenter] postNotificationName:CPDFListViewDidRemoveAnnotationNotification object:self
  709. userInfo:[NSDictionary dictionaryWithObjectsAndKeys:wasAnnotation, CPDFListViewAnnotationKey, page, CPDFListViewPageKey, nil]];
  710. });
  711. if([self.pdfListViewDelegate respondsToSelector:@selector(PDFListViewRemoveAnnotations:forRemoveAnnotations:inPage:)])
  712. [self.pdfListViewDelegate PDFListViewRemoveAnnotations:self forRemoveAnnotations:@[annotation] inPage:page];
  713. }
  714. }
  715. - (void)drawPage:(CPDFPage *)pdfPage toContext:(CGContextRef)context {
  716. [super drawPage:pdfPage toContext:context];
  717. [self drawSelectionForPage:pdfPage inContext:context];
  718. BOOL isIncludText = NO;
  719. NSMutableArray *currentActiveAnnotations = [NSMutableArray array];
  720. NSMutableArray *currentPageAnnotations = [NSMutableArray array];
  721. for (CPDFAnnotation *an in self.activeAnnotations) {
  722. if([an.page isEqual:pdfPage]) {
  723. if([an isKindOfClass:[CPDFFreeTextAnnotation class]])
  724. isIncludText = YES;
  725. //多选注释框去除mark注释与便签注释
  726. if(!([an isKindOfClass:[CPDFMarkupAnnotation class]] || [an isKindOfClass:[CPDFTextAnnotation class]])) {
  727. [currentActiveAnnotations addObject:an];
  728. }
  729. [currentPageAnnotations addObject:an];
  730. }
  731. }
  732. if(currentActiveAnnotations.count > 1) {
  733. NSRect rect = [self selectionMultipleBoundsWithAnnotations:currentActiveAnnotations];
  734. CGFloat lineWidth = [self unitWidthOnPage:pdfPage];
  735. CGContextSaveGState(context);
  736. CGColorRef color = [CPDFListViewConfig defaultManager].annotationBorderColor.CGColor;
  737. CGContextSetStrokeColorWithColor(context, color);
  738. CGContextStrokeRectWithWidth(context, CGRectInset(NSRectToCGRect(rect), 0,0), lineWidth);
  739. CGContextRestoreGState(context);
  740. CGContextSaveGState(context);
  741. if(isIncludText) {
  742. [CPDFListView DrawFreeTextResizeHandle:context rect:rect radius: 4.0 * lineWidth active:true];
  743. } else {
  744. [CPDFListView DrawResizeHandles:context rect:rect radius:4.0 * lineWidth active:true];
  745. }
  746. CGContextRestoreGState(context);
  747. }
  748. if(self.multiplAnnotationObject &&
  749. !CGRectIsEmpty(self.multiplAnnotationObject.drawRect) &&
  750. self.multiplAnnotationObject.page == pdfPage) {
  751. CGRect selectAnnotationBounds = self.multiplAnnotationObject.drawRect;
  752. NSRect rect = selectAnnotationBounds;
  753. CGFloat lineWidth = [self unitWidthOnPage:pdfPage];
  754. CGContextSaveGState(context);
  755. CGColorRef color = [CPDFListViewConfig defaultManager].annotationBorderColor.CGColor;
  756. CGContextSetStrokeColorWithColor(context, color);
  757. CGContextStrokeRectWithWidth(context, CGRectInset(NSRectToCGRect(rect), 0,0), lineWidth);
  758. CGContextRestoreGState(context);
  759. }
  760. [currentPageAnnotations enumerateObjectsUsingBlock:^(CPDFAnnotation *annotation, NSUInteger idx, BOOL * _Nonnull stop) {
  761. if (annotation.page && [annotation.page isEqual:pdfPage]) {
  762. [annotation drawSelectionHighlightForView:self inContext:context isHover:NO];
  763. }
  764. }];
  765. [self.selectAnnotations enumerateObjectsUsingBlock:^(CPDFAnnotation *annotation, NSUInteger idx, BOOL * _Nonnull stop) {
  766. if (annotation.page && [annotation.page isEqual:pdfPage]) {
  767. [annotation drawSelectionHighlightForView:self inContext:context isHover:NO];
  768. }
  769. }];
  770. if (self.hoverAnnotation.page && [self.hoverAnnotation.page isEqual:pdfPage] && ![self.activeAnnotations containsObject:self.hoverAnnotation]) {
  771. [self.hoverAnnotation drawSelectionHighlightForView:self inContext:context isHover:YES];
  772. }
  773. if(!CGRectEqualToRect(self.multiplSelectBounds, CGRectZero) && [self.multiplSelectPage isEqual:pdfPage]) {
  774. @synchronized (self) {
  775. CGContextSetLineCap(context, kCGLineCapRound);
  776. CGContextSetLineWidth(context, 1.0);
  777. CGContextSetStrokeColorWithColor(context, [CPDFListViewConfig defaultManager].annotationBorderColor.CGColor);
  778. CGContextBeginPath(context);
  779. CGContextMoveToPoint(context, self.multiplSelectBounds.origin.x, self.multiplSelectBounds.origin.y);
  780. CGContextAddLineToPoint(context, self.multiplSelectBounds.origin.x+ self.multiplSelectBounds.size.width,self.multiplSelectBounds.origin.y);
  781. CGContextAddLineToPoint(context, self.multiplSelectBounds.origin.x + self.multiplSelectBounds.size.width,self.multiplSelectBounds.origin.y + + self.multiplSelectBounds.size.height);
  782. CGContextAddLineToPoint(context, self.multiplSelectBounds.origin.x,self.multiplSelectBounds.origin.y + + self.multiplSelectBounds.size.height);
  783. CGContextClosePath(context);
  784. CGFloat lengths[] = {5,5};
  785. CGContextSetLineDash(context, 0, lengths,2);
  786. CGContextStrokePath(context);
  787. }
  788. }
  789. }
  790. - (void)drawSelectionForPage:(CPDFPage *)pdfPage inContext:(CGContextRef)context {
  791. NSRect rect;
  792. NSUInteger pageIndex;
  793. @synchronized (self) {
  794. pageIndex = self.selectionPageIndex;
  795. rect = self.selectionRect;
  796. }
  797. if (pageIndex != NSNotFound) {
  798. BOOL isWidget = NO;
  799. for (CPDFAnnotation * annotation in self.activeAnnotations) {
  800. if ([annotation isForm]) {
  801. isWidget = YES;
  802. break;
  803. }
  804. }
  805. if (isWidget &&
  806. _toolMode != CFormToolMode) {
  807. return;
  808. }
  809. NSRect bounds = [pdfPage boundsForBox:[self displayBox]];
  810. CGFloat radius = HANDLE_SIZE * [self unitWidthOnPage:pdfPage];
  811. CGColorRef color = CGColorCreateGenericGray(0.0, 0.6);
  812. CGContextSetFillColorWithColor(context, color);
  813. CGColorRelease(color);
  814. CGContextBeginPath(context);
  815. CGContextAddRect(context, NSRectToCGRect(bounds));
  816. CGContextAddRect(context, NSRectToCGRect(rect));
  817. CGContextEOFillPath(context);
  818. if ([pdfPage pageIndex] != pageIndex) {
  819. color = CGColorCreateGenericGray(0.0, 0.3);
  820. CGContextSetFillColorWithColor(context, color);
  821. CGColorRelease(color);
  822. CGContextFillRect(context, NSRectToCGRect(rect));
  823. }
  824. [CPDFListView DrawResizeHandles:context rect:rect radius:radius active:true];
  825. }
  826. }
  827. - (void)removeShapeLayer {
  828. if (![self.shapeLayerTopH isEqual:nil] && ![self.shapeLayerBottomH isEqual:nil] && ![self.shapeLayerLeftV isEqual:nil] && ![self.shapeLayerRightV isEqual:nil]) {
  829. [self.shapeLayerTopH removeFromSuperlayer];
  830. [self.shapeLayerBottomH removeFromSuperlayer];
  831. [self.shapeLayerLeftV removeFromSuperlayer];
  832. [self.shapeLayerRightV removeFromSuperlayer];
  833. self.shapeLayerTopH = nil;
  834. self.shapeLayerBottomH = nil;
  835. self.shapeLayerLeftV = nil;
  836. self.shapeLayerRightV = nil;
  837. }
  838. }
  839. - (BOOL)consistentTypeWithAnnotation:(CPDFAnnotation *)annotation {
  840. if (self.annotationType == CAnnotationTypeSignSignature) {
  841. if (![annotation isKindOfClass:[CPDFSignatureAnnotation class]])
  842. return NO;
  843. } else if (self.annotationType == CAnnotationTypeStamp) {
  844. if (![annotation isKindOfClass:[CPDFStampAnnotation class]])
  845. return NO;
  846. } else if (self.annotationType == CAnnotationTypeAnchored) {
  847. if (![annotation isKindOfClass:[CPDFTextAnnotation class]])
  848. return NO;
  849. } else if (self.annotationType == CAnnotationTypeLink) {
  850. if (![annotation isKindOfClass:[CPDFLinkAnnotation class]])
  851. return NO;
  852. } else if (self.annotationType == CAnnotationTypeFreeText) {
  853. if (![annotation isKindOfClass:[CPDFFreeTextAnnotation class]])
  854. return NO;
  855. } else if (self.annotationType == CAnnotationTypeLine || self.annotationType == CAnnotationTypeArrow || self.annotationType == CAnnotationTypeSquare || self.annotationType == CAnnotationTypeCircle) {
  856. if (![annotation isKindOfClass:[CPDFLineAnnotation class]] && ![annotation isKindOfClass:[CPDFSquareAnnotation class]] && ![annotation isKindOfClass:[CPDFCircleAnnotation class]])
  857. return NO;
  858. } else if (self.annotationType == CAnnotationTypeHighlight || self.annotationType == CAnnotationTypeUnderline || self.annotationType == CAnnotationTypeStrikeOut) {
  859. if (![annotation isKindOfClass:[CPDFMarkupAnnotation class]])
  860. return NO;
  861. } else if (self.annotationType == CAnnotationTypeInk) {
  862. if (![annotation isKindOfClass:[CPDFInkAnnotation class]])
  863. return NO;
  864. } else if (self.annotationType == CAnnotationTypeRadioButton) {
  865. } else if (self.annotationType == CAnnotationTypeCheckBox) {
  866. } else if (self.annotationType == CAnnotationTypeTextField) {
  867. } else if (self.annotationType == CAnnotationTypeComboBox) {
  868. } else if (self.annotationType == CAnnotationTypeListMenu) {
  869. } else if (self.annotationType == CAnnotationTypeActionButton) {
  870. } else if (self.annotationType == CAnnotationTypeSignature) {
  871. } else if (self.annotationType == CAnnotationTypeSignText) {
  872. } else if (self.annotationType == CAnnotationTypeSignFalse) {
  873. } else if (self.annotationType == CAnnotationTypeSignTure) {
  874. } else if (self.annotationType == CAnnotationTypeSignCircle) {
  875. } else if (self.annotationType == CAnnotationTypeSignLine) {
  876. } else if (self.annotationType == CAnnotationTypeSignDot) {
  877. } else if (self.annotationType == CAnnotationTypeSignConfig) {
  878. } else if (self.annotationType == CAnnotationTypeSignDate) {
  879. }
  880. return YES;
  881. }
  882. #pragma mark Zooming
  883. - (void)zoomToRect:(NSRect)rect onPage:(CPDFPage *)page {
  884. if (NSIsEmptyRect(rect) == NO) {
  885. BOOL isLegacy = [NSScroller respondsToSelector:@selector(preferredScrollerStyle)] == NO || [NSScroller preferredScrollerStyle] == NSScrollerStyleLegacy;
  886. NSRect bounds = [self bounds];
  887. CGFloat scale = 1.0;
  888. if (isLegacy) {
  889. bounds.size.width -= [NSScroller scrollerWidth];
  890. bounds.size.height -= [NSScroller scrollerWidth];
  891. }
  892. if (NSWidth(bounds) * NSHeight(rect) > NSWidth(rect) * NSHeight(bounds))
  893. scale = NSHeight(bounds) / NSHeight(rect);
  894. else
  895. scale = NSWidth(bounds) / NSWidth(rect);
  896. [self setScaleFactor:scale];
  897. NSScrollView *scrollView = [self scrollView];
  898. if (isLegacy && ([scrollView hasHorizontalScroller] == NO || [scrollView hasVerticalScroller] == NO)) {
  899. if ([scrollView hasVerticalScroller])
  900. bounds.size.width -= [NSScroller scrollerWidth];
  901. if ([scrollView hasHorizontalScroller])
  902. bounds.size.height -= [NSScroller scrollerWidth];
  903. if (NSWidth(bounds) * NSHeight(rect) > NSWidth(rect) * NSHeight(bounds))
  904. scale = NSHeight(bounds) / NSHeight(rect);
  905. else
  906. scale = NSWidth(bounds) / NSWidth(rect);
  907. [self setScaleFactor:scale];
  908. }
  909. [self goToRect:rect onPage:page];
  910. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  911. CGPoint tPagePoint = CGPointMake(rect.origin.x, rect.origin.y+rect.size.height);
  912. [self goToTargetPoint:tPagePoint onPage:page atScrollPosition:CScrollPositionTop];
  913. });
  914. }
  915. }
  916. #pragma mark - NSPopoverDelegate
  917. - (void)popoverWillClose:(NSNotification *)notification {
  918. NSPopover *popover = notification.object;
  919. if([popover.contentViewController isKindOfClass:[CPDFListEditAnnotationViewController class]]) {
  920. CPDFListEditAnnotationViewController *listEditAnnotationViewController = (CPDFListEditAnnotationViewController *)popover.contentViewController;
  921. CPDFAnnotation *annotation = listEditAnnotationViewController.annotation;
  922. NSString *contensString = listEditAnnotationViewController.contentString;
  923. if([annotation isKindOfClass:[CPDFTextAnnotation class]]) {
  924. if(contensString && contensString.length > 0) {
  925. annotation.contents = listEditAnnotationViewController.contentString;
  926. [self setNeedsDisplayAnnotation:annotation];
  927. } else {
  928. if([self.activeAnnotations containsObject:annotation]) {
  929. [self.activeAnnotations removeObject:annotation];
  930. }
  931. [self removeAnnotation:annotation];
  932. }
  933. } else {
  934. annotation.contents = listEditAnnotationViewController.contentString;
  935. [self setNeedsDisplayAnnotation:annotation];
  936. }
  937. }
  938. self.popOver = nil;
  939. }
  940. - (void)handlePageChangedNotification:(NSNotification *)notification {
  941. self.copyCount = 0;
  942. }
  943. #pragma mark - Form Alignment
  944. - (KMPDFViewActiveFormsType)formAnnotionsType:(NSArray *)annotationArray {
  945. NSSet *set = [NSSet setWithArray:[annotationArray valueForKey:@"type"]];
  946. NSArray *array = [set allObjects];
  947. if (array.count > 1 || ![array.firstObject isEqualToString:@"Widget"]) {
  948. return KMPDFViewActiveFormsType_NotAllform;
  949. }
  950. PDFAnnotation *referenceAnnotation = annotationArray.firstObject;
  951. Class referenceAnnotationClass = [referenceAnnotation class];
  952. if ([referenceAnnotationClass isEqual:[CPDFButtonWidgetAnnotation class]]) {
  953. referenceAnnotationClass = [CPDFButtonWidgetAnnotation superclass];
  954. }
  955. if ([referenceAnnotationClass isEqual:[CPDFTextWidgetAnnotation class]]) {
  956. referenceAnnotationClass = [CPDFTextWidgetAnnotation superclass];
  957. }
  958. if ([referenceAnnotationClass isEqual:[CPDFChoiceWidgetAnnotation class]]) {
  959. referenceAnnotationClass = [CPDFChoiceWidgetAnnotation superclass];
  960. }
  961. if (annotationArray.count == 1) {
  962. if ([referenceAnnotation isKindOfClass:[CPDFTextWidgetAnnotation class]]) {
  963. return KMPDFViewActiveFormsType_TextSingle;
  964. } else {
  965. return KMPDFViewActiveFormsType_PureSingle;
  966. }
  967. }
  968. for (NSInteger i = 0; i < annotationArray.count; i++) {
  969. PDFAnnotation *annotation = [annotationArray objectAtIndex:i];
  970. if (![annotation isKindOfClass:[CPDFButtonWidgetAnnotation class]] && ![annotation isKindOfClass:[CPDFTextWidgetAnnotation class]] && ![annotation isKindOfClass:[CPDFChoiceWidgetAnnotation class]]) {
  971. return KMPDFViewActiveFormsType_Mix;
  972. }
  973. }
  974. if ([referenceAnnotation isKindOfClass:[CPDFTextWidgetAnnotation class]]){
  975. return KMPDFViewActiveFormsType_Text;
  976. }
  977. return KMPDFViewActiveFormsType_Pure;
  978. }
  979. - (void)formsAnnotationAlign:(KMPDFActiveFormsAlignType)type {
  980. CGRect zeroRect = CGRectNull;
  981. CGRect highestRect = CGRectZero;
  982. CGRect widthestRect = CGRectZero;
  983. CPDFAnnotation *firstAnnotation = self.activeAnnotations.firstObject;
  984. CGRect leftestRect = firstAnnotation.bounds;
  985. CGRect rightestRect = firstAnnotation.bounds;
  986. CGRect topestRect = firstAnnotation.bounds;
  987. CGRect bottomestRect = firstAnnotation.bounds;
  988. CPDFAnnotation *leftestAnnotation = firstAnnotation;
  989. CPDFAnnotation *rightestAnnotation = firstAnnotation;
  990. CPDFAnnotation *topestAnnotation = firstAnnotation;
  991. CPDFAnnotation *bottomestAnnotation = firstAnnotation;
  992. CGFloat totalWidth = 0.;
  993. CGFloat totalHeight = 0.;
  994. for (CPDFAnnotation *annotation in self.activeAnnotations) {
  995. zeroRect = CGRectUnion(zeroRect, annotation.bounds);
  996. totalWidth += CGRectGetWidth(annotation.bounds);
  997. totalHeight += CGRectGetHeight(annotation.bounds);
  998. if (CGRectGetHeight(annotation.bounds) > CGRectGetHeight(highestRect)) {
  999. highestRect = annotation.bounds;
  1000. }
  1001. if (CGRectGetWidth(annotation.bounds) > CGRectGetWidth(widthestRect)) {
  1002. widthestRect = annotation.bounds;
  1003. }
  1004. if (CGRectGetMinX(leftestRect) > CGRectGetMinX(annotation.bounds)) {
  1005. leftestRect = annotation.bounds;
  1006. leftestAnnotation = annotation;
  1007. }
  1008. if (CGRectGetMaxX(annotation.bounds) > CGRectGetMaxX(rightestRect)) {
  1009. rightestRect = annotation.bounds;
  1010. rightestAnnotation = annotation;
  1011. }
  1012. if (CGRectGetMaxY(annotation.bounds) > CGRectGetMaxY(topestRect)) {
  1013. topestRect = annotation.bounds;
  1014. topestAnnotation = annotation;
  1015. }
  1016. if (CGRectGetMinY(bottomestRect) > CGRectGetMinY(annotation.bounds)) {
  1017. bottomestRect = annotation.bounds;
  1018. bottomestAnnotation = annotation;
  1019. }
  1020. }
  1021. if (type == KMPDFActiveFormsAlignType_Left ) {
  1022. NSMutableArray *newBoundsArray = [NSMutableArray array];
  1023. for (NSInteger i = 0; i< self.activeAnnotations.count; i++) {
  1024. CPDFAnnotation *annotation = [self.activeAnnotations objectAtIndex:i];
  1025. CGRect bounds = annotation.bounds;
  1026. bounds.origin.x = zeroRect.origin.x;
  1027. [newBoundsArray addObject:NSStringFromRect(bounds)];
  1028. }
  1029. [self changeAnnotationsAlign:self.activeAnnotations newBounds:newBoundsArray];
  1030. } else if (type == KMPDFActiveFormsAlignType_Right) {
  1031. NSMutableArray *newBoundsArray = [NSMutableArray array];
  1032. for (NSInteger i = 0; i< self.activeAnnotations.count; i++) {
  1033. CPDFAnnotation *annotation = [self.activeAnnotations objectAtIndex:i];
  1034. CGRect bounds = annotation.bounds;
  1035. bounds.origin.x = CGRectGetMaxX(zeroRect) - bounds.size.width;
  1036. [newBoundsArray addObject:NSStringFromRect(bounds)];
  1037. }
  1038. [self changeAnnotationsAlign:self.activeAnnotations newBounds:newBoundsArray];
  1039. } else if (type == KMPDFActiveFormsAlignType_Top) {
  1040. NSMutableArray *newBoundsArray = [NSMutableArray array];
  1041. for (NSInteger i = 0; i< self.activeAnnotations.count; i++) {
  1042. CPDFAnnotation *annotation = [self.activeAnnotations objectAtIndex:i];
  1043. CGRect bounds = annotation.bounds;
  1044. bounds.origin.y = CGRectGetMaxY(zeroRect) - bounds.size.height;
  1045. [newBoundsArray addObject:NSStringFromRect(bounds)];
  1046. }
  1047. [self changeAnnotationsAlign:self.activeAnnotations newBounds:newBoundsArray];
  1048. } else if (type == KMPDFActiveFormsAlignType_Bottom) {
  1049. NSMutableArray *newBoundsArray = [NSMutableArray array];
  1050. for (NSInteger i = 0; i< self.activeAnnotations.count; i++) {
  1051. CPDFAnnotation *annotation = [self.activeAnnotations objectAtIndex:i];
  1052. CGRect bounds = annotation.bounds;
  1053. bounds.origin.y = CGRectGetMinY(zeroRect);
  1054. [newBoundsArray addObject:NSStringFromRect(bounds)];
  1055. }
  1056. [self changeAnnotationsAlign:self.activeAnnotations newBounds:newBoundsArray];
  1057. } else if (type == KMPDFActiveFormsAlignType_Horizontally) {
  1058. NSMutableArray *newBoundsArray = [NSMutableArray array];
  1059. for (NSInteger i = 0; i< self.activeAnnotations.count; i++) {
  1060. CPDFAnnotation *annotation = [self.activeAnnotations objectAtIndex:i];
  1061. CGRect bounds = annotation.bounds;
  1062. bounds.origin.y = CGRectGetMidY(highestRect) - CGRectGetHeight(bounds)/2;
  1063. [newBoundsArray addObject:NSStringFromRect(bounds)];
  1064. }
  1065. [self changeAnnotationsAlign:self.activeAnnotations newBounds:newBoundsArray];
  1066. } else if (type == KMPDFActiveFormsAlignType_Vertical) {
  1067. NSMutableArray *newBoundsArray = [NSMutableArray array];
  1068. for (NSInteger i = 0; i< self.activeAnnotations.count; i++) {
  1069. CPDFAnnotation *annotation = [self.activeAnnotations objectAtIndex:i];
  1070. CGRect bounds = annotation.bounds;
  1071. bounds.origin.x = CGRectGetMidX(widthestRect) - CGRectGetWidth(bounds)/2;
  1072. [newBoundsArray addObject:NSStringFromRect(bounds)];
  1073. }
  1074. [self changeAnnotationsAlign:self.activeAnnotations newBounds:newBoundsArray];
  1075. } else if (type == KMPDFActiveFormsAlignType_DisHorizontally) {
  1076. CGFloat middleGap = CGRectGetWidth(zeroRect) - CGRectGetWidth(leftestRect) - CGRectGetWidth(rightestRect);
  1077. CGFloat otherAnnotationsTotalWidth = totalWidth - CGRectGetWidth(leftestRect) - CGRectGetWidth(rightestRect);
  1078. CGFloat gap = (middleGap - otherAnnotationsTotalWidth)/(self.activeAnnotations.count - 1);
  1079. NSMutableArray *annotationsCopyArray = [self.activeAnnotations mutableCopy];
  1080. [annotationsCopyArray sortUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) {
  1081. CPDFAnnotation *annotation1 = (CPDFAnnotation *)obj1;
  1082. CPDFAnnotation *annotation2 = (CPDFAnnotation *)obj2;
  1083. if (annotation1.bounds.origin.x < annotation2.bounds.origin.x) {
  1084. return NSOrderedAscending;
  1085. } else {
  1086. return NSOrderedDescending;
  1087. }
  1088. }];
  1089. [annotationsCopyArray removeObject:leftestAnnotation];
  1090. [annotationsCopyArray removeObject:rightestAnnotation];
  1091. NSMutableArray *newBoundsArray = [NSMutableArray array];
  1092. CGFloat leftStartX = CGRectGetMaxX(leftestRect) + gap;
  1093. for (NSInteger i = 0; i < annotationsCopyArray.count; i++) {
  1094. CPDFAnnotation *annotation = [annotationsCopyArray objectAtIndex:i];
  1095. CGRect bounds = annotation.bounds;
  1096. bounds.origin.x = leftStartX;
  1097. [newBoundsArray addObject:NSStringFromRect(bounds)];
  1098. leftStartX += CGRectGetWidth(bounds) + gap;
  1099. }
  1100. [self changeAnnotationsAlign:annotationsCopyArray newBounds:newBoundsArray];
  1101. } else if (type == KMPDFActiveFormsAlignType_DisVertical) {
  1102. CGFloat middleGap = CGRectGetHeight(zeroRect) - CGRectGetHeight(topestRect) - CGRectGetHeight(bottomestRect);
  1103. CGFloat otherAnnotationsTotalHeight = totalHeight - CGRectGetHeight(topestRect) - CGRectGetHeight(bottomestRect);
  1104. CGFloat gap = (middleGap - otherAnnotationsTotalHeight)/(self.activeAnnotations.count - 1);
  1105. NSMutableArray *annotationsCopyArray = [self.activeAnnotations mutableCopy];
  1106. [annotationsCopyArray sortUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) {
  1107. CPDFAnnotation *annotation1 = (CPDFAnnotation *)obj1;
  1108. CPDFAnnotation *annotation2 = (CPDFAnnotation *)obj2;
  1109. if (annotation1.bounds.origin.y < annotation2.bounds.origin.y) {
  1110. return NSOrderedAscending;
  1111. } else {
  1112. return NSOrderedDescending;
  1113. }
  1114. }];
  1115. [annotationsCopyArray removeObject:topestAnnotation];
  1116. [annotationsCopyArray removeObject:bottomestAnnotation];
  1117. NSMutableArray *newBoundsArray = [NSMutableArray array];
  1118. CGFloat bottomStartY = CGRectGetMaxY(bottomestRect) + gap;
  1119. for (NSInteger i = 0; i < annotationsCopyArray.count; i++) {
  1120. PDFAnnotation *annotation = [annotationsCopyArray objectAtIndex:i];
  1121. CGRect bounds = annotation.bounds;
  1122. bounds.origin.y = bottomStartY;
  1123. [newBoundsArray addObject:NSStringFromRect(bounds)];
  1124. bottomStartY += CGRectGetHeight(bounds) + gap;
  1125. }
  1126. [self changeAnnotationsAlign:annotationsCopyArray newBounds:newBoundsArray];
  1127. }
  1128. }
  1129. - (void)changeAnnotationsAlign:(NSArray *)annotationArray newBounds:(NSArray *)newBoundsArray {
  1130. NSMutableArray * oldBoundsArray = [NSMutableArray array];
  1131. for (NSInteger i = 0; i < annotationArray.count; i++) {
  1132. CPDFAnnotation *annotation = [annotationArray objectAtIndex:i];
  1133. [oldBoundsArray addObject:NSStringFromRect(annotation.bounds)];
  1134. annotation.bounds = NSRectFromString([newBoundsArray objectAtIndex:i]);
  1135. }
  1136. [[[self undoManager] prepareWithInvocationTarget:self] changeAnnotationsAlign:annotationArray newBounds:oldBoundsArray];
  1137. [self setNeedsDisplay:YES];
  1138. }
  1139. @end