Ver código fonte

【2025】【Tools】批量处理,OCR,处理时会提示未检测到表格-修复

lizhe 1 mês atrás
pai
commit
912b38d18e

+ 29 - 10
PDF Office/PDF Master/KMClass/NewBatch/Manager/KMBatchManager.swift

@@ -111,7 +111,7 @@ extension KMBatchManager {
             let document = self.fetchDocument(filePath: filePath, model: item)
 
             let settingData = data as? KMBatchConvertPDFViewModel ?? KMBatchConvertPDFViewModel()
-            var path = self.fetchFilePath(type: .convertPDF, filePath: filePath, outputFolderPath: outputFolderPath)
+            let path = self.fetchFilePath(type: .convertPDF, filePath: filePath, outputFolderPath: outputFolderPath)
 
             let convert = self.addConvertParameter(settingData)
             let pageCount = document.pageCount
@@ -266,7 +266,8 @@ extension KMBatchManager {
             if document != nil {
                 //计算需要处理的页面
                 
-                var path = self.fetchFilePath(type: .OCR, filePath: item.filePath, outputFolderPath: outputFolderPath)
+                let path = self.fetchFilePath(type: .OCR, filePath: item.filePath, outputFolderPath: outputFolderPath)
+                data.pageRange = self.fetchValidPageIndexs(document!, model: item) ?? []
                 
                 self.convertOCR(outputFolderPath: outputFolderPath, document: document!, fileName: path.deletingPathExtension.lastPathComponent, data: data) { [unowned self] progress in
                     self.itemProgress(item: item, processValue: progress)
@@ -311,7 +312,7 @@ extension KMBatchManager {
                 let item = filesData[i]
                 let document = CPDFDocument.init(url: URL(fileURLWithPath: item.filePath))
                 if document != nil {
-                    var path = self.fetchFilePath(type: .compress, filePath: item.filePath, outputFolderPath: outputFolderPath)
+                    let path = self.fetchFilePath(type: .compress, filePath: item.filePath, outputFolderPath: outputFolderPath)
                     
                     KMCompressManager.shared.compress(documentURL: URL(fileURLWithPath: item.filePath), fileURL: URL(fileURLWithPath: path), limit: false, model: data) { currentPage, totalPages in
                         let progress = Float(currentPage) / Float(totalPages)
@@ -346,7 +347,7 @@ extension KMBatchManager {
                 let item = filesData[i]
                 let docuemt = CPDFDocument.init(url: URL(fileURLWithPath: item.filePath))
                 if (docuemt != nil) {
-                    var path = self.fetchFilePath(type: .security, filePath: item.filePath, outputFolderPath: outputFolderPath)
+                    let path = self.fetchFilePath(type: .security, filePath: item.filePath, outputFolderPath: outputFolderPath)
                     
                     var options: [CPDFDocumentWriteOption : Any] = [:]
                     //开启密码
@@ -405,7 +406,7 @@ extension KMBatchManager {
         if filesData.count != 0 {
             for i in 0..<filesData.count {
                 let item = filesData[i]
-                var path = self.fetchFilePath(type: .watermark, filePath: item.filePath, outputFolderPath: outputFolderPath)
+                let path = self.fetchFilePath(type: .watermark, filePath: item.filePath, outputFolderPath: outputFolderPath)
                 let document = CPDFDocument.init(url: URL(fileURLWithPath: item.filePath))
 
                 if (document?.allowsPrinting == false || document?.allowsCopying == false) {
@@ -453,7 +454,7 @@ extension KMBatchManager {
         if filesData.count != 0 {
             for i in 0..<filesData.count {
                 let item = filesData[i]
-                var path = self.fetchFilePath(type: .background, filePath: item.filePath, outputFolderPath: outputFolderPath)
+                let path = self.fetchFilePath(type: .background, filePath: item.filePath, outputFolderPath: outputFolderPath)
                 let document = CPDFDocument.init(url: URL(fileURLWithPath: item.filePath))
 
                 if (document?.allowsPrinting == false || document?.allowsCopying == false) {
@@ -504,7 +505,7 @@ extension KMBatchManager {
         if filesData.count != 0 {
             for i in 0..<filesData.count {
                 let item = filesData[i]
-                var path = self.fetchFilePath(type: .headerAndFooter, filePath: item.filePath, outputFolderPath: outputFolderPath)
+                let path = self.fetchFilePath(type: .headerAndFooter, filePath: item.filePath, outputFolderPath: outputFolderPath)
                 let document = CPDFDocument.init(url: URL(fileURLWithPath: item.filePath))
 
                 if (document?.allowsPrinting == false || document?.allowsCopying == false) {
@@ -552,7 +553,7 @@ extension KMBatchManager {
         if filesData.count != 0 {
             for i in 0..<filesData.count {
                 let item = filesData[i]
-                var path = self.fetchFilePath(type: .batesNumber, filePath: item.filePath, outputFolderPath: outputFolderPath)
+                let path = self.fetchFilePath(type: .batesNumber, filePath: item.filePath, outputFolderPath: outputFolderPath)
                 let document = CPDFDocument.init(url: URL(fileURLWithPath: item.filePath))
 
                 if (document?.allowsPrinting == false || document?.allowsCopying == false) {
@@ -599,7 +600,7 @@ extension KMBatchManager {
             for i in 0..<filesData.count {
                 let item = filesData[i]
 //                DispatchQueue.global().async {
-                var path = self.fetchFilePath(type: .batchRemove, filePath: item.filePath, outputFolderPath: outputFolderPath)
+                let path = self.fetchFilePath(type: .batchRemove, filePath: item.filePath, outputFolderPath: outputFolderPath)
                     let document = CPDFDocument.init(url: URL(fileURLWithPath: item.filePath))
                     if document != nil {
                         if (document!.allowsPrinting == false || document!.allowsCopying == false) {
@@ -835,6 +836,22 @@ extension KMBatchManager {
         return nil
     }
     
+    func fetchValidPageIndexs(_ document: CPDFDocument, model: KMBatchProcessingTableViewModel) -> [Int]? {
+        if model.pageRange == .all {
+            let pages = Array(0..<Int(document.pageCount))
+            return pages
+        } else {
+            let data = KMOCRModel()
+            data.pageRangeType = model.pageRange
+            data.pageRangeString = model.pageRangeString
+            
+            let pages:[Int] = KMOCRManager.fetchPageIndex(document: document, model: data)
+
+            return pages
+        }
+        return []
+    }
+    
     func fetchDocument(filePath: String, model: KMBatchProcessingTableViewModel) -> CPDFDocument {
         var document = CPDFDocument(url: URL(fileURLWithPath: filePath))
         if model.password.count != 0 {
@@ -894,7 +911,7 @@ extension KMBatchManager {
             fileName = NSLocalizedString("Untitled", comment: "")
         }
 
-        var path = outputFolderPath + "/" + fileName + ".pdf"
+        let path = outputFolderPath + "/" + fileName + ".pdf"
 
 //        // 检查文件是否已存在,如果存在,则添加数字后缀
 //        var finalPath = path
@@ -987,6 +1004,8 @@ extension KMBatchManager {
             errorString = NSLocalizedString("Unsupported security scheme", comment: "")
         } else if myError.code == 9 {
             errorString = NSLocalizedString("Page not found or content error", comment: "")
+        } else if myError.code == 404 {
+            errorString = NSLocalizedString("Please check if the information is wrong or the network is error.", comment: "")
         } else {
             errorString = NSLocalizedString("Table not found", comment: "")
         }