KMSignatureAnnotationViewController.m 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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. NSWindowController *winC = [KMPurchaseCompareWindowController sharedInstance];
  115. if (self.kEventTag == 1) {
  116. winC.kEventName = @"Reading_FillSign_Signature_BuyNow";
  117. } else {
  118. winC.kEventName = @"Reading_Signature_BuyNow";
  119. }
  120. [winC showWindow:nil];
  121. return;
  122. }
  123. [self showSignatureWindow];
  124. }
  125. - (void)showSignatureWindow{
  126. if (self.winC != nil) {
  127. return;
  128. }
  129. KMSignatureWindowController *signatureWindowController = [[KMSignatureWindowController alloc] init];
  130. self.winC = signatureWindowController;
  131. [self.view.window beginSheet:signatureWindowController.window completionHandler:^(NSModalResponse returnCode) {
  132. self.winC = nil;
  133. if (returnCode != 0) {
  134. [self reloadDataSignaturesIsSelectLast:YES];
  135. }
  136. }];
  137. }
  138. - (void)reloadDataSignatures {
  139. [self reloadDataSignaturesIsSelectLast:NO];
  140. }
  141. - (void)reloadDataSignaturesIsSelectLast:(BOOL)selectLast {
  142. [self.signatures removeAllObjects];
  143. KMSignatureManager *signatureManager = [[KMSignatureManager alloc] init];
  144. [signatureManager loadAllSignatureList];
  145. [self.signatures addObjectsFromArray:signatureManager.signatureList];
  146. if (selectLast && self.signatures.count > 0) {
  147. [self.selectSignatures removeAllObjects];
  148. KMSignature *selectedSig = self.signatures.lastObject;
  149. selectedSig.isSelect = YES;
  150. [self.selectSignatures addObject:selectedSig];
  151. CStampSignatureObject *sigObject = [[CStampSignatureObject alloc] initWithSignatureImage:selectedSig.pathsImage];
  152. self.clickSignature = selectedSig;
  153. [self.pdfView setAddStampObject:sigObject keepToolModel:true];
  154. }
  155. if (_signatures.count > 0) {
  156. [self reloadCustomEmptyState:NO];
  157. [self tableViewMenu];
  158. } else {
  159. [self reloadCustomEmptyState:YES];
  160. self.collectionView.menu = nil;
  161. }
  162. [self.collectionView reloadData];
  163. }
  164. - (void)reloadCustomEmptyState:(BOOL)isEmpty {
  165. if (isEmpty) {
  166. self.emptyBox.hidden = NO;
  167. self.collectionView.hidden = YES;
  168. } else {
  169. self.emptyBox.hidden = YES;
  170. self.collectionView.hidden = NO;
  171. }
  172. }
  173. //MARK: menu Action
  174. - (void)exportCustomizeStamp:(NSMenuItem *)sender {
  175. if (self.collectionView.selectionIndexes.count != 1) {
  176. return;
  177. }
  178. NSInteger index = 0;
  179. for (NSIndexPath *indexPath in self.collectionView.selectionIndexPaths) {
  180. index = indexPath.item;
  181. }
  182. NSInteger type = sender.tag;
  183. NSImage *image = self.signatures[index].pathsImage;
  184. if (type == 0) {
  185. NSData *data = image.TIFFRepresentation;
  186. NSBitmapImageRep *imageRep = [NSBitmapImageRep imageRepWithData:data];
  187. [imageRep setSize:image.size];
  188. NSData *imageData = [imageRep representationUsingType:NSBitmapImageFileTypePNG properties:@{}];
  189. NSSavePanel *savePanel = [NSSavePanel savePanel];
  190. savePanel.allowedFileTypes = @[@"png"];
  191. [savePanel beginSheetModalForWindow:[NSApp mainWindow] completionHandler:^(NSInteger result) {
  192. if (result) {
  193. if ([imageData writeToURL:savePanel.URL atomically:YES]) {
  194. [[NSWorkspace sharedWorkspace] selectFile:savePanel.URL.path
  195. inFileViewerRootedAtPath:@""];
  196. }
  197. }
  198. }];
  199. } else if (type == 1) {
  200. NSData *data = image.TIFFRepresentation;
  201. NSBitmapImageRep *imageRep = [NSBitmapImageRep imageRepWithData:data];
  202. [imageRep setSize:image.size];
  203. NSData *imageData = [imageRep representationUsingType:NSBitmapImageFileTypeJPEG properties:@{}];
  204. NSSavePanel *savePanel = [NSSavePanel savePanel];
  205. savePanel.allowedFileTypes = @[@"jpg"];
  206. [savePanel beginSheetModalForWindow:[NSApp mainWindow] completionHandler:^(NSInteger result) {
  207. if (result) {
  208. if ([imageData writeToURL:savePanel.URL atomically:YES]) {
  209. [[NSWorkspace sharedWorkspace] selectFile:savePanel.URL.path
  210. inFileViewerRootedAtPath:@""];
  211. }
  212. }
  213. }];
  214. } else {
  215. CPDFDocument *pdf = [[CPDFDocument alloc] init];
  216. NSString *signatureImagePath =[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"signatureImage.png"];
  217. NSData *data = image.TIFFRepresentation;
  218. [data writeToURL:[NSURL fileURLWithPath:signatureImagePath] atomically:YES];
  219. [pdf insertPage:image.size withImage:signatureImagePath atIndex:0];
  220. NSSavePanel *savePanel = [NSSavePanel savePanel];
  221. savePanel.allowedFileTypes = @[@"pdf"];
  222. [savePanel beginSheetModalForWindow:[NSApp mainWindow] completionHandler:^(NSInteger result) {
  223. if (result) {
  224. if ([pdf writeToURL:savePanel.URL]) {
  225. [[NSWorkspace sharedWorkspace] selectFile:savePanel.URL.path
  226. inFileViewerRootedAtPath:@""];
  227. }
  228. }
  229. }];
  230. }
  231. }
  232. - (void)deleteCustomizeStamp {
  233. if (self.collectionView.selectionIndexes.count != 1) {
  234. return;
  235. }
  236. NSInteger index = 0;
  237. for (NSIndexPath *indexPath in self.collectionView.selectionIndexPaths) {
  238. index = indexPath.item;
  239. }
  240. NSInteger clickIndex = [self.signatures indexOfObject:self.clickSignature];
  241. if(index < self.signatures.count) {
  242. KMSignatureManager *signatureManager = [[KMSignatureManager alloc] init];
  243. [signatureManager loadAllSignatureList];
  244. [signatureManager removeObjectWithIndex:index];
  245. [signatureManager saveSingaturesToFile];
  246. [self reloadDataSignatures];
  247. }
  248. if(clickIndex == index) {
  249. [self.pdfView setAddStampObject:nil keepToolModel:false];
  250. }
  251. }
  252. - (void)deleteAllCustomizeStamp {
  253. NSAlert *alert = [[NSAlert alloc] init];
  254. alert.messageText = NSLocalizedString(@"Are you sure to delete all signatures?", nil);
  255. [alert addButtonWithTitle:NSLocalizedString(@"Delete", nil)];
  256. [alert addButtonWithTitle:NSLocalizedString(@"Cancel", nil)];
  257. if ([alert runModal] != NSAlertFirstButtonReturn) { /// 取消
  258. return;
  259. }
  260. KMSignatureManager *signatureManager = [[KMSignatureManager alloc] init];
  261. [signatureManager loadAllSignatureList];
  262. [signatureManager removeAllObject];
  263. [signatureManager saveSingaturesToFile];
  264. [self reloadDataSignatures];
  265. [self.pdfView setAddStampObject:nil keepToolModel:NO];
  266. }
  267. #pragma mark - NSCollectionViewDelegate
  268. - (NSInteger)numberOfSectionsInCollectionView:(NSCollectionView *)collectionView {
  269. return 1;
  270. }
  271. - (NSInteger)collectionView:(NSCollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  272. return self.signatures.count;
  273. }
  274. - (nonnull NSCollectionViewItem *)collectionView:(nonnull NSCollectionView *)collectionView itemForRepresentedObjectAtIndexPath:(nonnull NSIndexPath *)indexPath {
  275. if (indexPath.item >= self.signatures.count) {
  276. return nil;
  277. }
  278. KMSigntureViewItem *item = [collectionView makeItemWithIdentifier:@"kmSigntureViewItem" forIndexPath:indexPath];
  279. KMSignature *signature = [self.signatures objectAtIndex:indexPath.item];
  280. item.iconImg.image = signature.pathsImage;
  281. item.deleteIconBtn.tag = indexPath.item;
  282. [item.deleteIconBtn setTarget:self];
  283. [item.deleteIconBtn setAction:@selector(tableCellCloseButton_Click:)];
  284. item.delegate = self;
  285. item.itemIndex = indexPath.item;
  286. item.isSelect = signature.isSelect;
  287. return item;
  288. }
  289. - (CGSize)collectionView:(NSCollectionView *)collectionView layout:(NSCollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
  290. return CGSizeMake(228, 64);
  291. }
  292. - (NSEdgeInsets)collectionView:(NSCollectionView *)collectionView layout:(NSCollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
  293. return NSEdgeInsetsMake(0, 16, 16, 16);
  294. }
  295. - (CGFloat)collectionView:(NSCollectionView *)collectionView layout:(NSCollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
  296. return 16;
  297. }
  298. - (CGFloat)collectionView:(NSCollectionView *)collectionView layout:(NSCollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
  299. return 16;
  300. }
  301. - (void)collectionView:(NSCollectionView *)collectionView didSelectItemsAtIndexPaths:(NSSet<NSIndexPath *> *)indexPaths {
  302. if (self.isClickSignatureList) {
  303. NSInteger index = 0;
  304. for (NSIndexPath *indexPath in self.collectionView.selectionIndexPaths) {
  305. index = indexPath.item;
  306. }
  307. KMSignature *selectSignature = [self.signatures objectAtIndex:index];
  308. CPDFAnnotation *editAnnotation = (CPDFAnnotation *)self.annotationModel.annotations.firstObject;
  309. CPDFPage *page = editAnnotation.page;
  310. if ([editAnnotation isKindOfClass:[CPDFSignatureWidgetAnnotation class]]) {
  311. CPDFSignatureWidgetAnnotation * signatureaAnnotation = nil;
  312. if ([editAnnotation isKindOfClass:[CPDFSignatureWidgetAnnotation class]]) {
  313. signatureaAnnotation = (CPDFSignatureWidgetAnnotation *)editAnnotation;
  314. } else {
  315. // signatureaAnnotation= [[CPDFSignatureWidgetAnnotation alloc] initKMNoteWithBounds:editAnnotation.bounds document:self.pdfView.document];
  316. signatureaAnnotation = [[CPDFSignatureWidgetAnnotation alloc] initWithKMNoteBounds:editAnnotation.bounds document:self.pdfView.document];
  317. signatureaAnnotation.fieldName = [(CPDFSignatureWidgetAnnotation *)editAnnotation fieldName];
  318. signatureaAnnotation.backgroundColor = [(CPDFSignatureWidgetAnnotation *)editAnnotation backgroundColor];
  319. signatureaAnnotation.contents = editAnnotation.contents;
  320. signatureaAnnotation.userName = editAnnotation.userName;
  321. signatureaAnnotation.modificationDate = editAnnotation.modificationDate;
  322. }
  323. [signatureaAnnotation signWithImage:selectSignature.pathsImage];
  324. [page removeAnnotation:editAnnotation];
  325. [page addAnnotation:signatureaAnnotation];
  326. [self.pdfView setNeedsDisplayAnnotationViewForPage:page];
  327. [signatureaAnnotation setShouldDisplay:YES];
  328. if (self.view.window != nil) {
  329. [KMTools setDocumentEditedStateWithWindow:self.view.window];
  330. }
  331. }
  332. return;
  333. }
  334. if (indexPaths.count == 1) {
  335. NSInteger index = 0;
  336. for (NSIndexPath *indexPath in self.collectionView.selectionIndexPaths) {
  337. index = indexPath.item;
  338. }
  339. for (KMSignature *signature in self.selectSignatures) {
  340. signature.isSelect = NO;
  341. }
  342. [self.collectionView reloadData];
  343. if (index < self.signatures.count) {
  344. [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"KMContinuousAdditionStamp"];
  345. KMSignature *signature = [self.signatures objectAtIndex:index];
  346. signature.isSelect = YES;
  347. CStampSignatureObject *sigObject = [[CStampSignatureObject alloc] initWithSignatureImage:signature.pathsImage];
  348. self.clickSignature = signature;
  349. [self.pdfView setAddStampObject:sigObject keepToolModel:true];
  350. [self.collectionView reloadItemsAtIndexPaths:self.collectionView.selectionIndexPaths];
  351. [self.selectSignatures addObject:signature];
  352. }
  353. }
  354. }
  355. #pragma mark - KMSigntureViewItemDelegate
  356. - (void)kmSigntureViewItem:(KMSigntureViewItem *)item didClickMenuWithItem:(NSMenuItem *)menuItem {
  357. [self.collectionView deselectAll:nil];
  358. [self.collectionView selectItemsAtIndexPaths:[NSSet setWithObjects:[NSIndexPath indexPathForItem:item.itemIndex inSection:0],nil] scrollPosition:NSCollectionViewScrollPositionNone];
  359. [self exportCustomizeStamp:menuItem];
  360. }
  361. - (void)kmSigntureViewItemDidClickDeleteMenuItem:(KMSigntureViewItem *)item {
  362. [self.collectionView deselectAll:nil];
  363. [self.collectionView selectItemsAtIndexPaths:[NSSet setWithObjects:[NSIndexPath indexPathForItem:item.itemIndex inSection:0],nil] scrollPosition:NSCollectionViewScrollPositionNone];
  364. [self deleteCustomizeStamp];
  365. }
  366. - (void)kmSigntureViewItemDidClickAllDeleteMenuItem:(KMSigntureViewItem *)item {
  367. [self.collectionView deselectAll:nil];
  368. [self.collectionView selectItemsAtIndexPaths:[NSSet setWithObjects:[NSIndexPath indexPathForItem:item.itemIndex inSection:0],nil] scrollPosition:NSCollectionViewScrollPositionNone];
  369. [self deleteAllCustomizeStamp];
  370. }
  371. @end