Browse Source

【综合】图章注释应用多页优化处理

niehaoyu 8 months ago
parent
commit
f8e2f41040

+ 2 - 0
PDF Office/PDF Master/Class/PDFWindowController/PDFListView/CPDFListViewExtension/CPDFListView+Extension.h

@@ -23,6 +23,8 @@
 
 - (CPDFAnnotation *)addPDFStampAnotationWithPagePoint:(CGPoint)pagePoint withPage:(CPDFPage *)page;
 
+- (CPDFAnnotation *)addPDFStampAnotationWithObject:(CStampSignatureObject *)stampObject atPagePoint:(CGPoint)pagePoint withPage:(CPDFPage *)page;
+
 - (void)addAnnotationWithImage:(NSImage *)image isRemoveBGColor:(BOOL)isRemoveBGColor;
 
 - (void)addAnnotationWithTable;

+ 68 - 0
PDF Office/PDF Master/Class/PDFWindowController/PDFListView/CPDFListViewExtension/CPDFListView+Extension.m

@@ -126,6 +126,74 @@ CGFloat DEFAULT_SNAPSHOT_HEIGHT = 200.0;
     return annotation;
 }
 
+- (CPDFAnnotation *)addPDFStampAnotationWithObject:(CStampSignatureObject *)stampObject atPagePoint:(CGPoint)pagePoint withPage:(CPDFPage *)page {
+    CGFloat defaultWidth = 160;
+    CGFloat defaultHeight = 40;
+    if (CPDFStampTypeImage == stampObject.stampCategoryType) {
+        defaultWidth = 360;
+        defaultHeight = 90;
+    }
+    
+    if ((CPDFStampTypeStandard == stampObject.stampCategoryType) &&
+        (CStandardStampTypeCircle == stampObject.stampType ||
+         CStandardStampTypeAccepted == stampObject.stampType ||
+         CStandardStampTypeRejected == stampObject.stampType ||
+         CStandardStampTypeChick == stampObject.stampType ||
+         CStandardStampTypeCross == stampObject.stampType)) {
+        defaultHeight = defaultWidth = 40;
+    }
+    CPDFAnnotation *annotation = nil;
+    NSSize defaultSize = NSMakeSize(defaultWidth, defaultHeight);
+    
+    pagePoint = CPDFListViewIntegralPoint(pagePoint);
+    CGRect bounds = CPDFListViewRectFromCenterAndSize(pagePoint, defaultSize);
+    
+    if (CPDFStampTypeImage == stampObject.stampCategoryType) {
+        NSImage * image = [[NSImage alloc] initWithContentsOfFile:stampObject.imagePath];
+        annotation = [[CPDFListStampAnnotation alloc] initWithDocument:self.document image:image];
+        
+        CGFloat newWidth = image.size.width;
+        CGFloat newHeight = image.size.height;
+        CGSize size = CGSizeMake(360, 360);
+        if (bounds.size.width > newWidth || bounds.size.height > newWidth) {
+            if (size.width > size.height) {
+                newHeight = newHeight/size.width * size.height;
+            } else {
+                newWidth = newWidth/size.height * size.width;
+            }
+            bounds.size = CGSizeMake(newWidth, newHeight);
+        }
+        
+        bounds = CPDFListViewRectFromCenterAndSize(pagePoint, bounds.size);
+        annotation.bounds = bounds;
+    } else if(stampObject.isSignatureAnnotation) {
+        NSImage * image = stampObject.signatureImage;
+        annotation = [[CPDFListSignatureAnnotation alloc] initWithDocument:self.document];
+        [(CPDFListSignatureAnnotation *)annotation setImage:image];
+        annotation.bounds = NSMakeRect(pagePoint.x, pagePoint.y-annotation.bounds.size.height, annotation.bounds.size.width, annotation.bounds.size.height);
+    } else if (CPDFStampTypeText == stampObject.stampCategoryType) {
+        annotation = [[CPDFListStampAnnotation alloc] initWithDocument:self.document text:stampObject.text detailText:stampObject.detailText style:stampObject.style shape:stampObject.shape];
+        annotation.bounds = NSMakeRect(pagePoint.x, pagePoint.y-annotation.bounds.size.height, annotation.bounds.size.width, annotation.bounds.size.height);
+    } else if (4/*CPDFStampTypeDynamic*/ == stampObject.stampCategoryType) {
+        annotation = [[CPDFListStampAnnotation alloc] initWithDocument:self.document text:stampObject.text detailText:stampObject.detailText style:stampObject.style shape:stampObject.shape];
+        annotation.bounds = NSMakeRect(pagePoint.x, pagePoint.y-annotation.bounds.size.height, annotation.bounds.size.width, annotation.bounds.size.height);
+    } else if (CPDFStampTypeDigital == stampObject.stampCategoryType) {
+        annotation = [[CPDFListStampAnnotation alloc] initWithDocument:self.document text:stampObject.text detailText:stampObject.detailText dateText:stampObject.dateText color:stampObject.color];
+        annotation.bounds = NSMakeRect(pagePoint.x, pagePoint.y-40, 80, 80);
+    } else if (CPDFStampTypeStandard == stampObject.stampCategoryType) {
+        if(stampObject.stampType > CStandardStampTypeCircle) {
+            annotation = [[CPDFListStampAnnotation alloc] initWithDocument:self.document text:stampObject.text detailText:stampObject.detailText style:stampObject.style shape:stampObject.shape];
+            annotation.bounds = NSMakeRect(pagePoint.x, pagePoint.y-annotation.bounds.size.height, annotation.bounds.size.width, annotation.bounds.size.height);
+        } else {
+            annotation = [[CPDFListStampAnnotation alloc] initWithDocument:self.document type:stampObject.stampType];
+            annotation.bounds = NSMakeRect(pagePoint.x, pagePoint.y - annotation.bounds.size.height, annotation.bounds.size.width, annotation.bounds.size.height);
+        }
+        
+    }
+    
+    return annotation;
+}
+
 - (void)addAnnotationWithImage:(NSImage *)image isRemoveBGColor:(BOOL)isRemoveBGColor {
     // First try the current mouse position
     NSPoint center = [self convertPoint:[[self window] mouseLocationOutsideOfEventStream] fromView:nil];

+ 17 - 7
PDF Office/PDF Master/Class/PDFWindowController/PDFListView/CPDFListViewExtension/CPDFListView+Function.swift

@@ -20,17 +20,27 @@ extension CPDFListView {
             guard let index = (pages.object(at: i) as? NSNumber)?.intValue else {
                 continue
             }
-            if(index - 1 < self.document.pageCount) {
+            if(index - 1 < self.document.pageCount && index != annotation.pageIndex() + 1) {
                 let page = self.document.page(at: UInt(index-1))
                 
                 var newAnnotation: CPDFAnnotation?
                 
-                if let stampAnnotation = annotation as? CPDFStampAnnotation {
-                    newAnnotation = CPDFStampAnnotation(document: self.document, image: stampAnnotation.stampImage())
-                }
-                if let stampAnnotation = annotation as? CPDFSignatureAnnotation {
-                    newAnnotation = CPDFListSignatureAnnotation(document: self.document)
-                    (newAnnotation as! CPDFListSignatureAnnotation).setImage(stampAnnotation.signImage)
+                if let stampAnnotation = annotation as? CPDFListStampAnnotation {
+                    if stampAnnotation.stamp != nil {
+                        newAnnotation = self.addPDFStampAnotation(with: stampAnnotation.stamp, atPagePoint: stampAnnotation.bounds.origin, with: page)
+                        newAnnotation?.updateAppearanceStream()
+                        (newAnnotation as? CPDFListStampAnnotation)?.setBorderBounds(stamp: stampAnnotation.stamp!)
+                    }
+                } else if let stampAnnotation = annotation as? CPDFStampAnnotation {
+                    guard let image = stampAnnotation.stampImage() else {
+                        return
+                    }
+                    let imagePath = KMStampManager.defaultManager.addStampToFolder(withImage: image)
+                    let newImage = NSImage(contentsOfFile: imagePath)
+                    newAnnotation = CPDFStampAnnotation(document: self.document, image: newImage)
+                } else if let stampAnnotation = annotation as? CPDFSignatureAnnotation {
+                    newAnnotation = CPDFSignatureAnnotation(document: self.document)
+                    (newAnnotation as! CPDFSignatureAnnotation).setImage(stampAnnotation.signImage)
                 }
                 guard newAnnotation != nil else {
                     return

+ 34 - 0
PDF Office/PDF Master/Class/PDFWindowController/Side/RightSide/StampList/KMStampManager.swift

@@ -507,6 +507,40 @@ let CPDFDynamicStampNeedAuthorKey1 = "km_pdfview_dynamic_stamp_need_author"
         return false
     }
     
+    func addStampToFolder(withImage image: NSImage) -> String {
+        do {
+            if !FileManager.default.fileExists(atPath: kStampFolderPath.path) {
+                try FileManager.default.createDirectory(atPath: kStampFolderPath.path, withIntermediateDirectories: true, attributes: nil)
+            }
+            
+            if !FileManager.default.fileExists(atPath: kStampPlistPath.path) {
+                if !FileManager.default.createFile(atPath: kStampPlistPath.path, contents: nil, attributes: nil) {
+                    return ""
+                }
+            }
+            
+            let tag = tagString()
+            let rPath = (kStampFolderPath.path as NSString).appendingPathComponent(tag).stringByAppendingPathExtension("jpg")
+            
+            image.lockFocus()
+            let bits = NSBitmapImageRep(focusedViewRect: NSMakeRect(0, 0, image.size.width, image.size.height))
+            image.unlockFocus()
+            let imageProps = [NSBitmapImageRep.PropertyKey.compressionFactor: 0.5]
+            if let imageData = bits?.representation(using: .jpeg, properties: imageProps) {
+                try imageData.write(to: URL(fileURLWithPath: rPath), options: .atomic)
+            } else {
+                return ""
+            }
+            if FileManager.default.fileExists(atPath: rPath) {
+                return rPath
+            }
+            
+        } catch {
+            return ""
+        }
+        return ""
+    }
+    
     func addCustomStamp(withImagePath path: String, categoryString: CPDFStampType, nameString: String) -> Bool {
         do {
             if !FileManager.default.fileExists(atPath: kStampFolderPath.path) {