浏览代码

【2025】【Tools】串接背景功能

niehaoyu 1 月之前
父节点
当前提交
19541205ba

+ 1 - 1
PDF Office/PDF Master/Class/PDFTools/Compress/Controller/KMCompressWindowController.xib

@@ -18,7 +18,7 @@
             <windowStyleMask key="styleMask" titled="YES" closable="YES" miniaturizable="YES" resizable="YES"/>
             <windowPositionMask key="initialPositionMask" leftStrut="YES" rightStrut="YES" topStrut="YES" bottomStrut="YES"/>
             <rect key="contentRect" x="196" y="240" width="480" height="415"/>
-            <rect key="screenRect" x="0.0" y="0.0" width="2560" height="1415"/>
+            <rect key="screenRect" x="0.0" y="0.0" width="1920" height="1055"/>
             <view key="contentView" id="se5-gp-TjO">
                 <rect key="frame" x="0.0" y="0.0" width="480" height="415"/>
                 <autoresizingMask key="autoresizingMask"/>

+ 72 - 22
PDF Office/PDF Master/KMClass/NewBatch/Manager/KMBatchManager.swift

@@ -65,7 +65,7 @@ class KMBatchManager: NSObject {
                 
                 break
             case .background:
-                
+                self.backgroundApplay(data: data, outputFolderPath: outputFolderPath)
                 break
             case .headerAndFooter:
                 
@@ -353,13 +353,13 @@ extension KMBatchManager: KMBatchSettingViewExport {
                         !data.openPasswordString.isEmpty {
                         options.updateValue(data.openPasswordString, forKey: .userPasswordOption)
                     }
-//
+                    //
                     //权限密码
                     if data.isPermission &&
                         !data.permissionString.isEmpty {
                         options.updateValue(data.permissionString, forKey: .ownerPasswordOption)
                     }
-                        
+                    
                     // 限制打印
                     if data.restrictOptions.contains(.print) {
                         options.updateValue(false, forKey: .allowsPrintingOption)
@@ -376,7 +376,7 @@ extension KMBatchManager: KMBatchSettingViewExport {
                     
                     
                     let result = docuemt!.write(toFile: path, withOptions: options)
-//                    let result = docuemt!.write(to: URL(fileURLWithPath: path), withOptions: options)
+                    //                    let result = docuemt!.write(to: URL(fileURLWithPath: path), withOptions: options)
                     if result {
                         KMPrint("成功")
                         self.itemSuccess(item: item)
@@ -394,41 +394,91 @@ extension KMBatchManager: KMBatchSettingViewExport {
         }
     }
     
-    
-    func waterMarkApplay(data: KMBatchSettingItemViewModel) {
-
-    }
-    
-//    func waterMarkFile(data: KMWatermarkAdjectiveBaseModel, filesData: [KMBatchProcessingTableViewModel]?) {
-//
-//    }
-    
-    func backgroundApplay(data: KMBatchSettingItemViewModel) {
+    //MARK: 水印
+    func waterMarkApplay(data: KMBatchSettingItemViewModel, outputFolderPath: String) {
         
     }
     
-    func backgroundFile(outputFolderPath: String, data: KMBackgroundModel, filesData: [KMBatchProcessingTableViewModel]?) {
+    func waterMarkFile(outputFolderPath: String, data: KMBatchSettingItemViewModel, filesData: [KMBatchProcessingTableViewModel]?) {
+        
         
+    }
+    //MARK: 背景
+    func backgroundApplay(data: KMBatchSettingItemViewModel, outputFolderPath: String) {
+        if let data = data as? KMBatchBackgroundModel {
+            self.backgroundFile(outputFolderPath: outputFolderPath, data: data, filesData: self.batchFilesData)
+        }
     }
     
+    func backgroundFile(outputFolderPath: String, data: KMBatchBackgroundModel, 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 background = document?.background(), let model = data.backgroundModel {
+                    KMBackgroundManager.defaultManager.updateBackground(background, withModel: model)
+                    
+                    var pages = Array(0..<Int(document!.pageCount))
+                    let pageIndexString = pages.isEmpty ? "" : pages.map { "\($0)" }.joined(separator: ",")
+                    background.pageString = pageIndexString
+                    background.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)])
+            }
+        }
+    }
     
-    func headAndFooterApplay(data: KMBatchSettingItemViewModel) {
+    //MARK: 页眉页脚
+    func headAndFooterApplay(data: KMBatchSettingItemViewModel, outputFolderPath: String) {
         
     }
     
-//    func headAndFooterFile(outputFolderPath: String, data: KMHeaderFooterModel, filesData: [KMBatchProcessingTableViewModel]?) {
-//
-//    }
+    func headAndFooterFile(outputFolderPath: String, data: KMBatchSettingItemViewModel, filesData: [KMBatchProcessingTableViewModel]?) {
+
+    }
     
-    func batesApplay(data: KMBatchSettingItemViewModel) {
+    //MARK: 贝茨码
+    func batesApplay(data: KMBatchSettingItemViewModel, outputFolderPath: String) {
         
     }
     
-    func batesFile(outputFolderPath: String, data: KMBatesModel, filesData: [KMBatchProcessingTableViewModel]?) {
+    func batesFile(outputFolderPath: String, data: KMBatchSettingItemViewModel, filesData: [KMBatchProcessingTableViewModel]?) {
         
     }
     
-    
+    //MARK: 移除
     func removeApplay(data: KMBatchSettingItemViewModel, outputFolderPath: String) {
         self.removeFile(outputFolderPath: outputFolderPath, data: data as! KMBatchRemoveViewModel, filesData: self.batchFilesData)
     }

+ 14 - 5
PDF Office/PDF Master/KMClass/NewBatch/View/Setting/Background/KMBatchBackgroundView.swift

@@ -8,6 +8,10 @@
 import Cocoa
 import KMComponentLibrary
 
+class KMBatchBackgroundModel: KMBatchSettingItemViewModel {
+    var backgroundModel: KMBackgroundModel?
+}
+
 class KMBatchBackgroundView: KMBatchSettingItemView {
     @IBOutlet var saveTemplateButton: ComponentButton!
     @IBOutlet var applyButton: ComponentButton!
@@ -15,12 +19,13 @@ class KMBatchBackgroundView: KMBatchSettingItemView {
     
     @IBOutlet weak var itemContentView: NSView!
     
-    var propertyController: KMBackgroundPropertyController = KMBackgroundPropertyController.init()
-    var templatesController: KMBGTemplateController = KMBGTemplateController()
+    private var propertyController: KMBackgroundPropertyController = KMBackgroundPropertyController.init()
+    private var templatesController: KMBGTemplateController = KMBGTemplateController()
+    private var saveTemplateMessage: ComponentMessage = ComponentMessage()
     
-    var saveTemplateMessage: ComponentMessage = ComponentMessage()
+    private var backgroundModel: KMBackgroundModel?
     
-    var backgroundModel: KMBackgroundModel?
+    var data: KMBatchBackgroundModel = KMBatchBackgroundModel()
     
     var editSubType: KMPDFEditSubModeType = .none {
         didSet {
@@ -99,7 +104,7 @@ class KMBatchBackgroundView: KMBatchSettingItemView {
                                                                 onlyIcon: true,
                                                                 icon: NSImage(named: "icon_wm_template_save"),
                                                                 keepPressState: false)
-        saveTemplateButton.setTarget(self, action: #selector(applyButtonAction(_:)))
+        saveTemplateButton.setTarget(self, action: #selector(saveTemplateAction(_:)))
         saveTemplateButton.reloadData()
         
         applyButton.properties = ComponentButtonProperty(type: .primary,
@@ -172,7 +177,11 @@ class KMBatchBackgroundView: KMBatchSettingItemView {
     }
     
     @objc func applyButtonAction(_ sender: NSView) {
+        self.data.backgroundModel = self.backgroundModel
         
+        if self.batchExport != nil {
+            self.batchExport!(self, self.data)
+        }
     }
     
     //MARK: - mouseEvent