|
@@ -414,13 +414,58 @@ extension KMBatchManager {
|
|
|
|
|
|
//MARK: 水印
|
|
|
func waterMarkApplay(data: KMBatchSettingItemViewModel, outputFolderPath: String) {
|
|
|
-
|
|
|
+ if let data = data as? KMBatchWatermarkModel {
|
|
|
+ self.waterMarkFile(outputFolderPath: outputFolderPath, data: data, filesData: self.batchFilesData)
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- func waterMarkFile(outputFolderPath: String, data: KMBatchSettingItemViewModel, filesData: [KMBatchProcessingTableViewModel]?) {
|
|
|
-
|
|
|
-
|
|
|
+ func waterMarkFile(outputFolderPath: String, data: KMBatchWatermarkModel, filesData: [KMBatchProcessingTableViewModel]) {
|
|
|
+ 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"
|
|
|
+ let document = CPDFDocument.init(url: URL(fileURLWithPath: item.filePath))
|
|
|
+
|
|
|
+ if (document?.allowsPrinting == false || document?.allowsCopying == false) {
|
|
|
+ let alert = NSAlert()
|
|
|
+ alert.alertStyle = .critical
|
|
|
+ alert.messageText = "此文档不允许修改"
|
|
|
+ alert.runModal()
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ if let watermarks = document?.watermarks(), let model = data.watermarkModel, let document = document {
|
|
|
+ let pageString = self.fetchValidPageIndexString(document, model: item)
|
|
|
+ let watermark = KMPDFWatermarkData.returnWaterMarkWith(model, document)
|
|
|
+ watermark.pageString = pageString
|
|
|
+ document.addWatermark(watermark)
|
|
|
+ }
|
|
|
+
|
|
|
+ if (FileManager.default.fileExists(atPath: path)) {
|
|
|
+ try?FileManager.default.removeItem(atPath: path)
|
|
|
+ }
|
|
|
+
|
|
|
+ let result = document?.write(to: URL(fileURLWithPath: path)) ?? false
|
|
|
+ if (result) {
|
|
|
+ KMPrint("removeFile成功")
|
|
|
+ self.itemSuccess(item: item)
|
|
|
+ } else {
|
|
|
+ KMPrint("removeFile失败")
|
|
|
+ self.itemFailure(item: item, error: nil)
|
|
|
+ }
|
|
|
+ if i == filesData.count - 1 {
|
|
|
+ self.batchSuccess()
|
|
|
+ }
|
|
|
+ NSWorkspace.shared.activateFileViewerSelecting([URL(fileURLWithPath: path)])
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
+
|
|
|
//MARK: 背景
|
|
|
func backgroundApplay(data: KMBatchSettingItemViewModel, outputFolderPath: String) {
|
|
|
if let data = data as? KMBatchBackgroundModel {
|
|
@@ -530,17 +575,60 @@ extension KMBatchManager {
|
|
|
NSWorkspace.shared.activateFileViewerSelecting([URL(fileURLWithPath: path)])
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
-
|
|
|
}
|
|
|
|
|
|
//MARK: 贝茨码
|
|
|
func batesApplay(data: KMBatchSettingItemViewModel, outputFolderPath: String) {
|
|
|
-
|
|
|
+ if let data = data as? KMBatchBatesModel {
|
|
|
+ self.batesFile(outputFolderPath: outputFolderPath, data: data, filesData: self.batchFilesData)
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- func batesFile(outputFolderPath: String, data: KMBatchSettingItemViewModel, filesData: [KMBatchProcessingTableViewModel]?) {
|
|
|
-
|
|
|
+ func batesFile(outputFolderPath: String, data: KMBatchBatesModel, filesData: [KMBatchProcessingTableViewModel]) {
|
|
|
+ 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"
|
|
|
+ let document = CPDFDocument.init(url: URL(fileURLWithPath: item.filePath))
|
|
|
+
|
|
|
+ if (document?.allowsPrinting == false || document?.allowsCopying == false) {
|
|
|
+ let alert = NSAlert()
|
|
|
+ alert.alertStyle = .critical
|
|
|
+ alert.messageText = "此文档不允许修改"
|
|
|
+ alert.runModal()
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ if let bates = document?.bates(), let model = data.batesModel, let document = document {
|
|
|
+ let pageString = self.fetchValidPageIndexString(document, model: item)
|
|
|
+ KMBatesManager.defaultManager.updateCPDFBates(bates, withModel: model, Int(document.pageCount))
|
|
|
+ bates.pageString = pageString
|
|
|
+ bates.update()
|
|
|
+ }
|
|
|
+
|
|
|
+ if (FileManager.default.fileExists(atPath: path)) {
|
|
|
+ try?FileManager.default.removeItem(atPath: path)
|
|
|
+ }
|
|
|
+
|
|
|
+ let result = document?.write(to: URL(fileURLWithPath: path)) ?? false
|
|
|
+ if (result) {
|
|
|
+ KMPrint("removeFile成功")
|
|
|
+ self.itemSuccess(item: item)
|
|
|
+ } else {
|
|
|
+ KMPrint("removeFile失败")
|
|
|
+ self.itemFailure(item: item, error: nil)
|
|
|
+ }
|
|
|
+ if i == filesData.count - 1 {
|
|
|
+ self.batchSuccess()
|
|
|
+ }
|
|
|
+ NSWorkspace.shared.activateFileViewerSelecting([URL(fileURLWithPath: path)])
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
//MARK: 移除
|