CPDFListView.m 63 KB

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