Ver Fonte

【2025】【Form】签名预处理

niehaoyu há 2 meses atrás
pai
commit
8a4c58fed6

PDF Office/PDF Master/Class/PDFWindowController/PDFListView/CPDFKitExtensions/CPDFAnnotationExtensions/Form/CPDFSignatureAnnotation+PDFListView.swift → PDF Office/PDF Master/Class/PDFWindowController/PDFListView/CPDFKitExtensions/CPDFAnnotationExtensions/CPDFSignatureAnnotation+PDFListView.swift


+ 172 - 6
PDF Office/PDF Master/Class/PDFWindowController/PDFListView/CPDFKitExtensions/CPDFAnnotationExtensions/Form/CPDFSignatureWidgetAnnotation+PDFListView.swift

@@ -10,12 +10,12 @@ import Foundation
 @objc extension CPDFSignatureWidgetAnnotation {
     convenience init(PDFListViewNoteWith document: CPDFDocument) {
         self.init(document: document)
-        if let model = CPDFAnnotationModel(annotationType: .signature) {
-            self.backgroundColor = model.backgroundColor()
-            self.backgroundOpacity = model.backgroundOpacity()
-            self.border = CPDFBorder(style: model.style(), lineWidth: model.lineWidth(), dashPattern: model.dashPattern())
-            self.setFieldName(String(format: "%@%@", "Signature_", CPDFAnnotationModel.tagString()))
-        }
+     
+        self.backgroundColor = CPDFSignatureWidgetAnnotation.defaultFillColor()
+        self.backgroundOpacity = 1
+        self.border = CPDFBorder(style: .solid, lineWidth: 1, dashPattern: [5])
+        self.setFieldName(self.getValidName(inPage: self.page))
+        
     }
     
     override func isMovable() -> Bool {
@@ -25,4 +25,170 @@ import Foundation
         return true
     }
     
+    func getValidName(inPage page: CPDFPage?) -> String {
+        guard let page = page else {
+            return "Signature"
+        }
+        var availableIndex = 1
+        for annotation in page.annotations {
+            if annotation is CPDFSignatureWidgetAnnotation {
+                let name = (annotation as! CPDFSignatureWidgetAnnotation).fieldName() ?? ""
+                if name.hasPrefix("Signature") {
+                    if let index = Int(name.dropFirst("Signature".count)), index >= availableIndex {
+                        availableIndex = index + 1
+                    }
+                }
+            }
+        }
+        return "Signature\(availableIndex)"
+    }
+}
+
+//MARK: - Update Default默认值
+extension CPDFSignatureWidgetAnnotation {
+    
+    class func defaultTextColor() -> NSColor {
+        return CPDFAnnotationConfig.getDefaultColor(forKey: CAnnotationTextWidgetFontColorKey) ?? NSColor(red: 0, green: 0, blue: 0, alpha: 1)
+    }
+    
+    class func defaultBorderColor() -> NSColor {
+        return CPDFAnnotationConfig.getDefaultColor(forKey: CAnnotationTextWidgetBorderColorKey) ?? NSColor.clear
+    }
+    
+    class func defaultFillColor() -> NSColor {
+        return CPDFAnnotationConfig.getDefaultColor(forKey: CAnnotationTextWidgetBackgroundColorKey) ?? NSColor.clear
+    }
+    
+    class func defaultFont() -> CPDFFont {
+        return CPDFFont(familyName: CPDFTextWidgetAnnotation.defaultFontName(), fontStyle: CPDFTextWidgetAnnotation.defaultFontStyle())
+    }
+    
+    class func defaultFontName() -> String {
+        return CPDFAnnotationConfig.getDefaultStringValue(forKey: SKAnnotationTextWidgetFontNameKey) ?? "Helvetica"
+    }
+    
+    class func defaultFontStyle() -> String {
+        return CPDFAnnotationConfig.getDefaultStringValue(forKey: SKAnnotationTextWidgetFontStyleKey) ?? "Regular"
+    }
+    
+    
+}
+
+//MARK: - Update
+extension CPDFSignatureWidgetAnnotation {
+    
+    class func update(_ annotations: [CPDFTextWidgetAnnotation], state typeIndex: CPDFWidgetShowState, PDFView pdfView: CPDFListView?) {
+        guard let pdfView = pdfView else {
+            return
+        }
+        
+        for annotation in annotations {
+            if typeIndex == .Visiable {
+                annotation.setShouldPrint(true)
+                annotation.setShouldDisplay(true)
+            } else if typeIndex == .Hidden {
+                annotation.setShouldPrint(false)
+                annotation.setShouldDisplay(false)
+            } else if typeIndex == .ShowNoPrint {
+                annotation.setShouldPrint(false)
+                annotation.setShouldDisplay(true)
+            } else if typeIndex == .HideAndPrint {
+                annotation.setShouldPrint(true)
+                annotation.setShouldDisplay(false)
+            }
+        }
+        pdfView.setNeedsDisplayMultiAnnotations(annotations)
+        
+    }
+    
+    class func update(_ annotations: [CPDFTextWidgetAnnotation], fieldName name: String?, PDFView pdfView: CPDFListView?) {
+        guard let resultValue = name else {
+            return
+        }
+        guard let pdfView = pdfView else {
+            return
+        }
+        for annotation in annotations {
+            annotation.setFieldName(resultValue)
+        }
+        pdfView.setNeedsDisplayMultiAnnotations(annotations)
+    }
+    
+    class func update(_ annotations: [CPDFTextWidgetAnnotation], stringValue name: String?, PDFView pdfView: CPDFListView?) {
+        guard let resultValue = name else {
+            return
+        }
+        guard let pdfView = pdfView else {
+            return
+        }
+        for annotation in annotations {
+            annotation.stringValue = resultValue
+        }
+        pdfView.setNeedsDisplayMultiAnnotations(annotations)
+    }
+    
+    class func update(_ annotations: [CPDFTextWidgetAnnotation], textColor color: NSColor?, PDFView pdfView: CPDFListView?) {
+        guard let resultColor = color else {
+            return
+        }
+        guard let pdfView = pdfView else {
+            return
+        }
+        
+        for annotation in annotations {
+            annotation.fontColor = resultColor
+        }
+        pdfView.setNeedsDisplayMultiAnnotations(annotations)
+        
+        CPDFAnnotationConfig.setDefaultColor(resultColor, toKey: CAnnotationTextWidgetFontColorKey)
+        
+    }
+    
+    class func update(_ annotations: [CPDFTextWidgetAnnotation], borderColor color: NSColor?, PDFView pdfView: CPDFListView?) {
+        guard let resultColor = color else {
+            return
+        }
+        guard let pdfView = pdfView else {
+            return
+        }
+        
+        for annotation in annotations {
+            annotation.borderColor = resultColor
+        }
+        pdfView.setNeedsDisplayMultiAnnotations(annotations)
+        
+        CPDFAnnotationConfig.setDefaultColor(resultColor, toKey: CAnnotationTextWidgetBorderColorKey)
+    }
+    
+    class func update(_ annotations: [CPDFTextWidgetAnnotation], fillColor color: NSColor?, PDFView pdfView: CPDFListView?) {
+        guard let resultColor = color else {
+            return
+        }
+        guard let pdfView = pdfView else {
+            return
+        }
+        for annotation in annotations {
+            annotation.backgroundColor = resultColor
+        }
+        pdfView.setNeedsDisplayMultiAnnotations(annotations)
+        
+        CPDFAnnotationConfig.setDefaultColor(resultColor, toKey: CAnnotationTextWidgetBackgroundColorKey)
+    }
+    
+    class func updateFont(_ annotations: [CPDFTextWidgetAnnotation], _ cfont: CPDFFont, withPDFView pdfView: CPDFListView?) {
+        guard let pdfView = pdfView else {
+            return
+        }
+        for annotation in annotations {
+            annotation.cFont = cfont
+        }
+        pdfView.setNeedsDisplayMultiAnnotations(annotations)
+        
+        CPDFAnnotationConfig.setDefaultStringValue(cfont.familyName, toKey: SKAnnotationTextWidgetFontNameKey)
+        
+        if let styleName = cfont.styleName {
+            CPDFAnnotationConfig.setDefaultStringValue(styleName, toKey: SKAnnotationTextWidgetFontStyleKey)
+        }
+    }
+    
 }

+ 1 - 38
PDF Office/PDF Master/KMClass/KMPDFViewController/RightSideController/Views/EditPDF/Manager/KMEditPDFTextManager.swift

@@ -225,44 +225,7 @@ extension KMEditPDFTextManager {
         self.textPresuppositionFontNameArray = resultArray
         return resultArray
     }
-    
-    func fetchFontStyleWithFontName(fontName: String) -> [String] {
-        var styleArray: [String] = []
-        let fontArray = CPDFAnnotationModel.supportFonts() as! [NSDictionary]
-        
-        for i in 0 ... fontArray.count-1 {
-            let dic : NSDictionary = fontArray[i]
-            let familyString = dic.allKeys.first as! String
-            
-            let fontNameArray = fontName.components(separatedBy: "-")
-            var name = ""
-            if fontNameArray.count > 0 {
-                name = fontNameArray.first!
-            } else {
-                name = FONTNAME_DEFAULT
-            }
-            
-            if name == familyString {
-                let styles : NSArray = dic.object(forKey: fontName) as! NSArray
-                for j in 0 ... styles.count-1 {
-                    let style = styles.object(at: j) as! String
-                    var attributeFontDescriptor = NSFontDescriptor()
-                    if style == "Regular" || style == "Roman" {
-                        attributeFontDescriptor = NSFontDescriptor.init(fontAttributes: [NSFontDescriptor.AttributeName.family : fontName])
-                    } else {
-                        attributeFontDescriptor = NSFontDescriptor.init(fontAttributes: [NSFontDescriptor.AttributeName.family : fontName,NSFontDescriptor.AttributeName.face : style])
-                    }
-                    let font = NSFont.init(descriptor: attributeFontDescriptor, size: 12)
-                    let attrited : [NSAttributedString.Key : Any] = [NSAttributedString.Key.font:font!]
-                    let string : NSAttributedString = NSAttributedString(string: style, attributes: attrited)
-                    styleArray.append(string.string)
-                }
-                break
-            }
-        }
-        return styleArray
-    }
-    
+     
     func fetchTextImage(textString: String = "Sample", alignment: NSTextAlignment, fontName: String, fontSize: CGFloat, color: NSColor, imageSize: CGSize) -> NSImage {
         let string = NSLocalizedString(textString, comment: "")
         let paragraphStyle = NSMutableParagraphStyle()

+ 1 - 1
PDF Office/PDF Reader Pro.xcodeproj/project.pbxproj

@@ -7401,7 +7401,6 @@
 			isa = PBXGroup;
 			children = (
 				BB6013872AD3A3CB00A76FB2 /* CPDFWidgetAnnotation+PDFListView.swift */,
-				BB60138B2AD3A94200A76FB2 /* CPDFSignatureAnnotation+PDFListView.swift */,
 				BB6013832AD3A0DE00A76FB2 /* CPDFTextWidgetAnnotation+PDFListView.swift */,
 				BB6719F82AD2CC05003D44D5 /* CPDFSignatureWidgetAnnotation+PDFListView.swift */,
 				BBBB6CCD2AD13E210035AA66 /* CPDFButtonWidgetAnnotation+PDFListView.swift */,
@@ -12183,6 +12182,7 @@
 				BB6719FC2AD2CE1B003D44D5 /* CPDFSquareAnnotation+PDFListView.swift */,
 				BB671A002AD2D2A0003D44D5 /* CPDFStampAnnotation+PDFListView.swift */,
 				BB60137F2AD38E0100A76FB2 /* CPDFTextAnnotation+PDFListView.swift */,
+				BB60138B2AD3A94200A76FB2 /* CPDFSignatureAnnotation+PDFListView.swift */,
 				BB0A2CE22D1D518E00FF019B /* Redact */,
 				BB3AFF022D1A4D4300A48759 /* Measure */,
 				BB6192362D13F204003FDEA6 /* Fill */,