KMSignatureAnnotationViewController.m 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. //
  2. // KMSignatureAnnotationViewController.m
  3. // PDF Reader Pro
  4. //
  5. // Created by lxy on 2023/1/10.
  6. //
  7. #import "KMSignatureAnnotationViewController.h"
  8. #import <PDF_Reader_Pro-Swift.h>
  9. @interface KMSignatureAnnotationViewController ()<NSCollectionViewDelegate,
  10. NSCollectionViewDataSource,
  11. KMSigntureViewItemDelegate>
  12. @property (weak) IBOutlet NSBox *emptyBox;
  13. @property (weak) IBOutlet NSTextField *signatureLabel;
  14. @property (weak) IBOutlet NSTextField *emptyTipLabel;
  15. @property (weak) IBOutlet NSTextField *emptyMinTipLabel;
  16. @property (nonatomic,strong) NSMutableArray <KMSignature *>*signatures;
  17. @property (nonatomic,strong) NSMutableArray <KMSignature *>* selectSignatures;
  18. @property (strong) IBOutlet NSButton *addButton;
  19. @property (strong) IBOutlet NSCollectionView *collectionView;
  20. @property (nonatomic,strong) KMSignature *clickSignature;
  21. @property (nonatomic, retain) CALayer *addButtonLayer;
  22. @property (nonatomic, strong) KMSignatureWindowController *winC;
  23. @property (nonatomic, assign) BOOL isFirstAppear;
  24. @end
  25. @implementation KMSignatureAnnotationViewController
  26. - (void)viewDidAppear{
  27. [super viewDidAppear];
  28. KMSignatureManager *signatureManager = [[KMSignatureManager alloc] init];
  29. [signatureManager loadAllSignatureList];
  30. if (signatureManager.signatureList.count <= 0) {
  31. if (self.isFirstAppear == NO) {
  32. return;
  33. }
  34. [self showSignatureWindow];
  35. }
  36. self.isFirstAppear = NO;
  37. }
  38. - (void)viewDidLoad {
  39. [super viewDidLoad];
  40. self.isFirstAppear = YES;
  41. self.signatures = [[NSMutableArray alloc] init];
  42. self.selectSignatures = [[NSMutableArray alloc] init];
  43. [self reloadDataSignatures];
  44. self.collectionView.delegate = self;
  45. self.collectionView.dataSource = self;
  46. [self.collectionView registerClass:[KMSigntureViewItem class] forItemWithIdentifier:@"kmSigntureViewItem"];
  47. self.collectionView.backgroundColors = @[[NSColor clearColor]];
  48. self.collectionView.selectable = YES;
  49. self.collectionView.allowsMultipleSelection = YES;
  50. [self setup];
  51. [self updateLanguage];
  52. // [self insertSignatureButton_Click:nil];
  53. }
  54. - (void)setup {
  55. self.signatureLabel.font = [NSFont SFProTextSemiboldFont: 14.0];
  56. self.signatureLabel.textColor = [KMAppearance KMColor_Layout_H0];
  57. // [NSColor km_initWithHex:@"#252629" alpha:1];
  58. self.emptyTipLabel.font = [NSFont SFProTextRegularFont: 14.0];
  59. self.emptyTipLabel.textColor = [NSColor km_initWithHex:@"#616469" alpha:1];
  60. _addButton.wantsLayer = YES;
  61. _addButton.layer.backgroundColor = [NSColor colorWithRed:78/255. green:127/255. blue:219/255. alpha:1.0].CGColor;//[KMAppearance KMColor_Interactive_M0].CGColor;
  62. _addButton.title = [NSString stringWithFormat:@" %@", NSLocalizedString(@"New Signature", nil)];
  63. [_addButton setTitleColor:[NSColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:1.0]];
  64. _addButton.image = [NSImage imageNamed:@"KMImageNameUXIconBtnAddWhite"];
  65. // self.addButtonLayer = [CALayer layer];
  66. // [self.addButton.layer addSublayer:self.addButtonLayer];
  67. // self.addButtonLayer.backgroundColor = [KMAppearance KMColor_Status_Hov].CGColor;
  68. // self.addButtonLayer.cornerRadius = 0;
  69. // self.addButtonLayer.hidden = YES;
  70. }
  71. - (void)updateLanguage {
  72. self.signatureLabel.stringValue = NSLocalizedString(@"Signature", nil);
  73. self.emptyTipLabel.stringValue = NSLocalizedString(@"No Signatures", nil);
  74. self.emptyMinTipLabel.stringValue = NSLocalizedString(@"Click \"New Signature\" button below to create a signature.", nil);
  75. NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
  76. paragraphStyle.lineHeightMultiple = 1.32;
  77. paragraphStyle.alignment = NSTextAlignmentCenter;
  78. NSDictionary* dic = [NSDictionary dictionaryWithObjectsAndKeys: [NSFont SFProTextRegularFont: 12.0],NSFontAttributeName,
  79. [NSColor km_initWithHex:@"#94989C" alpha:1],
  80. NSForegroundColorAttributeName,paragraphStyle,NSParagraphStyleAttributeName,nil];
  81. NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:self.emptyMinTipLabel.stringValue];
  82. [string addAttributes:dic range:NSMakeRange(0, string.length)];
  83. NSRange range = [self.emptyMinTipLabel.stringValue rangeOfString:NSLocalizedString(@"Add button", comment: "")];
  84. [string setAttributes:[NSDictionary dictionaryWithObjectsAndKeys: [NSFont SFProTextSemiboldFont: 12.0],NSFontAttributeName,
  85. [NSColor km_initWithHex:@"#94989C" alpha:1],
  86. NSForegroundColorAttributeName,paragraphStyle,NSParagraphStyleAttributeName,nil] range:range];
  87. self.emptyMinTipLabel.attributedStringValue = string;
  88. }
  89. - (void)tableViewMenu {
  90. NSMenu *menu = [[NSMenu alloc] init];
  91. // NSMenuItem *item = [menu addItemWithTitle:NSLocalizedString(@"Export stamp", nil) action:nil target:self];
  92. // NSMenu *tSubMenu = [[NSMenu alloc] init];
  93. // NSMenuItem *tMenuItem = [tSubMenu insertItemWithTitle:NSLocalizedString(@"PNG", @"Menu item title") action:@selector(exportCustomizeStamp:) target:self atIndex:0];
  94. // tMenuItem.tag = 0;
  95. // tMenuItem = [tSubMenu insertItemWithTitle:NSLocalizedString(@"JPG", @"Menu item title") action:@selector(exportCustomizeStamp:) target:self atIndex:1];
  96. // tMenuItem.tag = 1;
  97. // tMenuItem = [tSubMenu insertItemWithTitle:NSLocalizedString(@"PDF", @"Menu item title") action:@selector(exportCustomizeStamp:) target:self atIndex:2];
  98. // tMenuItem.tag = 2;
  99. // item.submenu = tSubMenu;
  100. // [menu addItem:[NSMenuItem separatorItem]];
  101. // [menu addItemWithTitle:NSLocalizedString(@"Delete stamp", nil) action:@selector(deleteCustomizeStamp) target:self];
  102. [menu addItemWithTitle:NSLocalizedString(@"Remove All", nil) action:@selector(deleteAllCustomizeStamp) target:self];
  103. self.collectionView.menu = menu;
  104. }
  105. - (void)tableCellCloseButton_Click:(NSButton *)button {
  106. NSInteger clickIndex = [self.signatures indexOfObject:self.clickSignature];
  107. if(button.tag >= 0 && button.tag < self.signatures.count) {
  108. KMSignatureManager *signatureManager = [[KMSignatureManager alloc] init];
  109. [signatureManager loadAllSignatureList];
  110. [signatureManager removeObjectWithIndex:button.tag];
  111. [signatureManager saveSingaturesToFile];
  112. [self reloadDataSignatures];
  113. }
  114. if(clickIndex == button.tag) {
  115. [self.pdfView setAddStampObject:nil keepToolModel:false];
  116. }
  117. }
  118. - (IBAction)insertSignatureButton_Click:(id)sender {
  119. if (![[IAPProductsManager defaultManager] isAvailableAllFunction]) {
  120. NSWindowController *winC = [KMPurchaseCompareWindowController sharedInstance];
  121. if (self.kEventTag == 1) {
  122. winC.kEventName = @"Reading_FillSign_Signature_BuyNow";
  123. } else {
  124. winC.kEventName = @"Reading_Signature_BuyNow";
  125. }
  126. [winC showWindow:nil];
  127. return;
  128. }
  129. [self showSignatureWindow];
  130. }
  131. - (void)showSignatureWindow{
  132. if (self.winC != nil) {
  133. return;
  134. }
  135. KMSignatureWindowController *signatureWindowController = [[KMSignatureWindowController alloc] init];
  136. self.winC = signatureWindowController;
  137. [self.view.window beginSheet:signatureWindowController.window completionHandler:^(NSModalResponse returnCode) {
  138. self.winC = nil;
  139. if (returnCode != 0) {
  140. [self reloadDataSignaturesIsSelectLast:YES];
  141. }
  142. }];
  143. }
  144. - (void)reloadDataSignatures {
  145. [self reloadDataSignaturesIsSelectLast:NO];
  146. }
  147. - (void)reloadDataSignaturesIsSelectLast:(BOOL)selectLast {
  148. [self.signatures removeAllObjects];
  149. KMSignatureManager *signatureManager = [[KMSignatureManager alloc] init];
  150. [signatureManager loadAllSignatureList];
  151. [self.signatures addObjectsFromArray:signatureManager.signatureList];
  152. if (selectLast && self.signatures.count > 0) {
  153. [self.selectSignatures removeAllObjects];
  154. KMSignature *selectedSig = self.signatures.lastObject;
  155. selectedSig.isSelect = YES;
  156. [self.selectSignatures addObject:selectedSig];
  157. CStampSignatureObject *sigObject = [[CStampSignatureObject alloc] initWithSignatureImage:selectedSig.pathsImage];
  158. self.clickSignature = selectedSig;
  159. [self.pdfView setAddStampObject:sigObject keepToolModel:true];
  160. }
  161. if (_signatures.count > 0) {
  162. [self reloadCustomEmptyState:NO];
  163. [self tableViewMenu];
  164. } else {
  165. [self reloadCustomEmptyState:YES];
  166. self.collectionView.menu = nil;
  167. }
  168. [self.collectionView reloadData];
  169. }
  170. - (void)reloadCustomEmptyState:(BOOL)isEmpty {
  171. if (isEmpty) {
  172. self.emptyBox.hidden = NO;
  173. self.collectionView.hidden = YES;
  174. } else {
  175. self.emptyBox.hidden = YES;
  176. self.collectionView.hidden = NO;
  177. }
  178. }
  179. //MARK: menu Action
  180. - (void)exportCustomizeStamp:(NSMenuItem *)sender {
  181. if (self.collectionView.selectionIndexes.count != 1) {
  182. return;
  183. }
  184. NSInteger index = 0;
  185. for (NSIndexPath *indexPath in self.collectionView.selectionIndexPaths) {
  186. index = indexPath.item;
  187. }
  188. NSInteger type = sender.tag;
  189. NSImage *image = self.signatures[index].pathsImage;
  190. if (type == 0) {
  191. NSData *data = image.TIFFRepresentation;
  192. NSBitmapImageRep *imageRep = [NSBitmapImageRep imageRepWithData:data];
  193. [imageRep setSize:image.size];
  194. NSData *imageData = [imageRep representationUsingType:NSBitmapImageFileTypePNG properties:@{}];
  195. NSSavePanel *savePanel = [NSSavePanel savePanel];
  196. savePanel.allowedFileTypes = @[@"png"];
  197. [savePanel beginSheetModalForWindow:[NSApp mainWindow] completionHandler:^(NSInteger result) {
  198. if (result) {
  199. if ([imageData writeToURL:savePanel.URL atomically:YES]) {
  200. [[NSWorkspace sharedWorkspace] selectFile:savePanel.URL.path
  201. inFileViewerRootedAtPath:@""];
  202. }
  203. }
  204. }];
  205. } else if (type == 1) {
  206. NSData *data = image.TIFFRepresentation;
  207. NSBitmapImageRep *imageRep = [NSBitmapImageRep imageRepWithData:data];
  208. [imageRep setSize:image.size];
  209. NSData *imageData = [imageRep representationUsingType:NSBitmapImageFileTypeJPEG properties:@{}];
  210. NSSavePanel *savePanel = [NSSavePanel savePanel];
  211. savePanel.allowedFileTypes = @[@"jpg"];
  212. [savePanel beginSheetModalForWindow:[NSApp mainWindow] completionHandler:^(NSInteger result) {
  213. if (result) {
  214. if ([imageData writeToURL:savePanel.URL atomically:YES]) {
  215. [[NSWorkspace sharedWorkspace] selectFile:savePanel.URL.path
  216. inFileViewerRootedAtPath:@""];
  217. }
  218. }
  219. }];
  220. } else {
  221. CPDFDocument *pdf = [[CPDFDocument alloc] init];
  222. NSString *signatureImagePath =[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"signatureImage.png"];
  223. NSData *data = image.TIFFRepresentation;
  224. [data writeToURL:[NSURL fileURLWithPath:signatureImagePath] atomically:YES];
  225. [pdf insertPage:image.size withImage:signatureImagePath atIndex:0];
  226. NSSavePanel *savePanel = [NSSavePanel savePanel];
  227. savePanel.allowedFileTypes = @[@"pdf"];
  228. [savePanel beginSheetModalForWindow:[NSApp mainWindow] completionHandler:^(NSInteger result) {
  229. if (result) {
  230. if ([pdf writeToURL:savePanel.URL]) {
  231. [[NSWorkspace sharedWorkspace] selectFile:savePanel.URL.path
  232. inFileViewerRootedAtPath:@""];
  233. }
  234. }
  235. }];
  236. }
  237. }
  238. - (void)deleteCustomizeStamp {
  239. if (self.collectionView.selectionIndexes.count != 1) {
  240. return;
  241. }
  242. NSInteger index = 0;
  243. for (NSIndexPath *indexPath in self.collectionView.selectionIndexPaths) {
  244. index = indexPath.item;
  245. }
  246. NSInteger clickIndex = [self.signatures indexOfObject:self.clickSignature];
  247. if(index < self.signatures.count) {
  248. KMSignatureManager *signatureManager = [[KMSignatureManager alloc] init];
  249. [signatureManager loadAllSignatureList];
  250. [signatureManager removeObjectWithIndex:index];
  251. [signatureManager saveSingaturesToFile];
  252. [self reloadDataSignatures];
  253. }
  254. if(clickIndex == index) {
  255. [self.pdfView setAddStampObject:nil keepToolModel:false];
  256. }
  257. }
  258. - (void)deleteAllCustomizeStamp {
  259. NSAlert *alert = [[NSAlert alloc] init];
  260. alert.messageText = NSLocalizedString(@"Are you sure to delete all signatures?", nil);
  261. [alert addButtonWithTitle:NSLocalizedString(@"Delete", nil)];
  262. [alert addButtonWithTitle:NSLocalizedString(@"Cancel", nil)];
  263. if ([alert runModal] != NSAlertFirstButtonReturn) { /// 取消
  264. return;
  265. }
  266. KMSignatureManager *signatureManager = [[KMSignatureManager alloc] init];
  267. [signatureManager loadAllSignatureList];
  268. [signatureManager removeAllObject];
  269. [signatureManager saveSingaturesToFile];
  270. [self reloadDataSignatures];
  271. [self.pdfView setAddStampObject:nil keepToolModel:NO];
  272. }
  273. #pragma mark - NSCollectionViewDelegate
  274. - (NSInteger)numberOfSectionsInCollectionView:(NSCollectionView *)collectionView {
  275. return 1;
  276. }
  277. - (NSInteger)collectionView:(NSCollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  278. return self.signatures.count;
  279. }
  280. - (nonnull NSCollectionViewItem *)collectionView:(nonnull NSCollectionView *)collectionView itemForRepresentedObjectAtIndexPath:(nonnull NSIndexPath *)indexPath {
  281. if (indexPath.item >= self.signatures.count) {
  282. return nil;
  283. }
  284. KMSigntureViewItem *item = [collectionView makeItemWithIdentifier:@"kmSigntureViewItem" forIndexPath:indexPath];
  285. KMSignature *signature = [self.signatures objectAtIndex:indexPath.item];
  286. item.iconImg.image = signature.pathsImage;
  287. item.deleteIconBtn.tag = indexPath.item;
  288. [item.deleteIconBtn setTarget:self];
  289. [item.deleteIconBtn setAction:@selector(tableCellCloseButton_Click:)];
  290. item.delegate = self;
  291. item.itemIndex = indexPath.item;
  292. item.isSelect = signature.isSelect;
  293. return item;
  294. }
  295. - (CGSize)collectionView:(NSCollectionView *)collectionView layout:(NSCollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
  296. return CGSizeMake(228, 64);
  297. }
  298. - (NSEdgeInsets)collectionView:(NSCollectionView *)collectionView layout:(NSCollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
  299. return NSEdgeInsetsMake(0, 16, 16, 16);
  300. }
  301. - (CGFloat)collectionView:(NSCollectionView *)collectionView layout:(NSCollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
  302. return 16;
  303. }
  304. - (CGFloat)collectionView:(NSCollectionView *)collectionView layout:(NSCollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
  305. return 16;
  306. }
  307. - (void)collectionView:(NSCollectionView *)collectionView didSelectItemsAtIndexPaths:(NSSet<NSIndexPath *> *)indexPaths {
  308. if (self.isClickSignatureList) {
  309. NSInteger index = 0;
  310. for (NSIndexPath *indexPath in self.collectionView.selectionIndexPaths) {
  311. index = indexPath.item;
  312. }
  313. KMSignature *selectSignature = [self.signatures objectAtIndex:index];
  314. CPDFAnnotation *editAnnotation = (CPDFAnnotation *)self.annotationModel.annotations.firstObject;
  315. CPDFPage *page = editAnnotation.page;
  316. if ([editAnnotation isKindOfClass:[CPDFSignatureWidgetAnnotation class]]) {
  317. CPDFSignatureWidgetAnnotation * signatureaAnnotation = nil;
  318. if ([editAnnotation isKindOfClass:[CPDFSignatureWidgetAnnotation class]]) {
  319. signatureaAnnotation = (CPDFSignatureWidgetAnnotation *)editAnnotation;
  320. } else {
  321. // signatureaAnnotation= [[CPDFSignatureWidgetAnnotation alloc] initKMNoteWithBounds:editAnnotation.bounds document:self.pdfView.document];
  322. signatureaAnnotation = [[CPDFSignatureWidgetAnnotation alloc] initWithKMNoteBounds:editAnnotation.bounds document:self.pdfView.document];
  323. signatureaAnnotation.fieldName = [(CPDFSignatureWidgetAnnotation *)editAnnotation fieldName];
  324. signatureaAnnotation.backgroundColor = [(CPDFSignatureWidgetAnnotation *)editAnnotation backgroundColor];
  325. signatureaAnnotation.contents = editAnnotation.contents;
  326. signatureaAnnotation.userName = editAnnotation.userName;
  327. signatureaAnnotation.modificationDate = editAnnotation.modificationDate;
  328. }
  329. [signatureaAnnotation signWithImage:selectSignature.pathsImage];
  330. [page removeAnnotation:editAnnotation];
  331. [page addAnnotation:signatureaAnnotation];
  332. [self.pdfView setNeedsDisplayAnnotationViewForPage:page];
  333. [signatureaAnnotation setShouldDisplay:YES];
  334. if (self.view.window != nil) {
  335. [KMTools setDocumentEditedStateWithWindow:self.view.window];
  336. }
  337. }
  338. return;
  339. }
  340. if (indexPaths.count == 1) {
  341. NSInteger index = 0;
  342. for (NSIndexPath *indexPath in self.collectionView.selectionIndexPaths) {
  343. index = indexPath.item;
  344. }
  345. for (KMSignature *signature in self.selectSignatures) {
  346. signature.isSelect = NO;
  347. }
  348. [self.collectionView reloadData];
  349. if (index < self.signatures.count) {
  350. [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"KMContinuousAdditionStamp"];
  351. KMSignature *signature = [self.signatures objectAtIndex:index];
  352. signature.isSelect = YES;
  353. CStampSignatureObject *sigObject = [[CStampSignatureObject alloc] initWithSignatureImage:signature.pathsImage];
  354. self.clickSignature = signature;
  355. [self.pdfView setAddStampObject:sigObject keepToolModel:true];
  356. [self.collectionView reloadItemsAtIndexPaths:self.collectionView.selectionIndexPaths];
  357. [self.selectSignatures addObject:signature];
  358. }
  359. }
  360. }
  361. #pragma mark - KMSigntureViewItemDelegate
  362. - (void)kmSigntureViewItem:(KMSigntureViewItem *)item didClickMenuWithItem:(NSMenuItem *)menuItem {
  363. [self.collectionView deselectAll:nil];
  364. [self.collectionView selectItemsAtIndexPaths:[NSSet setWithObjects:[NSIndexPath indexPathForItem:item.itemIndex inSection:0],nil] scrollPosition:NSCollectionViewScrollPositionNone];
  365. [self exportCustomizeStamp:menuItem];
  366. }
  367. - (void)kmSigntureViewItemDidClickDeleteMenuItem:(KMSigntureViewItem *)item {
  368. [self.collectionView deselectAll:nil];
  369. [self.collectionView selectItemsAtIndexPaths:[NSSet setWithObjects:[NSIndexPath indexPathForItem:item.itemIndex inSection:0],nil] scrollPosition:NSCollectionViewScrollPositionNone];
  370. [self deleteCustomizeStamp];
  371. }
  372. - (void)kmSigntureViewItemDidClickAllDeleteMenuItem:(KMSigntureViewItem *)item {
  373. [self.collectionView deselectAll:nil];
  374. [self.collectionView selectItemsAtIndexPaths:[NSSet setWithObjects:[NSIndexPath indexPathForItem:item.itemIndex inSection:0],nil] scrollPosition:NSCollectionViewScrollPositionNone];
  375. [self deleteAllCustomizeStamp];
  376. }
  377. @end