Browse Source

ComPDFKit(RN) - 部分接口的代码接口调整

yangliuhua 1 week ago
parent
commit
bd917c1496

+ 6 - 7
ios/RCTCPDFPageUtil.swift

@@ -77,9 +77,7 @@ class RCTCPDFPageUtil: NSObject {
                 }
             case "Freehand":
                 if let inkAnnotation = annoation as? CPDFInkAnnotation {
-                    let lowertype = lowercaseFirstLetter(of: type)
-                    
-                    annotaionDict["type"] = lowertype
+                    annotaionDict["type"] = "ink"
                     annotaionDict["title"] = inkAnnotation.userName()
                     annotaionDict["page"] = pageIndex
                     annotaionDict["content"] = inkAnnotation.contents
@@ -108,8 +106,11 @@ class RCTCPDFPageUtil: NSObject {
             case "Stamp", "Image":
                 if let stampAnnotation = annoation as? CPDFStampAnnotation {
                     let lowertype = lowercaseFirstLetter(of: type)
-                    
+        
                     annotaionDict["type"] = lowertype
+                    if type == "Image" {
+                        annotaionDict["type"] = "pictures"
+                    }
                     annotaionDict["title"] = stampAnnotation.userName()
                     annotaionDict["page"] = pageIndex
                     annotaionDict["content"] = stampAnnotation.contents
@@ -196,9 +197,7 @@ class RCTCPDFPageUtil: NSObject {
                         
                     case "SignatureFields":
                         if let signatureWidget = form as? CPDFSignatureWidgetAnnotation {
-                            let lowertype = lowercaseFirstLetter(of: widgetType)
-                            
-                            formDict["type"] = lowertype
+                            formDict["type"] = "signaturesFields"
                             formDict["title"] = signatureWidget.fieldName()
                             formDict["page"] = pageIndex
                         }

+ 14 - 25
ios/RCTCPDFView.swift

@@ -95,10 +95,11 @@ class RCTCPDFView: UIView, CPDFViewBaseControllerDelete {
             
             var indexSet = IndexSet()
             for page in pages {
-                indexSet.insert(IndexSet.Element(page+1))
+                indexSet.insert(IndexSet.Element(page))
             }
             
             let success = pdfListView.document.importPages(indexSet, from: document, at: _index)
+            pdfListView.layoutDocumentView()
             
             return success
         } else {
@@ -106,7 +107,7 @@ class RCTCPDFView: UIView, CPDFViewBaseControllerDelete {
         }
     }
     
-    func extractPDFDocument(_ savePath: String, Pages pages: [Int]) -> Bool {
+    func extractPDFDocument(_ savePath: URL, Pages pages: [Int]) -> Bool {
         if let pdfListView = self.pdfViewController?.pdfListView {
             var indexSet = IndexSet()
             for page in pages {
@@ -116,7 +117,7 @@ class RCTCPDFView: UIView, CPDFViewBaseControllerDelete {
             let document = CPDFDocument()
             document?.importPages(indexSet, from: pdfListView.document, at: 0)
             
-            let success = document?.write(to: URL(fileURLWithPath: savePath), isSaveFontSubset: true) ?? false
+            let success = document?.write(to: savePath, isSaveFontSubset: true) ?? false
             
             return success
         } else {
@@ -687,14 +688,10 @@ class RCTCPDFView: UIView, CPDFViewBaseControllerDelete {
         }
     }
     
-    func flattenAllPages(info : NSDictionary, completionHandler: @escaping (Bool) -> Void) {
+    func flattenAllPages(savePath: URL, fontSubset: Bool, completionHandler: @escaping (Bool) -> Void) {
         if let pdfListView = self.pdfViewController?.pdfListView {
-            let _info = info as? [String: Any]
-            
-            let savePath: String = self.getValue(from: _info, key: "save_path", defaultValue: "")
-            let fontSubset: Bool = self.getValue(from: _info, key: "font_sub_set", defaultValue: true)
             
-            let success = pdfListView.document.writeFlatten(to: URL(fileURLWithPath: savePath), isSaveFontSubset: fontSubset)
+            let success = pdfListView.document.writeFlatten(to: savePath, isSaveFontSubset: fontSubset)
             
             completionHandler(success)
         } else {
@@ -702,19 +699,14 @@ class RCTCPDFView: UIView, CPDFViewBaseControllerDelete {
         }
     }
     
-    func saveAs(info : NSDictionary, completionHandler: @escaping (Bool) -> Void) {
+    func saveAs(savePath: URL, removeSecurity: Bool, fontSubset: Bool, completionHandler: @escaping (Bool) -> Void) {
         if let pdfListView = self.pdfViewController?.pdfListView {
-            let _info = info as? [String: Any]
-            
-            let savePath: String = self.getValue(from: _info, key: "save_path", defaultValue: "")
-            let fontSubset: Bool = self.getValue(from: _info, key: "font_sub_set", defaultValue: true)
-            let removeSecurity: Bool = self.getValue(from: _info, key: "remove_security", defaultValue: true)
-            
+           
             var success: Bool = false
             if removeSecurity {
-                pdfListView.document.writeDecrypt(to: URL(fileURLWithPath: savePath), isSaveFontSubset: fontSubset)
+                success = pdfListView.document.writeDecrypt(to: savePath, isSaveFontSubset: fontSubset)
             } else {
-                pdfListView.document.write(to: URL(fileURLWithPath: savePath), isSaveFontSubset: fontSubset)
+                success = pdfListView.document.write(to: savePath, isSaveFontSubset: fontSubset)
             }
             completionHandler(success)
         } else {
@@ -728,7 +720,8 @@ class RCTCPDFView: UIView, CPDFViewBaseControllerDelete {
             
             let password: String = self.getValue(from: _info, key: "password", defaultValue: "")
             let pages: [Int] = self.getValue(from: _info, key: "pages", defaultValue: [])
-            let insert_position: UInt = self.getValue(from: _info, key: "insert_position", defaultValue: 0)
+            let insert_position: Int = self.getValue(from: _info, key: "insert_position", defaultValue: 0)
+
             
             let _document = CPDFDocument(url: filePath)
             
@@ -736,19 +729,15 @@ class RCTCPDFView: UIView, CPDFViewBaseControllerDelete {
                 _document?.unlock(withPassword: password)
             }
             
-            let success = self.insertPDFDocument(_document!, Pages: pages, Position: insert_position)
+            let success = self.insertPDFDocument(_document!, Pages: pages, Position: UInt(insert_position))
             completionHandler(success)
         } else {
             completionHandler(false)
         }
     }
     
-    func splitDocumentPages(info : NSDictionary, completionHandler: @escaping (Bool) -> Void) {
+    func splitDocumentPages(savePath: URL, pages: [Int], completionHandler: @escaping (Bool) -> Void) {
         if let pdfListView = self.pdfViewController?.pdfListView {
-            let _info = info as? [String: Any]
-            
-            let savePath: String = self.getValue(from: _info, key: "save_path", defaultValue: "")
-            let pages: [Int] = self.getValue(from: _info, key: "pages", defaultValue: [])
             
             let success = self.extractPDFDocument(savePath, Pages: pages)
             

+ 6 - 6
ios/RCTCPDFViewManager.swift

@@ -326,16 +326,16 @@ class RCTCPDFReaderView: RCTViewManager, RCTCPDFViewDelegate {
         })
     }
     
-    func flattenAllPages(forCPDFViewTag tag : Int, info : NSDictionary, completionHandler: @escaping (Bool) -> Void) {
+    func flattenAllPages(forCPDFViewTag tag : Int, savePath: URL, fontSubset: Bool, completionHandler: @escaping (Bool) -> Void) {
         let rtcCPDFView = cpdfViews[tag]
-        rtcCPDFView?.flattenAllPages(info: info, completionHandler: { success in
+        rtcCPDFView?.flattenAllPages(savePath: savePath, fontSubset: fontSubset,  completionHandler: { success in
             completionHandler(success)
         })
     }
     
-    func saveAs(forCPDFViewTag tag : Int, info : NSDictionary, completionHandler: @escaping (Bool) -> Void) {
+    func saveAs(forCPDFViewTag tag : Int, savePath: URL, removeSecurity: Bool, fontSubset: Bool, completionHandler: @escaping (Bool) -> Void) {
         let rtcCPDFView = cpdfViews[tag]
-        rtcCPDFView?.saveAs(info: info, completionHandler: { success in
+        rtcCPDFView?.saveAs(savePath: savePath, removeSecurity: removeSecurity, fontSubset: fontSubset, completionHandler: { success in
             completionHandler(success)
         })
     }
@@ -347,9 +347,9 @@ class RCTCPDFReaderView: RCTViewManager, RCTCPDFViewDelegate {
         })
     }
     
-    func splitDocumentPages(forCPDFViewTag tag : Int, info : NSDictionary, completionHandler: @escaping (Bool) -> Void) {
+    func splitDocumentPages(forCPDFViewTag tag : Int, savePath: URL, pages: [Int], completionHandler: @escaping (Bool) -> Void) {
         let rtcCPDFView = cpdfViews[tag]
-        rtcCPDFView?.splitDocumentPages(info: info, completionHandler: { success in
+        rtcCPDFView?.splitDocumentPages(savePath: savePath, pages: pages, completionHandler: { success in
             completionHandler(success)
         })
     }

+ 7 - 3
ios/RCTDocumentManager.m

@@ -192,12 +192,15 @@ RCT_EXTERN_METHOD(getDocumentPath:(NSInteger *)tag
                     withRejecter:(RCTPromiseRejectBlock)reject)
 
 RCT_EXTERN_METHOD(flattenAllPages:(NSInteger)tag
-                  withInfo:(NSDictionary) info
+                  withSavePath:(NSURL *) savePath
+                  withFontSubset:(BOOL) fontSubset
                   withResolver:(RCTPromiseResolveBlock)resolve
                   withRejecter:(RCTPromiseRejectBlock)reject)
 
 RCT_EXTERN_METHOD(saveAs:(NSInteger)tag
-                  withInfo:(NSDictionary) info
+                  withSavePath:(NSURL *) savePath
+                  withRemoveSecurity:(BOOL) removeSecurity
+                  withFontSubset:(BOOL) fontSubset
                   withResolver:(RCTPromiseResolveBlock)resolve
                   withRejecter:(RCTPromiseRejectBlock)reject)
 
@@ -208,7 +211,8 @@ RCT_EXTERN_METHOD(importDocument:(NSInteger)tag
                   withRejecter:(RCTPromiseRejectBlock)reject)
 
 RCT_EXTERN_METHOD(splitDocumentPages:(NSInteger)tag
-                  withInfo:(NSDictionary) info
+                  withSavePath:(NSURL *) savePath
+                  withPages:(NSArray *) pages
                   withResolver:(RCTPromiseResolveBlock)resolve
                   withRejecter:(RCTPromiseRejectBlock)reject)
 

+ 10 - 10
ios/RCTDocumentManager.swift

@@ -505,28 +505,28 @@ class RCTDocumentManager: NSObject, RCTBridgeModule {
         }
     }
     
-    @objc(flattenAllPages: withInfo: withResolver: withRejecter:)
-    func flattenAllPages(forCPDFViewTag tag : Int, info: NSDictionary, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
+    @objc(flattenAllPages: withSavePath: withFontSubset: withResolver: withRejecter:)
+    func flattenAllPages(forCPDFViewTag tag : Int, savePath: URL, fontSubset: Bool, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
         DispatchQueue.main.async {
             let reader = self.readerView()
-            reader.flattenAllPages(forCPDFViewTag: tag, info: info) { success in
+            reader.flattenAllPages(forCPDFViewTag: tag, savePath: savePath, fontSubset: fontSubset) { success in
                 resolve(success)
             }
         }
     }
     
-    @objc(saveAs: withInfo: withResolver: withRejecter:)
-    func saveAs(forCPDFViewTag tag : Int, info: NSDictionary, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
+    @objc(saveAs: withSavePath: withRemoveSecurity: withFontSubset:  withResolver: withRejecter:)
+    func saveAs(forCPDFViewTag tag : Int, savePath: URL, removeSecurity: Bool, fontSubset: Bool, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
         DispatchQueue.main.async {
             let reader = self.readerView()
-            reader.saveAs(forCPDFViewTag: tag, info: info) { success in
+            reader.saveAs(forCPDFViewTag: tag, savePath: savePath, removeSecurity: removeSecurity, fontSubset: fontSubset) { success in
                 resolve(success)
             }
         }
     }
     
     @objc(importDocument: withFilePath: withInfo: withResolver: withRejecter:)
-    func importDocument(forCPDFViewTag tag : Int, filePath: URL, info: NSDictionary, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
+    func importDocument(forCPDFViewTag tag : Int, filePath: URL, info: NSDictionary, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock)  {
         DispatchQueue.main.async {
             let reader = self.readerView()
             reader.importDocument(forCPDFViewTag: tag, filePath: filePath, info: info) { success in
@@ -535,11 +535,11 @@ class RCTDocumentManager: NSObject, RCTBridgeModule {
         }
     }
     
-    @objc(splitDocumentPages: withInfo: withResolver: withRejecter:)
-    func splitDocumentPages(forCPDFViewTag tag : Int, info: NSDictionary, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
+    @objc(splitDocumentPages: withSavePath: withPages: withResolver: withRejecter:)
+    func splitDocumentPages(forCPDFViewTag tag : Int, savePath: URL, pages: [Int], resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
         DispatchQueue.main.async {
             let reader = self.readerView()
-            reader.splitDocumentPages(forCPDFViewTag: tag, info: info) { success in
+            reader.splitDocumentPages(forCPDFViewTag: tag, savePath: savePath, pages: pages) { success in
                 resolve(success)
             }
         }