123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457 |
- //
- // KMSignatureAnnotationViewController.m
- // PDF Reader Pro
- //
- // Created by lxy on 2023/1/10.
- //
- #import "KMSignatureAnnotationViewController.h"
- #import <PDF_Reader_Pro-Swift.h>
- @interface KMSignatureAnnotationViewController ()<NSCollectionViewDelegate,
- NSCollectionViewDataSource,
- KMSigntureViewItemDelegate>
- @property (weak) IBOutlet NSBox *emptyBox;
- @property (weak) IBOutlet NSTextField *signatureLabel;
- @property (weak) IBOutlet NSTextField *emptyTipLabel;
- @property (weak) IBOutlet NSTextField *emptyMinTipLabel;
- @property (nonatomic,strong) NSMutableArray <KMSignature *>*signatures;
- @property (nonatomic,strong) NSMutableArray <KMSignature *>* selectSignatures;
- @property (strong) IBOutlet NSButton *addButton;
- @property (strong) IBOutlet NSCollectionView *collectionView;
- @property (nonatomic,strong) KMSignature *clickSignature;
- @property (nonatomic, retain) CALayer *addButtonLayer;
- @property (nonatomic, strong) KMSignatureWindowController *winC;
- @property (nonatomic, assign) BOOL isFirstAppear;
- @end
- @implementation KMSignatureAnnotationViewController
- - (void)viewDidAppear{
- [super viewDidAppear];
- KMSignatureManager *signatureManager = [[KMSignatureManager alloc] init];
- [signatureManager loadAllSignatureList];
- if (signatureManager.signatureList.count <= 0) {
- if (self.isFirstAppear == NO) {
- return;
- }
- [self showSignatureWindow];
- }
-
- self.isFirstAppear = NO;
- }
- - (void)viewDidLoad {
- [super viewDidLoad];
- self.isFirstAppear = YES;
-
- self.signatures = [[NSMutableArray alloc] init];
- self.selectSignatures = [[NSMutableArray alloc] init];
- [self reloadDataSignatures];
-
- self.collectionView.delegate = self;
- self.collectionView.dataSource = self;
- [self.collectionView registerClass:[KMSigntureViewItem class] forItemWithIdentifier:@"kmSigntureViewItem"];
- self.collectionView.backgroundColors = @[[NSColor clearColor]];
- self.collectionView.selectable = YES;
- self.collectionView.allowsMultipleSelection = YES;
-
- [self setup];
- [self updateLanguage];
-
- // [self insertSignatureButton_Click:nil];
- }
- - (void)setup {
- self.signatureLabel.font = [NSFont SFProTextSemiboldFont: 14.0];
- self.signatureLabel.textColor = [KMAppearance KMColor_Layout_H0];
- // [NSColor km_initWithHex:@"#252629" alpha:1];
-
- self.emptyTipLabel.font = [NSFont SFProTextRegularFont: 14.0];
- self.emptyTipLabel.textColor = [NSColor km_initWithHex:@"#616469" alpha:1];
-
- _addButton.wantsLayer = YES;
- _addButton.layer.backgroundColor = [NSColor colorWithRed:78/255. green:127/255. blue:219/255. alpha:1.0].CGColor;//[KMAppearance KMColor_Interactive_M0].CGColor;
- _addButton.title = [NSString stringWithFormat:@" %@", NSLocalizedString(@"New Signature", nil)];
- [_addButton setTitleColor:[NSColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:1.0]];
- _addButton.image = [NSImage imageNamed:@"KMImageNameUXIconBtnAddWhite"];
- // self.addButtonLayer = [CALayer layer];
- // [self.addButton.layer addSublayer:self.addButtonLayer];
- // self.addButtonLayer.backgroundColor = [KMAppearance KMColor_Status_Hov].CGColor;
- // self.addButtonLayer.cornerRadius = 0;
- // self.addButtonLayer.hidden = YES;
- }
- - (void)updateLanguage {
- self.signatureLabel.stringValue = NSLocalizedString(@"Signature", nil);
- self.emptyTipLabel.stringValue = NSLocalizedString(@"No Signatures", nil);
- self.emptyMinTipLabel.stringValue = NSLocalizedString(@"Click \"New Signature\" button below to create a signature.", nil);
-
- NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
- paragraphStyle.lineHeightMultiple = 1.32;
- paragraphStyle.alignment = NSTextAlignmentCenter;
- NSDictionary* dic = [NSDictionary dictionaryWithObjectsAndKeys: [NSFont SFProTextRegularFont: 12.0],NSFontAttributeName,
- [NSColor km_initWithHex:@"#94989C" alpha:1],
- NSForegroundColorAttributeName,paragraphStyle,NSParagraphStyleAttributeName,nil];
-
- NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:self.emptyMinTipLabel.stringValue];
- [string addAttributes:dic range:NSMakeRange(0, string.length)];
-
- NSRange range = [self.emptyMinTipLabel.stringValue rangeOfString:NSLocalizedString(@"Add button", comment: "")];
- [string setAttributes:[NSDictionary dictionaryWithObjectsAndKeys: [NSFont SFProTextSemiboldFont: 12.0],NSFontAttributeName,
- [NSColor km_initWithHex:@"#94989C" alpha:1],
- NSForegroundColorAttributeName,paragraphStyle,NSParagraphStyleAttributeName,nil] range:range];
-
- self.emptyMinTipLabel.attributedStringValue = string;
- }
- - (void)tableViewMenu {
- NSMenu *menu = [[NSMenu alloc] init];
- // NSMenuItem *item = [menu addItemWithTitle:NSLocalizedString(@"Export stamp", nil) action:nil target:self];
- // NSMenu *tSubMenu = [[NSMenu alloc] init];
- // NSMenuItem *tMenuItem = [tSubMenu insertItemWithTitle:NSLocalizedString(@"PNG", @"Menu item title") action:@selector(exportCustomizeStamp:) target:self atIndex:0];
- // tMenuItem.tag = 0;
- // tMenuItem = [tSubMenu insertItemWithTitle:NSLocalizedString(@"JPG", @"Menu item title") action:@selector(exportCustomizeStamp:) target:self atIndex:1];
- // tMenuItem.tag = 1;
- // tMenuItem = [tSubMenu insertItemWithTitle:NSLocalizedString(@"PDF", @"Menu item title") action:@selector(exportCustomizeStamp:) target:self atIndex:2];
- // tMenuItem.tag = 2;
- // item.submenu = tSubMenu;
- // [menu addItem:[NSMenuItem separatorItem]];
- // [menu addItemWithTitle:NSLocalizedString(@"Delete stamp", nil) action:@selector(deleteCustomizeStamp) target:self];
- [menu addItemWithTitle:NSLocalizedString(@"Remove All", nil) action:@selector(deleteAllCustomizeStamp) target:self];
- self.collectionView.menu = menu;
- }
- - (void)tableCellCloseButton_Click:(NSButton *)button {
- NSInteger clickIndex = [self.signatures indexOfObject:self.clickSignature];
- if(button.tag >= 0 && button.tag < self.signatures.count) {
- KMSignatureManager *signatureManager = [[KMSignatureManager alloc] init];
- [signatureManager loadAllSignatureList];
- [signatureManager removeObjectWithIndex:button.tag];
- [signatureManager saveSingaturesToFile];
- [self reloadDataSignatures];
- }
-
- if(clickIndex == button.tag) {
- [self.pdfView setAddStampObject:nil keepToolModel:false];
- }
- }
- - (IBAction)insertSignatureButton_Click:(id)sender {
- if (![[IAPProductsManager defaultManager] isAvailableAllFunction]) {
- NSWindowController *winC = [KMPurchaseCompareWindowController sharedInstance];
- if (self.kEventTag == 1) {
- winC.kEventName = @"Reading_FillSign_Signature_BuyNow";
- } else {
- winC.kEventName = @"Reading_Signature_BuyNow";
- }
-
- [winC showWindow:nil];
- return;
- }
- [self showSignatureWindow];
- }
- - (void)showSignatureWindow{
- if (self.winC != nil) {
- return;
- }
-
- KMSignatureWindowController *signatureWindowController = [[KMSignatureWindowController alloc] init];
- self.winC = signatureWindowController;
- [self.view.window beginSheet:signatureWindowController.window completionHandler:^(NSModalResponse returnCode) {
- self.winC = nil;
- if (returnCode != 0) {
- [self reloadDataSignaturesIsSelectLast:YES];
- }
- }];
- }
- - (void)reloadDataSignatures {
- [self reloadDataSignaturesIsSelectLast:NO];
- }
- - (void)reloadDataSignaturesIsSelectLast:(BOOL)selectLast {
- [self.signatures removeAllObjects];
- KMSignatureManager *signatureManager = [[KMSignatureManager alloc] init];
- [signatureManager loadAllSignatureList];
- [self.signatures addObjectsFromArray:signatureManager.signatureList];
- if (selectLast && self.signatures.count > 0) {
- [self.selectSignatures removeAllObjects];
- KMSignature *selectedSig = self.signatures.lastObject;
- selectedSig.isSelect = YES;
- [self.selectSignatures addObject:selectedSig];
-
- CStampSignatureObject *sigObject = [[CStampSignatureObject alloc] initWithSignatureImage:selectedSig.pathsImage];
- self.clickSignature = selectedSig;
- [self.pdfView setAddStampObject:sigObject keepToolModel:true];
- }
-
- if (_signatures.count > 0) {
- [self reloadCustomEmptyState:NO];
- [self tableViewMenu];
- } else {
- [self reloadCustomEmptyState:YES];
- self.collectionView.menu = nil;
- }
- [self.collectionView reloadData];
- }
- - (void)reloadCustomEmptyState:(BOOL)isEmpty {
- if (isEmpty) {
- self.emptyBox.hidden = NO;
- self.collectionView.hidden = YES;
- } else {
- self.emptyBox.hidden = YES;
- self.collectionView.hidden = NO;
- }
- }
- //MARK: menu Action
- - (void)exportCustomizeStamp:(NSMenuItem *)sender {
- if (self.collectionView.selectionIndexes.count != 1) {
- return;
- }
-
- NSInteger index = 0;
- for (NSIndexPath *indexPath in self.collectionView.selectionIndexPaths) {
- index = indexPath.item;
- }
- NSInteger type = sender.tag;
- NSImage *image = self.signatures[index].pathsImage;
- if (type == 0) {
- NSData *data = image.TIFFRepresentation;
- NSBitmapImageRep *imageRep = [NSBitmapImageRep imageRepWithData:data];
- [imageRep setSize:image.size];
- NSData *imageData = [imageRep representationUsingType:NSBitmapImageFileTypePNG properties:@{}];
-
- NSSavePanel *savePanel = [NSSavePanel savePanel];
- savePanel.allowedFileTypes = @[@"png"];
- [savePanel beginSheetModalForWindow:[NSApp mainWindow] completionHandler:^(NSInteger result) {
- if (result) {
- if ([imageData writeToURL:savePanel.URL atomically:YES]) {
- [[NSWorkspace sharedWorkspace] selectFile:savePanel.URL.path
- inFileViewerRootedAtPath:@""];
- }
- }
- }];
- } else if (type == 1) {
- NSData *data = image.TIFFRepresentation;
- NSBitmapImageRep *imageRep = [NSBitmapImageRep imageRepWithData:data];
- [imageRep setSize:image.size];
- NSData *imageData = [imageRep representationUsingType:NSBitmapImageFileTypeJPEG properties:@{}];
-
- NSSavePanel *savePanel = [NSSavePanel savePanel];
- savePanel.allowedFileTypes = @[@"jpg"];
- [savePanel beginSheetModalForWindow:[NSApp mainWindow] completionHandler:^(NSInteger result) {
- if (result) {
- if ([imageData writeToURL:savePanel.URL atomically:YES]) {
- [[NSWorkspace sharedWorkspace] selectFile:savePanel.URL.path
- inFileViewerRootedAtPath:@""];
- }
- }
- }];
- } else {
- CPDFDocument *pdf = [[CPDFDocument alloc] init];
- NSString *signatureImagePath =[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"signatureImage.png"];
- NSData *data = image.TIFFRepresentation;
- [data writeToURL:[NSURL fileURLWithPath:signatureImagePath] atomically:YES];
- [pdf insertPage:image.size withImage:signatureImagePath atIndex:0];
- NSSavePanel *savePanel = [NSSavePanel savePanel];
- savePanel.allowedFileTypes = @[@"pdf"];
- [savePanel beginSheetModalForWindow:[NSApp mainWindow] completionHandler:^(NSInteger result) {
- if (result) {
- if ([pdf writeToURL:savePanel.URL]) {
- [[NSWorkspace sharedWorkspace] selectFile:savePanel.URL.path
- inFileViewerRootedAtPath:@""];
- }
- }
- }];
- }
- }
- - (void)deleteCustomizeStamp {
- if (self.collectionView.selectionIndexes.count != 1) {
- return;
- }
-
- NSInteger index = 0;
- for (NSIndexPath *indexPath in self.collectionView.selectionIndexPaths) {
- index = indexPath.item;
- }
- NSInteger clickIndex = [self.signatures indexOfObject:self.clickSignature];
- if(index < self.signatures.count) {
- KMSignatureManager *signatureManager = [[KMSignatureManager alloc] init];
- [signatureManager loadAllSignatureList];
- [signatureManager removeObjectWithIndex:index];
- [signatureManager saveSingaturesToFile];
- [self reloadDataSignatures];
- }
-
- if(clickIndex == index) {
- [self.pdfView setAddStampObject:nil keepToolModel:false];
- }
- }
- - (void)deleteAllCustomizeStamp {
- NSAlert *alert = [[NSAlert alloc] init];
- alert.messageText = NSLocalizedString(@"Are you sure to delete all signatures?", nil);
- [alert addButtonWithTitle:NSLocalizedString(@"Delete", nil)];
- [alert addButtonWithTitle:NSLocalizedString(@"Cancel", nil)];
- if ([alert runModal] != NSAlertFirstButtonReturn) { /// 取消
- return;
- }
-
- KMSignatureManager *signatureManager = [[KMSignatureManager alloc] init];
- [signatureManager loadAllSignatureList];
- [signatureManager removeAllObject];
- [signatureManager saveSingaturesToFile];
- [self reloadDataSignatures];
- [self.pdfView setAddStampObject:nil keepToolModel:NO];
- }
- #pragma mark - NSCollectionViewDelegate
- - (NSInteger)numberOfSectionsInCollectionView:(NSCollectionView *)collectionView {
- return 1;
- }
- - (NSInteger)collectionView:(NSCollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
- return self.signatures.count;
- }
- - (nonnull NSCollectionViewItem *)collectionView:(nonnull NSCollectionView *)collectionView itemForRepresentedObjectAtIndexPath:(nonnull NSIndexPath *)indexPath {
- if (indexPath.item >= self.signatures.count) {
- return nil;
- }
- KMSigntureViewItem *item = [collectionView makeItemWithIdentifier:@"kmSigntureViewItem" forIndexPath:indexPath];
- KMSignature *signature = [self.signatures objectAtIndex:indexPath.item];
-
- item.iconImg.image = signature.pathsImage;
- item.deleteIconBtn.tag = indexPath.item;
- [item.deleteIconBtn setTarget:self];
- [item.deleteIconBtn setAction:@selector(tableCellCloseButton_Click:)];
- item.delegate = self;
- item.itemIndex = indexPath.item;
- item.isSelect = signature.isSelect;
-
- return item;
- }
- - (CGSize)collectionView:(NSCollectionView *)collectionView layout:(NSCollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
- return CGSizeMake(228, 64);
- }
- - (NSEdgeInsets)collectionView:(NSCollectionView *)collectionView layout:(NSCollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
- return NSEdgeInsetsMake(0, 16, 16, 16);
- }
- - (CGFloat)collectionView:(NSCollectionView *)collectionView layout:(NSCollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
- return 16;
- }
- - (CGFloat)collectionView:(NSCollectionView *)collectionView layout:(NSCollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
- return 16;
- }
- - (void)collectionView:(NSCollectionView *)collectionView didSelectItemsAtIndexPaths:(NSSet<NSIndexPath *> *)indexPaths {
- if (self.isClickSignatureList) {
- NSInteger index = 0;
- for (NSIndexPath *indexPath in self.collectionView.selectionIndexPaths) {
- index = indexPath.item;
- }
- KMSignature *selectSignature = [self.signatures objectAtIndex:index];
-
- CPDFAnnotation *editAnnotation = (CPDFAnnotation *)self.annotationModel.annotations.firstObject;
- CPDFPage *page = editAnnotation.page;
- if ([editAnnotation isKindOfClass:[CPDFSignatureWidgetAnnotation class]]) {
- CPDFSignatureWidgetAnnotation * signatureaAnnotation = nil;
- if ([editAnnotation isKindOfClass:[CPDFSignatureWidgetAnnotation class]]) {
- signatureaAnnotation = (CPDFSignatureWidgetAnnotation *)editAnnotation;
- } else {
- // signatureaAnnotation= [[CPDFSignatureWidgetAnnotation alloc] initKMNoteWithBounds:editAnnotation.bounds document:self.pdfView.document];
- signatureaAnnotation = [[CPDFSignatureWidgetAnnotation alloc] initWithKMNoteBounds:editAnnotation.bounds document:self.pdfView.document];
- signatureaAnnotation.fieldName = [(CPDFSignatureWidgetAnnotation *)editAnnotation fieldName];
- signatureaAnnotation.backgroundColor = [(CPDFSignatureWidgetAnnotation *)editAnnotation backgroundColor];
- signatureaAnnotation.contents = editAnnotation.contents;
- signatureaAnnotation.userName = editAnnotation.userName;
- signatureaAnnotation.modificationDate = editAnnotation.modificationDate;
- }
-
- [signatureaAnnotation signWithImage:selectSignature.pathsImage];
-
- [page removeAnnotation:editAnnotation];
- [page addAnnotation:signatureaAnnotation];
- [self.pdfView setNeedsDisplayAnnotationViewForPage:page];
- [signatureaAnnotation setShouldDisplay:YES];
-
- if (self.view.window != nil) {
- [KMTools setDocumentEditedStateWithWindow:self.view.window];
- }
- }
-
- return;
- }
-
- if (indexPaths.count == 1) {
- NSInteger index = 0;
- for (NSIndexPath *indexPath in self.collectionView.selectionIndexPaths) {
- index = indexPath.item;
- }
-
- for (KMSignature *signature in self.selectSignatures) {
- signature.isSelect = NO;
- }
- [self.collectionView reloadData];
-
- if (index < self.signatures.count) {
- [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"KMContinuousAdditionStamp"];
- KMSignature *signature = [self.signatures objectAtIndex:index];
- signature.isSelect = YES;
- CStampSignatureObject *sigObject = [[CStampSignatureObject alloc] initWithSignatureImage:signature.pathsImage];
- self.clickSignature = signature;
- [self.pdfView setAddStampObject:sigObject keepToolModel:true];
- [self.collectionView reloadItemsAtIndexPaths:self.collectionView.selectionIndexPaths];
- [self.selectSignatures addObject:signature];
- }
- }
-
- }
- #pragma mark - KMSigntureViewItemDelegate
- - (void)kmSigntureViewItem:(KMSigntureViewItem *)item didClickMenuWithItem:(NSMenuItem *)menuItem {
- [self.collectionView deselectAll:nil];
- [self.collectionView selectItemsAtIndexPaths:[NSSet setWithObjects:[NSIndexPath indexPathForItem:item.itemIndex inSection:0],nil] scrollPosition:NSCollectionViewScrollPositionNone];
-
- [self exportCustomizeStamp:menuItem];
- }
- - (void)kmSigntureViewItemDidClickDeleteMenuItem:(KMSigntureViewItem *)item {
- [self.collectionView deselectAll:nil];
- [self.collectionView selectItemsAtIndexPaths:[NSSet setWithObjects:[NSIndexPath indexPathForItem:item.itemIndex inSection:0],nil] scrollPosition:NSCollectionViewScrollPositionNone];
-
- [self deleteCustomizeStamp];
- }
- - (void)kmSigntureViewItemDidClickAllDeleteMenuItem:(KMSigntureViewItem *)item {
- [self.collectionView deselectAll:nil];
- [self.collectionView selectItemsAtIndexPaths:[NSSet setWithObjects:[NSIndexPath indexPathForItem:item.itemIndex inSection:0],nil] scrollPosition:NSCollectionViewScrollPositionNone];
-
- [self deleteAllCustomizeStamp];
- }
- @end
|