Browse Source

【转档工具】- 转档功能优化

jiajie 1 year ago
parent
commit
ff42849551

+ 123 - 0
PDF Office/PDF Master/Class/PDFTools/Convert/Common/KMPDFConvert.swift

@@ -217,6 +217,129 @@ class KMPDFConvert: Operation {
             dpi = self.options[KMPDFConvertOptionsKeyImageDPI] as! Int
         }
         let options: [String:Any] = [CPDFConvertOptionsKey.imageDPI.rawValue:dpi,CPDFConvertOptionsKey.allInOneSheet.rawValue:isAllInOneSheet]
+        
+        if self.convertType == .word {
+            self.converter = CPDFConverterWord.init(url: URL(fileURLWithPath: filePath), password: self.password)
+            self.converter.delegate = self
+            let options = CPDFConvertWordOptions()
+            options.layoutOptions = self.isAllInOneSheet ? .retainPageLayout : .retainFlowingText
+            options.isContainAnnotations = true
+//            options.isAllowOCR = self.isAllowOCR
+//            if (self.isAllowOCR) {
+//                options.isContainOCRBgImage = self.isContainOCRBgImage
+//                if let language = self.ocrLanguage {
+//                    options.language = language
+//                } else {
+//                    options.language = .english
+//                }
+//            } else {
+                options.isContainImages = true
+//                options.isContainOCRBgImage = false
+//            }
+            self.converter.convert(toFilePath: self.outputFilePath, pageIndexs: self.pages, options: options)
+            return
+        }
+        if self.convertType == .excel {
+            self.converter = CPDFConverterExcel.init(url: URL(fileURLWithPath: filePath), password: self.password)
+            self.converter.delegate = self
+            let options = CPDFConvertExcelOptions()
+            options.isContainAnnotations = true
+//            options.isAllowOCR = self.isAllowOCR
+//            if (self.isAllowOCR) {
+//                options.isContainOCRBgImage = self.isContainOCRBgImage
+//                if let language = self.ocrLanguage {
+//                    options.language = language
+//                } else {
+//                    options.language = .english
+//                }
+//            } else {
+                options.isContainImages = true
+                options.isContainOCRBgImage = false
+//            }
+            
+            options.contentOptions = self.excelContentOption ?? .allContent
+            options.worksheetOptions = self.excelWorksheetOption ?? .forEachPage
+            
+            self.converter.convert(toFilePath: self.outputFilePath, pageIndexs: self.pages, options: options)
+            return
+        }
+        if self.convertType == .ppt {
+            self.converter = CPDFConverterPPT.init(url: URL(fileURLWithPath: filePath), password: self.password)
+            self.converter.delegate = self
+            let options = CPDFConvertPPTOptions()
+            options.isContainAnnotations = true
+//            options.isAllowOCR = self.isAllowOCR
+            options.isContainImages = true
+            self.converter.convert(toFilePath: self.outputFilePath, pageIndexs: self.pages, options: options)
+            return
+        }
+        
+        if self.convertType == .csv{
+            self.converter = CPDFConverterCsv.init(url: URL(fileURLWithPath: filePath), password: self.password)
+            self.converter.delegate = self
+            let options = CPDFConvertCsvOptions()
+ 
+            self.converter.convert(toFilePath: self.outputFilePath, pageIndexs: self.pages, options: options)
+            return
+        }
+        
+        if self.convertType == .rtf{
+            self.converter = CPDFConverterRtf.init(url: URL(fileURLWithPath: filePath), password: self.password)
+            self.converter.delegate = self
+            let options = CPDFConvertRtfOptions()
+            options.isContainAnnotations = true
+            options.isAllowOCR = true
+//            if (self.isAllowOCR) {
+//                options.isContainOCRBgImage = self.isContainOCRBgImage
+//                if let language = self.ocrLanguage {
+//                    options.language = language
+//                } else {
+//                    options.language = .english
+//                }
+//            } else {
+                options.isContainImages = true
+//                options.isContainOCRBgImage = false
+//            }
+            self.converter.convert(toFilePath: self.outputFilePath, pageIndexs: self.pages, options: options)
+            return
+        }
+        
+        if self.convertType == .html{
+            self.converter = CPDFConverterHtml.init(url: URL(fileURLWithPath: filePath), password: self.password)
+            self.converter.delegate = self
+            let options = CPDFConvertHtmlOptions()
+            options.isContainAnnotations = true
+            options.isAllowOCR = true
+//            if (self.isAllowOCR) {
+//                options.isContainOCRBgImage = self.isContainOCRBgImage
+//                if let language = self.ocrLanguage {
+//                    options.language = language
+//                } else {
+//                    options.language = .english
+//                }
+//            } else {
+                options.isContainImages = true
+//                options.isContainOCRBgImage = false
+//            }
+            self.converter.convert(toFilePath: self.outputFilePath, pageIndexs: self.pages, options: options)
+            return
+        }
+        if self.convertType == .text{
+            self.converter = CPDFConverterTxt.init(url: URL(fileURLWithPath: filePath), password: self.password)
+            self.converter.delegate = self
+            let options = CPDFConvertTxtOptions()
+            options.isAllowOCR = self.isAllowOCR
+            if (self.isAllowOCR) {
+                if let language = self.ocrLanguage {
+                    options.language = language
+                } else {
+                    options.language = .english
+                }
+            }
+            self.converter.convert(toFilePath: self.outputFilePath, pageIndexs: self.pages, options: options)
+            return
+        }
+        
         fpPDFConverter.convertPDF(atPath: filePath, pdfPassword: self.password, pdfPageIndexs: pages, destDocType: pathExtension, destDocPath: outputFilePath, moreOptions: options)
     }
     

+ 2 - 2
PDF Office/PDF Master/Class/PDFTools/Convert/Controller/KMConvertBaseWindowController.swift

@@ -963,8 +963,8 @@ extension KMConvertBaseWindowController: CPDFViewDelegate {
     }
 }
 
-private let kKMConvertCountKey = "KMConvertCountKey"
-private let kKMConvertCountMax = 2
+let kKMConvertCountKey = "KMConvertCountKey"
+let kKMConvertCountMax = 2
 extension KMConvertBaseWindowController {
     func convertCountIsReach() -> Bool {
         #if VERSION_DMG

+ 24 - 8
PDF Office/PDF Master/Class/PDFTools/Convert/NewController/KMConvertWindowController.swift

@@ -532,7 +532,7 @@ class KMConvertWindowController: NSWindowController, NSTextFieldDelegate, NSWind
         if let msg = alertMsg {
             alert.informativeText = msg
         } else {
-            var st = self.pdfDocument?.documentURL.lastPathComponent.count ?? 0 > 0 ? self.pdfDocument?.documentURL.lastPathComponent : KMLocalizedString("Untitled.pdf", nil)
+            let st = self.pdfDocument?.documentURL.lastPathComponent.count ?? 0 > 0 ? self.pdfDocument?.documentURL.lastPathComponent : KMLocalizedString("Untitled.pdf", nil)
             
             alert.messageText = String(format: "%@ %@", st!, KMLocalizedString("Invalid page range or the page number is out of range. Please try again.", nil))
         }
@@ -689,7 +689,7 @@ class KMConvertWindowController: NSWindowController, NSTextFieldDelegate, NSWind
         openPanel.canChooseFiles = false
         openPanel.canChooseDirectories = true
         openPanel.canCreateDirectories = true
-        openPanel.beginSheet(self.window!) { (result) in
+        openPanel.beginSheetModal(for: self.window!) { result in
             if result == .OK {
                 self.savePath = openPanel.url!.path
                 self.beginConvert()
@@ -745,9 +745,12 @@ class KMConvertWindowController: NSWindowController, NSTextFieldDelegate, NSWind
         
         self.showWaitting()
         let pages = NSMutableArray()
-        DispatchQueue.global(qos: .background).async {
-            let filePath = kConvertFileSavePath
-            if self.prePDFView.document.write(toFile: kConvertFileSavePath) {
+        DispatchQueue.global(qos: .background).async { [self] in
+            let filePath = getConvertFileSavePath()
+            if (FileManager.default.fileExists(atPath: filePath)) {
+                try?FileManager.default.removeItem(atPath: filePath)
+            }
+            if self.prePDFView.document.write(toFile: filePath) {
                 for i in 0..<self.prePDFView.document.pageCount {
                     pages.add(i + 1)
                 }
@@ -778,7 +781,7 @@ class KMConvertWindowController: NSWindowController, NSTextFieldDelegate, NSWind
                 }
                 
                 let convert = KMPDFConvert()
-                convert.filePath = filePath ?? ""
+                convert.filePath = filePath
                 convert.pages = selectPages
                 convert.isAllInOneSheet = self.isAllInOneSheet
                 if self.convertType == .Word {
@@ -788,7 +791,7 @@ class KMConvertWindowController: NSWindowController, NSTextFieldDelegate, NSWind
                 }
                 convert.isExtractTable = self.extractTableButton.state == .on
                 convert.outputFolderPath = self.savePath
-                convert.outputFileName = (self.pdfDocument?.documentURL.path.lastPathComponent ?? NSLocalizedString("Untitled", comment: "")).deletingPathExtension
+                convert.outputFileName = (self.pdfDocument?.documentURL.deletingPathExtension().lastPathComponent) ?? NSLocalizedString("Untitled", comment: "")
                 if self.convertType == .AdvancedWord {
                     convert.convertType = .word
                 } else if self.convertType == .JPEG ||
@@ -871,6 +874,19 @@ class KMConvertWindowController: NSWindowController, NSTextFieldDelegate, NSWind
             }
         }
     }
+    func getConvertFileSavePath() -> String {
+        var path = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.applicationSupportDirectory, FileManager.SearchPathDomainMask.userDomainMask, true).last
+        path?.append("/")
+        path?.append(Bundle.main.bundleIdentifier!)
+        if (FileManager.default.fileExists(atPath: path!) == false) {
+            try?FileManager.default.createDirectory(atPath: path!, withIntermediateDirectories: false)
+        }
+        
+        path?.append("/")
+        path?.append("convert.pdf")
+        
+        return path!
+    }
     @IBAction func buttonClicked_MoreConvert(_ sender: Any) {
         var vc: KMToolCompareWindowController? = nil
         if .Word == self.convertType || .AdvancedWord == self.convertType {
@@ -907,7 +923,7 @@ class KMConvertWindowController: NSWindowController, NSTextFieldDelegate, NSWind
             fileAttribute.bAllPage = false
             fileAttribute.pagesString = self.pageRangeTextField.stringValue
             if fileAttribute.fetchSelectPages().count > 0 {
-                var pagesArray = NSMutableArray()
+                let pagesArray = NSMutableArray()
                 for pageNum in fileAttribute.fetchSelectPages() {
                     var tpage = self.pdfDocument?.page(at: UInt(truncating: pageNum) - 1)
                 }