KMPDFWatermarkData.swift 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. //
  2. // KMPDFWatermarkData.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by tangchao on 2023/11/2.
  6. //
  7. import Cocoa
  8. @objcMembers class KMPDFWatermarkData: NSObject {
  9. var watermarkType: CPDFWatermarkType = .text
  10. var text: String? = nil
  11. var fontName: String = ""
  12. var fontStyle: String? = ""
  13. var fontSize: CGFloat = 12
  14. var textColor: NSColor = NSColor.clear
  15. var imagePath:String? = nil
  16. var isScale: Bool = false
  17. var scale: CGFloat = 0.5
  18. var rotation: CGFloat = 0
  19. var opacity: CGFloat = 1
  20. var verticalPosition: CPDFWatermarkVerticalPosition = .center
  21. var horizontalPosition: CPDFWatermarkHorizontalPosition = .center
  22. var tx: CGFloat = 0
  23. var ty: CGFloat = 0
  24. var isFront: Bool = true
  25. var isTilePage: Bool = false
  26. var verticalSpacing: CGFloat = 60
  27. var horizontalSpacing: CGFloat = 60
  28. var tag: String = ""
  29. var watermarkName: String = ""
  30. var cPDFWatermark: CPDFWatermark?
  31. class func defaultWDData() -> KMPDFWatermarkData {
  32. let data = KMPDFWatermarkData()
  33. data.watermarkType = .text
  34. data.text = KMLocalizedString("Watermark")
  35. data.fontName = "Helvetica"
  36. data.fontStyle = "Regular"
  37. data.fontSize = 12
  38. data.textColor = NSColor(red: 205/255, green: 205/255, blue: 205/255, alpha: 1)
  39. data.imagePath = nil
  40. data.isScale = false
  41. data.scale = 0.5
  42. data.rotation = 0
  43. data.opacity = 1
  44. data.verticalPosition = .center
  45. data.horizontalPosition = .center
  46. data.tx = 0
  47. data.ty = 0
  48. data.isFront = true
  49. data.isTilePage = false
  50. data.verticalSpacing = 60
  51. data.horizontalSpacing = 60
  52. data.tag = NSString.tagString()
  53. return data
  54. }
  55. func modelPreviewImage() -> NSImage? {
  56. if let resultPath = KMWatermarkManager.defaultManager.watermarkImageFolder?.stringByAppendingPathComponent((self.tag + ".png")) {
  57. if let image = NSImage(contentsOfFile: resultPath) {
  58. return image
  59. }
  60. }
  61. return nil
  62. }
  63. func image() -> NSImage? {
  64. if let imagePath = self.imagePath {
  65. if let image = NSImage(contentsOfFile: imagePath) {
  66. return image
  67. }
  68. }
  69. return nil
  70. }
  71. //更新预览图
  72. func updatePreviewImage() {
  73. if let resultPath = KMWatermarkManager.defaultManager.watermarkImageFolder?.stringByAppendingPathComponent((self.tag + ".png")) {
  74. if let image = KMWatermarkManager.defaultManager.drawImageAtpageRect(rect: CGRectMake(0, 0, 136, 181), data: self) {
  75. try?image.pngData()?.write(to: URL(fileURLWithPath: resultPath))
  76. }
  77. }
  78. }
  79. class func returnWaterMarkWith(_ model: KMPDFWatermarkData, _ document: CPDFDocument) -> CPDFWatermark {
  80. var watermark = CPDFWatermark()
  81. if (model.watermarkType == .text) {
  82. watermark = CPDFWatermark(document: document, type: .text)
  83. watermark.text = model.text
  84. watermark.textColor = model.textColor
  85. watermark.cFont = CPDFFont(familyName: model.fontName, fontStyle: model.fontStyle ?? "Regular")
  86. if model.isScale {
  87. watermark.setAutoSizeWithPercentage(model.scale)
  88. } else {
  89. watermark.fontSize = model.fontSize
  90. }
  91. } else if model.watermarkType == .image {
  92. watermark = CPDFWatermark(document: document, type: .image)
  93. watermark.image = model.image()
  94. if model.isScale {
  95. watermark.scale = model.scale
  96. } else {
  97. watermark.scale = 1
  98. }
  99. }
  100. watermark.opacity = model.opacity
  101. watermark.rotation = model.rotation
  102. watermark.tx = model.tx
  103. watermark.ty = model.ty
  104. watermark.isFront = model.isFront
  105. watermark.horizontalPosition = model.horizontalPosition
  106. watermark.verticalPosition = model.verticalPosition
  107. watermark.isTilePage = model.isTilePage
  108. watermark.horizontalSpacing = model.horizontalSpacing
  109. watermark.verticalSpacing = model.verticalSpacing
  110. return watermark
  111. }
  112. func isWatermarkModelChanged() -> Bool {
  113. let defaultModel = KMWatermarkManager.defaultManager.defaultWatermarkData
  114. if self.watermarkType == .text {
  115. if self.text != defaultModel.text {
  116. return true
  117. }
  118. } else if self.watermarkType == .image {
  119. if self.imagePath != nil {
  120. return true
  121. }
  122. }
  123. if self.fontName != defaultModel.fontName {
  124. return true
  125. }
  126. if self.fontStyle != defaultModel.fontStyle {
  127. return true
  128. }
  129. if self.fontSize != defaultModel.fontSize {
  130. return true
  131. }
  132. if self.textColor != defaultModel.textColor {
  133. return true
  134. }
  135. if self.isScale {
  136. return true
  137. }
  138. if self.scale != defaultModel.scale {
  139. return true
  140. }
  141. if self.rotation != defaultModel.rotation {
  142. return true
  143. }
  144. if self.opacity != defaultModel.opacity {
  145. return true
  146. }
  147. if self.verticalPosition != defaultModel.verticalPosition {
  148. return true
  149. }
  150. if self.horizontalPosition != defaultModel.horizontalPosition {
  151. return true
  152. }
  153. if self.tx != defaultModel.tx {
  154. return true
  155. }
  156. if self.ty != defaultModel.ty {
  157. return true
  158. }
  159. if self.isFront != defaultModel.isFront {
  160. return true
  161. }
  162. if self.isTilePage != defaultModel.isTilePage {
  163. return true
  164. }
  165. if self.verticalSpacing != defaultModel.verticalSpacing {
  166. return true
  167. }
  168. if self.horizontalSpacing != defaultModel.horizontalSpacing {
  169. return true
  170. }
  171. return false
  172. }
  173. }