Browse Source

ComPDFKit(RN) - iOS端 文本域、按钮域和签名域更改值并刷新的串接

yangliuhua 6 days ago
parent
commit
fee4ad1fdb
4 changed files with 259 additions and 4 deletions
  1. 127 4
      ios/RCTCPDFPageUtil.swift
  2. 49 0
      ios/RCTCPDFViewManager.swift
  3. 36 0
      ios/RCTDocumentManager.m
  4. 47 0
      ios/RCTDocumentManager.swift

+ 127 - 4
ios/RCTCPDFPageUtil.swift

@@ -27,7 +27,7 @@ class RCTCPDFPageUtil: NSObject {
     
     func getAnnotations() -> [Dictionary<String, Any>] {
         var annotaionDicts:[Dictionary<String, Any>] = []
-        
+       
         let annoations = page?.annotations ?? []
         
         for  annoation in annoations {
@@ -42,7 +42,9 @@ class RCTCPDFPageUtil: NSObject {
             case "Highlight", "Squiggly", "Underline", "Strikeout":
                 if let markupAnnotation = annoation as? CPDFMarkupAnnotation {
                     let lowertype = lowercaseFirstLetter(of: type)
+                    let memory = getMemoryAddress(markupAnnotation)
                     
+                    annotaionDict["uuid"] = memory
                     annotaionDict["type"] = lowertype
                     annotaionDict["title"] = markupAnnotation.userName()
                     annotaionDict["page"] = pageIndex
@@ -51,7 +53,9 @@ class RCTCPDFPageUtil: NSObject {
             case "Circle":
                 if let circleAnnotation = annoation as? CPDFCircleAnnotation {
                     let lowertype = lowercaseFirstLetter(of: type)
+                    let memory = getMemoryAddress(circleAnnotation)
                     
+                    annotaionDict["uuid"] = memory
                     annotaionDict["type"] = lowertype
                     annotaionDict["title"] = circleAnnotation.userName()
                     annotaionDict["page"] = pageIndex
@@ -60,7 +64,9 @@ class RCTCPDFPageUtil: NSObject {
             case "Square":
                 if let squareAnnotation = annoation as? CPDFSquareAnnotation {
                     let lowertype = lowercaseFirstLetter(of: type)
+                    let memory = getMemoryAddress(squareAnnotation)
                     
+                    annotaionDict["uuid"] = memory
                     annotaionDict["type"] = lowertype
                     annotaionDict["title"] = squareAnnotation.userName()
                     annotaionDict["page"] = pageIndex
@@ -69,7 +75,9 @@ class RCTCPDFPageUtil: NSObject {
             case "Line", "Arrow":
                 if let lineAnnotation = annoation as? CPDFLineAnnotation {
                     let lowertype = lowercaseFirstLetter(of: type)
+                    let memory = getMemoryAddress(lineAnnotation)
                     
+                    annotaionDict["uuid"] = memory
                     annotaionDict["type"] = lowertype
                     annotaionDict["title"] = lineAnnotation.userName()
                     annotaionDict["page"] = pageIndex
@@ -77,6 +85,9 @@ class RCTCPDFPageUtil: NSObject {
                 }
             case "Freehand":
                 if let inkAnnotation = annoation as? CPDFInkAnnotation {
+                    let memory = getMemoryAddress(inkAnnotation)
+                    
+                    annotaionDict["uuid"] = memory
                     annotaionDict["type"] = "ink"
                     annotaionDict["title"] = inkAnnotation.userName()
                     annotaionDict["page"] = pageIndex
@@ -86,7 +97,9 @@ class RCTCPDFPageUtil: NSObject {
             case "Note":
                 if let noteAnnotation = annoation as? CPDFTextAnnotation {
                     let lowertype = lowercaseFirstLetter(of: type)
+                    let memory = getMemoryAddress(noteAnnotation)
                     
+                    annotaionDict["uuid"] = memory
                     annotaionDict["type"] = lowertype
                     annotaionDict["title"] = noteAnnotation.userName()
                     annotaionDict["page"] = pageIndex
@@ -95,6 +108,9 @@ class RCTCPDFPageUtil: NSObject {
                 
             case "FreeText":
                 if let freeTextAnnotation = annoation as? CPDFFreeTextAnnotation {
+                    let memory = getMemoryAddress(freeTextAnnotation)
+                    
+                    annotaionDict["uuid"] = memory
                     annotaionDict["type"] = "freetext"
                     annotaionDict["title"] = freeTextAnnotation.userName()
                     annotaionDict["page"] = pageIndex
@@ -104,7 +120,9 @@ class RCTCPDFPageUtil: NSObject {
             case "Stamp", "Image":
                 if let stampAnnotation = annoation as? CPDFStampAnnotation {
                     let lowertype = lowercaseFirstLetter(of: type)
-        
+                    let memory = getMemoryAddress(stampAnnotation)
+                    
+                    annotaionDict["uuid"] = memory
                     annotaionDict["type"] = lowertype
                     if type == "Image" {
                         annotaionDict["type"] = "pictures"
@@ -117,7 +135,9 @@ class RCTCPDFPageUtil: NSObject {
             case "Link":
                 if let linkAnnotation = annoation as? CPDFLinkAnnotation {
                     let lowertype = lowercaseFirstLetter(of: type)
+                    let memory = getMemoryAddress(linkAnnotation)
                     
+                    annotaionDict["uuid"] = memory
                     annotaionDict["type"] = lowertype
                     annotaionDict["title"] = linkAnnotation.userName()
                     annotaionDict["page"] = pageIndex
@@ -126,6 +146,9 @@ class RCTCPDFPageUtil: NSObject {
                 
             case "Media":
                 if let mediaAnnotation = annoation as? CPDFSoundAnnotation {
+                    let memory = getMemoryAddress(mediaAnnotation)
+                    
+                    annotaionDict["uuid"] = memory
                     annotaionDict["type"] = "sound"
                     annotaionDict["title"] = mediaAnnotation.userName()
                     annotaionDict["page"] = pageIndex
@@ -133,6 +156,9 @@ class RCTCPDFPageUtil: NSObject {
                 }
             case "":
                 if let signatureAnnotation = annoation as? CPDFSignatureAnnotation {
+                    let memory = getMemoryAddress(signatureAnnotation)
+                    
+                    annotaionDict["uuid"] = memory
                     annotaionDict["type"] = "signature"
                     annotaionDict["title"] = signatureAnnotation.userName()
                     annotaionDict["page"] = pageIndex
@@ -153,7 +179,7 @@ class RCTCPDFPageUtil: NSObject {
     
     func getForms() -> [Dictionary<String, Any>] {
         var formDicts:[Dictionary<String, Any>] = []
-        
+
         let forms = page?.annotations ?? []
         
         for form in forms {
@@ -168,7 +194,9 @@ class RCTCPDFPageUtil: NSObject {
                     case "CheckBox", "RadioButton", "PushButton":
                         if let buttonWidget = form as? CPDFButtonWidgetAnnotation {
                             let lowertype = lowercaseFirstLetter(of: widgetType)
+                            let memory = getMemoryAddress(buttonWidget)
                             
+                            formDict["uuid"] = memory
                             formDict["type"] = lowertype
                             formDict["title"] = buttonWidget.fieldName()
                             formDict["page"] = pageIndex
@@ -185,7 +213,9 @@ class RCTCPDFPageUtil: NSObject {
                     case "TextField":
                         if let textFieldWidget = form as? CPDFTextWidgetAnnotation {
                             let lowertype = lowercaseFirstLetter(of: widgetType)
+                            let memory = getMemoryAddress(textFieldWidget)
                             
+                            formDict["uuid"] = memory
                             formDict["type"] = lowertype
                             formDict["title"] = textFieldWidget.fieldName()
                             formDict["page"] = pageIndex
@@ -195,7 +225,9 @@ class RCTCPDFPageUtil: NSObject {
                     case "ListBox", "ComboBox":
                         if let choiceWidget = form as? CPDFChoiceWidgetAnnotation {
                             let lowertype = lowercaseFirstLetter(of: widgetType)
+                            let memory = getMemoryAddress(choiceWidget)
                             
+                            formDict["uuid"] = memory
                             formDict["type"] = lowertype
                             formDict["title"] = choiceWidget.fieldName()
                             formDict["page"] = pageIndex
@@ -203,6 +235,9 @@ class RCTCPDFPageUtil: NSObject {
                         
                     case "SignatureFields":
                         if let signatureWidget = form as? CPDFSignatureWidgetAnnotation {
+                            let memory = getMemoryAddress(signatureWidget)
+                            
+                            formDict["uuid"] = memory
                             formDict["type"] = "signaturesFields"
                             formDict["title"] = signatureWidget.fieldName()
                             formDict["page"] = pageIndex
@@ -216,12 +251,95 @@ class RCTCPDFPageUtil: NSObject {
                 }
             }
         }
-        
+    
         return formDicts
     }
     
+    func setWidgetIsChecked(uuid: String, isChecked: Bool) {
+        if let widget = self.getForm(formUUID: uuid) {
+            if let buttonWidget = widget as? CPDFButtonWidgetAnnotation {
+                buttonWidget.setState(isChecked ? 1 : 0)
+            }
+        }
+    }
+    
+    func setTextWidgetText(uuid: String, text: String) {
+        if let widget = self.getForm(formUUID: uuid) {
+            if let textFieldWidget = widget as? CPDFTextWidgetAnnotation {
+                textFieldWidget.stringValue = text
+            }
+        }
+    }
+    
+    func addWidgetImageSignature(uuid: String, imagePath: URL, completionHandler: @escaping (Bool) -> Void) {
+        if let widget = self.getForm(formUUID: uuid) {
+            if let signatureWidget = widget as? CPDFSignatureWidgetAnnotation {
+                let image = UIImage(contentsOfFile: imagePath.path)
+                signatureWidget.sign(with: image)
+                completionHandler(true)
+            }
+        } else if let annotation = self.getAnnotation(formUUID: uuid) {
+            if let signatureAnnotation = annotation as? CPDFSignatureAnnotation {
+                let image = UIImage(contentsOfFile: imagePath.path)
+                signatureAnnotation.setImage(image)
+                completionHandler(true)
+            }
+        } else {
+            completionHandler(false)
+        }
+    }
+    
+    func updateAp(uuid: String) {
+        if let widget = self.getForm(formUUID: uuid) {
+            widget.updateAppearanceStream()
+        } else if let annotation = self.getAnnotation(formUUID: uuid) {
+            annotation.updateAppearanceStream()
+        }
+    }
+    
     //MARK: - Private Methods
     
+    
+    func getAnnotation(formUUID uuid: String) -> CPDFAnnotation? {
+        let annoations = page?.annotations ?? []
+        
+        for  annoation in annoations {
+            let type: String = annoation.type
+            if annoation.type == "Widget" {
+                continue
+            }
+            
+            let _uuid = getMemoryAddress(annoation)
+            
+            if _uuid == uuid {
+                return annoation
+            }
+        }
+        
+        return nil
+    }
+    
+    func getForm(formUUID uuid: String) -> CPDFWidgetAnnotation? {
+        let annoations = page?.annotations ?? []
+        
+        for  annoation in annoations {
+            let type: String = annoation.type
+            if annoation.type == "Widget" {
+                if let widgetAnnotation = annoation as? CPDFWidgetAnnotation {
+                    
+                    let _uuid = getMemoryAddress(annoation)
+                    
+                    if _uuid == uuid {
+                        return widgetAnnotation
+                    }
+                }
+            }
+        }
+        
+        return nil
+    }
+
+    
     func lowercaseFirstLetter(of string: String) -> String {
         guard !string.isEmpty else { return string }
         
@@ -230,4 +348,9 @@ class RCTCPDFPageUtil: NSObject {
         return lowercaseString
     }
     
+    func getMemoryAddress<T: AnyObject>(_ object: T) -> String {
+        let pointer = Unmanaged.passUnretained(object).toOpaque()
+        return String(describing: pointer)
+    }
+    
 }

+ 49 - 0
ios/RCTCPDFViewManager.swift

@@ -28,6 +28,8 @@ class RCTCPDFReaderView: RCTViewManager, RCTCPDFViewDelegate {
         return rtcCPDFView
     }
     
+    // MARK: - Document Methods
+    
     func saveDocument(forCPDFViewTag tag: Int, completionHandler: @escaping (Bool) -> Void) {
         let rtcCPDFView = cpdfViews[tag]
         rtcCPDFView?.saveDocument(completionHandler: { success in
@@ -354,6 +356,8 @@ class RCTCPDFReaderView: RCTViewManager, RCTCPDFViewDelegate {
         })
     }
     
+    // MARK: - Pages Methods
+    
     func getAnnotations(forCPDFViewTag tag : Int, pageIndex: Int, completionHandler: @escaping ([Dictionary<String, Any>]) -> Void) {
         let rtcCPDFView = cpdfViews[tag]
         
@@ -376,6 +380,51 @@ class RCTCPDFReaderView: RCTViewManager, RCTCPDFViewDelegate {
         completionHandler(widgets)
     }
     
+    func setWidgetIsChecked(forCPDFViewTag tag :Int, pageIndex: Int, uuid: String, isChecked: Bool) {
+        let rtcCPDFView = cpdfViews[tag]
+        let page = rtcCPDFView?.getPage(UInt(pageIndex))
+        let pageUtil = RCTCPDFPageUtil(page: page)
+        pageUtil.pageIndex = pageIndex
+        
+        pageUtil.setWidgetIsChecked(uuid: uuid, isChecked: isChecked)
+    }
+    
+    func setTextWidgetText(forCPDFViewTag tag :Int, pageIndex: Int, uuid: String, text: String) {
+        let rtcCPDFView = cpdfViews[tag]
+        let page = rtcCPDFView?.getPage(UInt(pageIndex))
+        let pageUtil = RCTCPDFPageUtil(page: page)
+        pageUtil.pageIndex = pageIndex
+        
+        pageUtil.setTextWidgetText(uuid: uuid, text: text)
+    }
+
+    func addWidgetImageSignature(forCPDFViewTag tag :Int, pageIndex: Int, uuid: String, imagePath: URL, completionHandler: @escaping (Bool) -> Void) {
+        let rtcCPDFView = cpdfViews[tag]
+        let page = rtcCPDFView?.getPage(UInt(pageIndex))
+        let pageUtil = RCTCPDFPageUtil(page: page)
+        pageUtil.pageIndex = pageIndex
+        
+        pageUtil.addWidgetImageSignature(uuid: uuid, imagePath: imagePath) { success in
+            completionHandler(success)
+        }
+    }
+    
+    func updateAp(forCPDFViewTag tag : Int, pageIndex: Int, uuid: String) {
+        let rtcCPDFView = cpdfViews[tag]
+        
+        let page = rtcCPDFView?.getPage(UInt(pageIndex))
+        let pageUtil = RCTCPDFPageUtil(page: page)
+        pageUtil.pageIndex = pageIndex
+        pageUtil.updateAp(uuid: uuid)
+    }
+    
+    func reloadPages(forCPDFViewTag tag : Int) {
+        let rtcCPDFView = cpdfViews[tag]
+    
+        rtcCPDFView?.pdfViewController?.pdfListView?.setNeedsDisplayForVisiblePages()
+        rtcCPDFView?.pdfViewController?.pdfListView?.layoutDocumentView()
+    }
+    
     // MARK: - RCTCPDFViewDelegate
     
     func cpdfViewAttached(_ cpdfView: RCTCPDFView) {

+ 36 - 0
ios/RCTDocumentManager.m

@@ -10,6 +10,8 @@
 
 @interface RCT_EXTERN_MODULE(CPDFViewManager, NSObject)
 
+// MARK: - Document Methods
+
 RCT_EXTERN_METHOD(save:(NSInteger)tag
                   withResolver:(RCTPromiseResolveBlock)resolve
                   withRejecter:(RCTPromiseRejectBlock)reject)
@@ -216,6 +218,8 @@ RCT_EXTERN_METHOD(splitDocumentPages:(NSInteger)tag
                   withResolver:(RCTPromiseResolveBlock)resolve
                   withRejecter:(RCTPromiseRejectBlock)reject)
 
+// MARK: - Pages Methods
+
 RCT_EXTERN_METHOD(getAnnotations:(NSInteger)tag
                   withPageIndex:(NSInteger) pageIndex
                   withResolver:(RCTPromiseResolveBlock)resolve
@@ -226,6 +230,38 @@ RCT_EXTERN_METHOD(getForms:(NSInteger)tag
                   withResolver:(RCTPromiseResolveBlock)resolve
                   withRejecter:(RCTPromiseRejectBlock)reject)
 
+RCT_EXTERN_METHOD(setWidgetIsChecked:(NSInteger)tag
+                  withPage:(NSInteger) page
+                  withUuid:(NSString *) uuid
+                  withIsChecked:(BOOL)isChecked
+                  withResolver:(RCTPromiseResolveBlock)resolve
+                  withRejecter:(RCTPromiseRejectBlock)reject)
+
+RCT_EXTERN_METHOD(setTextWidgetText:(NSInteger)tag
+                  withPage:(NSInteger) page
+                  withUuid:(NSString *) uuid
+                  withText:(NSString *)text
+                  withResolver:(RCTPromiseResolveBlock)resolve
+                  withRejecter:(RCTPromiseRejectBlock)reject)
+
+RCT_EXTERN_METHOD(addWidgetImageSignature:(NSInteger)tag
+                  withPage:(NSInteger) page
+                  withUuid:(NSString *) uuid
+                  withImagePath:(NSURL *)imagePath
+                  withResolver:(RCTPromiseResolveBlock)resolve
+                  withRejecter:(RCTPromiseRejectBlock)reject)
+
+
+RCT_EXTERN_METHOD(updateAp:(NSInteger)tag
+                  withPage:(NSInteger) page
+                  withUuid:(NSString *) uuid
+                  withResolver:(RCTPromiseResolveBlock)resolve
+                  withRejecter:(RCTPromiseRejectBlock)reject)
+
+RCT_EXTERN_METHOD(reloadPages:(NSInteger)tag
+                  withResolver:(RCTPromiseResolveBlock)resolve
+                  withRejecter:(RCTPromiseRejectBlock)reject)
+
 + (BOOL)requiresMainQueueSetup
 {
   return NO;

+ 47 - 0
ios/RCTDocumentManager.swift

@@ -39,6 +39,8 @@ class RCTDocumentManager: NSObject, RCTBridgeModule {
         }
     }
     
+    // MARK: - Document Methods
+    
     @objc(setMargins: withEdges:)
     func setMargins(tag : Int, edges: [Int]) -> Void {
         DispatchQueue.main.async {
@@ -545,6 +547,8 @@ class RCTDocumentManager: NSObject, RCTBridgeModule {
         }
     }
     
+    // MARK: - Pages Methods
+    
     @objc(getAnnotations: withPageIndex: withResolver: withRejecter:)
     func getAnnotations(forCPDFViewTag tag : Int, pageIndex: Int, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
         DispatchQueue.main.async {
@@ -565,4 +569,47 @@ class RCTDocumentManager: NSObject, RCTBridgeModule {
         }
     }
     
+    @objc(setWidgetIsChecked: withPage: withUuid: withIsChecked: withResolver: withRejecter:)
+    func setWidgetIsChecked(forCPDFViewTag tag : Int, page: Int, uuid: String, isChecked: Bool, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
+        DispatchQueue.main.async {
+            let reader = self.readerView()
+            reader.setWidgetIsChecked(forCPDFViewTag: tag, pageIndex: page, uuid: uuid, isChecked: isChecked)
+        }
+    }
+    
+    @objc(setTextWidgetText: withPage: withUuid: withText: withResolver: withRejecter:)
+    func setTextWidgetText(forCPDFViewTag tag : Int, page: Int, uuid: String, text: String, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
+        DispatchQueue.main.async {
+            let reader = self.readerView()
+            reader.setTextWidgetText(forCPDFViewTag: tag, pageIndex: page, uuid: uuid, text: text)
+        }
+    }
+    
+    @objc(addWidgetImageSignature: withPage: withUuid: withImagePath: withResolver: withRejecter:)
+    func addWidgetImageSignature(forCPDFViewTag tag : Int, page: Int, uuid: String, imagePath: URL, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
+        DispatchQueue.main.async {
+            let reader = self.readerView()
+            reader.addWidgetImageSignature(forCPDFViewTag: tag, pageIndex: page, uuid: uuid, imagePath: imagePath) { success in
+                resolve(success)
+                
+            }
+        }
+    }
+    
+    @objc(updateAp: withPage: withUuid: withResolver: withRejecter:)
+    func updateAp(forCPDFViewTag tag : Int, page: Int, uuid: String, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
+        DispatchQueue.main.async {
+            let reader = self.readerView()
+            reader.updateAp(forCPDFViewTag: tag, pageIndex: page, uuid: uuid)
+        }
+    }
+    
+    @objc(reloadPages: withResolver: withRejecter:)
+    func reloadPages(forCPDFViewTag tag : Int, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
+        DispatchQueue.main.async {
+            let reader = self.readerView()
+            reader.reloadPages(forCPDFViewTag: tag)
+        }
+    }
+    
 }