|
@@ -97,62 +97,6 @@ extension KMBatchManager {
|
|
|
self.convertFile(outputFolderPath: outputFolderPath, data: data, filesData: self.batchFilesData)
|
|
|
}
|
|
|
|
|
|
-// func convertFile(outputFolderPath: String, data: KMBatchSettingItemViewModel, filesData: [KMBatchProcessingTableViewModel]) {
|
|
|
-// if filesData.count != 0 {
|
|
|
-// DispatchQueue.global().async {
|
|
|
-// for i in 0..<filesData.count {
|
|
|
-// let item = filesData[i]
|
|
|
-// //创建Document
|
|
|
-// let filePath = item.filePath
|
|
|
-// let document = self.fetchDocument(filePath: filePath, model: item)
|
|
|
-//
|
|
|
-// let settingData = data as? KMBatchConvertPDFViewModel ?? KMBatchConvertPDFViewModel()
|
|
|
-// var fileName = filePath.deletingPathExtension.lastPathComponent
|
|
|
-// if ((fileName.isEmpty)) {
|
|
|
-// fileName = NSLocalizedString("Untitled", comment: "")
|
|
|
-// }
|
|
|
-//
|
|
|
-// let convert = self.addConvertParameter(settingData)
|
|
|
-//
|
|
|
-// let pageCount = document.pageCount
|
|
|
-// //获取page
|
|
|
-// var pages:[Int] = []
|
|
|
-// for i in 0..<pageCount {
|
|
|
-// pages.append(Int(i)+1)
|
|
|
-// }
|
|
|
-//
|
|
|
-// convert.outputFolderPath = outputFolderPath
|
|
|
-// convert.filePath = filePath
|
|
|
-// convert.outputFileName = fileName
|
|
|
-// convert.pages = pages
|
|
|
-//
|
|
|
-// convert.isAllowOCR = settingData.needRecognizeText
|
|
|
-// convert.ocrLanguage = settingData.languageType
|
|
|
-//
|
|
|
-// item.state = .clock
|
|
|
-// KMPDFConvertManager.defaultManager.convert(convert: convert, progress: { [unowned self] progressValue in
|
|
|
-// print("转档进度 - \(progressValue)")
|
|
|
-// let progress = Float(progressValue) / Float(pageCount)
|
|
|
-// self.itemProgress(item: item, processValue: progress)
|
|
|
-// }, completion: { [unowned self] finished, error in
|
|
|
-// if finished {
|
|
|
-// if FileManager.default.fileExists(atPath: outputFolderPath) {
|
|
|
-// NSWorkspace.shared.activateFileViewerSelecting([URL(fileURLWithPath: outputFolderPath)])
|
|
|
-// }
|
|
|
-// self.itemSuccess(item: item)
|
|
|
-// } else {
|
|
|
-// self.itemFailure(item: item, error: error! as NSError)
|
|
|
-// }
|
|
|
-//
|
|
|
-// if i == filesData.count - 1 {
|
|
|
-// self.batchSuccess()
|
|
|
-// }
|
|
|
-// })
|
|
|
-// }
|
|
|
-// }
|
|
|
-// }
|
|
|
-// }
|
|
|
-
|
|
|
func convertFile(outputFolderPath: String, data: KMBatchSettingItemViewModel, filesData: [KMBatchProcessingTableViewModel]) {
|
|
|
guard !filesData.isEmpty else { return }
|
|
|
|
|
@@ -167,10 +111,7 @@ extension KMBatchManager {
|
|
|
let document = self.fetchDocument(filePath: filePath, model: item)
|
|
|
|
|
|
let settingData = data as? KMBatchConvertPDFViewModel ?? KMBatchConvertPDFViewModel()
|
|
|
- var fileName = filePath.deletingPathExtension.lastPathComponent
|
|
|
- if fileName.isEmpty {
|
|
|
- fileName = NSLocalizedString("Untitled", comment: "")
|
|
|
- }
|
|
|
+ var path = self.fetchFilePath(type: .convertPDF, filePath: filePath, outputFolderPath: outputFolderPath)
|
|
|
|
|
|
let convert = self.addConvertParameter(settingData)
|
|
|
let pageCount = document.pageCount
|
|
@@ -180,7 +121,7 @@ extension KMBatchManager {
|
|
|
|
|
|
convert.outputFolderPath = outputFolderPath
|
|
|
convert.filePath = filePath
|
|
|
- convert.outputFileName = fileName
|
|
|
+ convert.outputFileName = path.deletingPathExtension.lastPathComponent
|
|
|
convert.pages = pages
|
|
|
convert.isAllowOCR = settingData.needRecognizeText
|
|
|
convert.ocrLanguage = settingData.languageType
|
|
@@ -327,13 +268,9 @@ extension KMBatchManager {
|
|
|
if document != nil {
|
|
|
//计算需要处理的页面
|
|
|
|
|
|
- var fileName = item.filePath.deletingPathExtension.lastPathComponent
|
|
|
- if ((fileName.isEmpty)) {
|
|
|
- fileName = NSLocalizedString("Untitled", comment: "")
|
|
|
- }
|
|
|
- let path = outputFolderPath + "/" + fileName + ".pdf"
|
|
|
+ var path = self.fetchFilePath(type: .OCR, filePath: item.filePath, outputFolderPath: outputFolderPath)
|
|
|
|
|
|
- self.convertOCR(outputFolderPath: outputFolderPath, document: document!, fileName: fileName, data: data) { [unowned self] progress in
|
|
|
+ self.convertOCR(outputFolderPath: outputFolderPath, document: document!, fileName: path.deletingPathExtension.lastPathComponent, data: data) { [unowned self] progress in
|
|
|
self.itemProgress(item: item, processValue: progress)
|
|
|
} complete: { [unowned self] document, text, error in
|
|
|
if error == nil {
|
|
@@ -376,11 +313,7 @@ extension KMBatchManager {
|
|
|
let item = filesData[i]
|
|
|
let document = CPDFDocument.init(url: URL(fileURLWithPath: item.filePath))
|
|
|
if document != nil {
|
|
|
- var fileName = item.filePath.deletingPathExtension.lastPathComponent
|
|
|
- if ((fileName.isEmpty)) {
|
|
|
- fileName = NSLocalizedString("Untitled", comment: "")
|
|
|
- }
|
|
|
- let path = outputFolderPath + "/" + fileName + ".pdf"
|
|
|
+ var 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)
|
|
@@ -415,11 +348,7 @@ extension KMBatchManager {
|
|
|
let item = filesData[i]
|
|
|
let docuemt = CPDFDocument.init(url: URL(fileURLWithPath: item.filePath))
|
|
|
if (docuemt != nil) {
|
|
|
- var fileName = item.filePath.deletingPathExtension.lastPathComponent
|
|
|
- if ((fileName.isEmpty)) {
|
|
|
- fileName = NSLocalizedString("Untitled", comment: "")
|
|
|
- }
|
|
|
- let path = outputFolderPath + "/" + fileName + ".pdf"
|
|
|
+ var path = self.fetchFilePath(type: .security, filePath: item.filePath, outputFolderPath: outputFolderPath)
|
|
|
|
|
|
var options: [CPDFDocumentWriteOption : Any] = [:]
|
|
|
//开启密码
|
|
@@ -479,12 +408,7 @@ extension KMBatchManager {
|
|
|
if filesData.count != 0 {
|
|
|
for i in 0..<filesData.count {
|
|
|
let item = filesData[i]
|
|
|
- var fileName = item.filePath.deletingPathExtension.lastPathComponent
|
|
|
- if ((fileName.isEmpty)) {
|
|
|
- fileName = NSLocalizedString("Untitled", comment: "")
|
|
|
- }
|
|
|
-
|
|
|
- let path = outputFolderPath + "/" + fileName + ".pdf"
|
|
|
+ var 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) {
|
|
@@ -533,12 +457,7 @@ extension KMBatchManager {
|
|
|
if filesData.count != 0 {
|
|
|
for i in 0..<filesData.count {
|
|
|
let item = filesData[i]
|
|
|
- var fileName = item.filePath.deletingPathExtension.lastPathComponent
|
|
|
- if ((fileName.isEmpty)) {
|
|
|
- fileName = NSLocalizedString("Untitled", comment: "")
|
|
|
- }
|
|
|
-
|
|
|
- let path = outputFolderPath + "/" + fileName + ".pdf"
|
|
|
+ var 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) {
|
|
@@ -590,12 +509,7 @@ extension KMBatchManager {
|
|
|
if filesData.count != 0 {
|
|
|
for i in 0..<filesData.count {
|
|
|
let item = filesData[i]
|
|
|
- var fileName = item.filePath.deletingPathExtension.lastPathComponent
|
|
|
- if ((fileName.isEmpty)) {
|
|
|
- fileName = NSLocalizedString("Untitled", comment: "")
|
|
|
- }
|
|
|
-
|
|
|
- let path = outputFolderPath + "/" + fileName + ".pdf"
|
|
|
+ var 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) {
|
|
@@ -644,12 +558,7 @@ extension KMBatchManager {
|
|
|
if filesData.count != 0 {
|
|
|
for i in 0..<filesData.count {
|
|
|
let item = filesData[i]
|
|
|
- var fileName = item.filePath.deletingPathExtension.lastPathComponent
|
|
|
- if ((fileName.isEmpty)) {
|
|
|
- fileName = NSLocalizedString("Untitled", comment: "")
|
|
|
- }
|
|
|
-
|
|
|
- let path = outputFolderPath + "/" + fileName + ".pdf"
|
|
|
+ var 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) {
|
|
@@ -697,12 +606,7 @@ extension KMBatchManager {
|
|
|
for i in 0..<filesData.count {
|
|
|
let item = filesData[i]
|
|
|
// DispatchQueue.global().async {
|
|
|
- var fileName = item.filePath.deletingPathExtension.lastPathComponent
|
|
|
- if ((fileName.isEmpty)) {
|
|
|
- fileName = NSLocalizedString("Untitled", comment: "")
|
|
|
- }
|
|
|
-
|
|
|
- let path = outputFolderPath + "/" + fileName + ".pdf"
|
|
|
+ var 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) {
|
|
@@ -769,8 +673,7 @@ extension KMBatchManager {
|
|
|
if data.isNewPDF {
|
|
|
if data.isMergeAll {
|
|
|
let item = filesData[0]
|
|
|
- var fileName = item.filePath.deletingPathExtension.lastPathComponent
|
|
|
- let path = outputFolderPath + "/" + fileName + ".pdf"
|
|
|
+ var path = self.fetchFilePath(type: .imageToPDF, filePath: item.filePath, outputFolderPath: outputFolderPath)
|
|
|
|
|
|
|
|
|
var pdfDocument = CPDFDocument()
|
|
@@ -792,7 +695,7 @@ extension KMBatchManager {
|
|
|
model.pageRange = pages
|
|
|
|
|
|
self.batchProgress()
|
|
|
- self.convertOCR(outputFolderPath: outputFolderPath, document: pdfDocument!, fileName: fileName, data: model) { progress in
|
|
|
+ self.convertOCR(outputFolderPath: outputFolderPath, document: pdfDocument!, fileName: path.deletingPathExtension.lastPathComponent, data: model) { progress in
|
|
|
self.batchProgress()
|
|
|
} complete: { document, text, error in
|
|
|
self.batchSuccess()
|
|
@@ -813,7 +716,7 @@ extension KMBatchManager {
|
|
|
}
|
|
|
} else {
|
|
|
var fileName = data.selectFilePath.deletingPathExtension.lastPathComponent
|
|
|
- let path = outputFolderPath + "/" + fileName + ".pdf"
|
|
|
+ let path = self.fetchFilePath(type: .imageToPDF, filePath: data.selectFilePath, outputFolderPath: outputFolderPath)
|
|
|
|
|
|
var pdfDocument = CPDFDocument(url: NSURL(fileURLWithPath: data.selectFilePath) as URL)
|
|
|
let count: Int = Int(pdfDocument?.pageCount ?? 0)
|
|
@@ -869,8 +772,7 @@ extension KMBatchManager {
|
|
|
let item = filesData[index]
|
|
|
|
|
|
if data.isOCR {
|
|
|
- let fileName = item.filePath.deletingPathExtension.lastPathComponent
|
|
|
- let path = outputFolderPath + "/" + fileName + ".pdf"
|
|
|
+ let path = self.fetchFilePath(type: .imageToPDF, filePath: item.filePath, outputFolderPath: outputFolderPath)
|
|
|
|
|
|
let pdfDocument = CPDFDocument()
|
|
|
pdfDocument?.km_insert(image: item.image, at: pdfDocument?.pageCount ?? 0)
|
|
@@ -887,15 +789,14 @@ extension KMBatchManager {
|
|
|
model.pageRange = pages
|
|
|
|
|
|
self.itemProgress(item: item, processValue: 0)
|
|
|
- self.convertOCR(outputFolderPath: outputFolderPath, document: pdfDocument!, fileName: fileName, data: model) { [unowned self] progress in
|
|
|
+ self.convertOCR(outputFolderPath: outputFolderPath, document: pdfDocument!, fileName: path.deletingPathExtension.lastPathComponent, data: model) { [unowned self] progress in
|
|
|
self.itemProgress(item: item, processValue: progress)
|
|
|
} complete: { [unowned self] document, text, error in
|
|
|
self.itemSuccess(item: filesData[index])
|
|
|
processFile(at: index + 1, outputFolderPath: outputFolderPath, data: data)
|
|
|
}
|
|
|
} else {
|
|
|
- let fileName = item.filePath.deletingPathExtension.lastPathComponent
|
|
|
- let path = outputFolderPath + "/" + fileName + ".pdf"
|
|
|
+ let path = self.fetchFilePath(type: .imageToPDF, filePath: item.filePath, outputFolderPath: outputFolderPath)
|
|
|
|
|
|
let pdfDocument = CPDFDocument()
|
|
|
pdfDocument?.km_insert(image: item.image, at: pdfDocument?.pageCount ?? 0)
|
|
@@ -983,6 +884,29 @@ extension KMBatchManager {
|
|
|
try?FileManager.default.removeItem(atPath: path)
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ func fetchFilePath(type: KMBatchCollectionViewType, filePath: String, outputFolderPath: String) -> String {
|
|
|
+ var fileName = filePath.deletingPathExtension.lastPathComponent
|
|
|
+ if fileName.isEmpty {
|
|
|
+ fileName = NSLocalizedString("Untitled", comment: "")
|
|
|
+ }
|
|
|
+
|
|
|
+ var path = outputFolderPath + "/" + fileName + ".pdf"
|
|
|
+
|
|
|
+// // 检查文件是否已存在,如果存在,则添加数字后缀
|
|
|
+// var finalPath = path
|
|
|
+// var count = 1
|
|
|
+// while FileManager.default.fileExists(atPath: finalPath) {
|
|
|
+// let newFileName = "\(fileName) \(count)"
|
|
|
+// finalPath = outputFolderPath + "/" + newFileName + ".pdf"
|
|
|
+// count += 1
|
|
|
+// }
|
|
|
+
|
|
|
+ // 使用最终路径进行保存或其他操作
|
|
|
+// path = finalPath
|
|
|
+
|
|
|
+ return path
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
//MARK: Alert
|