KMSignatureAnnotationViewController.m 19 KB

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