KMBatesPreviewController.swift 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. //
  2. // KMBatesPreviewController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by tangchao on 2022/12/28.
  6. //
  7. import Cocoa
  8. /// Bates码 预览控制器
  9. class KMBatesPreviewController: KMWatermarkAdjectivePreViewBaseController {
  10. override func viewDidLoad() {
  11. super.viewDidLoad()
  12. let itemTitles = [[NSLocalizedString("Add Bates Number", comment: ""), NSLocalizedString("Delete Bates Number", comment: "")], [NSLocalizedString("Batch", comment: "")]]
  13. var itemModels: Array<Array<KMWatermarkAdjectiveTopBarItemModel>> = []
  14. for items in itemTitles {
  15. var array: Array<KMWatermarkAdjectiveTopBarItemModel> = []
  16. for title in items {
  17. let model = KMWatermarkAdjectiveTopBarItemModel()
  18. model.iconName = ""
  19. model.itemTitle = title
  20. array.append(model)
  21. }
  22. itemModels.append(array)
  23. }
  24. self.topBarView.initItemData(itemArrays: itemModels)
  25. self.topBarView.selectTopItem(index: 0)
  26. let preView: KMWatermarkPDFView_OC = KMWatermarkPDFView_OC()
  27. self.preView = preView
  28. self.preView.frame = self.preViewBox.contentView!.bounds
  29. self.preView.autoresizingMask = [.width, .height]
  30. self.preViewBox.contentView?.addSubview(self.preView)
  31. self.preView.setDisplay(.singlePage)
  32. let controller = KMBatesPropertyHomeController()
  33. controller.view.frame = self.rightBox.contentView!.bounds
  34. controller.view.autoresizingMask = [.width, .height]
  35. self.right_gotoViewController(viewController: controller)
  36. controller.modelDidChange = { [weak self] model in
  37. if (model == nil || (model! as! KMHeaderFooterObject).hasVaild == false) {
  38. self!.topBarView.isCanApply(can: false)
  39. self?.model = nil
  40. } else {
  41. self!.topBarView.isCanApply(can: true)
  42. self!.model = model as! KMWatermarkAdjectiveBaseModel
  43. self!.model?.pageCount = Int((self?.preView.document.pageCount)!)
  44. }
  45. (self?.preView as! KMWatermarkPDFView_OC).model = model
  46. }
  47. }
  48. override func viewDidAppear() {
  49. super.viewDidAppear()
  50. (self.rightViewController as! KMBatesPropertyHomeController).pageCount = Int(self.preView.document.pageCount)
  51. }
  52. override func topItemClick(index: Int) {
  53. if (index == 0) { /// 添加
  54. return
  55. }
  56. if (index == 2) { /// 批量
  57. // KMBatchWindowController.openFile(nil, .BatesCode)
  58. return
  59. }
  60. /// 移除
  61. self.beginLoading()
  62. self.deleteBates(toPath: self.preView.document.documentURL.path) {
  63. result in
  64. self.endLoading()
  65. if (result) {
  66. DispatchQueue.main.async {
  67. self.preView.document = CPDFDocument(url: self.preView.document.documentURL)
  68. }
  69. } else {
  70. let alert = NSAlert()
  71. alert.alertStyle = .critical
  72. alert.messageText = NSLocalizedString("Failure", comment: "")
  73. alert.runModal()
  74. }
  75. }
  76. }
  77. override func applyAction() {
  78. self.view.window?.makeFirstResponder(nil)
  79. if (self.model == nil) {
  80. let alert = NSAlert()
  81. alert.alertStyle = .critical
  82. alert.messageText = NSLocalizedString("Failure", comment: "")
  83. alert.runModal()
  84. return
  85. }
  86. if ((self.model as! KMHeaderFooterObject).hasVaild == false) {
  87. let alert = NSAlert()
  88. alert.alertStyle = .critical
  89. alert.messageText = NSLocalizedString("Failure", comment: "")
  90. alert.runModal()
  91. return
  92. }
  93. self.beginLoading()
  94. self.addBates(model: self.model! as! KMHeaderFooterObject, toPath: self.preView.document.documentURL.path, completion: {
  95. result in
  96. DispatchQueue.main.async {
  97. self.preView.layoutDocumentView()
  98. self.preView.setNeedsDisplayForVisiblePages()
  99. }
  100. self.endLoading()
  101. if (result) {
  102. guard let callback = self.itemClick else {
  103. return
  104. }
  105. callback(1, nil)
  106. } else {
  107. let alert = NSAlert()
  108. alert.alertStyle = .critical
  109. alert.messageText = NSLocalizedString("Failure", comment: "")
  110. alert.runModal()
  111. }
  112. })
  113. }
  114. private func addBates(model: KMHeaderFooterObject, toPath: String, completion: @escaping (_ result: Bool) -> ()) {
  115. DispatchQueue.global().async {
  116. let document: CPDFDocument = self.applyDocument!
  117. var property = document.bates()
  118. var fontSize = 0.0
  119. var fontName: String = ""
  120. switch self.model!.textFont {
  121. case .font(name: let name, size: let size):
  122. fontName = name
  123. fontSize = size
  124. break
  125. default:
  126. break
  127. }
  128. let font = NSFont.boldSystemFont(ofSize:fontSize)
  129. let style = NSMutableParagraphStyle()
  130. style.alignment = .center
  131. style.lineBreakMode = .byCharWrapping
  132. let size: NSSize = "text".boundingRect(with: NSSize(width: 1000, height: 1000), options: NSString.DrawingOptions(rawValue: 3), attributes: [NSAttributedString.Key.font : font, NSAttributedString.Key.paragraphStyle : style]).size
  133. property?.margin = NSEdgeInsetsMake(max(CGFloat(model.topMargin)-size.height, 0), CGFloat(model.leftMargin), max(CGFloat(model.bottomMargin)-size.height, 0), CGFloat(model.rightMargin))
  134. let strings = [model.topLeftString, model.topCenterString, model.topRightString, model.bottomLeftString, model.bottomCenterString, model.bottomRightString]
  135. var count: Int = 0
  136. var color: NSColor!
  137. switch model.textColor {
  138. case .color(red: let red, green: let green, blue: let blue, alpha: let alpha):
  139. color = NSColor(red: red, green: green, blue: blue, alpha: alpha)
  140. default:
  141. break
  142. }
  143. if (color == nil) {
  144. color = NSColor.black
  145. }
  146. for text in strings {
  147. property?.setText(text, at: UInt(count))
  148. property?.setTextColor(color, at: UInt(count))
  149. property?.setFontSize(fontSize, at: UInt(count))
  150. property?.setFontName(fontName, at: UInt(count))
  151. count += 1
  152. }
  153. let pagesString = self.findPagesString(model)
  154. if (pagesString.isEmpty) {
  155. property?.pageString = "0-\(document.pageCount-1)"
  156. } else {
  157. property?.pageString = pagesString
  158. }
  159. property?.update()
  160. /// 保存到临时路径
  161. let documentPath = NSTemporaryDirectory()
  162. let tempPath: String = "\(documentPath)/\(toPath.lastPathComponent)"
  163. if (FileManager.default.fileExists(atPath: tempPath)) {
  164. try?FileManager.default.removeItem(atPath: tempPath)
  165. }
  166. let result = document.write(to: URL(fileURLWithPath: tempPath))
  167. if (result) {
  168. if (FileManager.default.fileExists(atPath: toPath)) {
  169. try?FileManager.default.removeItem(atPath: toPath)
  170. }
  171. try?FileManager.default.moveItem(atPath: tempPath, toPath: toPath)
  172. } else {
  173. try?FileManager.default.removeItem(atPath: tempPath)
  174. }
  175. DispatchQueue.main.async {
  176. completion(result)
  177. }
  178. }
  179. }
  180. func deleteBates(toPath: String, completion: @escaping (_ result: Bool) -> ()) {
  181. if (self.preView.document.allowsPrinting == false || self.preView.document.allowsCopying == false) {
  182. let alert = NSAlert()
  183. alert.alertStyle = .critical
  184. alert.messageText = NSLocalizedString("This PDF document's user permissions does not allow modifying, content copying and printing.", comment: "")
  185. alert.runModal()
  186. completion(false)
  187. return
  188. }
  189. DispatchQueue.global().async {
  190. let document: CPDFDocument = self.preView.document
  191. var property = document.bates()
  192. property?.clear()
  193. /// 保存到临时路径
  194. let documentPath = NSTemporaryDirectory()
  195. let tempPath: String = "\(documentPath)/\(toPath.lastPathComponent)"
  196. if (FileManager.default.fileExists(atPath: tempPath)) {
  197. try?FileManager.default.removeItem(atPath: tempPath)
  198. }
  199. let result = document.write(to: URL(fileURLWithPath: tempPath))
  200. if (result) {
  201. if (FileManager.default.fileExists(atPath: toPath)) {
  202. try?FileManager.default.removeItem(atPath: toPath)
  203. }
  204. try?FileManager.default.moveItem(atPath: tempPath, toPath: toPath)
  205. } else {
  206. try?FileManager.default.removeItem(atPath: tempPath)
  207. }
  208. DispatchQueue.main.async {
  209. completion(result)
  210. }
  211. }
  212. }
  213. }