Browse Source

【UI替换】编辑工具 - 安全 设置密码逻辑串接

lizhe 1 year ago
parent
commit
e0c74b678a

+ 1 - 0
PDF Office/PDF Master/Class/Common/Base/KMBaseWindowController.swift

@@ -10,6 +10,7 @@ import Cocoa
 class KMBaseWindowController: NSWindowController {
     
     var cancelAction: KMCommonBlock?
+    var pdfDocument: CPDFDocument?
     
     deinit {
         Swift.debugPrint(self.className + "已释放")

+ 2 - 1
PDF Office/PDF Master/Class/Common/Tools/KMTools.swift

@@ -388,13 +388,14 @@ extension KMTools {
         return self.saveWatermarkDocument(document: document, to: URL(fileURLWithPath: filePath), secureOptions: secureOptions, removePWD:  removePWD)
     }
     
-    @objc class func saveWatermarkDocument(document: CPDFDocument, to url: URL, secureOptions: [CPDFDocumentWriteOption : Any]? = nil, removePWD: Bool = false) -> URL? {
+    @objc class func saveWatermarkDocument(document: CPDFDocument, to url: URL, secureOptions: [CPDFDocumentWriteOption : Any]? = nil, documentAttribute: [CPDFDocumentAttribute : Any]? = nil, removePWD: Bool = false) -> URL? {
         guard let _document = self._saveDocumentForWatermark(document: document) else {
             return nil
         }
         
         // 保存文档
         if let data = secureOptions, !data.isEmpty {
+            _document.setDocumentAttributes(documentAttribute)
             _document.write(to: url, withOptions: data)
         } else if (removePWD) {
             _document.writeDecrypt(to: url)

+ 2 - 1
PDF Office/PDF Master/Class/Document/KMMainDocument.swift

@@ -584,6 +584,7 @@ typealias KMMainDocumentCloudUploadHanddler = (@escaping(Bool, String)->()) -> (
 //                    } else {
                         if (mainViewController!.needSave) {
                             if let options = self.mainViewController?.secureOptions, !options.isEmpty {
+                                self.mainViewController!.document?.setDocumentAttributes(self.mainViewController?.documentAttribute)
                                 success = self.mainViewController!.document!.write(to: url, withOptions: options)
                             } else if let flag = self.mainViewController?.removeSecureFlag, flag {
                                 success = self.mainViewController!.document!.writeDecrypt(to: url)
@@ -652,7 +653,7 @@ typealias KMMainDocumentCloudUploadHanddler = (@escaping(Bool, String)->()) -> (
                                     callback(false, KMResult.cancel, true)
                                     return
                                 }
-                                guard let _url = KMTools.saveWatermarkDocument(document: _document, to: url!, secureOptions: self.mainViewController?.secureOptions, removePWD: self.mainViewController!.removeSecureFlag) else {
+                                guard let _url = KMTools.saveWatermarkDocument(document: _document, to: url!, secureOptions: self.mainViewController?.secureOptions, documentAttribute: self.mainViewController?.documentAttribute,removePWD: self.mainViewController!.removeSecureFlag) else {
                                     callback(false, KMResult.failure)
                                     return
                                 }

+ 126 - 2
PDF Office/PDF Master/Class/PDFTools/AddPassword/New/KMSecurityWindowController.swift

@@ -7,16 +7,18 @@
 
 import Cocoa
 
+typealias KMSecurityWindowControllerDoneAction = (_ controller: NSWindowController, _ option: [CPDFDocumentWriteOption : Any], _ attribute: [CPDFDocumentAttribute: Any]) -> Void
+
 class KMSecurityWindowController: KMBaseWindowController {
     @IBOutlet weak var securityView: KMSecurityView!
     
     var batchAction: KMBatchActionBlock?
+    var doneAction: KMSecurityWindowControllerDoneAction?
     
     override func windowDidLoad() {
         super.windowDidLoad()
 
         // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
-        
         securityView.cancelAction = { [unowned self] view in
             self.cancelAction?(self)
         }
@@ -24,6 +26,128 @@ class KMSecurityWindowController: KMBaseWindowController {
         securityView.batchAction = { [unowned self] view, files in
             self.batchAction?(self, files)
         }
+        
+        securityView.doneAction = { [unowned self] view, model, files in
+            self.setPassword(model: model, files: files)
+        }
+    }
+}
+
+extension KMSecurityWindowController {
+    func setPassword(model: KMSecureEncryptModel, files: [KMFileAttribute]) {
+        if pdfDocument == nil {
+            guard let filePath = files.first?.filePath else { return }
+        }
+        
+//        let myDocument = CPDFDocument(url: NSURL(fileURLWithPath: filePath) as URL)
+        if (pdfDocument != nil) {
+            var options: [CPDFDocumentWriteOption : Any] = [:]
+            var attribute: [CPDFDocumentAttribute : Any] = [:]
+            if model.openPasswordOn && model.ownerPasswordOn { ///  开启密码 & 权限密码
+                if (!model.openPassword.isEmpty) {
+                    options.updateValue(model.openPassword, forKey: .userPasswordOption)
+                }
+                
+                if (!model.ownerPassword.isEmpty) {
+                    options.updateValue(model.ownerPassword, forKey: .ownerPasswordOption)
+                }
+                
+                /// 允许打印
+                if model.printEnabled {
+                    if model.printAllowed == false {
+                        options.updateValue(false, forKey: .allowsPrintingOption)
+                    } else {
+                        options.updateValue(true, forKey: .allowsPrintingOption)
+                        if model.printSelectedIndex == 2 {
+                            options.updateValue(true, forKey: .allowsHighQualityPrintingOption)
+                        }
+                    }
+                }
+                
+                /// 允许更改
+                if model.editEnabled {
+                    if model.editSelectedIndex == 1 {
+                        options.updateValue(true, forKey: .allowsDocumentChangesOption)
+                        options.updateValue(true, forKey: .allowsDocumentAssemblyOption)
+                    } else if model.editSelectedIndex == 2 {
+                        options.updateValue(true, forKey: .allowsDocumentChangesOption)
+                        options.updateValue(true, forKey: .allowsFormFieldEntryOption)
+                    } else if model.editSelectedIndex == 3 {
+                        options.updateValue(true, forKey: .allowsDocumentChangesOption)
+                        options.updateValue(true, forKey: .allowsFormFieldEntryOption)
+                        options.updateValue(true, forKey: .allowsCommentingOption)
+                    } else if model.editAllowed == true {
+                        options.updateValue(true, forKey: .allowsCopyingOption)
+                        options.updateValue(true, forKey: .allowsDocumentChangesOption)
+                        options.updateValue(true, forKey: .allowsDocumentAssemblyOption)
+                        options.updateValue(true, forKey: .allowsCommentingOption)
+                        options.updateValue(true, forKey: .allowsFormFieldEntryOption)
+                    } else {
+                        options.updateValue(false, forKey: .allowsCopyingOption)
+                    }
+                }
+                /// 加密层级 sdk 缺接口
+            } else if model.openPasswordOn { /// 开启密码
+                if (!model.openPassword.isEmpty) {
+                    options.updateValue(model.openPassword, forKey: .userPasswordOption)
+                }
+                
+                /// 加密层级 sdk 缺接口
+            } else if model.ownerPasswordOn { /// 权限密码
+                if (!model.ownerPassword.isEmpty) {
+                    options.updateValue(model.ownerPassword, forKey: .ownerPasswordOption)
+                }
+                
+                /// 允许打印
+                if model.printEnabled {
+                    if model.printAllowed == false {
+                        options.updateValue(false, forKey: .allowsPrintingOption)
+                    } else {
+                        options.updateValue(true, forKey: .allowsPrintingOption)
+                        if model.printSelectedIndex == 2 {
+                            options.updateValue(true, forKey: .allowsHighQualityPrintingOption)
+                        }
+                    }
+                }
+                
+                /// 允许更改
+                if model.editEnabled {
+                    if model.editSelectedIndex == 1 {
+                        options.updateValue(true, forKey: .allowsDocumentChangesOption)
+                        options.updateValue(true, forKey: .allowsDocumentAssemblyOption)
+                    } else if model.editSelectedIndex == 2 {
+                        options.updateValue(true, forKey: .allowsDocumentChangesOption)
+                        options.updateValue(true, forKey: .allowsFormFieldEntryOption)
+                    } else if model.editSelectedIndex == 3 {
+                        options.updateValue(true, forKey: .allowsDocumentChangesOption)
+                        options.updateValue(true, forKey: .allowsFormFieldEntryOption)
+                        options.updateValue(true, forKey: .allowsCommentingOption)
+                    } else if model.editAllowed == true {
+                        options.updateValue(true, forKey: .allowsCopyingOption)
+                        options.updateValue(true, forKey: .allowsDocumentChangesOption)
+                        options.updateValue(true, forKey: .allowsDocumentAssemblyOption)
+                        options.updateValue(true, forKey: .allowsCommentingOption)
+                        options.updateValue(true, forKey: .allowsFormFieldEntryOption)
+                    } else {
+                        options.updateValue(false, forKey: .allowsCopyingOption)
+                    }
+                }
+                
+                /// 加密层级 sdk 缺接口
+            }
+            
+            attribute.updateValue(model.title, forKey: .titleAttribute)
+            attribute.updateValue(model.author, forKey: .authorAttribute)
+            attribute.updateValue(model.subject, forKey: .subjectAttribute)
+            attribute.updateValue(model.keywords, forKey: .keywordsAttribute)
+            
+            //            let result = myDocument.write(to: documentURL, withOptions: options)
+            //            myDocument.setPasswordOptions(options)
+            guard let callback = doneAction else {
+                return
+            }
+            
+            callback(self, options, attribute)
+        }
     }
-    
 }

+ 164 - 0
PDF Office/PDF Master/Class/PDFTools/AddPassword/New/View/KMSecurityView.swift

@@ -9,23 +9,155 @@ import Cocoa
 
 typealias KMSecurityViewBatchAction = (_ view: KMSecurityView, _ files: [KMFileAttribute]) -> Void
 typealias KMSecurityViewCancelAction = (_ view: KMSecurityView) -> Void
+typealias KMSecurityViewDoneAction = (_ view: KMSecurityView, _ model: KMSecureEncryptModel, _ files: [KMFileAttribute]) -> Void
 
 class KMSecurityView: KMBaseXibView {
     @IBOutlet weak var batchButton: NSButton!
     @IBOutlet weak var cancelButton: NSButton!
     @IBOutlet weak var doneButton: NSButton!
     
+    @IBOutlet weak var box1: NSBox!
+    @IBOutlet weak var boxLabel1: NSTextField!
+    @IBOutlet weak var openPwdCheckBtn: NSButton!
+    @IBOutlet weak var openPasswordPLabel: NSTextField!
+    @IBOutlet weak var openPassword: NSSecureTextField!
+    
+    @IBOutlet weak var ownerPwdCheckBtn: NSButton!
+    @IBOutlet weak var ownerPasswordLabel: NSTextField!
+    @IBOutlet weak var ownerPassword: NSSecureTextField!
+    @IBOutlet weak var notPrintCheckBtn: NSButton!
+    @IBOutlet weak var notCopyCheckBtn: NSButton!
+    
+    @IBOutlet weak var box2: NSBox!
+    @IBOutlet weak var boxLabel2: NSTextField!
+    @IBOutlet weak var titleTextField: NSTextField!
+    @IBOutlet weak var authorTextField: NSTextField!
+    @IBOutlet weak var subjectTextField: NSTextField!
+    @IBOutlet weak var keywordTextField: NSTextField!
+    
+    @IBOutlet weak var titleLabel: NSTextField!
+    @IBOutlet weak var authorLabel: NSTextField!
+    @IBOutlet weak var subjectLabel: NSTextField!
+    @IBOutlet weak var keywordLabel: NSTextField!
+
     var batchAction: KMSecurityViewBatchAction?
     var cancelAction: KMSecurityViewCancelAction?
+    var doneAction: KMSecurityViewDoneAction?
+    
+    var openPasswordString: String?
+    var ownerPasswordString: String?
     
     var files: [KMFileAttribute] = []
+    
+    private var model: KMSecureEncryptModel = KMSecureEncryptModel()
+    var canEncrypt: Bool = false
+    
     override func draw(_ dirtyRect: NSRect) {
         super.draw(dirtyRect)
 
         // Drawing code here.
     }
     
+    override func setup() {
+        openPassword.delegate = self
+        ownerPassword.delegate = self
+        
+        self.updateOwnerButtonState()
+    }
+    
+    override func updateLanguage() {
+        batchButton.title = NSLocalizedString("Batch", comment: "")
+//        cancelButton.title = NSLocalizedString("Remove All", comment: "")
+        doneButton.title = NSLocalizedString("Encrypt", comment: "")
+        cancelButton.title = NSLocalizedString("Close", comment: "")
+        
+        boxLabel1.stringValue = NSLocalizedString("Password Security Settings", comment: "")
+        boxLabel2.stringValue = NSLocalizedString("Document Description", comment: "")
+        openPwdCheckBtn.title = NSLocalizedString("Require a password to open the document.", comment: "")
+        ownerPwdCheckBtn.title = NSLocalizedString("Restrict printing and copying of the document.", comment: "")
+        notPrintCheckBtn.title = NSLocalizedString("Restrict document printing", comment: "")
+        notCopyCheckBtn.title = NSLocalizedString("Restrict content copying", comment: "")
+        openPasswordPLabel.stringValue = NSLocalizedString("Open Password:", comment: "")
+        ownerPasswordLabel.stringValue = NSLocalizedString("Owner Password:", comment: "")
+        titleLabel.stringValue = NSLocalizedString("Title:", comment: "")
+        authorLabel.stringValue = NSLocalizedString("Author:", comment: "")
+        subjectLabel.stringValue = NSLocalizedString("Subject:", comment: "")
+        keywordLabel.stringValue = NSLocalizedString("Keywords:", comment: "")
+    }
+    
+    override func reloadData() {
+        
+        openPassword.isEnabled = model.openPasswordOn
+        ownerPassword.isEnabled = model.ownerPasswordOn
+
+        notPrintCheckBtn.isEnabled = model.printEnabled
+        notCopyCheckBtn.isEnabled = model.editEnabled
+        
+        notPrintCheckBtn.state = model.printAllowed ?.off:.on
+        notCopyCheckBtn.state = model.editAllowed ?.off:.on
+        
+        self.updateEncryptButtonEnabledState()
+    }
+}
+
+extension KMSecurityView {
+    func updateEncryptButtonEnabledState() {
+        var enabled = false
+        if model.openPasswordOn {
+            if !model.openPassword.isEmpty {
+                enabled = true
+            }
+        }
+        
+        if enabled {
+            if model.ownerPasswordOn {
+                if (!model.ownerPassword.isEmpty && (!model.printAllowed || !model.editAllowed)) {
+                    enabled = false
+                }
+            }
+        } else {
+            if model.ownerPasswordOn {
+                if !model.ownerPassword.isEmpty && (!model.printAllowed || !model.editAllowed) {
+                    enabled = true
+                }
+            }
+        }
+        
+        self.canEncrypt = enabled
+        if enabled {
+            self.doneButton.isEnabled = true
+        } else {
+            self.doneButton.isEnabled = false
+        }
+    }
+    
+    func updatePasswordState() {
+        self.model.openPassword = openPassword.stringValue
+        self.model.ownerPassword = ownerPassword.stringValue
+        self.model.title = titleTextField.stringValue
+        self.model.author = titleTextField.stringValue
+        self.model.subject = titleTextField.stringValue
+        self.model.keywords = titleTextField.stringValue
+        self.reloadData()
+    }
     
+    func updateOwnerButtonState() {
+        if self.model.ownerPasswordOn {
+            self.model.printEnabled = true
+            self.model.editEnabled = true
+            self.model.printAllowed = false
+            self.model.editAllowed = false
+        } else {
+            self.model.printEnabled = false
+            self.model.editEnabled = false
+            self.model.printAllowed = true
+            self.model.editAllowed = true
+        }
+        self.reloadData()
+    }
+}
+
+extension KMSecurityView {
     @IBAction func batchButtonAction(_ sender: Any) {
         guard let callBack = batchAction else { return }
         
@@ -39,7 +171,39 @@ class KMSecurityView: KMBaseXibView {
     }
     
     @IBAction func doneButtonAction(_ sender: Any) {
+        self.updatePasswordState()
+        guard let callBack = doneAction else { return }
         
+        callBack(self, model, files)
+    }
+    
+    @IBAction func openPasswordButtonAction(_ sender: Any) {
+        self.model.openPasswordOn = self.openPwdCheckBtn.state == .on ? true:false
+        self.reloadData()
     }
     
+    @IBAction func ownerPaasswordButtonAction(_ sender: Any) {
+        self.model.ownerPasswordOn = self.ownerPwdCheckBtn.state == .on ? true:false
+        self.updateOwnerButtonState()
+    }
+    
+    @IBAction func printButtonAction(_ sender: Any) {
+        self.model.printAllowed = self.notPrintCheckBtn.state == .on ? false:true
+        self.reloadData()
+    }
+    
+    @IBAction func copyButtonAction(_ sender: Any) {
+        self.model.editAllowed = self.notCopyCheckBtn.state == .on ? false:true
+        self.reloadData()
+    }
+}
+
+extension  KMSecurityView: NSTextFieldDelegate {
+    func controlTextDidEndEditing(_ obj: Notification) {
+        self.updatePasswordState()
+    }
+    
+    func controlTextDidChange(_ obj: Notification) {
+        self.updatePasswordState()
+    }
 }

+ 36 - 13
PDF Office/PDF Master/Class/PDFTools/AddPassword/New/View/KMSecurityView.xib

@@ -8,9 +8,29 @@
     <objects>
         <customObject id="-2" userLabel="File's Owner" customClass="KMSecurityView" customModule="PDF_Master" customModuleProvider="target">
             <connections>
+                <outlet property="authorLabel" destination="feB-OM-BTK" id="z0F-u2-2gZ"/>
+                <outlet property="authorTextField" destination="FbH-uh-vhX" id="TSb-Px-cuK"/>
                 <outlet property="batchButton" destination="uXk-aN-bU2" id="wuM-he-LvR"/>
+                <outlet property="box1" destination="ST2-wX-xoy" id="U5Y-7a-KRx"/>
+                <outlet property="box2" destination="AuG-7F-1zd" id="jPC-lV-K5l"/>
+                <outlet property="boxLabel1" destination="aXy-qa-30P" id="bCh-vV-qX9"/>
+                <outlet property="boxLabel2" destination="bDa-JE-YYY" id="tF7-OJ-t8p"/>
                 <outlet property="cancelButton" destination="J4j-YE-zds" id="SI8-j0-Zvg"/>
                 <outlet property="doneButton" destination="zu9-tu-Eu4" id="4N6-12-ny5"/>
+                <outlet property="keywordLabel" destination="Pye-Uj-4I0" id="4zl-eR-4Qq"/>
+                <outlet property="keywordTextField" destination="SX3-7U-7ea" id="qXG-PG-cEL"/>
+                <outlet property="notCopyCheckBtn" destination="Q12-nq-8Ww" id="gpQ-Ln-6cH"/>
+                <outlet property="notPrintCheckBtn" destination="Df0-VK-282" id="KkW-CT-YkD"/>
+                <outlet property="openPassword" destination="6g4-S8-Wrw" id="yCD-AY-913"/>
+                <outlet property="openPasswordPLabel" destination="4FR-2o-FxP" id="TQs-H9-tf2"/>
+                <outlet property="openPwdCheckBtn" destination="twD-kg-pju" id="e9v-Qo-If0"/>
+                <outlet property="ownerPassword" destination="KlB-P6-WnC" id="bVf-dr-b7Z"/>
+                <outlet property="ownerPasswordLabel" destination="HTv-gT-hdG" id="zhb-Nz-5py"/>
+                <outlet property="ownerPwdCheckBtn" destination="MN5-MX-XNZ" id="fJo-jR-iVq"/>
+                <outlet property="subjectLabel" destination="621-mt-orF" id="3M7-JD-z4o"/>
+                <outlet property="subjectTextField" destination="wJ2-bm-ZhO" id="Ebc-P3-2hQ"/>
+                <outlet property="titleLabel" destination="G69-bY-klh" id="pad-lb-MQU"/>
+                <outlet property="titleTextField" destination="1qu-Ea-pYg" id="xHO-9b-n9k"/>
             </connections>
         </customObject>
         <customObject id="-1" userLabel="First Responder" customClass="FirstResponder"/>
@@ -34,6 +54,9 @@
                                             <behavior key="behavior" changeContents="YES" doesNotDimImage="YES" lightByContents="YES"/>
                                             <font key="font" metaFont="system"/>
                                         </buttonCell>
+                                        <connections>
+                                            <action selector="openPasswordButtonAction:" target="-2" id="H9S-kO-FSs"/>
+                                        </connections>
                                     </button>
                                     <textField focusRingType="none" horizontalHuggingPriority="251" verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="4FR-2o-FxP">
                                         <rect key="frame" x="34" y="164" width="169" height="16"/>
@@ -60,6 +83,9 @@
                                             <behavior key="behavior" changeContents="YES" doesNotDimImage="YES" lightByContents="YES"/>
                                             <font key="font" metaFont="system"/>
                                         </buttonCell>
+                                        <connections>
+                                            <action selector="ownerPaasswordButtonAction:" target="-2" id="mag-bW-xf5"/>
+                                        </connections>
                                     </button>
                                     <textField focusRingType="none" horizontalHuggingPriority="251" verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="HTv-gT-hdG">
                                         <rect key="frame" x="34" y="97" width="169" height="16"/>
@@ -75,6 +101,9 @@
                                             <behavior key="behavior" changeContents="YES" doesNotDimImage="YES" lightByContents="YES"/>
                                             <font key="font" metaFont="system"/>
                                         </buttonCell>
+                                        <connections>
+                                            <action selector="printButtonAction:" target="-2" id="hCd-vg-okT"/>
+                                        </connections>
                                     </button>
                                     <button translatesAutoresizingMaskIntoConstraints="NO" id="Q12-nq-8Ww">
                                         <rect key="frame" x="63" y="31" width="175" height="18"/>
@@ -82,6 +111,9 @@
                                             <behavior key="behavior" changeContents="YES" doesNotDimImage="YES" lightByContents="YES"/>
                                             <font key="font" metaFont="system"/>
                                         </buttonCell>
+                                        <connections>
+                                            <action selector="copyButtonAction:" target="-2" id="o3r-bc-w8E"/>
+                                        </connections>
                                     </button>
                                     <secureTextField focusRingType="none" verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="KlB-P6-WnC" userLabel="Owner Password">
                                         <rect key="frame" x="206" y="94" width="248" height="21"/>
@@ -173,16 +205,13 @@ DQ
                                             <color key="backgroundColor" name="controlColor" catalog="System" colorSpace="catalog"/>
                                         </textFieldCell>
                                     </textField>
-                                    <textField focusRingType="none" verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="1qu-Ea-pYg" customClass="KMSecurityTextField">
+                                    <textField focusRingType="none" verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="1qu-Ea-pYg">
                                         <rect key="frame" x="85" y="132" width="395" height="21"/>
                                         <textFieldCell key="cell" scrollable="YES" lineBreakMode="clipping" selectable="YES" editable="YES" sendsActionOnEndEditing="YES" state="on" borderStyle="bezel" drawsBackground="YES" id="fDe-4Q-Pyw">
                                             <font key="font" metaFont="system"/>
                                             <color key="textColor" name="controlTextColor" catalog="System" colorSpace="catalog"/>
                                             <color key="backgroundColor" name="textBackgroundColor" catalog="System" colorSpace="catalog"/>
                                         </textFieldCell>
-                                        <connections>
-                                            <outlet property="nextKeyView" destination="FbH-uh-vhX" id="YWQ-a7-d32"/>
-                                        </connections>
                                     </textField>
                                     <textField focusRingType="none" horizontalHuggingPriority="251" verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="feB-OM-BTK">
                                         <rect key="frame" x="14" y="99" width="67" height="16"/>
@@ -192,16 +221,13 @@ DQ
                                             <color key="backgroundColor" name="controlColor" catalog="System" colorSpace="catalog"/>
                                         </textFieldCell>
                                     </textField>
-                                    <textField focusRingType="none" verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="FbH-uh-vhX" customClass="KMSecurityTextField">
+                                    <textField focusRingType="none" verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="FbH-uh-vhX">
                                         <rect key="frame" x="85" y="96" width="395" height="21"/>
                                         <textFieldCell key="cell" scrollable="YES" lineBreakMode="clipping" selectable="YES" editable="YES" sendsActionOnEndEditing="YES" state="on" borderStyle="bezel" drawsBackground="YES" id="dHR-ed-Cv8">
                                             <font key="font" metaFont="system"/>
                                             <color key="textColor" name="controlTextColor" catalog="System" colorSpace="catalog"/>
                                             <color key="backgroundColor" name="textBackgroundColor" catalog="System" colorSpace="catalog"/>
                                         </textFieldCell>
-                                        <connections>
-                                            <outlet property="nextKeyView" destination="wJ2-bm-ZhO" id="b5g-99-KkW"/>
-                                        </connections>
                                     </textField>
                                     <textField focusRingType="none" horizontalHuggingPriority="251" verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="Pye-Uj-4I0">
                                         <rect key="frame" x="14" y="27" width="67" height="16"/>
@@ -211,18 +237,15 @@ DQ
                                             <color key="backgroundColor" name="controlColor" catalog="System" colorSpace="catalog"/>
                                         </textFieldCell>
                                     </textField>
-                                    <textField focusRingType="none" verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="wJ2-bm-ZhO" customClass="KMSecurityTextField">
+                                    <textField focusRingType="none" verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="wJ2-bm-ZhO">
                                         <rect key="frame" x="85" y="60" width="395" height="21"/>
                                         <textFieldCell key="cell" scrollable="YES" lineBreakMode="clipping" selectable="YES" editable="YES" sendsActionOnEndEditing="YES" state="on" borderStyle="bezel" drawsBackground="YES" id="nPj-Eh-Hsz">
                                             <font key="font" metaFont="system"/>
                                             <color key="textColor" name="controlTextColor" catalog="System" colorSpace="catalog"/>
                                             <color key="backgroundColor" name="textBackgroundColor" catalog="System" colorSpace="catalog"/>
                                         </textFieldCell>
-                                        <connections>
-                                            <outlet property="nextKeyView" destination="SX3-7U-7ea" id="5nH-Xr-4LJ"/>
-                                        </connections>
                                     </textField>
-                                    <textField focusRingType="none" verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="SX3-7U-7ea" customClass="KMSecurityTextField">
+                                    <textField focusRingType="none" verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="SX3-7U-7ea">
                                         <rect key="frame" x="85" y="24" width="395" height="21"/>
                                         <textFieldCell key="cell" scrollable="YES" lineBreakMode="clipping" selectable="YES" editable="YES" sendsActionOnEndEditing="YES" state="on" borderStyle="bezel" drawsBackground="YES" id="HXf-ig-qVm">
                                             <font key="font" metaFont="system"/>

+ 5 - 0
PDF Office/PDF Master/Class/PDFTools/Secure/Model/KMSecureEncryptModel.swift

@@ -26,4 +26,9 @@ class KMSecureEncryptModel: NSObject {
     
     var encryptLevetArray: [String] = ["128-bit-RC4","128-bit-AES","256-bit-AES"]
     var encryptLevelSelectedIndex = 0
+    
+    var title: String = ""
+    var author: String = ""
+    var subject: String = ""
+    var keywords: String = ""
 }

+ 16 - 16
PDF Office/PDF Master/Class/PDFTools/Secure/Window/KMPasswordInputWindow.xib

@@ -1,12 +1,12 @@
 <?xml version="1.0" encoding="UTF-8"?>
-<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="21507" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct">
+<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="22155" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct">
     <dependencies>
         <deployment identifier="macosx"/>
-        <plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="21507"/>
+        <plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="22155"/>
         <capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
     </dependencies>
     <objects>
-        <customObject id="-2" userLabel="File's Owner" customClass="KMPasswordInputWindow" customModule="PDF_Office" customModuleProvider="target">
+        <customObject id="-2" userLabel="File's Owner" customClass="KMPasswordInputWindow" customModule="PDF_Master" customModuleProvider="target">
             <connections>
                 <outlet property="cancelButton" destination="jwm-QT-mwz" id="4v7-86-Phe"/>
                 <outlet property="confirmButton" destination="Dpd-lf-2h1" id="wfr-lB-56E"/>
@@ -19,7 +19,7 @@
         </customObject>
         <customObject id="-1" userLabel="First Responder" customClass="FirstResponder"/>
         <customObject id="-3" userLabel="Application" customClass="NSObject"/>
-        <window title="Window" allowsToolTipsWhenApplicationIsInactive="NO" autorecalculatesKeyViewLoop="NO" releasedWhenClosed="NO" visibleAtLaunch="NO" animationBehavior="default" id="QvC-M9-y7g" customClass="KMPasswordInputWindow" customModule="PDF_Office" customModuleProvider="target">
+        <window title="Window" allowsToolTipsWhenApplicationIsInactive="NO" autorecalculatesKeyViewLoop="NO" releasedWhenClosed="NO" visibleAtLaunch="NO" animationBehavior="default" id="QvC-M9-y7g" customClass="KMPasswordInputWindow" customModule="PDF_Master" customModuleProvider="target">
             <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="432" height="220"/>
@@ -36,7 +36,7 @@
                             <rect key="frame" x="0.0" y="0.0" width="432" height="44"/>
                             <autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
                             <subviews>
-                                <textField horizontalHuggingPriority="251" verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="X38-4p-51v">
+                                <textField focusRingType="none" horizontalHuggingPriority="251" verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="X38-4p-51v">
                                     <rect key="frame" x="14" y="16" width="37" height="16"/>
                                     <textFieldCell key="cell" lineBreakMode="clipping" title="Label" id="mNB-xx-ixe">
                                         <font key="font" usesAppearanceFont="YES"/>
@@ -66,7 +66,7 @@
                             <rect key="frame" x="0.0" y="0.0" width="432" height="102"/>
                             <autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
                             <subviews>
-                                <textField verticalHuggingPriority="750" horizontalCompressionResistancePriority="250" translatesAutoresizingMaskIntoConstraints="NO" id="xRE-aR-QBJ">
+                                <textField focusRingType="none" verticalHuggingPriority="750" horizontalCompressionResistancePriority="250" translatesAutoresizingMaskIntoConstraints="NO" id="xRE-aR-QBJ">
                                     <rect key="frame" x="102" y="80" width="316" height="16"/>
                                     <constraints>
                                         <constraint firstAttribute="height" relation="lessThanOrEqual" constant="66" id="Usx-e2-2hd"/>
@@ -77,13 +77,13 @@
                                         <color key="backgroundColor" name="textBackgroundColor" catalog="System" colorSpace="catalog"/>
                                     </textFieldCell>
                                 </textField>
-                                <customView translatesAutoresizingMaskIntoConstraints="NO" id="nlf-dC-L2j" customClass="KMSecureTextFiled" customModule="PDF_Office" customModuleProvider="target">
+                                <customView translatesAutoresizingMaskIntoConstraints="NO" id="nlf-dC-L2j" customClass="KMSecureTextFiled" customModule="PDF_Master" customModuleProvider="target">
                                     <rect key="frame" x="104" y="40" width="312" height="32"/>
                                     <constraints>
                                         <constraint firstAttribute="height" constant="32" id="jiZ-zN-rn8"/>
                                     </constraints>
                                 </customView>
-                                <textField verticalHuggingPriority="750" horizontalCompressionResistancePriority="250" translatesAutoresizingMaskIntoConstraints="NO" id="gST-4X-s69">
+                                <textField focusRingType="none" verticalHuggingPriority="750" horizontalCompressionResistancePriority="250" translatesAutoresizingMaskIntoConstraints="NO" id="gST-4X-s69">
                                     <rect key="frame" x="102" y="16" width="304" height="16"/>
                                     <constraints>
                                         <constraint firstAttribute="width" constant="300" id="N0S-qp-xpS"/>
@@ -129,10 +129,6 @@
                                 </box>
                                 <button verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="jwm-QT-mwz">
                                     <rect key="frame" x="254" y="16" width="78" height="32"/>
-                                    <constraints>
-                                        <constraint firstAttribute="width" constant="78" id="9On-lg-Ifu"/>
-                                        <constraint firstAttribute="height" constant="32" id="YVJ-Ck-tWB"/>
-                                    </constraints>
                                     <buttonCell key="cell" type="bevel" title="Button" bezelStyle="rounded" alignment="center" imageScaling="proportionallyDown" inset="2" id="9ln-dP-nCD">
                                         <behavior key="behavior" pushIn="YES" lightByBackground="YES" lightByGray="YES"/>
                                         <font key="font" metaFont="system"/>
@@ -140,13 +136,13 @@
 Gw
 </string>
                                     </buttonCell>
+                                    <constraints>
+                                        <constraint firstAttribute="width" constant="78" id="9On-lg-Ifu"/>
+                                        <constraint firstAttribute="height" constant="32" id="YVJ-Ck-tWB"/>
+                                    </constraints>
                                 </button>
                                 <button verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="Dpd-lf-2h1">
                                     <rect key="frame" x="348" y="16" width="68" height="32"/>
-                                    <constraints>
-                                        <constraint firstAttribute="height" constant="32" id="19C-1p-OYf"/>
-                                        <constraint firstAttribute="width" constant="68" id="OOr-ag-SdW"/>
-                                    </constraints>
                                     <buttonCell key="cell" type="bevel" title="Button" bezelStyle="rounded" alignment="center" imageScaling="proportionallyDown" inset="2" id="uBB-Ls-gQs">
                                         <behavior key="behavior" pushIn="YES" lightByBackground="YES" lightByGray="YES"/>
                                         <font key="font" metaFont="system"/>
@@ -154,6 +150,10 @@ Gw
 DQ
 </string>
                                     </buttonCell>
+                                    <constraints>
+                                        <constraint firstAttribute="height" constant="32" id="19C-1p-OYf"/>
+                                        <constraint firstAttribute="width" constant="68" id="OOr-ag-SdW"/>
+                                    </constraints>
                                 </button>
                             </subviews>
                             <constraints>

+ 14 - 36
PDF Office/PDF Master/Class/PDFWindowController/ViewController/KMMainViewController+Action.swift

@@ -2950,43 +2950,21 @@ extension KMMainViewController : KMMainToolbarControllerDelegate {
                 #endif
                 
                 if (index == 1) { /// 设置密码
-//                    if (!self.listView.document!.allowsCopying || !self.listView.document!.allowsPrinting) {
-//                        KMPasswordInputWindow.openWindow(window: self.view.window!, type: .owner, url: self.listView.document.documentURL) { [weak self]  result, password in
-//                            if (result == .cancel) {
-//                                return
-//                            }
-//                            
-//                            self?.isSaveKeyChain = false
-//                            self?.listView.document.unlock(withPassword: password)
-//                            self?.hiddenSecureLimitTip()
-//                            
-//                            self?.showSecureWindow((self?.listView.document.documentURL)!)
-//                        }
-//                        return
-//                    }
-//                    self.showSecureWindow(self.listView.document.documentURL)
-                    let controller = KMSecurityWindowController(windowNibName: "KMSecurityWindowController")
-                    self.currentWindowController = controller
-                    
-                    controller.batchAction = { [unowned self] controller, files in
-                        self.view.window?.endSheet((self.currentWindowController.window)!)
-                        self.currentWindowController = nil
-                        
-                        self.toolbarController.cancelSelected(KMToolbarToolCompressItemIdentifier)
-                        
-                        let batchWindowController = KMBatchOperateWindowController.sharedWindowController
-                                batchWindowController.window?.makeKeyAndOrderFront("")
-                        let batchOperateFile = KMBatchOperateFile(filePath: files.first?.filePath ?? "")
-                        batchWindowController.switchToOperateType(.AddPassword, files: [batchOperateFile])
-                        batchWindowController.window?.makeKeyAndOrderFront("")
-                    }
-                    
-                    controller.cancelAction = { [unowned self] controller in
-                        self.view.window?.endSheet((self.currentWindowController.window)!)
-                        self.currentWindowController = nil
+                    if (!self.listView.document!.allowsCopying || !self.listView.document!.allowsPrinting) {
+                        KMPasswordInputWindow.openWindow(window: self.view.window!, type: .owner, url: self.listView.document.documentURL) { [weak self]  result, password in
+                            if (result == .cancel) {
+                                return
+                            }
+                            
+                            self?.isSaveKeyChain = false
+                            self?.listView.document.unlock(withPassword: password)
+                            self?.hiddenSecureLimitTip()
+                            
+                            self?.showSecureWindow((self?.listView.document.documentURL)!)
+                        }
+                        return
                     }
-                    await NSWindow.currentWindow().beginSheet(controller.window!)
-                    
+                    self.showSecureWindow(self.listView.document.documentURL)
                     return
                 } else if index == 2 {
                     let controller = KMRemovePasswordWindowController(windowNibName: "KMRemovePasswordWindowController")

+ 66 - 19
PDF Office/PDF Master/Class/PDFWindowController/ViewController/KMMainViewController.swift

@@ -146,12 +146,15 @@ import Cocoa
         return vc
     }()
     private var background_mask: NSView?
+    
     fileprivate var _secureOptions: [CPDFDocumentWriteOption : Any]?
     var secureOptions: [CPDFDocumentWriteOption : Any]? {
         get {
             return self._secureOptions
         }
     }
+    var documentAttribute: [CPDFDocumentAttribute : Any]?
+    
     fileprivate var _removeSecureFlag = false
     var removeSecureFlag: Bool {
         get {
@@ -902,28 +905,67 @@ import Cocoa
     // MARR: 显示加密弹窗
     
     public func showSecureWindow(_ url: URL) {
-        let windowController = KMSecureEncryptWindowController(windowNibName: "KMSecureEncryptWindowController")
-        windowController.documentURL = url
-        windowController.myDocument = self.listView.document
-        self.currentWindowController = windowController
-
-        windowController.itemClick = { [weak self] index in
-            self?.view.window?.endSheet((self?.currentWindowController.window)!)
-            self?.currentWindowController = nil
+//        let windowController = KMSecureEncryptWindowController(windowNibName: "KMSecureEncryptWindowController")
+//        windowController.documentURL = url
+//        windowController.myDocument = self.listView.document
+//        self.currentWindowController = windowController
+//
+//        windowController.itemClick = { [weak self] index in
+//            self?.view.window?.endSheet((self?.currentWindowController.window)!)
+//            self?.currentWindowController = nil
+//        }
+        
+//        windowController.resultCallback = { [weak self] result in
+//            let windowController_secure = self?.currentWindowController as! KMSecureEncryptWindowController
+//            self?.view.window?.endSheet((self?.currentWindowController.window)!)
+//            self?.currentWindowController = nil
+//            
+//            self?._secureOptions = windowController_secure.options
+//            self?.needSave = true
+//            self?.recordIsPDFDocumentEdited(type: .setPassword)
+//            
+//            if (result) {
+//                self?.showSecureSuccessTip()
+//                self?.recordSaveWatermarkFlag(type: .setPassword)
+//            } else {
+//                let alert = NSAlert()
+//                alert.messageText = NSLocalizedString("Failure", comment: "")
+//                alert.runModal()
+//            }
+//        }
+        
+//        self.view.window?.beginSheet(windowController.window!)
+        
+        let controller = KMSecurityWindowController(windowNibName: "KMSecurityWindowController")
+        controller.pdfDocument = self.document
+        self.currentWindowController = controller
+        
+        controller.batchAction = { [unowned self] controller, files in
+            self.view.window?.endSheet((self.currentWindowController.window)!)
+            self.currentWindowController = nil
+            
+            self.toolbarController.cancelSelected(KMToolbarToolCompressItemIdentifier)
+            
+            let batchWindowController = KMBatchOperateWindowController.sharedWindowController
+                    batchWindowController.window?.makeKeyAndOrderFront("")
+            let batchOperateFile = KMBatchOperateFile(filePath: files.first?.filePath ?? "")
+            batchWindowController.switchToOperateType(.AddPassword, files: [batchOperateFile])
+            batchWindowController.window?.makeKeyAndOrderFront("")
         }
         
-        windowController.resultCallback = { [weak self] result in
-            let windowController_secure = self?.currentWindowController as! KMSecureEncryptWindowController
-            self?.view.window?.endSheet((self?.currentWindowController.window)!)
-            self?.currentWindowController = nil
+        controller.doneAction = { [unowned self] controller, options, attribute in
+//            let windowController_secure = self.currentWindowController as! KMSecureEncryptWindowController
+            self.view.window?.endSheet((self.currentWindowController.window)!)
+            self.currentWindowController = nil
             
-            self?._secureOptions = windowController_secure.options
-            self?.needSave = true
-            self?.recordIsPDFDocumentEdited(type: .setPassword)
+            self._secureOptions = options
+            self.documentAttribute = attribute
+            self.needSave = true
+            self.recordIsPDFDocumentEdited(type: .setPassword)
             
-            if (result) {
-                self?.showSecureSuccessTip()
-                self?.recordSaveWatermarkFlag(type: .setPassword)
+            if (options.count != 0) {
+                self.showSecureSuccessTip()
+                self.recordSaveWatermarkFlag(type: .setPassword)
             } else {
                 let alert = NSAlert()
                 alert.messageText = NSLocalizedString("Failure", comment: "")
@@ -931,7 +973,11 @@ import Cocoa
             }
         }
         
-        self.view.window?.beginSheet(windowController.window!)
+        controller.cancelAction = { [unowned self] controller in
+            self.view.window?.endSheet((self.currentWindowController.window)!)
+            self.currentWindowController = nil
+        }
+        NSWindow.currentWindow().beginSheet(controller.window!)
     }
     
     // MARK: -
@@ -1346,6 +1392,7 @@ import Cocoa
     
     public func clearSecureOptions() {
         self._secureOptions = nil
+        self.documentAttribute
     }
     
     public func recordRemoveSecureFlag() {