Browse Source

[批量] - 页眉页脚&bates码

liujiajie 1 year ago
parent
commit
dac42c399b

+ 6 - 0
PDF Office/PDF Master/Class/Batch/Tools/KMBatchRemoveHeaderFooterOperation.swift

@@ -110,6 +110,9 @@ class KMBatchRemoveHeaderFooterOperation: KMBatchOperation{
             document?.unlock(withPassword: password)
         }
         
+        var tBates = document?.bates()
+        tBates?.clear()
+        
         let documentPath = NSTemporaryDirectory()
         let tempPath = documentPath.stringByAppendingPathComponent(path.lastPathComponent)
         if FileManager.default.fileExists(atPath: tempPath) {
@@ -138,6 +141,9 @@ class KMBatchRemoveHeaderFooterOperation: KMBatchOperation{
             document?.unlock(withPassword: password)
         }
         
+        var tHeaderFooter = document?.headerFooter()
+        tHeaderFooter?.clear()
+        
         let documentPath = NSTemporaryDirectory()
         let tempPath = documentPath.stringByAppendingPathComponent(path.lastPathComponent)
         if FileManager.default.fileExists(atPath: tempPath) {

+ 1 - 1
PDF Office/PDF Master/Class/Batch/WindowController/KMBatchOperateLeftViewController.xib

@@ -105,7 +105,7 @@
                                         <tableColumnResizingMask key="resizingMask" resizeWithTable="YES" userResizable="YES"/>
                                         <prototypeCellViews>
                                             <tableCellView id="Mgh-hp-ErW">
-                                                <rect key="frame" x="81" y="1" width="100" height="17"/>
+                                                <rect key="frame" x="81" y="1" width="101" height="17"/>
                                                 <autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
                                             </tableCellView>
                                         </prototypeCellViews>

+ 3 - 3
PDF Office/PDF Master/Class/PDFTools/AddHeaderFooter/Model/KMBatchAddHeaderFooterOperation.swift

@@ -26,7 +26,7 @@ class KMBatchAddHeaderFooterOperation: KMBatchOperation{
         }
     }
     func currentParameter() -> KMBatchBaseParameter {
-        if ((headerFooter?.isBates) != nil) {
+        if headerFooter!.isBates{
             return operateFile!.addBatesInfo
         }
         return operateFile!.addHeaderFooterInfo
@@ -109,7 +109,7 @@ class KMBatchAddHeaderFooterOperation: KMBatchOperation{
             return
         }
         
-        if let password = password {
+        if password?.count ?? 0 > 0 {
             document.unlock(withPassword: password)
         }
         
@@ -122,7 +122,7 @@ class KMBatchAddHeaderFooterOperation: KMBatchOperation{
         dictionary[.font] = font
         let size = "text".boundingRect(with: CGSize(width: CGFloat(MAXFLOAT), height: CGFloat(MAXFLOAT)), options: [.usesLineFragmentOrigin, .usesFontLeading], attributes: dictionary).size
         
-        if ((self.headerFooter?.isBates) != nil) {
+        if self.headerFooter!.isBates {
             var bates: CPDFHeaderFooter = document.bates()
             bates.margin = NSEdgeInsets(top: max(CGFloat(self.headerFooter!.topMargin) - size.height, 0), left: CGFloat(self.headerFooter!.leftMargin), bottom: max(CGFloat(self.headerFooter!.bottomMargin) - size.height, 0), right: CGFloat(self.headerFooter!.rightMargin))
             

+ 7 - 1
PDF Office/PDF Master/Class/PDFTools/AddHeaderFooter/New/KMHeaderFooterWindowController.swift

@@ -12,6 +12,7 @@ typealias KMHeaderFooterWindowControllerOperateCallBack = (_ controller: NSWindo
 class KMHeaderFooterWindowController: KMBaseWindowController {
     @IBOutlet weak var headerFooterView: KMHeaderFooterView!
 
+    var headerFooterObj: KMHeaderFooterModel = KMHeaderFooterModel()
     var type: KMBatchModifyTemplateType = .Use
     var operateCallBack: KMHeaderFooterWindowControllerOperateCallBack?
     
@@ -19,15 +20,20 @@ class KMHeaderFooterWindowController: KMBaseWindowController {
         super.windowDidLoad()
 
         // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
+        if type == .Edit {
+            headerFooterView.headerFooterObj = headerFooterObj
+            headerFooterView.originalHeaderFooterObj = headerFooterObj
+        }
         headerFooterView.isHiddenBatchBtn = isBatch
         headerFooterView.isBates = isBates
-        headerFooterView.pdfDocument = pdfDocument
         headerFooterView.type = type
+        headerFooterView.pdfDocument = pdfDocument
         headerFooterView.cancelAction = { [unowned self] view in
             cancelAction?(self)
         }
         
         headerFooterView.operateCallBack = { [unowned self] model in
+            cancelAction?(self)
             operateCallBack?(self, model)
         }
     }

+ 1 - 1
PDF Office/PDF Master/Class/PDFTools/AddHeaderFooter/New/View/KMHeaderFooterView.swift

@@ -328,7 +328,7 @@ class KMHeaderFooterView: KMBaseXibView, NSTextViewDelegate, NSComboBoxDelegate
         } else {
             self.fontSizeCombobox.stringValue = string
         }
-
+        self.textColorWell.color = self.headerFooterObj.getTextColor()
         self.dateFormateCombobox.removeAllItems()
         self.dateFormateCombobox.addItems(withObjectValues: self.dateFormatArray)
         self.dateFormateCombobox.selectItem(withObjectValue: self.headerFooterObj.dateFormatString)

+ 6 - 0
PDF Office/PDF Master/Class/PDFTools/AddHeaderFooter/New/View/KMHeaderFooterView.xib

@@ -635,6 +635,7 @@ DQ
                                         <color key="backgroundColor" name="textBackgroundColor" catalog="System" colorSpace="catalog"/>
                                         <size key="minSize" width="173" height="38"/>
                                         <size key="maxSize" width="296" height="10000000"/>
+                                        <color key="insertionPointColor" name="textInsertionPointColor" catalog="System" colorSpace="catalog"/>
                                     </textView>
                                 </subviews>
                             </clipView>
@@ -663,6 +664,7 @@ DQ
                                         <color key="backgroundColor" name="textBackgroundColor" catalog="System" colorSpace="catalog"/>
                                         <size key="minSize" width="172" height="38"/>
                                         <size key="maxSize" width="295" height="10000000"/>
+                                        <color key="insertionPointColor" name="textInsertionPointColor" catalog="System" colorSpace="catalog"/>
                                     </textView>
                                 </subviews>
                             </clipView>
@@ -688,6 +690,7 @@ DQ
                                         <color key="backgroundColor" name="textBackgroundColor" catalog="System" colorSpace="catalog"/>
                                         <size key="minSize" width="173" height="38"/>
                                         <size key="maxSize" width="311" height="10000000"/>
+                                        <color key="insertionPointColor" name="textInsertionPointColor" catalog="System" colorSpace="catalog"/>
                                     </textView>
                                 </subviews>
                             </clipView>
@@ -788,6 +791,7 @@ DQ
                                         <color key="backgroundColor" name="textBackgroundColor" catalog="System" colorSpace="catalog"/>
                                         <size key="minSize" width="173" height="38"/>
                                         <size key="maxSize" width="311" height="10000000"/>
+                                        <color key="insertionPointColor" name="textInsertionPointColor" catalog="System" colorSpace="catalog"/>
                                     </textView>
                                 </subviews>
                             </clipView>
@@ -813,6 +817,7 @@ DQ
                                         <color key="backgroundColor" name="textBackgroundColor" catalog="System" colorSpace="catalog"/>
                                         <size key="minSize" width="172" height="38"/>
                                         <size key="maxSize" width="310" height="10000000"/>
+                                        <color key="insertionPointColor" name="textInsertionPointColor" catalog="System" colorSpace="catalog"/>
                                     </textView>
                                 </subviews>
                             </clipView>
@@ -838,6 +843,7 @@ DQ
                                         <color key="backgroundColor" name="textBackgroundColor" catalog="System" colorSpace="catalog"/>
                                         <size key="minSize" width="173" height="38"/>
                                         <size key="maxSize" width="311" height="10000000"/>
+                                        <color key="insertionPointColor" name="textInsertionPointColor" catalog="System" colorSpace="catalog"/>
                                     </textView>
                                 </subviews>
                             </clipView>

+ 145 - 20
PDF Office/PDF Master/Class/PDFTools/AddHeaderFooter/VC/KMBatchOperateAddHeaderFooterViewController.swift

@@ -214,7 +214,8 @@ class KMBatchOperateAddHeaderFooterViewController: KMBatchOperateBaseViewControl
         }
     }
     @objc func batchFilesCountNotification(notification: Notification)  {
-        let files = notification.object as? [Any]
+        let files: Array? = notification.object as? [KMBatchOperateFile]
+        self.files? = files ?? []
         if files?.count ?? 0 > 0 {
             haveFiles = true
         } else {
@@ -242,11 +243,11 @@ class KMBatchOperateAddHeaderFooterViewController: KMBatchOperateBaseViewControl
     }
     func deleteAll() {
         if self.isBates {
-            for waterMark in KMHeaderFooterManager.defaultManager.onlyBatesObjects {
+            for waterMark in KMHeaderFooterManager.defaultManager.onlyBatesObjects() {
                 KMHeaderFooterManager.defaultManager.removeHeaderFooter(waterMark)
             }
         } else {
-            for waterMark in KMHeaderFooterManager.defaultManager.onlyHeaderFooterObjects {
+            for waterMark in KMHeaderFooterManager.defaultManager.onlyHeaderFooterObjects() {
                 KMHeaderFooterManager.defaultManager.removeHeaderFooter(waterMark)
             }
         }
@@ -259,13 +260,13 @@ class KMBatchOperateAddHeaderFooterViewController: KMBatchOperateBaseViewControl
         var count = 0
         var array: [KMHeaderFooterModel] = []
         if self.isBates {
-            count = KMHeaderFooterManager.defaultManager.onlyBatesObjects.count
-            array = KMHeaderFooterManager.defaultManager.onlyBatesObjects
+            count = KMHeaderFooterManager.defaultManager.onlyBatesObjects().count
+            array = KMHeaderFooterManager.defaultManager.onlyBatesObjects()
         } else {
-            count = KMHeaderFooterManager.defaultManager.onlyHeaderFooterObjects.count
-            array = KMHeaderFooterManager.defaultManager.onlyHeaderFooterObjects
+            count = KMHeaderFooterManager.defaultManager.onlyHeaderFooterObjects().count
+            array = KMHeaderFooterManager.defaultManager.onlyHeaderFooterObjects()
         }
-        if array.contains(self.currentObject!) {
+        if array.contains(self.currentObject ?? KMHeaderFooterModel()) {
             let row = array.firstIndex(of: self.currentObject!)!
             let indexSet = IndexSet(integer: row)
             self.tableView.selectRowIndexes(indexSet, byExtendingSelection: false)
@@ -323,7 +324,102 @@ class KMBatchOperateAddHeaderFooterViewController: KMBatchOperateBaseViewControl
         
     }
     
-    @IBAction func buttonClicked_Action(_ sender: Any) {
+    @IBAction func buttonClicked_Action(_ sender: NSButton) {
+        if (!self.onlyManagerTemplate) {
+            if (!self.haveFiles) {
+                return
+            }
+        }
+        if (self.onlyManagerTemplate) {
+            if (sender.tag == 1) {
+                var headFooter: KMHeaderFooterModel?
+                let row = self.tableView.selectedRow
+                if (row == -1) {
+                    NSSound.beep()
+                    return
+                } else {
+                    if (self.isBates) {
+                        headFooter = KMHeaderFooterManager.defaultManager.onlyBatesObjects()[row]
+                    } else {
+                        headFooter = KMHeaderFooterManager.defaultManager.onlyHeaderFooterObjects()[row]
+                    }
+                }
+
+                let openPanel = NSOpenPanel()
+                openPanel.canChooseFiles = false
+                openPanel.canChooseDirectories = true
+                openPanel.canCreateDirectories = true
+                openPanel.beginSheetModal(for: self.view.window!) { result in
+                    if (result == .OK) {
+                        for fileURL in openPanel.urls {
+                            self.hiddenWindowCloseButtonIfNeeded()
+                            self.successFilePathURLArray?.removeAll()
+                            if (self.isBates) {
+                                let file = KMBatchOperateFile(filePath: self.pdfView?.document.documentURL.path ?? "")
+                                if file.fileType == .PDF {
+                                    file.addBatesInfo.pageChoice = KMBatchOperatePageChoice(rawValue: headFooter?.pageRangeType.rawValue ?? 0) ?? .All
+                                    file.addBatesInfo.pageRangeString = headFooter?.pageRangeString
+                                    file.addBatesInfo.savePath = fileURL.path
+                                    if file.status == .Waiting {
+                                        let op = KMBatchAddHeaderFooterOperation(file: file, headerFooter: headFooter!)
+                                        op.delegate = self
+                                        self.queue?.addOperation(op)
+                                    }
+                                }
+                            } else {
+                                self.successFilePathURLArray?.removeAll()
+                                let file = KMBatchOperateFile(filePath: self.pdfView?.document.documentURL.path ?? "")
+                                if file.fileType == .PDF {
+                                    file.addHeaderFooterInfo.pageChoice = KMBatchOperatePageChoice(rawValue: headFooter?.pageRangeType.rawValue ?? 0) ?? .All
+                                    file.addHeaderFooterInfo.pageRangeString = headFooter?.pageRangeString
+                                    file.addHeaderFooterInfo.savePath = fileURL.path
+                                    if file.status == .Waiting {
+                                        let op = KMBatchAddHeaderFooterOperation(file: file, headerFooter: headFooter!)
+                                        op.delegate = self
+                                        self.queue?.addOperation(op)
+                                    }
+                                }
+                            }
+                            if self.queue?.operations.count ?? 0 > 0 {
+                                self.interfaceStatus = .Processing
+                            }
+                        }
+                    }
+                }
+            } else if (sender.tag == 0) {
+                if let cnt = self.queue?.operations.count, cnt > 0 {
+                    self.queue?.cancelAllOperations()
+                }
+                self.interfaceStatus = .PrepareProcess
+            }
+        } else {
+            //点击开始
+            if (sender.tag == 1) {
+                if (!self.checkAndResetTask()) {
+                    NSSound.beep()
+                    return;
+                }
+                var headFooter: KMHeaderFooterModel?
+                let row = self.tableView.selectedRow
+                if (row == -1 || self.files?.count ?? 0 < 1) {
+                    NSSound.beep()
+                    return;
+                } else {
+                    if (self.isBates) {
+                        headFooter = KMHeaderFooterManager.defaultManager.onlyBatesObjects()[row]
+                    } else {
+                        headFooter = KMHeaderFooterManager.defaultManager.onlyHeaderFooterObjects()[row]
+                    }
+                }
+                
+                self.choosePathAndBeginOperation(headFooter!)
+            } else if (sender.tag == 0) {
+                if let cnt = self.queue?.operations.count, cnt > 0 {
+                    self.queue?.cancelAllOperations()
+                }
+                self.interfaceStatus = .PrepareProcess
+            }
+        }
         
     }
     func checkAndResetTask() -> Bool {
@@ -416,6 +512,29 @@ class KMBatchOperateAddHeaderFooterViewController: KMBatchOperateBaseViewControl
 //            blockSelf.postNotification()
 //        }
 //        wc?.beginSheetModalForWindow(NSApp.keyWindow, completionHandler: nil)
+        
+        let filePath: String = Bundle.main.path(forResource: "PDF Master User Guide", ofType: "pdf") ?? ""
+        let cdocument = CPDFDocument(url: URL(fileURLWithPath: filePath))
+        
+        let windowController = KMHeaderFooterWindowController(windowNibName: "KMHeaderFooterWindowController")
+        windowController.headerFooterObj = obj
+        windowController.isBatch = isBatchOperation
+        windowController.type = .Edit
+        windowController.isBates = isBates
+        windowController.pdfDocument = cdocument
+        
+        windowController.cancelAction = { [unowned self] controller in
+            self.km_endSheet()
+        }
+        windowController.operateCallBack = { wController, model in
+            self.currentObject = model
+            self.loadData()
+            self.postNotification()
+            let set = NSIndexSet(index: 0)
+            self.tableView.selectRowIndexes(set as IndexSet, byExtendingSelection: false)
+        }
+        self.km_beginSheet(windowC: windowController)
+        
     }
     func delete(obj: KMHeaderFooterModel) {
         KMHeaderFooterManager.defaultManager.removeHeaderFooter(obj)
@@ -426,7 +545,7 @@ class KMBatchOperateAddHeaderFooterViewController: KMBatchOperateBaseViewControl
     func validateMenuItem(_ menuItem: NSMenuItem) -> Bool {
         let action = menuItem.action
         if action == #selector(buttonItemClick_CleanAll(_:)) {
-            if (self.isBates && KMHeaderFooterManager.defaultManager.onlyBatesObjects.count < 1) || (!self.isBates && KMHeaderFooterManager.defaultManager.onlyHeaderFooterObjects.count < 1){
+            if (self.isBates && KMHeaderFooterManager.defaultManager.onlyBatesObjects().count < 1) || (!self.isBates && KMHeaderFooterManager.defaultManager.onlyHeaderFooterObjects().count < 1){
                 return false
             }
             return true
@@ -435,14 +554,14 @@ class KMBatchOperateAddHeaderFooterViewController: KMBatchOperateBaseViewControl
     }
     func headerFooterInterfaceSelectHeaderFooter(headerFooter: KMHeaderFooterModel) {
         self.tableView.reloadData()
-        if let index = KMHeaderFooterManager.defaultManager.onlyHeaderFooterObjects.index(of: headerFooter) {
+        if let index = KMHeaderFooterManager.defaultManager.onlyHeaderFooterObjects().firstIndex(of: headerFooter) {
             let indexSet = IndexSet(integer: index)
             self.tableView.selectRowIndexes(indexSet, byExtendingSelection: false)
         }
     }
     func batesInterfaceSelectBates(headerFooter: KMHeaderFooterModel) {
         self.tableView.reloadData()
-        if let index = KMHeaderFooterManager.defaultManager.onlyBatesObjects.index(of: headerFooter) {
+        if let index = KMHeaderFooterManager.defaultManager.onlyBatesObjects().firstIndex(of: headerFooter) {
             let indexSet = IndexSet(integer: index)
             self.tableView.selectRowIndexes(indexSet, byExtendingSelection: false)
         }
@@ -451,9 +570,9 @@ class KMBatchOperateAddHeaderFooterViewController: KMBatchOperateBaseViewControl
     func numberOfRows(in tableView: NSTableView) -> Int {
         var count = 0
         if self.isBates {
-            count = KMHeaderFooterManager.defaultManager.onlyBatesObjects.count
+            count = KMHeaderFooterManager.defaultManager.onlyBatesObjects().count
         } else {
-            count = KMHeaderFooterManager.defaultManager.onlyHeaderFooterObjects.count
+            count = KMHeaderFooterManager.defaultManager.onlyHeaderFooterObjects().count
         }
         self.blankView.isHidden = count != 0
         return count
@@ -461,9 +580,9 @@ class KMBatchOperateAddHeaderFooterViewController: KMBatchOperateBaseViewControl
     func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
         var obj: KMHeaderFooterModel? = nil
         if self.isBates {
-            obj = KMHeaderFooterManager.defaultManager.onlyBatesObjects[row]
+            obj = KMHeaderFooterManager.defaultManager.onlyBatesObjects()[row]
         } else {
-            obj = KMHeaderFooterManager.defaultManager.onlyHeaderFooterObjects[row]
+            obj = KMHeaderFooterManager.defaultManager.onlyHeaderFooterObjects()[row]
         }
         guard let cellView = tableView.makeView(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "mainCell"), owner: self) as? KMHeaderFooterTableCellView else {
             return nil
@@ -488,17 +607,23 @@ class KMBatchOperateAddHeaderFooterViewController: KMBatchOperateBaseViewControl
     }
     func tableView(_ tableView: NSTableView, heightOfRow row: Int) -> CGFloat { 
         if self.isBates {
-            return KMHeaderFooterManager.defaultManager.onlyBatesObjects[row].cellHeight
+            return KMHeaderFooterManager.defaultManager.onlyBatesObjects()[row].cellHeight
         } else {
-            return KMHeaderFooterManager.defaultManager.onlyHeaderFooterObjects[row].cellHeight
+            return KMHeaderFooterManager.defaultManager.onlyHeaderFooterObjects()[row].cellHeight
         }
     }
+    func tableView(_ tableView: NSTableView, shouldSelect tableColumn: NSTableColumn?) -> Bool {
+        return true
+    }
+    func tableView(_ tableView: NSTableView, shouldSelectRow row: Int) -> Bool {
+        return true
+    }
     func tableViewSelectionDidChange(_ notification: Notification) {
         let row = self.tableView.selectedRow
         updateActionButtonbackgroundColor()
         if row == -1 { return }
         if self.isBates {
-            let bates = KMHeaderFooterManager.defaultManager.onlyBatesObjects[row]
+            let bates = KMHeaderFooterManager.defaultManager.onlyBatesObjects()[row]
             self.currentObject = bates
             for i in 0..<(self.files?.count ?? 0) {
                 let file = self.files?[i]
@@ -514,7 +639,7 @@ class KMBatchOperateAddHeaderFooterViewController: KMBatchOperateBaseViewControl
                 }
             }
         } else {
-            let headerFooter = KMHeaderFooterManager.defaultManager.onlyHeaderFooterObjects[row]
+            let headerFooter = KMHeaderFooterManager.defaultManager.onlyHeaderFooterObjects()[row]
             self.currentObject = headerFooter
             for i in 0..<(self.files?.count ?? 0) {
                 let file = self.files?[i]

+ 1 - 1
PDF Office/PDF Master/Class/PDFTools/AddHeaderFooter/VC/KMBatchOperateAddHeaderFooterViewController.xib

@@ -138,7 +138,7 @@
                                         </textFieldCell>
                                         <tableColumnResizingMask key="resizingMask" resizeWithTable="YES" userResizable="YES"/>
                                         <prototypeCellViews>
-                                            <tableCellView identifier="mainCell" id="VP4-RR-pct" customClass="KMHeaderFooterTableCellView">
+                                            <tableCellView identifier="mainCell" id="VP4-RR-pct" customClass="KMHeaderFooterTableCellView" customModule="PDF_Master" customModuleProvider="target">
                                                 <rect key="frame" x="0.0" y="0.0" width="348" height="169"/>
                                                 <autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
                                                 <subviews>

+ 1 - 1
PDF Office/PDF Master/Class/PDFTools/AddHeaderFooter/View/KMHeaderFooterTableCellView.swift

@@ -119,7 +119,7 @@ class KMHeaderFooterTableCellView: NSTableCellView {
         self.titleTextFiled.stringValue = obj.id
     }
     func appendBaseString(_ prefixString: String, withString suffixString: String) -> String {
-        return "(prefixString): (suffixString)"
+        return "\(prefixString): \(suffixString)"
     }
     override func mouseEntered(with event: NSEvent) {
         super.mouseEntered(with: event)

+ 13 - 15
PDF Office/PDF Master/Class/PDFTools/HeaderFooter/Base/KMHeaderFooterAdjectiveModel.swift

@@ -25,28 +25,26 @@ import Cocoa
     
     var cellHeight: CGFloat {
         get {
-            var height: CGFloat = 12.0
-            height += 40
+            var height: CGFloat = 45.0
             
-            if (!self.topLeftString.isEmpty) {
-                height += 26.0
+            if (self.topLeftString.count > 0) {
+                height += 20.0
             }
-            if (!self.topCenterString.isEmpty) {
-                height += 26.0
+            if (self.topCenterString.count > 0) {
+                height += 20.0
             }
-            if (!self.topRightString.isEmpty) {
-                height += 26.0
+            if (self.topRightString.count > 0) {
+                height += 20.0
             }
-            if (!self.bottomLeftString.isEmpty) {
-                height += 26.0
+            if (self.bottomLeftString.count > 0) {
+                height += 20.0
             }
-            if (!self.bottomCenterString.isEmpty) {
-                height += 26.0
+            if (self.bottomCenterString.count > 0) {
+                height += 20.0
             }
-            if (!self.bottomRightString.isEmpty) {
-                height += 26.0
+            if (self.bottomRightString.count > 0) {
+                height += 20.0
             }
-            height += 25
             
             return height
         }

+ 60 - 33
PDF Office/PDF Master/Class/PDFTools/HeaderFooter/Tools/KMHeaderFooterManager.swift

@@ -15,30 +15,31 @@ class KMHeaderFooterManager: NSObject, NSCoding{
     let kFolderPath = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.applicationSupportDirectory, FileManager.SearchPathDomainMask.userDomainMask, true).last?.stringByAppendingPathComponent(Bundle.main.bundleIdentifier!).stringByAppendingPathComponent("headerfooter")
     let kPlistPath = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.applicationSupportDirectory, FileManager.SearchPathDomainMask.userDomainMask, true).last?.stringByAppendingPathComponent(Bundle.main.bundleIdentifier!).stringByAppendingPathComponent("headerfooter").stringByAppendingPathComponent("headerfooter.plist")
     
-    static let defaultManager: KMHeaderFooterManager = {
-        var manager = KMHeaderFooterManager()
-        if let storedData = UserDefaults.standard.value(forKey: kHeaderFooterInfoSaveKey) as? Data {
-            manager = NSKeyedUnarchiver.unarchiveObject(with: storedData) as! KMHeaderFooterManager
-        } else {
-            manager = KMHeaderFooterManager()
-            if manager.headFooterObjects == nil {
-                manager.headFooterObjects = []
-            }
-        }
-        return manager
-    }()
+    static let defaultManager = KMHeaderFooterManager()
+//    = {
+//        var manager = KMHeaderFooterManager()
+//        if let storedData = UserDefaults.standard.value(forKey: kHeaderFooterInfoSaveKey) as? Data {
+//            manager = NSKeyedUnarchiver.unarchiveObject(with: storedData) as! KMHeaderFooterManager
+//        } else {
+//            manager = KMHeaderFooterManager()
+////            if manager.headFooterObjects == nil {
+////                manager.headFooterObjects = []
+////            }
+//        }
+//        return manager
+//    }()
     
     func encode(with coder: NSCoder) {
-        coder.encode(self.headFooterObjects, forKey: "headFooterObjects")
+//        coder.encode(self.headFooterObjects, forKey: "headFooterObjects")
     }
     
     required init?(coder: NSCoder) {
-        self.headFooterObjects = coder.decodeObject(forKey: "headFooterObjects") as? [KMHeaderFooterModel]
+//        self.headFooterObjects = coder.decodeObject(forKey: "headFooterObjects") as? [KMHeaderFooterModel]
     }
     
     var datas: Array<KMHeaderFooterModel> = []
     
-    var headFooterObjects: [KMHeaderFooterModel]?
+//    var headFooterObjects: [KMHeaderFooterModel]?
     
     lazy var dateFormatArray: Array = {
         let arr = [
@@ -69,32 +70,32 @@ class KMHeaderFooterManager: NSObject, NSCoding{
         return arr
     }()
     
-    lazy var onlyBatesObjects: [KMHeaderFooterModel] = {
+    func onlyBatesObjects() -> [KMHeaderFooterModel] {
         var arr: [KMHeaderFooterModel] = Array<KMHeaderFooterModel>()
-        for i in 0..<(headFooterObjects?.count ?? 0) {
-            let obj = headFooterObjects?[i]
+        for i in 0..<datas.count {
+            let obj = self.datas[i]
             if KMDataVersionManager.updateBatesData() {
                 KMDataVersionManager.refrshBatesData(bates: obj)
                 store()
             }
             
-            if obj!.isBates {
-                arr.append(obj!)
+            if obj.isBates {
+                arr.append(obj)
             }
         }
         return arr
-    }()
-    lazy var onlyHeaderFooterObjects: [KMHeaderFooterModel] = {
+    }
+    func onlyHeaderFooterObjects() -> [KMHeaderFooterModel]{
         var arr: [KMHeaderFooterModel] = Array<KMHeaderFooterModel>()
-        for i in 0..<(headFooterObjects?.count ?? 0) {
-            let obj = headFooterObjects?[i]
+        for i in 0..<self.datas.count {
+            let obj = self.datas[i]
             
-            if !obj!.isBates {
-                arr.append(obj!)
+            if !obj.isBates {
+                arr.append(obj)
             }
         }
         return arr
-    }()
+    }
     override init() {
         super.init()
         if (FileManager.default.fileExists(atPath: kPlistPath!)) {
@@ -103,7 +104,7 @@ class KMHeaderFooterManager: NSObject, NSCoding{
                 return
             }
             
-            for keyIndex in 0 ..< (dataDict?.allKeys.count)! {
+            for keyIndex in 0 ..< (dataDict?.allKeys.count ?? 0) {
                 let key: String = dataDict?.allKeys[keyIndex] as! String
                 let backgroundDict: NSDictionary = dataDict?.object(forKey: key) as! NSDictionary
                 
@@ -127,7 +128,7 @@ class KMHeaderFooterManager: NSObject, NSCoding{
     
     func fetchBatesAvailableName() -> String {
         var availableIndex = 0
-        let nameArray = converArrType(arr: onlyBatesObjects, keyString: "id")
+        let nameArray = converArrType(arr: onlyBatesObjects(), keyString: "id")
         for i in 0..<nameArray.count {
             let string = nameArray[i]
             if string.hasPrefix("Bates") {
@@ -142,7 +143,7 @@ class KMHeaderFooterManager: NSObject, NSCoding{
     
     func fetchHeaderFooterAvailableName() -> String {
         var availableIndex = 0
-        let nameArray = converArrType(arr: onlyHeaderFooterObjects, keyString: "id")
+        let nameArray = converArrType(arr: onlyHeaderFooterObjects(), keyString: "id")
         for i in 0..<nameArray.count {
             let string = nameArray[i]
             if string.hasPrefix("HeaderFooter") {
@@ -188,7 +189,7 @@ class KMHeaderFooterManager: NSObject, NSCoding{
         
         let modelDict = self.parseModel(model: model)
         
-        let tag = tagString()
+        let tag = model.id
         newDict.addEntries(from: [tag : modelDict])
         model.id = tag
         let result = newDict.write(toFile: kPlistPath!, atomically: true)
@@ -301,8 +302,29 @@ class KMHeaderFooterManager: NSObject, NSCoding{
         return result
     }
     func removeHeaderFooter(_ obj: KMHeaderFooterModel) {
-        self.headFooterObjects?.removeObject(obj) 
-        self.store()
+        if (obj.id.count < 1) {
+            return
+        }
+        
+        if (!FileManager.default.fileExists(atPath: kPlistPath!)) {
+            return
+        }
+        
+        let key: String = obj.id
+        
+        let dictionary = NSDictionary(contentsOfFile: kPlistPath!)
+        var newDictionary: NSMutableDictionary!
+        if (dictionary != nil) {
+            newDictionary = NSMutableDictionary(dictionary: dictionary!)
+        } else {
+            newDictionary = NSMutableDictionary()
+        }
+        newDictionary.removeObject(forKey: key)
+        
+        let result = newDictionary.write(toFile: kPlistPath!, atomically: true)
+        if (result) {
+            self.datas.removeObject(obj)
+        }
     }
     
     /**
@@ -332,6 +354,7 @@ class KMHeaderFooterManager: NSObject, NSCoding{
         dict["rightMargin"] = model.rightMargin
         dict["bottomMargin"] = model.bottomMargin
         dict["topMargin"] = model.topMargin
+        dict["isBates"] = model.isBates ? "1" : "0"
         
         /// 内容
         dict["topLeftString"] = model.topLeftString
@@ -375,6 +398,10 @@ class KMHeaderFooterManager: NSObject, NSCoding{
         model.bottomLeftString = dict["bottomLeftString"] as! String
         model.bottomCenterString = dict["bottomCenterString"] as! String
         model.bottomRightString = dict["bottomRightString"] as! String
+        model.isBates = false
+        if (dict["isBates"] != nil) {
+            model.isBates = (dict["isBates"] as! String == "1")
+        }
         
         /// 日期
         model.dateFormatString = dict["dateFormatString"] as! String

+ 0 - 8
PDF Office/PDF Master/Class/PDFTools/Watermark/Controller/KMBatchOperateAddWatermarkViewController.swift

@@ -265,18 +265,10 @@ class KMBatchOperateAddWatermarkViewController: KMBatchOperateBaseViewController
                                 file.addWatermarkInfo.savePath = fileURL.path
                                 file.addWatermarkInfo.pageChoice = KMBatchOperatePageChoice(rawValue: watermark?.pageRangeType.rawValue ?? 0) ?? .All
                                 file.addWatermarkInfo.pageRangeString = watermark?.pagesString ?? ""
-//                                KMBatchAddWatermarkOperation *operation = [[[KMBatchAddWatermarkOperation alloc] initWithFile:file waterMark:watermark] autorelease];
-//                                operation.delegate = self;
-//                                [self.queue addOperation:operation];
                             } else {
                                 let file = KMBatchOperateFile(filePath: self.pdfView?.document.documentURL.path ?? "")
                                 file.addBackgroundInfo.savePath = fileURL.path
-//                                file.addBackgroundInfo.pageChoice = background.pageRangeType
                                 file.addBackgroundInfo.pageRangeString = background?.pagesString ?? ""
-
-//                                KMBatchAddBackgroundOperation *operation = [[[KMBatchAddBackgroundOperation alloc] initWithFile:file background:background] autorelease];
-//                                operation.delegate = self;
-//                                [self.queue addOperation:operation];
                             }
                             if let cnt = self.queue?.operations.count, cnt > 0 {
                                 self.interfaceStatus = .Processing