|
@@ -76,6 +76,9 @@ class KMBatchManager: NSObject {
|
|
|
case .batchRemove:
|
|
|
self.removeApplay(data: data, outputFolderPath: outputFolderPath)
|
|
|
break
|
|
|
+ case .imageToPDF:
|
|
|
+ self.imageToPDFExport(data: data, outputFolderPath: outputFolderPath)
|
|
|
+ break
|
|
|
default:
|
|
|
KMPrint("找不到")
|
|
|
break
|
|
@@ -87,8 +90,8 @@ class KMBatchManager: NSObject {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-protocol KMBatchSettingViewExport {}
|
|
|
-extension KMBatchManager: KMBatchSettingViewExport {
|
|
|
+//MARK: 批量
|
|
|
+extension KMBatchManager {
|
|
|
//MARK: 转档
|
|
|
func convertPDFExport(data: KMBatchSettingItemViewModel, outputFolderPath: String) {
|
|
|
self.convertFile(outputFolderPath: outputFolderPath, data: data, filesData: self.batchFilesData)
|
|
@@ -274,9 +277,9 @@ extension KMBatchManager: KMBatchSettingViewExport {
|
|
|
}
|
|
|
let path = outputFolderPath + "/" + fileName + ".pdf"
|
|
|
|
|
|
- KMOCRManager.manager.convertBatchOCR(document: document!, saveFilePath: path, model: data, progress: { [unowned self] progress in
|
|
|
+ self.convertOCR(outputFolderPath: outputFolderPath, document: document!, fileName: fileName, data: data) { [unowned self] progress in
|
|
|
self.itemProgress(item: item, processValue: progress)
|
|
|
- }) { [unowned self] document, text, error in
|
|
|
+ } complete: { [unowned self] document, text, error in
|
|
|
if error == nil {
|
|
|
self.itemSuccess(item: item)
|
|
|
} else {
|
|
@@ -291,6 +294,21 @@ extension KMBatchManager: KMBatchSettingViewExport {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ func convertOCR(outputFolderPath: String,
|
|
|
+ document: CPDFDocument,
|
|
|
+ fileName: String,
|
|
|
+ data: KMOCRModel,
|
|
|
+ progress: @escaping KMOCRManagerOCRProgress,
|
|
|
+ complete: @escaping KMOCRManagerOCRComplete) {
|
|
|
+ //计算需要处理的页面
|
|
|
+ let path = outputFolderPath + "/" + fileName + ".pdf"
|
|
|
+ KMOCRManager.manager.convertBatchOCR(document: document, saveFilePath: path, model: data, progress: { [unowned self] progressValue in
|
|
|
+ progress(progressValue)
|
|
|
+ }) { [unowned self] document, text, error in
|
|
|
+ complete(document, text, error)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
//MARK: 压缩
|
|
|
func compressExport(data: KMBatchSettingItemViewModel, outputFolderPath: String) {
|
|
|
self.compressFile(outputFolderPath: outputFolderPath, data: (data as? KMCompressSettingModel)!, filesData: self.batchFilesData)
|
|
@@ -548,6 +566,137 @@ extension KMBatchManager: KMBatchSettingViewExport {
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ //MARK: 图片转PDF
|
|
|
+ func imageToPDFExport(data: KMBatchSettingItemViewModel, outputFolderPath: String) {
|
|
|
+ self.imageToPDFFile(outputFolderPath: outputFolderPath, data: data as! KMBatchImageToPDFModel, filesData: self.batchFilesData)
|
|
|
+ }
|
|
|
+
|
|
|
+ func imageToPDFFile(outputFolderPath: String, data: KMBatchImageToPDFModel, filesData: [KMBatchProcessingTableViewModel]) {
|
|
|
+ if filesData.count != 0 {
|
|
|
+ self.batchProgress()
|
|
|
+ if data.isNewPDF {
|
|
|
+ if data.isMergeAll {
|
|
|
+ let item = filesData[0]
|
|
|
+ var fileName = item.filePath.deletingPathExtension.lastPathComponent
|
|
|
+ let path = outputFolderPath + "/" + fileName + ".pdf"
|
|
|
+
|
|
|
+
|
|
|
+ var pdfDocument = CPDFDocument()
|
|
|
+ for item in filesData {
|
|
|
+ pdfDocument?.km_insert(image: item.image, at: pdfDocument?.pageCount ?? 0)
|
|
|
+ }
|
|
|
+
|
|
|
+ if data.isOCR {
|
|
|
+ let model = KMOCRModel()
|
|
|
+ model.showType = .page
|
|
|
+ model.saveAsPDF = true
|
|
|
+ model.ocrType = data.ocrType
|
|
|
+ model.languageType = data.languageType
|
|
|
+ model.needTxT = data.isExtractText
|
|
|
+ model.pageRangeType = .all
|
|
|
+
|
|
|
+ //计算需要处理的页面
|
|
|
+ let pages:[Int] = KMOCRManager.fetchPageIndex(document: pdfDocument!, model: model)
|
|
|
+ model.pageRange = pages
|
|
|
+
|
|
|
+ self.batchProgress()
|
|
|
+ self.convertOCR(outputFolderPath: outputFolderPath, document: pdfDocument!, fileName: fileName, data: model) { progress in
|
|
|
+ self.batchProgress()
|
|
|
+ } complete: { document, text, error in
|
|
|
+ self.batchSuccess()
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ pdfDocument?.write(toFile: path)
|
|
|
+ }
|
|
|
+ self.batchSuccess()
|
|
|
+ } else {
|
|
|
+ for i in 0..<filesData.count {
|
|
|
+ let item = filesData[i]
|
|
|
+ if data.isOCR {
|
|
|
+ var fileName = item.filePath.deletingPathExtension.lastPathComponent
|
|
|
+ let path = outputFolderPath + "/" + fileName + ".pdf"
|
|
|
+
|
|
|
+
|
|
|
+ var pdfDocument = CPDFDocument()
|
|
|
+ for item in filesData {
|
|
|
+ pdfDocument?.km_insert(image: item.image, at: pdfDocument?.pageCount ?? 0)
|
|
|
+ }
|
|
|
+
|
|
|
+ let model = KMOCRModel()
|
|
|
+ model.showType = .page
|
|
|
+ model.saveAsPDF = true
|
|
|
+ model.ocrType = data.ocrType
|
|
|
+ model.languageType = data.languageType
|
|
|
+ model.needTxT = data.isExtractText
|
|
|
+ model.pageRangeType = .all
|
|
|
+
|
|
|
+ //计算需要处理的页面
|
|
|
+ let pages:[Int] = KMOCRManager.fetchPageIndex(document: pdfDocument!, model: model)
|
|
|
+ model.pageRange = pages
|
|
|
+
|
|
|
+ self.itemProgress(item: item, processValue: 0)
|
|
|
+ self.convertOCR(outputFolderPath: outputFolderPath, document: pdfDocument!, fileName: fileName, data: model) { [unowned self] progress in
|
|
|
+ self.itemProgress(item: item, processValue: progress)
|
|
|
+ } complete: { [unowned self] document, text, error in
|
|
|
+ self.itemSuccess(item: item)
|
|
|
+
|
|
|
+ if i == filesData.count - 1 {
|
|
|
+ self.batchSuccess()
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ var fileName = item.filePath.deletingPathExtension.lastPathComponent
|
|
|
+ let path = outputFolderPath + "/" + fileName + ".pdf"
|
|
|
+ var pdfDocument = CPDFDocument()
|
|
|
+ pdfDocument?.km_insert(image: item.image, at: pdfDocument?.pageCount ?? 0)
|
|
|
+ let success = pdfDocument?.write(toFile: path)
|
|
|
+ if success != nil {
|
|
|
+ self.batchSuccess()
|
|
|
+ } else {
|
|
|
+ self.batchFailure()
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ var fileName = data.selectFilePath.deletingPathExtension.lastPathComponent
|
|
|
+ let path = outputFolderPath + "/" + fileName + ".pdf"
|
|
|
+
|
|
|
+ var pdfDocument = CPDFDocument(url: NSURL(fileURLWithPath: data.selectFilePath) as URL)
|
|
|
+ let count: Int = Int(pdfDocument?.pageCount ?? 0)
|
|
|
+ for item in filesData {
|
|
|
+ pdfDocument?.km_insert(image: item.image, at: pdfDocument?.pageCount ?? 0)
|
|
|
+ }
|
|
|
+
|
|
|
+ if data.isOCR {
|
|
|
+ let model = KMOCRModel()
|
|
|
+ model.showType = .page
|
|
|
+ model.saveAsPDF = true
|
|
|
+ model.ocrType = data.ocrType
|
|
|
+ model.languageType = data.languageType
|
|
|
+ model.needTxT = data.isExtractText
|
|
|
+ model.pageRangeType = .all
|
|
|
+ //计算需要处理的页面
|
|
|
+ let pages:[Int] = KMOCRManager.fetchPageIndex(document: pdfDocument!, model: model)
|
|
|
+ model.pageRange = pages
|
|
|
+
|
|
|
+ self.convertOCR(outputFolderPath: outputFolderPath, document: pdfDocument!, fileName: fileName, data: model) { progress in
|
|
|
+
|
|
|
+ } complete: { document, text, error in
|
|
|
+ if (error != nil) {
|
|
|
+ self.batchFailure()
|
|
|
+ } else {
|
|
|
+ self.batchSuccess()
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ pdfDocument?.write(toFile: path)
|
|
|
+ }
|
|
|
+ self.batchSuccess()
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
//MARK: private
|
|
@@ -563,7 +712,7 @@ extension KMBatchManager {
|
|
|
data.pageRangeType = model.pageRange
|
|
|
data.pageRangeString = model.pageRangeString
|
|
|
|
|
|
- let pages:[Int] = KMOCRManager.manager.fetchPageIndex(document: document!, model: data)
|
|
|
+ let pages:[Int] = KMOCRManager.fetchPageIndex(document: document!, model: data)
|
|
|
|
|
|
var tempDocument = CPDFDocument()
|
|
|
for i in 0..<pages.count {
|