123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228 |
- //
- // KMPDFWatermarkData.swift
- // PDF Reader Pro
- //
- // Created by tangchao on 2023/11/2.
- //
- import Cocoa
-
- @objcMembers class KMPDFWatermarkData: NSObject {
-
- var watermarkType: CPDFWatermarkType = .text
-
- var text: String? = nil
- var fontName: String = ""
- var fontStyle: String? = ""
- var fontSize: CGFloat = 12
- var textColor: NSColor = NSColor.clear
-
- var imagePath:String? = nil
-
- var isScale: Bool = false
- var scale: CGFloat = 0.5
-
- var rotation: CGFloat = 0
- var opacity: CGFloat = 1
-
- var verticalPosition: CPDFWatermarkVerticalPosition = .center
- var horizontalPosition: CPDFWatermarkHorizontalPosition = .center
-
- var tx: CGFloat = 0
- var ty: CGFloat = 0
-
- var isFront: Bool = true
- var isTilePage: Bool = false
-
- var verticalSpacing: CGFloat = 60
- var horizontalSpacing: CGFloat = 60
-
- var tag: String = ""
-
- var watermarkName: String = ""
-
- var cPDFWatermark: CPDFWatermark?
-
- class func defaultWDData() -> KMPDFWatermarkData {
- let data = KMPDFWatermarkData()
- data.watermarkType = .text
- data.text = KMLocalizedString("Watermark")
-
- data.fontName = "Helvetica"
- data.fontStyle = "Regular"
- data.fontSize = 12
- data.textColor = NSColor(red: 205/255, green: 205/255, blue: 205/255, alpha: 1)
-
- data.imagePath = nil
-
- data.isScale = false
- data.scale = 0.5
-
- data.rotation = 0
- data.opacity = 1
-
- data.verticalPosition = .center
- data.horizontalPosition = .center
-
- data.tx = 0
- data.ty = 0
-
- data.isFront = true
- data.isTilePage = false
-
- data.verticalSpacing = 60
- data.horizontalSpacing = 60
-
- data.tag = NSString.tagString()
-
- return data
- }
-
- func modelPreviewImage() -> NSImage? {
- if let resultPath = KMWatermarkManager.defaultManager.watermarkImageFolder?.stringByAppendingPathComponent((self.tag + ".png")) {
- if let image = NSImage(contentsOfFile: resultPath) {
- return image
- }
- }
- return nil
- }
-
- func image() -> NSImage? {
- if let imagePath = self.imagePath {
- if let image = NSImage(contentsOfFile: imagePath) {
- return image
- }
- }
- return nil
- }
-
- //更新预览图
- func updatePreviewImage() {
- if let resultPath = KMWatermarkManager.defaultManager.watermarkImageFolder?.stringByAppendingPathComponent((self.tag + ".png")) {
- if let image = KMWatermarkManager.defaultManager.drawImageAtpageRect(rect: CGRectMake(0, 0, 136, 181), data: self) {
- try?image.pngData()?.write(to: URL(fileURLWithPath: resultPath))
- }
- }
- }
-
- class func returnWaterMarkWith(_ model: KMPDFWatermarkData, _ document: CPDFDocument) -> CPDFWatermark {
- var watermark = CPDFWatermark()
-
- if (model.watermarkType == .text) {
- watermark = CPDFWatermark(document: document, type: .text)
- watermark.text = model.text
- watermark.textColor = model.textColor
- watermark.cFont = CPDFFont(familyName: model.fontName, fontStyle: model.fontStyle ?? "Regular")
- if model.isScale {
- watermark.setAutoSizeWithPercentage(model.scale)
- } else {
- watermark.fontSize = model.fontSize
- }
- } else if model.watermarkType == .image {
- watermark = CPDFWatermark(document: document, type: .image)
- watermark.image = model.image()
-
- if model.isScale {
- watermark.scale = model.scale
- } else {
- watermark.scale = 1
- }
- }
-
- watermark.opacity = model.opacity
- watermark.rotation = model.rotation
-
- watermark.tx = model.tx
- watermark.ty = model.ty
-
- watermark.isFront = model.isFront
-
- watermark.horizontalPosition = model.horizontalPosition
- watermark.verticalPosition = model.verticalPosition
-
- watermark.isTilePage = model.isTilePage
- watermark.horizontalSpacing = model.horizontalSpacing
- watermark.verticalSpacing = model.verticalSpacing
-
- return watermark
-
- }
-
- func isWatermarkModelChanged() -> Bool {
-
- let defaultModel = KMWatermarkManager.defaultManager.defaultWatermarkData
-
- if self.watermarkType == .text {
- if self.text != defaultModel.text {
- return true
- }
- } else if self.watermarkType == .image {
- if self.imagePath != nil {
- return true
- }
- }
-
- if self.fontName != defaultModel.fontName {
- return true
- }
-
- if self.fontStyle != defaultModel.fontStyle {
- return true
- }
-
- if self.fontSize != defaultModel.fontSize {
- return true
- }
-
- if self.textColor != defaultModel.textColor {
- return true
- }
-
- if self.isScale {
- return true
- }
-
- if self.scale != defaultModel.scale {
- return true
- }
-
- if self.rotation != defaultModel.rotation {
- return true
- }
-
- if self.opacity != defaultModel.opacity {
- return true
- }
-
- if self.verticalPosition != defaultModel.verticalPosition {
- return true
- }
-
- if self.horizontalPosition != defaultModel.horizontalPosition {
- return true
- }
-
- if self.tx != defaultModel.tx {
- return true
- }
-
- if self.ty != defaultModel.ty {
- return true
- }
-
- if self.isFront != defaultModel.isFront {
- return true
- }
- if self.isTilePage != defaultModel.isTilePage {
- return true
- }
- if self.verticalSpacing != defaultModel.verticalSpacing {
- return true
- }
- if self.horizontalSpacing != defaultModel.horizontalSpacing {
- return true
- }
-
- return false
- }
- }
|