Explorar o código

Merge branch 'develop_2025' of git.kdan.cc:Mac_PDF/PDF_Office into develop_2025

# Conflicts:
#	PDF Office/PDF Master/KMClass/KMPDFViewController/KMMainViewController.swift
niehaoyu hai 2 meses
pai
achega
aab9f48982

+ 220 - 200
PDF Office/PDF Master/KMClass/KMPDFViewController/KMMainViewController.swift

@@ -1423,6 +1423,205 @@ struct KMNMWCFlags {
         
     }
     
+    // MARK: - Secure 【安全】
+    public func hiddenSecureLimitTip() {
+        self.secureAlertView?.removeFromSuperview()
+        self.secureAlertView = nil
+    }
+    
+    func savePageNumberIfNeed() {
+        if (KMPreferenceManager.shared.openLastUnlockedDocumentWhenAppStart) {
+            let scaleFactor = self.listView.scaleFactor ?? 0
+            if scaleFactor <= 0 {
+                return
+            }
+            if self.listView.document != nil {
+                KMPreferenceManager.shared.setPageNumber(self.listView.currentPageIndex, forKey: self.listView.document.documentURL.path)
+                KMPreferenceManager.shared.setPageScale(Float(self.listView.scaleFactor), forKey: self.listView.document.documentURL.path)
+            }
+        }
+    }
+    
+    // MARK: -显示加密弹窗
+    public func showSecureWindow() {
+        guard let url = self.listView.document?.documentURL else {
+            return
+        }
+        self.securityWindowController = KMSecurityWindowController(windowNibName: "KMSecurityWindowController")
+        guard let securityWindowController = securityWindowController else { return }
+        
+        securityWindowController.documentURL = self.listView.document?.documentURL
+        securityWindowController.batchAction = { [unowned self] controller, files in
+            self.view.window?.endSheet((securityWindowController.window)!)
+            
+            
+            let batchWindowController = KMBatchOperateWindowController.sharedWindowController
+            let batchOperateFile = KMBatchOperateFile(filePath: self.document?.documentURL.path ?? "", type: .AddPassword)
+            batchWindowController.switchToOperateType(.AddPassword, files: [batchOperateFile])
+            batchWindowController.window?.makeKeyAndOrderFront("")
+        }
+        
+        securityWindowController.doneAction = { [unowned self] controller, options, attribute in
+            let openPanel = NSOpenPanel()
+            openPanel.canChooseFiles = false
+            openPanel.canChooseDirectories = true
+            openPanel.canCreateDirectories = true
+            openPanel.beginSheetModal(for: NSWindow.currentWindow()) { (result) in
+                if result == NSApplication.ModalResponse.OK {
+                    for fileURL in openPanel.urls {
+                        let document = CPDFDocument(url: self.document?.documentURL)
+                        if document != nil {
+                            document!.setDocumentAttributes(attribute)
+                            
+                            let path = fileURL.path.stringByAppendingPathComponent(url.deletingPathExtension().lastPathComponent) + "_SetPassword" + "." + url.pathExtension
+                            let success = document!.write(to: NSURL(fileURLWithPath: path) as URL, withOptions: options)
+                            if success {
+                                self.view.window?.endSheet((securityWindowController.window)!)
+                                
+                                NSWorkspace.shared.activateFileViewerSelecting([URL(fileURLWithPath: path)])
+                            }
+                        }
+                    }
+                }
+            }
+        }
+        
+        securityWindowController.cancelAction = { [unowned self] controller in
+            self.view.window?.endSheet((securityWindowController.window)!)
+        }
+        NSWindow.currentWindow().beginSheet(securityWindowController.window!)
+    }
+    
+    public func showRemoveSecureWindow() {
+        if !self.document!.allowsCopying || !self.document!.allowsPrinting {
+            let alert = NSAlert()
+            alert.alertStyle = .warning
+            alert.messageText = NSLocalizedString("Warning", comment: "")
+            alert.informativeText = NSLocalizedString("This is a secured document. Editing is not permitted.", comment: "")
+            alert.addButton(withTitle: NSLocalizedString("OK", comment: ""))
+            alert.addButton(withTitle: NSLocalizedString("Remove Security", comment: ""))
+            alert.beginSheetModal(for: NSWindow.currentWindow()) { returnCode in
+                if returnCode == .alertSecondButtonReturn {
+                    DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.25) { [unowned self] in
+                        KMBaseWindowController.checkPassword(url: self.document!.documentURL!, type: .owner) { [unowned self] success, resultPassword in
+                            if success {
+                                self.document?.unlock(withPassword: resultPassword)
+                            }
+                        }
+                    }
+                }
+            }
+        } else {
+            let controller = KMRemovePasswordWindowController(windowNibName: "KMRemovePasswordWindowController")
+            controller.pdfDocument = self.document
+            self.currentWindowController = controller
+            
+            controller.batchAction = { [unowned self] controller, files in
+                self.view.window?.endSheet((self.currentWindowController.window)!)
+                self.currentWindowController = nil
+                
+                let baseWindowController = KMBatchOperateBaseWindowController(windowNibName: "KMBatchOperateBaseWindowController")
+                if #available(macOS 10.13, *) {
+                    baseWindowController.window?.makeKeyAndOrderFront(nil)
+                } else {
+                    baseWindowController.showWindow(nil)
+                }
+                let arr = NSMutableArray()
+                let file = KMBatchOperateFile(filePath: self.listView.document!.documentURL!.path, type: .RemovePassword)
+                file.password = document?.password ?? ""
+                arr.add(file)
+                
+                baseWindowController.switchToOperateType(.RemovePassword, files: arr as! [KMBatchOperateFile])
+            }
+            
+            controller.cancelAction = { [unowned self] controller in
+                self.view.window?.endSheet((self.currentWindowController.window)!)
+                self.currentWindowController = nil
+            }
+            
+            controller.doneAction = {  [unowned self] controller in
+                self.view.window?.endSheet((self.currentWindowController.window)!)
+                self.currentWindowController = nil
+                
+                KMBaseWindowController.checkPassword(url: self.document!.documentURL!, type: .owner, password: self.document?.password ?? "") { [unowned self] success, resultPassword in
+                    if success {
+                        
+                        let savePanel = NSSavePanel()
+                        savePanel.nameFieldStringValue = self.listView.document.documentURL.deletingPathExtension().lastPathComponent + "_RemovePassword"
+                        savePanel.allowedFileTypes = ["pdf"]
+                        
+                        savePanel.beginSheetModal(for: NSApp.mainWindow!) {[unowned self] result in
+                            guard result == .OK else { return }
+                            
+                            /// 删除安全性设置
+                            if (!self.listView.document!.allowsCopying || !self.listView.document!.allowsPrinting) {
+                                self.model.isSaveKeyChain = false
+                                self.listView.document.unlock(withPassword: resultPassword)
+                            }
+                            
+                            let document = CPDFDocument.init(url: self.listView.document.documentURL)
+                            guard let document = document else { return }
+                            
+                            document.unlock(withPassword: resultPassword)
+                            let success = document.writeDecrypt(to: savePanel.url)
+                            if success {
+                                self.hiddenSecureLimitTip()
+                                let tip = KMRemovePasswordResultTipView()
+                                tip.result = .success
+                                tip.showInView(superView: (self.listView.superview)!)
+                                
+                                NSWorkspace.shared.activateFileViewerSelecting([savePanel.url!])
+                            } else {
+                                self.hiddenSecureLimitTip()
+                                let tip = KMRemovePasswordResultTipView()
+                                tip.result = .failure
+                                tip.showInView(superView: (self.listView.superview)!)
+                            }
+                        }
+                    }
+                }
+            }
+            NSWindow.currentWindow().beginSheet(controller.window!)
+            
+        }
+    }
+    
+    //MARK: - 测量
+     
+    
+    @objc func cancelMeasureType() {
+        self.hideMeasureFloatingWindows()
+        
+    }
+    
+    func hideMeasureFloatingWindows() {
+        if distanceMeasureInfoWindowController?.window?.isVisible == true {
+            distanceMeasureInfoWindowController?.hideFloatingWindow()
+        } else if perimeterMeasureInfoWindowController?.window?.isVisible == true {
+            perimeterMeasureInfoWindowController?.hideFloatingWindow()
+        } else if areaMeasureInfoWindowController?.window?.isVisible == true {
+            areaMeasureInfoWindowController?.hideFloatingWindow()
+        }
+    }
+    
+    func showMeasureFloatingWindowsIfNeed() {
+        let toolMode = self.listView.toolMode
+        if toolMode != .CNoteToolMode {
+            return
+        }
+        let type = self.listView.annotationType
+        if type == .measureLine {
+            self.distanceMeasureInfoWindowController?.window?.orderFront(nil)
+        } else if type == .measurePolyLine {
+            self.perimeterMeasureInfoWindowController?.window?.orderFront(nil)
+        } else if type == .measurePolyGon {
+            self.areaMeasureInfoWindowController?.window?.orderFront(nil)
+        } else if type == .measureSquare {
+            self.areaMeasureInfoWindowController?.window?.orderFront(nil)
+        }
+    }
+    
+    
     //MARK: - Watermark水印
     func showWatermarkController() {
         viewManager.editType = .watermark
@@ -2476,6 +2675,10 @@ extension KMMainViewController {
             return
         }
         let window = KMOCRSettingWindowController(windowNibName: "KMOCRSettingWindowController")
+        window.OCRAction = {[unowned self] controller, model in
+            self.convertOCRScanFile(window: window, document: self.listView.document, model: model)
+        }
+        
         self.km_beginSheet(windowC: window)
     }
     
@@ -2524,169 +2727,19 @@ extension KMMainViewController {
         })
     }
     
-    // MARK: - Secure 【安全】
-    public func hiddenSecureLimitTip() {
-        self.secureAlertView?.removeFromSuperview()
-        self.secureAlertView = nil
-    }
-    
-    func savePageNumberIfNeed() {
-        if (KMPreferenceManager.shared.openLastUnlockedDocumentWhenAppStart) {
-            let scaleFactor = self.listView.scaleFactor ?? 0
-            if scaleFactor <= 0 {
-                return
-            }
-            if self.listView.document != nil {
-                KMPreferenceManager.shared.setPageNumber(self.listView.currentPageIndex, forKey: self.listView.document.documentURL.path)
-                KMPreferenceManager.shared.setPageScale(Float(self.listView.scaleFactor), forKey: self.listView.document.documentURL.path)
-            }
+    //OCR
+    func convertOCRScanFile(window: KMOCRSettingWindowController, document: CPDFDocument, model: KMOCRModel) {
+        window.beginLoading()
+        KMOCRManager.manager.convertScanFile(document: document, model: model) { document, error in
+            window.endLoading()
         }
+        //        self.listView.document.removePage(at: index)
+        //        self.listView.document.insertPageObject(tempPage, at: index)
+        //        self.listView.layoutDocumentView()
     }
-    
+     
     // MARK: -显示加密弹窗
-    public func showSecureWindow() {
-        guard let url = self.listView.document?.documentURL else {
-            return
-        }
-        self.securityWindowController = KMSecurityWindowController(windowNibName: "KMSecurityWindowController")
-        guard let securityWindowController = securityWindowController else { return }
-        
-        securityWindowController.documentURL = self.listView.document?.documentURL
-        securityWindowController.batchAction = { [unowned self] controller, files in
-            self.view.window?.endSheet((securityWindowController.window)!)
-            
-            
-            let batchWindowController = KMBatchOperateWindowController.sharedWindowController
-            let batchOperateFile = KMBatchOperateFile(filePath: self.document?.documentURL.path ?? "", type: .AddPassword)
-            batchWindowController.switchToOperateType(.AddPassword, files: [batchOperateFile])
-            batchWindowController.window?.makeKeyAndOrderFront("")
-        }
-        
-        securityWindowController.doneAction = { [unowned self] controller, options, attribute in
-            let openPanel = NSOpenPanel()
-            openPanel.canChooseFiles = false
-            openPanel.canChooseDirectories = true
-            openPanel.canCreateDirectories = true
-            openPanel.beginSheetModal(for: NSWindow.currentWindow()) { (result) in
-                if result == NSApplication.ModalResponse.OK {
-                    for fileURL in openPanel.urls {
-                        let document = CPDFDocument(url: self.document?.documentURL)
-                        if document != nil {
-                            document!.setDocumentAttributes(attribute)
-                            
-                            let path = fileURL.path.stringByAppendingPathComponent(url.deletingPathExtension().lastPathComponent) + "_SetPassword" + "." + url.pathExtension
-                            let success = document!.write(to: NSURL(fileURLWithPath: path) as URL, withOptions: options)
-                            if success {
-                                self.view.window?.endSheet((securityWindowController.window)!)
-                                
-                                NSWorkspace.shared.activateFileViewerSelecting([URL(fileURLWithPath: path)])
-                            }
-                        }
-                    }
-                }
-            }
-        }
-        
-        securityWindowController.cancelAction = { [unowned self] controller in
-            self.view.window?.endSheet((securityWindowController.window)!)
-        }
-        NSWindow.currentWindow().beginSheet(securityWindowController.window!)
-    }
-    
-    public func showRemoveSecureWindow() {
-        if !self.document!.allowsCopying || !self.document!.allowsPrinting {
-            let alert = NSAlert()
-            alert.alertStyle = .warning
-            alert.messageText = NSLocalizedString("Warning", comment: "")
-            alert.informativeText = NSLocalizedString("This is a secured document. Editing is not permitted.", comment: "")
-            alert.addButton(withTitle: NSLocalizedString("OK", comment: ""))
-            alert.addButton(withTitle: NSLocalizedString("Remove Security", comment: ""))
-            alert.beginSheetModal(for: NSWindow.currentWindow()) { returnCode in
-                if returnCode == .alertSecondButtonReturn {
-                    DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.25) { [unowned self] in
-                        KMBaseWindowController.checkPassword(url: self.document!.documentURL!, type: .owner) { [unowned self] success, resultPassword in
-                            if success {
-                                self.document?.unlock(withPassword: resultPassword)
-                                
-                            }
-                        }
-                    }
-                }
-            }
-        } else {
-            let controller = KMRemovePasswordWindowController(windowNibName: "KMRemovePasswordWindowController")
-            controller.pdfDocument = self.document
-            self.currentWindowController = controller
-            
-            controller.batchAction = { [unowned self] controller, files in
-                self.view.window?.endSheet((self.currentWindowController.window)!)
-                self.currentWindowController = nil
-                
-                let baseWindowController = KMBatchOperateBaseWindowController(windowNibName: "KMBatchOperateBaseWindowController")
-                if #available(macOS 10.13, *) {
-                    baseWindowController.window?.makeKeyAndOrderFront(nil)
-                } else {
-                    baseWindowController.showWindow(nil)
-                }
-                let arr = NSMutableArray()
-                let file = KMBatchOperateFile(filePath: self.listView.document!.documentURL!.path, type: .RemovePassword)
-                file.password = document?.password ?? ""
-                arr.add(file)
-                
-                baseWindowController.switchToOperateType(.RemovePassword, files: arr as! [KMBatchOperateFile])
-            }
-            
-            controller.cancelAction = { [unowned self] controller in
-                self.view.window?.endSheet((self.currentWindowController.window)!)
-                self.currentWindowController = nil
-            }
-            
-            controller.doneAction = {  [unowned self] controller in
-                self.view.window?.endSheet((self.currentWindowController.window)!)
-                self.currentWindowController = nil
-                
-                KMBaseWindowController.checkPassword(url: self.document!.documentURL!, type: .owner, password: self.document?.password ?? "") { [unowned self] success, resultPassword in
-                    if success {
-                        
-                        let savePanel = NSSavePanel()
-                        savePanel.nameFieldStringValue = self.listView.document.documentURL.deletingPathExtension().lastPathComponent + "_RemovePassword"
-                        savePanel.allowedFileTypes = ["pdf"]
-                        
-                        savePanel.beginSheetModal(for: NSApp.mainWindow!) {[unowned self] result in
-                            guard result == .OK else { return }
-                            
-                            /// 删除安全性设置
-                            if (!self.listView.document!.allowsCopying || !self.listView.document!.allowsPrinting) {
-                                self.model.isSaveKeyChain = false
-                                self.listView.document.unlock(withPassword: resultPassword)
-                            }
-                            
-                            let document = CPDFDocument.init(url: self.listView.document.documentURL)
-                            guard let document = document else { return }
-                            
-                            document.unlock(withPassword: resultPassword)
-                            let success = document.writeDecrypt(to: savePanel.url)
-                            if success {
-                                self.hiddenSecureLimitTip()
-                                let tip = KMRemovePasswordResultTipView()
-                                tip.result = .success
-                                tip.showInView(superView: (self.listView.superview)!)
-                                
-                                NSWorkspace.shared.activateFileViewerSelecting([savePanel.url!])
-                            } else {
-                                self.hiddenSecureLimitTip()
-                                let tip = KMRemovePasswordResultTipView()
-                                tip.result = .failure
-                                tip.showInView(superView: (self.listView.superview)!)
-                            }
-                        }
-                    }
-                }
-            }
-            NSWindow.currentWindow().beginSheet(controller.window!)
-            
-        }
-    }
+     
     
     //MARK: - Redact密文
     func showRedactProperty() {
@@ -2729,40 +2782,7 @@ extension KMMainViewController {
             cancelMeasureType()
         }
     }
-    
-    @objc func cancelMeasureType() {
-        self.hideMeasureFloatingWindows()
-        
-    }
-    
-    func hideMeasureFloatingWindows() {
-        if distanceMeasureInfoWindowController?.window?.isVisible == true {
-            distanceMeasureInfoWindowController?.hideFloatingWindow()
-        } else if perimeterMeasureInfoWindowController?.window?.isVisible == true {
-            perimeterMeasureInfoWindowController?.hideFloatingWindow()
-        } else if areaMeasureInfoWindowController?.window?.isVisible == true {
-            areaMeasureInfoWindowController?.hideFloatingWindow()
-        }
-    }
-    
-    func showMeasureFloatingWindowsIfNeed() {
-        let toolMode = self.listView.toolMode
-        if toolMode != .CNoteToolMode {
-            return
-        }
-        let type = self.listView.annotationType
-        if type == .measureLine {
-            self.distanceMeasureInfoWindowController?.window?.orderFront(nil)
-        } else if type == .measurePolyLine {
-            self.perimeterMeasureInfoWindowController?.window?.orderFront(nil)
-        } else if type == .measurePolyGon {
-            self.areaMeasureInfoWindowController?.window?.orderFront(nil)
-        } else if type == .measureSquare {
-            self.areaMeasureInfoWindowController?.window?.orderFront(nil)
-        }
-    }
-    
-    
+     
 }
 
 //MARK: - 代理方法
@@ -3135,7 +3155,7 @@ extension KMMainViewController: KMPDFToolbarControllerDelegate {
             } else if itemIdentifier == KMPDFToolbar_protect_redact_Property_Identifier {
                 self.showRedactProperty()
             } else if itemIdentifier == KMPDFToolbar_protect_redact_Apply_Identifier {
-                 
+                
             } else if itemIdentifier == KMPDFToolbar_protect_redact_Exit_Identifier {
                 
             } else if itemIdentifier == KMPDFToolbar_protect_security_Identifier {
@@ -4814,7 +4834,7 @@ extension KMMainViewController {
             }
         }
     }
-     
+    
     func loadOpenFileFunctionGuide(_ showType: KMGuideInfoType) -> Void {
         if showType == .openFileNormal && KMGuideInfoWindowController.availableShow(.openFileNormal) {
             
@@ -4981,7 +5001,7 @@ extension KMMainViewController {
     }
     
     // MARK: - Private Methods
-   
+    
     func isPDFPageCountExceedsLimit(filePath: String) -> Bool {
         let url = URL(fileURLWithPath: filePath)
         guard let document = PDFDocument(url: url) else {
@@ -4993,7 +5013,7 @@ extension KMMainViewController {
     }
     
     // MARK: - Redact 【标记密文】
-     func exeRedactConfirm(_ type: KMRedactConfirmType, callback: @escaping () -> ()?) {
+    func exeRedactConfirm(_ type: KMRedactConfirmType, callback: @escaping () -> ()?) {
         let windowController = KMRedactConfirmWindowController(type)
         self.currentWindowController = windowController
         self.view.window?.beginSheet(windowController.window!)
@@ -5275,7 +5295,7 @@ extension KMMainViewController {
         }
         AutoSaveManager.manager.removeAutoSavePath(self.listView.document?.documentURL.path ?? "")
     }
-     
+    
     // MARK: - 选择缩放模式
     
     @objc public func selectZoom(_ type: KMPDFZoomType) {
@@ -5548,5 +5568,5 @@ extension KMMainViewController {
         self.savePageNumberIfNeed()
         self.saveDocument()
     }
-     
+    
 }

+ 1 - 1
PDF Office/PDF Master/KMClass/KMPDFViewController/RightSideController/Views/OCR/Edit/View/KMOCRSettingView.swift

@@ -60,7 +60,7 @@ extension KMOCRSettingView {
     
     @objc func OCRButtonAction(_ sender: Any) {
         guard let callBack = OCRAction else { return }
-        
+        self.OCRPageView.reloadData()
         
         callBack(self, self.model)
     }

+ 8 - 1
PDF Office/PDF Master/KMClass/KMPDFViewController/RightSideController/Views/OCR/Edit/Window/KMOCRSettingWindowController.swift

@@ -7,9 +7,14 @@
 
 import Cocoa
 
+typealias KMOCRSettingWindowControllerOCRAction = (_ controller: KMOCRSettingWindowController, _ model: KMOCRModel) -> Void
+
 class KMOCRSettingWindowController: KMBaseWindowController {
 
     @IBOutlet weak var OCRSettingView: KMOCRSettingView!
+    
+    var OCRAction: KMOCRSettingWindowControllerOCRAction?
+    
     override func windowDidLoad() {
         super.windowDidLoad()
         
@@ -22,7 +27,9 @@ class KMOCRSettingWindowController: KMBaseWindowController {
         }
         
         OCRSettingView.OCRAction = { [unowned self] view, model in
-            model.ocrType
+            guard let callBack = OCRAction else { return }
+            
+            OCRAction?(self, model)
         }
     }
     

+ 4 - 1
PDF Office/PDF Master/KMClass/KMPDFViewController/RightSideController/Views/OCR/Tool/Contoller/KMOCRController.swift

@@ -38,7 +38,7 @@ class KMOCRController: NSViewController {
         // Do view setup here.
 //        self.pageView.reloadData()
         self.model = KMOCRModel()
-        self.model.document = pdfView?.document
+//        self.model.document = pdfView?.document
         self.updateUI()
         self.reloadData()
     }
@@ -141,6 +141,9 @@ extension KMOCRController {
         } else {
             guard let callBack = doneAction else { return }
             
+            //刷新page数据
+            self.pageView.reloadData()
+            
             callBack(self, model)
         }
     }

+ 108 - 0
PDF Office/PDF Master/KMClass/KMPDFViewController/RightSideController/Views/OCR/Tool/Manager/KMOCRManager.swift

@@ -6,10 +6,20 @@
 //
 
 import Cocoa
+import ComPDFKit_Conversion
+import ComDocumentAIKit
+
+typealias KMOCRManagerOCRComplete = (_ document: CPDFDocument, _ error: Error) -> Void
 
 class KMOCRManager: NSObject {
     static let manager = KMOCRManager()
     
+    private var converter: CPDFConverter?
+    private var outputPath = ""
+    private var searchablePages: [Int] = []
+
+    private var OCRComplete: KMOCRManagerOCRComplete?
+    
 //    var documentContainsImagePages: [String: Int] = [:]
     
     let languages: [String] = (KMGOCRManager.languages() as NSArray).value(forKeyPath: KMGOCRLanguageStringKey) as! [String]
@@ -39,3 +49,101 @@ class KMOCRManager: NSObject {
 //        return isContainsImagePage
 //    }
 }
+
+//MARK: Tool OCR
+extension KMOCRManager {
+    
+}
+
+//MARK: Edit OCR
+extension KMOCRManager {
+    func convertScanFile(document: CPDFDocument, model: KMOCRModel, complete: @escaping KMOCRManagerOCRComplete) {
+        OCRComplete = complete
+        
+//        KMPageRangeTools.findSelectPage(pageRangeString: <#T##String#>, pageCount: <#T##Int#>)
+//        let fileAttribute = KMNFileAttribute()
+////        fileAttribute.password = listView.document?.password ?? ""
+//        fileAttribute.pdfDocument = document
+////        fileAttribute.filePath =  listView.document?.documentURL.path ?? ""
+//        fileAttribute.bAllPage = false
+//        fileAttribute.pagesType = .PagesString
+////        fileAttribute.pagesString = model.pageRange
+//        
+//        let fetchSelectPages = fileAttribute.fetchSelectPages()
+        
+        
+        
+//        let error = NSError(domain: "com.example.MyApp", code: 404, userInfo: [NSLocalizedDescriptionKey: "Resource not found."])
+//        complete(resultDocument, error)
+    }
+    
+    func transitionPDF(filePath: String, index: UInt) {
+        if self.searchablePages.contains(Int(index - 1)) {
+            return
+        }
+        let tempDirectory = NSTemporaryDirectory()
+        let outputPath = (tempDirectory as NSString).appendingPathComponent("com.compdfkit.conversion")
+        let finalPath = (outputPath as NSString).appendingPathComponent("/Searchable")
+
+        let fileManager = FileManager.default
+
+        // 检查文件是否存在并删除
+        if fileManager.fileExists(atPath: finalPath) {
+            do {
+                try fileManager.removeItem(atPath: finalPath)
+            } catch {
+                print("Failed to remove file: \(error)")
+            }
+        }
+
+        // 创建目录
+        do {
+            try fileManager.createDirectory(atPath: finalPath, withIntermediateDirectories: true, attributes: nil)
+        } catch {
+            print("Failed to create directory: \(error)")
+        }
+        
+        self.outputPath = finalPath.deletingPathExtension + "/temp_Searchable.pdf";
+        self.converter = CPDFConverterSearchablePDF(url: NSURL.fileURL(withPath: filePath), password: "")
+        self.converter?.delegate = self
+        
+        let txtOptions = CPDFConvertSearchablePDFOptions()
+        txtOptions.language = .chinese
+        self.converter?.convert(toFilePath: self.outputPath, pageIndexs: [index], options:txtOptions)
+    }
+}
+
+//MARK: CPDFConverterDelegate
+extension KMOCRManager: CPDFConverterDelegate {
+    func converter(_ converter: CPDFConverter!, didEndConvert error: Error!) {
+        print("结束转档")
+        
+        DispatchQueue.main.async {
+            let tempDocument: CPDFDocument = CPDFDocument(url: NSURL.fileURL(withPath: self.outputPath));
+            let tempPage = tempDocument.page(at: 0)
+            
+            let index = 0//UInt(self.listView.currentPageIndex)
+//            self.listView.document.removePage(at: index)
+//            self.listView.document.insertPageObject(tempPage, at: index)
+//            self.listView.layoutDocumentView()
+            
+//            self.progressC?.increment(by: 1.0)
+//            self.progressC?.increment(by: 1.0)
+//            self.hiddenProgressWindow()
+            
+            self.searchablePages.append(Int(index))
+            self.OCRComplete?(tempDocument, error)
+        }
+    }
+    
+    func converter(_ converter: CPDFConverter!, didStartConvert error: Error!) {
+        print("开始转档")
+//        self.showProgressWindow(message: NSLocalizedString("转换", comment: ""))
+//        self.progressC?.maxValue = 3.0
+//        self.progressC?.increment(by: 1.0)
+    }
+    
+    func converter(_ converter: CPDFConverter!, pageIndex index: UInt, pageCount count: UInt) {
+        print("")
+    }
+}

+ 5 - 2
PDF Office/PDF Master/KMClass/KMPDFViewController/RightSideController/Views/OCR/Tool/Model/KMOCRModel.swift

@@ -23,9 +23,12 @@ class KMOCRModel: NSObject {
     var language: String = ""
     var imageEnhancement: Bool = false
     var imageCorrection: Bool = false
-    var pageRange: String = ""
     var saveAsPDF: Bool = false
     var saveType: KMOCRSaveType = .PDF
     
-    var document: CPDFDocument?
+    var pageRange: [Int] = []
+    var pageRangeString: String = ""
+    var pageRangeType: KMPageRange = .current
+    
+//    var document: CPDFDocument?
 }

+ 24 - 15
PDF Office/PDF Master/KMClass/KMPDFViewController/RightSideController/Views/OCR/Tool/View/Page/KMOCRPageView.swift

@@ -124,9 +124,7 @@ class KMOCRPageView: BaseXibView {
         
         saveAsPDFButton.setTarget(self, action: #selector(saveAsPDFAction(_:)))
         
-        guard let document = self.model.document else { return }
-        let pageIndex = self.pageSelectButton.getSelectedPageIndex(document)
-        self.model.language = self.pageSelectButton.getSelectedPageString(document, pageIndex.0)
+        self.pageSelectButton.pageSelectView.selectItemAtIndex(0)
     }
     
     func updatePlanUI() {
@@ -161,7 +159,24 @@ class KMOCRPageView: BaseXibView {
         self.enhancementSwitch.properties.open = model.imageEnhancement
         self.correctionSwitch.properties.open = model.imageCorrection
         
-        self.pageSelectButton.pageSelectView.selectItemAtIndex(0)
+        switch self.pageSelectButton.pageSelectView.indexOfSelect() {
+        case 0:
+            self.model.pageRangeType = .current
+        case 1:
+            self.model.pageRangeType = .all
+        case 2:
+            self.model.pageRangeType = .odd
+        case 3:
+            self.model.pageRangeType = .even
+        case 4:
+            self.model.pageRangeType = .custom
+            self.model.pageRangeString = self.pageSelectButton.pageSelectView.properties.text ?? ""
+        default:
+            self.model.pageRangeType = .current
+            break
+        }
+        
+//        self.model.language = self.languageSelectButton.properties.text ?? ""
     }
     
     func updateLanguages() {
@@ -229,17 +244,11 @@ extension KMOCRPageView {
 
 extension KMOCRPageView: ComponentSelectDelegate {
     func componentSelectDidSelect(view: ComponentSelect?, menuItemProperty: ComponentMenuitemProperty?) {
-        if(view == pageSelectButton) {
-//            if menuItemProperty?.identifier == "1" {
-//                let familyName = fontNameSelect.properties.text ?? "Helvetica"
-//                let styles = CPDFFont.fontNames(forFamilyName: familyName)
-//                
-//                compdfFont = CPDFFont(familyName: familyName, fontStyle: styles.first ?? "")
-//                annotationPopMode.setAnnotationFont(font: compdfFont ?? CPDFFont.init(familyName: "Helvetica", fontStyle: ""))
-//            } else if menuItemProperty?.identifier == "2"{ //字体样式
-//                compdfFont = CPDFFont(familyName: compdfFont?.familyName ?? "Helvetica", fontStyle: menuItemProperty?.text ?? "")
-//                annotationPopMode.setAnnotationFont(font: compdfFont ?? CPDFFont.init(familyName: "Helvetica", fontStyle: ""))
-//            }
+        if (view == languageSelectButton) {
+            model.language = menuItemProperty?.text ?? ""
+            let languages: [String] = KMOCRManager.manager.languages
+            let position = languages.firstIndex(of: model.language) ?? 0
+            self.selectIndex = position + 1
         }
     }
 }