KMPDFRedactViewController.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. //
  2. // KMPDFRedactViewController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by tangchao on 2023/12/18.
  6. //
  7. import Cocoa
  8. class KMPDFRedactViewController: NSViewController {
  9. var scaleFactor: CGFloat = 0
  10. var callback: ((Bool, Int, Bool, URL?)->Void)?
  11. var titleBack: ((String)->Void)?
  12. @IBOutlet var redactPdfView: KMRedactPDFView!
  13. @IBOutlet weak var redactTipLabel: NSTextField!
  14. @IBOutlet weak var propertiesLabel: NSTextField!
  15. @IBOutlet weak var redactTextLabel: NSTextField!
  16. @IBOutlet weak var exitBox: KMBox!
  17. @IBOutlet weak var applyBox: KMBox!
  18. @IBOutlet weak var exitButton: NSButton!
  19. @IBOutlet weak var applyButton: NSButton!
  20. @IBOutlet weak var toolView: NSView!
  21. @IBOutlet weak var redactTextBox: NSBox!
  22. @IBOutlet weak var propertiesBox: NSBox!
  23. @IBOutlet weak var redactTextButton: KMCustomButton!
  24. @IBOutlet weak var propertiesButton: KMCustomButton!
  25. private var _url: URL?
  26. private var url: URL? {
  27. get {
  28. return self._url
  29. }
  30. }
  31. private var _password: String?
  32. private var _indicator: NSProgressIndicator?
  33. deinit {
  34. KMPrint("KMPDFRedactViewController deinit.")
  35. NotificationCenter.default.removeObserver(self)
  36. self.redactPdfView.delegate = nil
  37. }
  38. convenience init(url: URL, password: String?) {
  39. self.init()
  40. self._url = url
  41. self._password = password
  42. }
  43. override func loadView() {
  44. super.loadView()
  45. self.propertiesBox.wantsLayer = true
  46. self.redactTextBox.wantsLayer = true
  47. self.redactTextBox.fillColor = KMAppearance.Layout.w30Color()
  48. self.redactTextBox.layer?.cornerRadius = 6
  49. self.redactTextLabel.textColor = KMAppearance.Layout.w0Color()
  50. self.propertiesLabel.textColor = KMAppearance.Layout.w0Color()
  51. self.redactTextLabel.stringValue = KMLocalizedString("Redact Text", nil)
  52. self.propertiesLabel.stringValue = KMLocalizedString("Properties", nil)
  53. self.propertiesButton.mouseMoveCallback = { [unowned self] mouseEntered in
  54. self.propertiesBox.layer?.cornerRadius = 6
  55. if mouseEntered {
  56. self.propertiesBox.fillColor = KMAppearance.Layout.w30Color()
  57. } else {
  58. self.propertiesBox.fillColor = .clear
  59. }
  60. }
  61. // Do view setup here.
  62. self.redactTipLabel.stringValue = KMLocalizedString("You are under redact mode", nil)
  63. self.applyButton.title = KMLocalizedString("Apply", nil)
  64. self.exitButton.toolTip = KMLocalizedString("Exit", nil)
  65. self.exitBox.fillColor = .clear
  66. self.exitBox.borderColor = KMAppearance.Layout.w70Color()
  67. self.exitBox.borderWidth = 1.0
  68. self.exitButton.title = KMLocalizedString("Exit", nil)
  69. self.exitButton.setTitleColor(KMAppearance.Layout.w0Color())
  70. self.applyButton.setTitleColor(KMAppearance.Interactive.a0Color())
  71. self.applyButton.wantsLayer = true
  72. self.applyBox.downCallback = { [unowned self] downEntered, mouseBox, event in
  73. if downEntered {
  74. self.apply_button(nil)
  75. }
  76. }
  77. self.exitBox.downCallback = { [unowned self] downEntered, mouseBox, event in
  78. if downEntered {
  79. self.exit_button(nil)
  80. }
  81. }
  82. self.toolView.wantsLayer = true
  83. self.toolView.layer?.backgroundColor = KMAppearance.Interactive.a0Color().cgColor
  84. let document = CPDFDocument(url: self.url!)
  85. if let pwd = self._password, pwd.isEmpty == false {
  86. document?.unlock(withPassword: pwd)
  87. }
  88. self.redactPdfView.document = document
  89. // self.redactPdfView.scaleFactor = self.scaleFactor;
  90. self.redactPdfView.autoScales = true
  91. self.redactPdfView.delegate = self
  92. // self.redactPdfView.toolMode = .redactToolMode
  93. self.redactPdfView.operationType = .redact
  94. // [self.redactPdfView layoutDocumentView];
  95. let frame = CGRectMake((self.redactPdfView.frame.size.width-32)/2.0, (self.redactPdfView.frame.size.height-32)/2.0, 32, 32)
  96. self._indicator = NSProgressIndicator(frame: frame)
  97. self._indicator?.autoresizingMask = [.minXMargin, .maxXMargin, .minYMargin, .maxYMargin]
  98. self._indicator?.style = .spinning
  99. self._indicator?.controlSize = .regular
  100. self._indicator?.isIndeterminate = true
  101. self._indicator?.startAnimation(nil)
  102. self.redactPdfView.addSubview(self._indicator!)
  103. self._indicator?.isHidden = true
  104. }
  105. override func viewDidLoad() {
  106. super.viewDidLoad()
  107. NotificationCenter.default.addObserver(self, selector: #selector(showCurrentRedactAnnotationProperties), name: KMRedactPDFView.showCurrentRedactAnnotationNotificationName, object: nil)
  108. NotificationCenter.default.addObserver(self, selector: #selector(apply), name: KMRedactPDFView.redactAnnotationApplyNotificationName, object: nil)
  109. NotificationCenter.default.addObserver(self, selector: #selector(across), name: KMRedactPDFView.redactAnnotationAcrossNotificationName, object: nil)
  110. }
  111. func setCurrentPageIndex(_ currentPageIndex: Int) {
  112. self.redactPdfView.go(toPageIndex: currentPageIndex, animated: false)
  113. }
  114. // MARK: - Button Actions
  115. @IBAction func showProperties_button(_ sender: AnyObject?) {
  116. let properties = KMRedactPropertiesWindowController()
  117. self.km_beginSheet(windowC: properties)
  118. properties.callback = { annotation in
  119. }
  120. }
  121. @IBAction func apply_button(_ sender: AnyObject?) {
  122. var isApply = false
  123. for i in 0 ..< self.redactPdfView.document.pageCount {
  124. let page = self.redactPdfView.document.page(at: i)
  125. for anno in page?.annotations ?? [] {
  126. if anno.isKind(of: CPDFRedactAnnotation.self) {
  127. isApply = true
  128. }
  129. }
  130. }
  131. if(self.redactPdfView.newAddAnnotation.count == 0 && !isApply) {
  132. return
  133. }
  134. let returnCode = KMAlertTool.runModelForMainThread_r(message: "", informative: KMLocalizedString("This will permanently remove the redacted information from this document. Once you save this document, you won’t be able to retrieve the redacted information.", nil), buttons: [KMLocalizedString("Apply", nil), KMLocalizedString("Cancel", nil)])
  135. if returnCode == .alertFirstButtonReturn {
  136. self.saveAsPath()
  137. }
  138. }
  139. @IBAction func exit_button(_ sender: AnyObject?) {
  140. guard let block = self.callback else {
  141. return
  142. }
  143. block(false, self.redactPdfView.currentPageIndex, false, nil)
  144. }
  145. func saveAsPath() {
  146. let saveAccessCtr = KMSavePanelAccessoryController()
  147. var fileName = self.redactPdfView.document.documentURL.deletingPathExtension().lastPathComponent
  148. fileName = String(format: "%@_%@.pdf", fileName, "Redact")
  149. let outputSavePanel = NSSavePanel()
  150. outputSavePanel.allowedFileTypes = ["pdf"]
  151. outputSavePanel.nameFieldStringValue = fileName
  152. outputSavePanel.accessoryView = saveAccessCtr.view
  153. outputSavePanel.beginSheetModal(for: NSApp.mainWindow!) { result in
  154. if result == .OK {
  155. self.redactPdfView.document.applyRedactions()
  156. self._indicator?.isHidden = false
  157. self._indicator?.startAnimation(nil)
  158. let savePDFPath = outputSavePanel.url!.path
  159. let isSuccess = self.redactPdfView.document.write(toFile: savePDFPath)
  160. if (isSuccess) {
  161. // [self.redactPdfView.newAddAnnotation removeAllObjects];
  162. if (saveAccessCtr.openAutomaticButton.state == .on) {
  163. NSDocumentController.shared.km_safe_openDocument(withContentsOf: outputSavePanel.url!, display: true) { _, _, _ in
  164. }
  165. } else {
  166. KMTools.viewFile(at: savePDFPath)
  167. }
  168. }
  169. self._indicator?.stopAnimation(nil)
  170. self._indicator?.isHidden = true
  171. guard let block = self.callback else {
  172. return
  173. }
  174. block(true, self.redactPdfView.currentPageIndex, false, outputSavePanel.url)
  175. }
  176. }
  177. }
  178. // MARK: - Button notice
  179. @objc func showCurrentRedactAnnotationProperties(_ notice: NSNotification) {
  180. guard let pdfview = notice.object as? KMRedactPDFView else {
  181. return
  182. }
  183. if(pdfview == self.redactPdfView) {
  184. let properties = KMRedactPropertiesWindowController()
  185. properties.readactAnnotation = self.redactPdfView.currentAnnotation
  186. // __block KMPDFRedactViewController *weak_self = self;
  187. self.km_beginSheet(windowC: properties)
  188. properties.callback = { [unowned self] annotation in
  189. if let page = self.redactPdfView.currentAnnotation?.page {
  190. if let anno = annotation {
  191. self.redactPdfView.newAddAnnotation.append(anno)
  192. }
  193. self.redactPdfView.setNeedsDisplayAnnotationViewFor(page)
  194. }
  195. }
  196. }
  197. }
  198. @objc func apply(_ notice: NSNotification) {
  199. guard let pdfview = notice.object as? KMRedactPDFView else {
  200. return
  201. }
  202. if(pdfview == self.redactPdfView) {
  203. self.apply_button(nil)
  204. }
  205. }
  206. @objc func across(_ notice: NSNotification) {
  207. guard let pdfview = notice.object as? KMRedactPDFView else {
  208. return
  209. }
  210. if(pdfview == self.redactPdfView) {
  211. let pagesWindowController = KMRedactSelectPagesWindowController(document: self.redactPdfView.document)
  212. self.km_beginSheet(windowC: pagesWindowController)
  213. pagesWindowController.callback = { [unowned self] pages in
  214. if pages.count > 0 {
  215. self.redactPdfView.acrossAddAnnotations(pages)
  216. }
  217. }
  218. }
  219. }
  220. }
  221. extension KMPDFRedactViewController: CPDFViewDelegate {
  222. func pdfViewCurrentPageDidChanged(_ pdfView: CPDFView!) {
  223. let fileName = pdfView.document.documentURL.deletingLastPathComponent().lastPathComponent
  224. let title = String(format: KMLocalizedString("%@ (page %ld / %ld)", "Window title format"), fileName, pdfView.currentPageIndex+1, pdfView.document.pageCount)
  225. if(self.titleBack != nil) {
  226. self.titleBack!(title)
  227. }
  228. }
  229. }