Browse Source

【综合】保存带水印,兼容加解密的延时存储和文本编辑

tangchao 1 year ago
parent
commit
ee5fa8f24f

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

@@ -347,7 +347,7 @@ extension KMTools {
         return NSLocalizedString("e.g. 1,3-5,10", comment: "")
     }
     
-    @objc class func saveDocumentForWatermark(document: CPDFDocument, to url: URL) -> URL? {
+    @objc class func saveDocumentForWatermark(document: CPDFDocument, to url: URL, secureOptions: [CPDFDocumentWriteOption : Any]? = nil, removePWD: Bool = false) -> URL? {
         // 将文档存入临时目录
         if let data = self.getTempFloderPath(), !FileManager.default.fileExists(atPath: data) {
             try?FileManager.default.createDirectory(atPath: data, withIntermediateDirectories: false)
@@ -411,7 +411,13 @@ extension KMTools {
         }
         
         // 保存文档
-        _document.write(to: url)
+        if let data = secureOptions, !data.isEmpty {
+            _document.write(to: url, withOptions: data)
+        } else if (removePWD) {
+            document.writeDecrypt(to: url)
+        } else {
+            _document.write(to: url)
+        }
         // 清除临时数据
         if (FileManager.default.fileExists(atPath: filePath)) {
             try?FileManager.default.removeItem(atPath: filePath)

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

@@ -226,6 +226,8 @@ typealias KMMainDocumentCloudUploadHanddler = (@escaping(Bool, String)->()) -> (
                         guard let _document = self.mainViewController?.document else {
                             return
                         }
+                        // 提交文本编辑的内容
+                        self.mainViewController?.commitEditingIfNeed()
                         DispatchQueue.main.async {
                             NSPanel.savePanel(NSApp.mainWindow!, true, panel:{ panel in
                                 panel.nameFieldStringValue = _document.documentURL.lastPathComponent
@@ -233,7 +235,7 @@ typealias KMMainDocumentCloudUploadHanddler = (@escaping(Bool, String)->()) -> (
                                 if (response == .cancel) {
                                     return
                                 }
-                                guard let _url = KMTools.saveDocumentForWatermark(document: _document, to: url!) else {
+                                guard let _url = KMTools.saveDocumentForWatermark(document: _document, to: url!, secureOptions: self.mainViewController?.secureOptions, removePWD: self.mainViewController!.removeSecureFlag) else {
                                     return
                                 }