Ver código fonte

【2025】【综合】批量背景&页眉页脚处理

niehaoyu 1 mês atrás
pai
commit
24ca82aec8

+ 4 - 4
PDF Office/PDF Master/Class/PDFTools/SelfSign/Window/KMProfileInfoWindowController.xib

@@ -128,7 +128,7 @@ DQ
                         </constraints>
                     </customView>
                     <textField focusRingType="none" horizontalHuggingPriority="251" verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="HcI-gL-dgj">
-                        <rect key="frame" x="38" y="392" width="37" height="18"/>
+                        <rect key="frame" x="38" y="390" width="37" height="18"/>
                         <textFieldCell key="cell" lineBreakMode="clipping" title="Label" id="YeK-uM-b3g">
                             <font key="font" size="13" name="PingFangSC-Regular"/>
                             <color key="textColor" name="labelColor" catalog="System" colorSpace="catalog"/>
@@ -136,13 +136,13 @@ DQ
                         </textFieldCell>
                     </textField>
                     <scrollView autohidesScrollers="YES" horizontalLineScroll="17" horizontalPageScroll="10" verticalLineScroll="17" verticalPageScroll="10" usesPredominantAxisScrolling="NO" translatesAutoresizingMaskIntoConstraints="NO" id="P2P-jH-JNm">
-                        <rect key="frame" x="40" y="74" width="584" height="317"/>
+                        <rect key="frame" x="40" y="74" width="584" height="315"/>
                         <clipView key="contentView" id="Txz-Kp-TvA">
-                            <rect key="frame" x="1" y="1" width="582" height="315"/>
+                            <rect key="frame" x="1" y="1" width="582" height="313"/>
                             <autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
                             <subviews>
                                 <tableView verticalHuggingPriority="750" allowsExpansionToolTips="YES" columnAutoresizingStyle="lastColumnOnly" columnSelection="YES" multipleSelection="NO" autosaveColumns="NO" rowSizeStyle="automatic" viewBased="YES" id="ldR-2V-TXt">
-                                    <rect key="frame" x="0.0" y="0.0" width="582" height="315"/>
+                                    <rect key="frame" x="0.0" y="0.0" width="582" height="313"/>
                                     <autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
                                     <color key="backgroundColor" name="controlBackgroundColor" catalog="System" colorSpace="catalog"/>
                                     <color key="gridColor" name="gridColor" catalog="System" colorSpace="catalog"/>

+ 7 - 3
PDF Office/PDF Master/KMClass/KMPDFViewController/KMMainViewController.swift

@@ -2749,8 +2749,7 @@ struct KMNMWCFlags {
     func showFileInfo() {
         KMInfoWindowController.shared.showWindow(nil)
     }
-    
-    
+     
     //MARK: - Share Action
     @objc private func shareDocument(sender: NSView) {
         let document = self.listView.document ?? CPDFDocument()
@@ -3062,7 +3061,7 @@ struct KMNMWCFlags {
     
     //MARK: - 属性
     @IBAction func menuItemAction_property(_ sender: Any?) {
-        KMInfoWindowController.shared.showWindow(sender)
+        showFileInfo()
     }
     
     //MARK: - 打印
@@ -4530,6 +4529,7 @@ extension KMMainViewController: KMEditToolbarViewDelegate {
                     listView.document?.refreshPageData()
                     listView.layoutDocumentView()
                 }
+                
             }
             
             exitEditToolbarView()
@@ -7510,6 +7510,10 @@ extension KMMainViewController {
             NSColorPanel.shared.close()
         }
         
+        if KMInfoWindowController.shared.window?.isVisible == true {
+            KMInfoWindowController.shared.close()
+        }
+        
     }
     
     public func clearSecureOptions() {

+ 74 - 8
PDF Office/PDF Master/KMClass/NewBatch/Manager/KMBatchManager.swift

@@ -62,16 +62,16 @@ class KMBatchManager: NSObject {
                 self.securityExport(data: data, outputFolderPath: outputFolderPath)
                 break
             case .watermark:
-                
+                self.waterMarkApplay(data: data, outputFolderPath: outputFolderPath)
                 break
             case .background:
                 self.backgroundApplay(data: data, outputFolderPath: outputFolderPath)
                 break
             case .headerAndFooter:
-                
+                self.headAndFooterApplay(data: data, outputFolderPath: outputFolderPath)
                 break
             case .batesNumber:
-                
+                self.batesApplay(data: data, outputFolderPath: outputFolderPath)
                 break
             case .batchRemove:
                 self.removeApplay(data: data, outputFolderPath: outputFolderPath)
@@ -448,11 +448,11 @@ extension KMBatchManager {
                     return
                 }
                 
-                if let background = document?.background(), let model = data.backgroundModel {
+                if let background = document?.background(), let model = data.backgroundModel, let document = document {
                     KMBackgroundManager.defaultManager.updateBackground(background, withModel: model)
                     
-                    var pages = Array(0..<Int(document!.pageCount))
-                    let pageIndexString = pages.isEmpty ? "" : pages.map { "\($0)" }.joined(separator: ",")
+                    let pageIndexString = self.fetchValidPageIndexString(document, model: item)
+                    
                     background.pageString = pageIndexString
                     background.update()
                     
@@ -480,11 +480,58 @@ extension KMBatchManager {
     
     //MARK: 页眉页脚
     func headAndFooterApplay(data: KMBatchSettingItemViewModel, outputFolderPath: String) {
-        
+        if let data = data as? KMBatchHeaderAndFooterModel {
+            self.headAndFooterFile(outputFolderPath: outputFolderPath, data: data, filesData: self.batchFilesData)
+        }
     }
     
-    func headAndFooterFile(outputFolderPath: String, data: KMBatchSettingItemViewModel, filesData: [KMBatchProcessingTableViewModel]?) {
+    func headAndFooterFile(outputFolderPath: String, data: KMBatchHeaderAndFooterModel, 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 headerFooter = document?.headerFooter(), let model = data.headerFooterModel, let document = document {
+                     let pageString = self.fetchValidPageIndexString(document, model: item)
+                     KMHeaderFooterManager.defaultManager.updateCPDFHeaderFooter(headerFooter, withModel: model, Int(document.pageCount))
+                        headerFooter.pageString = pageString
+                        headerFooter.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: 贝茨码
@@ -701,6 +748,25 @@ extension KMBatchManager {
 
 //MARK: private
 extension KMBatchManager {
+    func fetchValidPageIndexString(_ document: CPDFDocument, model: KMBatchProcessingTableViewModel) -> String? {
+        if model.pageRange == .all {
+            let pages = Array(0..<Int(document.pageCount))
+            let pageIndexString = pages.isEmpty ? "" : pages.map { "\($0)" }.joined(separator: ",")
+            
+            return pageIndexString
+        } else {
+            let data = KMOCRModel()
+            data.pageRangeType = model.pageRange
+            data.pageRangeString = model.pageRangeString
+            
+            let pages:[Int] = KMOCRManager.fetchPageIndex(document: document, model: data)
+            let pageIndexString = pages.isEmpty ? "" : pages.map { "\($0)" }.joined(separator: ",")
+            
+            return pageIndexString
+        }
+        return nil
+    }
+    
     func fetchDocument(filePath: String, model: KMBatchProcessingTableViewModel) -> CPDFDocument {
         var document = CPDFDocument(url: URL(fileURLWithPath: filePath))
         

+ 10 - 0
PDF Office/PDF Master/KMClass/NewBatch/View/Setting/Header&Footer/KMBatchHeaderAndFooterView.swift

@@ -8,6 +8,10 @@
 import Cocoa
 import KMComponentLibrary
 
+class KMBatchHeaderAndFooterModel: KMBatchSettingItemViewModel {
+    var headerFooterModel: KMHeaderFooterObject?
+}
+
 class KMBatchHeaderAndFooterView: KMBatchSettingItemView {
     @IBOutlet weak var itemContentView: NSView!
     
@@ -38,6 +42,8 @@ class KMBatchHeaderAndFooterView: KMBatchSettingItemView {
             resetUI()
         }
     }
+     
+    var data: KMBatchHeaderAndFooterModel = KMBatchHeaderAndFooterModel()
     
     //MARK: - func
     override func setup() {
@@ -177,7 +183,11 @@ class KMBatchHeaderAndFooterView: KMBatchSettingItemView {
     
     //MARK: - Action
     @objc func applyButtonAction(_ sender: NSButton) {
+        self.data.headerFooterModel = self.headerFooterModel
         
+        if self.batchExport != nil {
+            self.batchExport!(self, self.data)
+        }
     }
      
     @objc func saveButtonClicked(_ sender: NSView) {