|
@@ -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
|
|
|
+ }
|
|
|
}
|