KMWatermarkViewController.swift 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. //
  2. // KMWatermarkViewController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by tangchao on 2022/12/15.
  6. //
  7. import Cocoa
  8. /// 水印 预览控制器
  9. class KMWatermarkViewController: KMWatermarkAdjectivePreViewBaseController {
  10. var createController: KMWatermarkPropertyCreateController!
  11. var propertyController: KMWatermarkPropertyController!
  12. var editController: KMWatermarkPropertyCreateController!
  13. override func viewDidLoad() {
  14. super.viewDidLoad()
  15. let itemTitles = [[NSLocalizedString("Add Watermark", comment: ""), NSLocalizedString("Delete Watermark", comment: "")], [NSLocalizedString("Batch", comment: "")]]
  16. var itemModels: Array<Array<KMWatermarkAdjectiveTopBarItemModel>> = []
  17. for items in itemTitles {
  18. var array: Array<KMWatermarkAdjectiveTopBarItemModel> = []
  19. for title in items {
  20. let model = KMWatermarkAdjectiveTopBarItemModel()
  21. model.iconName = ""
  22. model.itemTitle = title
  23. array.append(model)
  24. }
  25. itemModels.append(array)
  26. }
  27. self.topBarView.initItemData(itemArrays: itemModels)
  28. self.topBarView.selectTopItem(index: 0)
  29. let preView: KMWatermarkPDFView_OC = KMWatermarkPDFView_OC()
  30. self.preView = preView
  31. self.preView.frame = self.preViewBox.contentView!.bounds
  32. self.preView.autoresizingMask = [.width, .height]
  33. self.preViewBox.contentView?.addSubview(self.preView)
  34. self.preView.setDisplay(.singlePage)
  35. let controller = KMWatermarkPropertyHomeController()
  36. controller.preView = self.preView
  37. controller.view.frame = self.rightBox.contentView!.bounds
  38. controller.view.autoresizingMask = [.width, .height]
  39. self.right_gotoViewController(viewController: controller)
  40. controller.modelDidChange = { [weak self] model in
  41. if (model == nil) {
  42. self!.topBarView.isCanApply(can: false)
  43. } else {
  44. self!.topBarView.isCanApply(can: true)
  45. }
  46. self!.model = model
  47. (self?.preView as! KMWatermarkPDFView_OC).model = model as Any
  48. }
  49. }
  50. override func viewDidAppear() {
  51. super.viewDidAppear()
  52. (self.rightViewController as! KMWatermarkPropertyHomeController).pageCount = Int(self.preView.document.pageCount)
  53. }
  54. override func applyAction() {
  55. super.applyAction()
  56. self.beginLoading()
  57. let preView: KMWatermarkPDFView_OC = self.preView as! KMWatermarkPDFView_OC
  58. self.addWatermark(model: preView.model as! KMWatermarkModel, toPath: self.preView.document.documentURL.path) {
  59. result in
  60. self.endLoading()
  61. DispatchQueue.main.async {
  62. self.preView.layoutDocumentView()
  63. self.preView.setNeedsDisplayForVisiblePages()
  64. if (result) {
  65. guard let callback = self.itemClick else {
  66. return
  67. }
  68. callback(1, nil)
  69. } else {
  70. let alert = NSAlert()
  71. alert.alertStyle = .critical
  72. alert.messageText = NSLocalizedString("Failure", comment: "")
  73. alert.runModal()
  74. }
  75. }
  76. }
  77. }
  78. override func topItemClick(index: Int) {
  79. super.topItemClick(index: index)
  80. if (index == 0) { /// 添加
  81. return
  82. }
  83. if (index == 2) { /// 批量
  84. // KMBatchWindowController.openFile(nil, .Watermark)
  85. return
  86. }
  87. /// 移除水印
  88. let watermarks = self.preView.document.watermarks()
  89. if (watermarks == nil || watermarks!.count <= 0) {
  90. let alert = NSAlert()
  91. alert.alertStyle = .warning
  92. alert.messageText = NSLocalizedString("PDFull cannot find a watermark in this file to remove.If you see a watermark,it was not added in PDFull,and cannot be detected as such?", comment: "")
  93. alert.addButton(withTitle: NSLocalizedString("OK", comment: ""))
  94. alert.runModal()
  95. return
  96. }
  97. let alert = NSAlert()
  98. alert.alertStyle = .warning
  99. alert.messageText = NSLocalizedString("Are you sure you want to remove the Watermark for”” documents?", comment: "")
  100. alert.addButton(withTitle: NSLocalizedString("Delete", comment: ""))
  101. alert.addButton(withTitle: NSLocalizedString("Cancel", comment: ""))
  102. let result = alert.runModal()
  103. if (result != .alertFirstButtonReturn) {
  104. return
  105. }
  106. self.beginLoading()
  107. self.deleteWatermarks(toPath: self.preView.document.documentURL.path) {
  108. result in
  109. DispatchQueue.main.async {
  110. self.preView.layoutDocumentView()
  111. self.preView.setNeedsDisplayForVisiblePages()
  112. }
  113. self.endLoading()
  114. }
  115. }
  116. func gotoPropertyController() {
  117. let propertyController = KMWatermarkPropertyController()
  118. propertyController.view.frame = self.rightBox.contentView!.bounds
  119. propertyController.view.autoresizingMask = NSView.AutoresizingMask(rawValue: 18)
  120. self.right_gotoViewController(viewController: propertyController)
  121. propertyController.preView = self.preView
  122. self.propertyController = propertyController
  123. self.propertyController.rightButtonClick = {
  124. [self] (index: Int)->() in
  125. /// 创建水印
  126. self.gotoPropertyCreateController()
  127. }
  128. self.propertyController.templateModelDidSelected = {
  129. (model: KMWatermarkModel) in
  130. let preView: KMWatermarkPDFView = self.preView as! KMWatermarkPDFView
  131. let templateModel: KMWatermarkTemplateModel = model as! KMWatermarkTemplateModel
  132. if (templateModel.isSelected) {
  133. preView.watermark = model
  134. self.topBarView.isCanApply(can: true)
  135. } else {
  136. preView.watermark = KMWatermarkModel()
  137. self.topBarView.isCanApply(can: false)
  138. }
  139. preView.setNeedsDisplay(preView.bounds)
  140. }
  141. self.propertyController.templateMenuItemClick = {
  142. (itemIndex: Int, model: KMWatermarkModel) in
  143. if (itemIndex == 1) {
  144. self.gotoPropertyEditController()
  145. let index: Int = self.propertyController.tabView.indexOfTabViewItem(self.propertyController.tabView.selectedTabViewItem!)
  146. self.editController.updateModel(model: model, type: index)
  147. }
  148. }
  149. }
  150. func gotoPropertyCreateController() {
  151. let watermarks = self.preView.document.watermarks()
  152. if (watermarks != nil && watermarks!.count > 0) { /// /// 替换水印
  153. let alert = NSAlert()
  154. alert.alertStyle = .warning
  155. alert.messageText = NSLocalizedString("文件中已添加水印,您要替换现有的水印吗?", comment: "")
  156. alert.addButton(withTitle: NSLocalizedString("替换", comment: ""))
  157. alert.addButton(withTitle: NSLocalizedString("Cancel", comment: ""))
  158. let result = alert.runModal()
  159. if (result != .alertFirstButtonReturn) {
  160. return
  161. }
  162. /// 替换
  163. for i in 0 ..< watermarks!.count {
  164. let model = watermarks![i]
  165. self.preView.document.removeWatermark(model)
  166. }
  167. self.preView.setNeedsDisplayForVisiblePages()
  168. }
  169. self.topBarView.isCanApply(can: true)
  170. let preView: KMWatermarkPDFView = self.preView as! KMWatermarkPDFView
  171. preView.watermark = self.createWatermarkTemplate()
  172. preView.setNeedsDisplay(preView.bounds)
  173. let createController = KMWatermarkPropertyCreateController()
  174. createController.preView = preView
  175. createController.model = self.createWatermarkTemplate()
  176. createController.view.frame = self.rightBox.contentView!.bounds
  177. createController.view.autoresizingMask = NSView.AutoresizingMask(rawValue: 18)
  178. self.right_gotoViewController(viewController: createController)
  179. self.createController = createController
  180. self.createController.tabView.selectTabViewItem(at: 0)
  181. self.createController.backClick = {
  182. ()->() in
  183. self.gotoPropertyController()
  184. }
  185. self.createController.rightButtonClick = {
  186. (index: Int)->() in
  187. /// 保存模块
  188. let result = KMWatermarkManager.defaultManager.addWatermark(watermark: self.createController.model as! KMWatermarkModel)
  189. if (result) {
  190. self.gotoPropertyController()
  191. }
  192. }
  193. }
  194. func gotoPropertyEditController() {
  195. let editController = KMWatermarkPropertyCreateController()
  196. editController.isEdit = true
  197. editController.preView = self.preView
  198. editController.model = self.model
  199. editController.view.frame = self.rightBox.contentView!.bounds
  200. editController.view.autoresizingMask = NSView.AutoresizingMask(rawValue: 18)
  201. self.right_gotoViewController(viewController: editController)
  202. self.editController = editController
  203. self.editController.backClick = {
  204. self.gotoPropertyController()
  205. let result = KMWatermarkManager.defaultManager.updateWatermark(watermark: self.editController.model as! KMWatermarkModel)
  206. if (result) {
  207. self.propertyController.updateDatas()
  208. }
  209. }
  210. }
  211. func createWatermarkTemplate() -> KMWatermarkModel {
  212. let model = KMWatermarkModel()
  213. model.textFont = .font(name: "Helvetica", size: 48)
  214. model.textColor = .color(red: 0, green: 0, blue: 0, alpha: 1.0)
  215. model.opacity = 1
  216. model.pageRangeType = .all
  217. model.scale = 0.5
  218. model.verticalMode = 1
  219. model.horizontalMode = 1
  220. model.isFront = true
  221. model.tileVerticalSpace = 6
  222. model.tileHorizontalSpace = 6
  223. model.isTilePage = false
  224. model.pagesString = ""
  225. model.isFront = true
  226. model.text = NSLocalizedString("Watermark", comment: "")
  227. model.watermarkID = KMWatermarkManager.defaultManager.fetchAvailableName()
  228. return model
  229. }
  230. }