Browse Source

【综合】保存带水印优化

tangchao 1 year ago
parent
commit
6db7e97308

+ 53 - 0
PDF Office/PDF Master/Class/Common/Tools/KMTools.swift

@@ -203,6 +203,10 @@ import Cocoa
         }
         return uniqueFilePath
     }
+    
+    @objc class func getTempFloderPath() -> String? {
+        return NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.applicationSupportDirectory, FileManager.SearchPathDomainMask.userDomainMask, true).last?.stringByAppendingPathComponent(Bundle.main.bundleIdentifier!).stringByAppendingPathComponent("KMTemp")
+    }
 }
 
 // MARK: -
@@ -342,4 +346,53 @@ extension KMTools {
     @objc class func pageRangePlaceholderString() -> String {
         return NSLocalizedString("e.g. 1,3-5,10", comment: "")
     }
+    
+    @objc class func saveDocumentForWatermark(document: CPDFDocument, to url: URL) -> URL? {
+        // 将文档存入临时目录
+        if let data = self.getTempFloderPath(), !FileManager.default.fileExists(atPath: data) {
+            try?FileManager.default.createDirectory(atPath: data, withIntermediateDirectories: false)
+        }
+        guard let filePath = self.getTempFloderPath()?.stringByAppendingPathComponent("temp_saveDocumentForWatermark.pdf") else {
+            return nil
+        }
+        // 清除临时数据
+        if (FileManager.default.fileExists(atPath: filePath)) {
+            try?FileManager.default.removeItem(atPath: filePath)
+        }
+        
+        document.write(toFile: filePath)
+        if (!FileManager.default.fileExists(atPath: filePath)) {
+            return nil
+        }
+        
+        guard let _document = CPDFDocument(url: URL(fileURLWithPath: filePath)) else {
+            return nil
+        }
+        
+        // 添加水印
+        let watermark = CPDFWatermark(document: _document, type: .image)
+        watermark?.image = NSImage(named: "KMImageNameWatermark")
+        watermark?.horizontalPosition = .left
+        watermark?.verticalPosition = .top
+        watermark?.scale = 0.3
+        _document.addWatermark(watermark)
+        // 添加 link注释
+        for i in 0 ..< _document.pageCount {
+            let page = _document.page(at: i)
+            
+            let anno = CPDFLinkAnnotation(document: _document)
+            anno?.bounds = NSMakeRect(0, _document.page(at: 0).bounds.size.height-20, 70, 20)
+            anno?.setURL(kKMPurchaseProductURLString)
+            page?.addAnnotation(anno)
+        }
+        
+        // 保存文档
+        _document.write(to: url)
+        // 清除临时数据
+        if (FileManager.default.fileExists(atPath: filePath)) {
+            try?FileManager.default.removeItem(atPath: filePath)
+        }
+        
+        return url
+    }
 }

+ 12 - 22
PDF Office/PDF Master/Class/Document/KMMainDocument.swift

@@ -224,36 +224,26 @@ typealias KMMainDocumentCloudUploadHanddler = (@escaping(Bool, String)->()) -> (
                     
                     // isWaterMarkExport
                     if (isWaterMarkExport) {
+                        guard let _document = self.mainViewController?.document else {
+                            return
+                        }
                         DispatchQueue.main.async {
                             NSPanel.savePanel(NSApp.mainWindow!, true, panel:{ panel in
-                                panel.nameFieldStringValue = (self.mainViewController?.document?.documentURL.lastPathComponent)!
+                                panel.nameFieldStringValue = _document.documentURL.lastPathComponent
                             }) { response, url, isOpen in
                                 if (response == .cancel) {
                                     return
                                 }
+                                guard let _url = KMTools.saveDocumentForWatermark(document: _document, to: url!) else {
+                                    return
+                                }
                                 
-                                if let _document = self.mainViewController?.document {
-                                    let watermark = CPDFWatermark(document: _document, type: .image)
-                                    watermark?.image = NSImage(named: "KMImageNameWatermark")
-                                    watermark?.horizontalPosition = .left
-                                    watermark?.verticalPosition = .top
-                                    watermark?.scale = 0.3
-                                    _document.addWatermark(watermark)
-                                    
-                                    
-                                    let linkA = self.mainViewController?.listView.addAnnotation(with: .link, selection: nil, page: _document.page(at: 0), bounds: NSMakeRect(0, _document.page(at: 0).bounds.size.height-20, 70, 20))
-                                    if let _linka = linkA as? CPDFLinkAnnotation {
-                                        _linka.setURL(kKMPurchaseProductURLString)
-                                    }
-                                    
-                                    _document.write(to: url)
-                                    if (isOpen) {
-                                        NSDocumentController.shared.km_safe_openDocument(withContentsOf: url!, display: true) { _, _, _ in
-                                            
-                                        }
-                                    } else {
-                                        NSWorkspace.shared.activateFileViewerSelecting([url!])
+                                if (isOpen) {
+                                    NSDocumentController.shared.km_safe_openDocument(withContentsOf: _url, display: true) { _, _, _ in
+                                        
                                     }
+                                } else {
+                                    NSWorkspace.shared.activateFileViewerSelecting([_url])
                                 }
                             }
                         }