KMAddBackgroundView.swift 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021
  1. //
  2. // KMAddBackgroundView.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by lizhe on 2023/11/14.
  6. //
  7. import Cocoa
  8. enum KMBackgroundManagerType: Int {
  9. case add = 0
  10. case edit
  11. case use
  12. }
  13. typealias KMAddBackgroundViewOperateCallBack = (_ background: KMBackgroundModel, _ countType: Int) -> ()
  14. typealias KMAddBackgroundViewBatchAction = (_ view: KMAddBackgroundView, _ files: [KMFileAttribute]) -> Void
  15. typealias KMAddBackgroundViewCancelAction = (_ view: KMAddBackgroundView) -> Void
  16. class KMAddBackgroundView: KMBaseXibView, NSComboBoxDelegate {
  17. @IBOutlet weak var pdfView: KMWatermarkPDFView!
  18. @IBOutlet weak var previousButton: NSButton!
  19. @IBOutlet weak var nextButton: NSButton!
  20. @IBOutlet weak var currentPageIndexTextField: NSTextField!
  21. @IBOutlet weak var totalPageCountlabel: NSTextField!
  22. @IBOutlet weak var typeBox: NSBox!
  23. @IBOutlet weak var colorButton: NSButton!
  24. @IBOutlet weak var colorWell: NSColorWell!
  25. @IBOutlet weak var fileButton: NSButton!
  26. @IBOutlet weak var filePathLabel: NSTextField!
  27. @IBOutlet weak var browseButton: NSButton!
  28. @IBOutlet weak var ratioLabel: NSTextField!
  29. @IBOutlet weak var ratioTextField: NSTextField!
  30. @IBOutlet weak var ratioStepper: NSStepper!
  31. @IBOutlet weak var appearanceBox: NSBox!
  32. @IBOutlet weak var angleLabel: NSTextField!
  33. @IBOutlet weak var angleTextField: NSTextField!
  34. @IBOutlet weak var angleStepper: NSStepper!
  35. @IBOutlet weak var left45IndicateView: KMAngleIndicateView!
  36. @IBOutlet weak var horizontalIndicateView: KMAngleIndicateView!
  37. @IBOutlet weak var right45IndicateView: KMAngleIndicateView!
  38. @IBOutlet weak var alphaLabel: NSTextField!
  39. @IBOutlet weak var alphaSlider: NSSlider!
  40. @IBOutlet weak var alphaTextField: NSTextField!
  41. @IBOutlet weak var alphaStepper: NSStepper!
  42. @IBOutlet weak var postionView: KMPostionIndicateView!
  43. @IBOutlet weak var verticalGapLabel: NSTextField!
  44. @IBOutlet weak var verticalGapTextField: NSTextField!
  45. @IBOutlet weak var verticalStepper: NSStepper!
  46. @IBOutlet weak var horizontalGapLabel: NSTextField!
  47. @IBOutlet weak var horizontalGapTextField: NSTextField!
  48. @IBOutlet weak var horizontalStepper: NSStepper!
  49. @IBOutlet weak var pageRangeComboBox: NSComboBox!
  50. @IBOutlet weak var pageRangeLabel: NSTextField!
  51. @IBOutlet weak var saveToTemplateButton: NSButton!
  52. @IBOutlet weak var templateNameLabel: NSTextField!
  53. @IBOutlet weak var templateNameTextField: NSTextField!
  54. @IBOutlet weak var doneButton: NSButton!
  55. @IBOutlet weak var cancelButton: NSButton!
  56. @IBOutlet weak var batchButton: NSButton!
  57. var isHiddenBatchBtn: Bool = false
  58. private var workItem: DispatchWorkItem?
  59. var isAllowReloadDocument = true
  60. lazy var background: KMBackgroundModel = {
  61. var bg = KMBackgroundModel()
  62. bg.type = .color
  63. bg.color = .red
  64. bg.opacity = 1.0
  65. bg.scale = 1.0
  66. bg.verticalMode = 1
  67. bg.horizontalMode = 1
  68. bg.backgroundID = KMBackgroundManager.defaultManager.fetchAvailableName()
  69. self.initialID = bg.backgroundID
  70. bg.pagesString = ""
  71. bg.pageRangeType = .all
  72. return bg
  73. }()
  74. var originalBackground: KMBackgroundModel = KMBackgroundModel()
  75. var filePath: String = Bundle.main.path(forResource: "Quick Start Guide", ofType: "pdf") ?? ""
  76. var password: String = ""
  77. var type: KMBackgroundManagerType = .use
  78. var pdfDocument: CPDFDocument? {
  79. didSet {
  80. self._fileAttri = KMFileAttribute()
  81. self._fileAttri?.password = self.pdfDocument?.password ?? ""
  82. self._fileAttri?.filePath = self.pdfDocument?.documentURL.path ?? ""
  83. self.password = pdfDocument?.password ?? ""
  84. self.pdfView.document = pdfDocument
  85. self.reloadData()
  86. }
  87. }
  88. var backgroundType: KMBackgroundType = .color
  89. var initialID: String!
  90. var currentType: Int = 0
  91. var cancelAction: KMAddBackgroundViewCancelAction?
  92. var batchAction: KMAddBackgroundViewCancelAction?
  93. var operateCallBack: KMAddBackgroundViewOperateCallBack?
  94. var onlyManagerTemplate: Bool = true
  95. private var _fileAttri: KMFileAttribute?
  96. // MARK: - Dealloc
  97. deinit {
  98. NotificationCenter.default.removeObserver(self)
  99. }
  100. // MARK: - Init Methods
  101. convenience init?(baseFile filePath: String, background backgroundObject: KMBackgroundModel, password: String, type: KMBackgroundManagerType, fileType countType: Int) {
  102. self.init()
  103. self.filePath = filePath
  104. self.password = password
  105. self.background = backgroundObject
  106. self.originalBackground = backgroundObject
  107. self.initialID = backgroundObject.backgroundID
  108. self.type = type
  109. self.pdfDocument = CPDFDocument(url: URL(fileURLWithPath: self.filePath))
  110. if pdfDocument!.isLocked {
  111. pdfDocument!.unlock(withPassword: password)
  112. }
  113. if pdfDocument!.isLocked {
  114. return nil
  115. }
  116. }
  117. override func setup() {
  118. // pdfView.background = background
  119. // pdfView.document = pdfDocument
  120. pdfView.autoScales = true
  121. pdfView.setDisplay(.singlePage)
  122. // pdfView.documentView?.enclosingScrollView?.hasVerticalScroller = false
  123. // pdfView.documentView?.enclosingScrollView?.hasHorizontalScroller = false
  124. for i in 0..<3 {
  125. for j in 0..<3 {
  126. if i == Int(background.horizontalMode) && j == Int(background.verticalMode) {
  127. postionView.style = KMPositionIndicateViewStyle(rawValue: i + 3 * j)!
  128. }
  129. }
  130. }
  131. postionView.styleChangedCallBack = { [weak self] in
  132. guard let self = self else { return }
  133. self.background.horizontalMode = self.postionView.style.rawValue % 3
  134. self.background.verticalMode = self.postionView.style.rawValue / 3
  135. self.self.updatePDFView()
  136. if self.filePathLabel.stringValue.count > 0 {
  137. self.doneButton.isEnabled = true
  138. }
  139. }
  140. currentPageIndexTextField.stringValue = "1"
  141. // let numberFormatter = currentPageIndexTextField.formatter as! NumberFormatter
  142. // numberFormatter.maximum = NSNumber(value: pdfDocument.pageCount)
  143. let countFormatter = TextFieldFormatter.init()
  144. countFormatter.allowedCharacterSet = "-"
  145. self.angleTextField.formatter = countFormatter
  146. angleTextField.delegate = self
  147. let formatter = TextFieldFormatter.init()
  148. formatter.allowedCharacterSet = "-."
  149. self.verticalGapTextField.formatter = formatter
  150. self.verticalGapTextField.delegate = self
  151. let formatter2 = TextFieldFormatter.init()
  152. formatter2.allowedCharacterSet = "-."
  153. self.horizontalGapTextField.formatter = formatter2
  154. self.horizontalGapTextField.delegate = self
  155. let formatter3 = TextFieldFormatter.init()
  156. formatter3.allowedCharacterSet = "%"
  157. self.alphaTextField.formatter = formatter3
  158. alphaTextField.delegate = self
  159. left45IndicateView.style = .left45
  160. left45IndicateView.touchCallBack = { [weak self] in
  161. guard let self = self else { return }
  162. self.background.rotation = -45
  163. self.angleStepper.doubleValue = -45
  164. self.angleTextField.stringValue = "\(-45)"
  165. self.checkAngle()
  166. if self.filePathLabel.stringValue.count > 0 {
  167. self.doneButton.isEnabled = true
  168. }
  169. self.updatePDFView()
  170. }
  171. horizontalIndicateView.style = .horizontal
  172. horizontalIndicateView.touchCallBack = { [weak self] in
  173. guard let self = self else { return }
  174. self.background.rotation = 0
  175. self.angleStepper.doubleValue = 0
  176. self.angleTextField.stringValue = "\(0)"
  177. self.checkAngle()
  178. if self.filePathLabel.stringValue.count > 0 {
  179. self.doneButton.isEnabled = true
  180. }
  181. self.updatePDFView()
  182. }
  183. right45IndicateView.style = .right45
  184. right45IndicateView.touchCallBack = { [weak self] in
  185. guard let self = self else { return }
  186. self.background.rotation = 45
  187. self.angleStepper.doubleValue = 45
  188. self.angleTextField.stringValue = "\(45)"
  189. self.checkAngle()
  190. if self.filePathLabel.stringValue.count > 0 {
  191. self.doneButton.isEnabled = true
  192. }
  193. self.updatePDFView()
  194. }
  195. checkAngle()
  196. typeBox.titleFont = NSFont.systemFont(ofSize: 13)
  197. colorWell.color = background.color ?? NSColor.red
  198. templateNameTextField.stringValue = background.backgroundID
  199. appearanceBox.titleFont = NSFont.systemFont(ofSize: 13)
  200. saveToTemplateButton.isEnabled = onlyManagerTemplate
  201. // if type == .use {
  202. // saveToTemplateButton.isHidden = true
  203. // saveToTemplateButton.state = .off
  204. // } else {
  205. // saveToTemplateButton.isHidden = false
  206. // saveToTemplateButton.state = .on
  207. // }
  208. pageRangeComboBox.removeAllItems()
  209. pageRangeComboBox.addItems(withObjectValues: [
  210. NSLocalizedString("All Pages", comment: ""),
  211. NSLocalizedString("Odd Pages Only", comment: ""),
  212. NSLocalizedString("Even Pages Only", comment: ""),
  213. NSLocalizedString("e.g. 1,3-5,10", comment: "")
  214. ])
  215. pageRangeComboBox.placeholderString = NSLocalizedString("e.g. 1,3-5,10", comment: "")
  216. pageRangeComboBox.delegate = nil
  217. pageRangeComboBox.selectItem(at: 0)
  218. pageRangeComboBox.isEditable = false
  219. pageRangeComboBox.delegate = self
  220. colorWell.target = self
  221. colorWell.action = #selector(colorWellDidChange)
  222. }
  223. override func addNotification() {
  224. NotificationCenter.default.addObserver(self, selector: #selector(pageChangeNotification), name: NSNotification.Name.CPDFViewPageChanged, object: self.pdfView)
  225. }
  226. override func updateLanguage() {
  227. typeBox.title = NSLocalizedString("Source", comment: "")
  228. self.templateNameLabel.stringValue = NSLocalizedString("Name:", comment: "")
  229. batchButton.title = NSLocalizedString("Batch", comment: "");
  230. cancelButton.title = NSLocalizedString("Cancel", comment: "");
  231. colorButton.title = NSLocalizedString("Color", comment: "")
  232. fileButton.title = NSLocalizedString("File", comment: "")
  233. browseButton.title = NSLocalizedString("Choose...", comment: "")
  234. ratioLabel.stringValue = "\(NSLocalizedString("Ratio", comment: "")):"
  235. appearanceBox.title = NSLocalizedString("Appearance", comment: "")
  236. angleLabel.stringValue = "\(NSLocalizedString("Rotation", comment: "")):"
  237. alphaLabel.stringValue = "\(NSLocalizedString("Opacity", comment: "")):"
  238. pageRangeLabel.stringValue = "\(NSLocalizedString("Page Range", comment: "")):"
  239. horizontalGapLabel.stringValue = "X:"
  240. verticalGapLabel.stringValue = "Y:"
  241. templateNameLabel.stringValue = NSLocalizedString("Label", comment: "")
  242. saveToTemplateButton.title = NSLocalizedString("Add to Template", comment: "")
  243. if (self.type == .add) {
  244. self.doneButton.title = NSLocalizedString("Apply", comment: "");
  245. self.batchButton.isHidden = true
  246. } else if (self.type == .edit) {
  247. self.doneButton.title = NSLocalizedString("Apply", comment: "");
  248. self.batchButton.isHidden = true
  249. } else if (self.type == .use) {
  250. self.doneButton.title = NSLocalizedString("Save & Apply", comment: "");
  251. }
  252. }
  253. private func checkAngle() {
  254. left45IndicateView.isSelcted = false
  255. horizontalIndicateView.isSelcted = false
  256. right45IndicateView.isSelcted = false
  257. if background.rotation == 45 {
  258. right45IndicateView.isSelcted = true
  259. } else if background.rotation == 0 {
  260. horizontalIndicateView.isSelcted = true
  261. } else if background.rotation == -45 {
  262. left45IndicateView.isSelcted = true
  263. }
  264. }
  265. override func reloadData() {
  266. guard let pdfDocument = pdfDocument else { return }
  267. totalPageCountlabel.stringValue = "/ \(pdfDocument.pageCount)"
  268. templateNameTextField.stringValue = background.backgroundID
  269. colorWell.color = background.color ?? NSColor.red
  270. filePathLabel.stringValue = background.imagePath
  271. filePathLabel.placeholderString = NSLocalizedString("Select a File", comment: "")
  272. angleStepper.doubleValue = Double(background.rotation)
  273. angleTextField.stringValue = "\(angleStepper.intValue)"
  274. alphaSlider.doubleValue = background.opacity
  275. alphaStepper.doubleValue = background.opacity
  276. let opacity = round(background.opacity * 100) / 100
  277. alphaTextField.stringValue = "\(Int(opacity * 100))%"
  278. ratioStepper.doubleValue = background.scale
  279. ratioTextField.stringValue = "\(Int(background.scale * 100))%"
  280. if currentType == 0 {
  281. changeTypeBoxState(true)
  282. } else {
  283. changeTypeBoxState(false)
  284. }
  285. pageRangeComboBox.delegate = nil
  286. switch background.pageRangeType {
  287. case .all:
  288. pageRangeComboBox.isEditable = false
  289. pageRangeComboBox.selectItem(at: 0)
  290. case .odd:
  291. pageRangeComboBox.isEditable = false
  292. pageRangeComboBox.selectItem(at: 1)
  293. case .even:
  294. pageRangeComboBox.isEditable = false
  295. pageRangeComboBox.selectItem(at: 2)
  296. case .other:
  297. pageRangeComboBox.isEditable = true
  298. pageRangeComboBox.selectItem(at: 3)
  299. pageRangeComboBox.stringValue = background.pagesString
  300. window?.makeFirstResponder(pageRangeComboBox)
  301. }
  302. pageRangeComboBox.delegate = self
  303. verticalStepper.doubleValue = background.verticalSpace
  304. verticalGapTextField.stringValue = "\(verticalStepper.doubleValue)"
  305. horizontalStepper.doubleValue = background.horizontalSpace
  306. horizontalGapTextField.stringValue = "\(horizontalStepper.doubleValue)"
  307. batchButton.isHidden = isHiddenBatchBtn
  308. if !isHiddenBatchBtn {
  309. self.batchButton.isHidden = type != .use
  310. }
  311. }
  312. private func changeTypeBoxState(_ isColor: Bool) {
  313. if isColor {
  314. currentType = 0
  315. colorButton.state = .on
  316. colorWell.isEnabled = true
  317. fileButton.state = .off
  318. browseButton.isEnabled = false
  319. ratioTextField.isEnabled = false
  320. ratioStepper.isEnabled = false
  321. background.color = colorWell.color
  322. background.type = .color
  323. background.scale = 1
  324. doneButton.isEnabled = true
  325. } else {
  326. currentType = 1
  327. colorButton.state = .off
  328. colorWell.isEnabled = false
  329. fileButton.state = .on
  330. browseButton.isEnabled = true
  331. ratioTextField.isEnabled = true
  332. ratioStepper.isEnabled = true
  333. background.color = NSColor.white
  334. background.type = .file
  335. background.scale = ratioStepper.doubleValue
  336. doneButton.isEnabled = filePathLabel.stringValue.count > 0
  337. }
  338. self.updatePDFView()
  339. }
  340. func updatePDFView() {
  341. // pdfView.needsDisplay = true
  342. // pdfView.layoutDocumentView()
  343. // pdfView.setNeedsDisplayForVisiblePages()
  344. // // Save to temporary path
  345. // let documentPath = NSTemporaryDirectory()
  346. // let tempPath = (documentPath as NSString).appendingPathComponent((path as NSString).lastPathComponent)
  347. // try? FileManager.default.removeItem(atPath: tempPath)
  348. //
  349. // let result = document.write(to: URL(fileURLWithPath: tempPath))
  350. // if result {
  351. // if FileManager.default.fileExists(atPath: path) {
  352. // try? FileManager.default.removeItem(atPath: path)
  353. // }
  354. // try? FileManager.default.moveItem(atPath: tempPath, toPath: path)
  355. // } else {
  356. // try? FileManager.default.removeItem(atPath: tempPath)
  357. // }
  358. // 如果前一次调用还在执行,则取消它
  359. workItem?.cancel()
  360. // 创建一个新的 DispatchWorkItem
  361. let newWorkItem = DispatchWorkItem {
  362. // 在这里执行你的方法的实际逻辑
  363. self.reloadDocument()
  364. }
  365. // 将新的 DispatchWorkItem 分配给实例变量
  366. workItem = newWorkItem
  367. // 在队列中执行 DispatchWorkItem
  368. DispatchQueue.main.async(execute: newWorkItem)
  369. }
  370. func reloadDocument() {
  371. if !self.isAllowReloadDocument {
  372. return
  373. }
  374. self.isAllowReloadDocument = false
  375. let document = CPDFDocument(url: self.pdfDocument?.documentURL)
  376. guard let document = document else { return }
  377. document.unlock(withPassword: self.pdfDocument?.password)
  378. let tBackground: CPDFBackground = document.background()
  379. tBackground.opacity = background.opacity
  380. tBackground.scale = background.scale
  381. tBackground.rotation = CGFloat(background.rotation)
  382. tBackground.horizontalAlignment = UInt(background.horizontalMode)
  383. tBackground.verticalAlignment = UInt(background.verticalMode)
  384. tBackground.xOffset = background.horizontalSpace
  385. tBackground.yOffset = background.verticalSpace
  386. if let color = background.color {
  387. tBackground.color = color
  388. tBackground.type = .color
  389. }
  390. if background.imagePath.count != 0 {
  391. if let data = background.image {
  392. tBackground.setImage(data)
  393. } else {
  394. if let image = NSImage(contentsOfFile: background.imagePath) {
  395. tBackground.setImage(image)
  396. }
  397. }
  398. tBackground.type = .image
  399. }
  400. if background.pagesString.count != 0 {
  401. tBackground.pageString = background.pagesString
  402. } else {
  403. let pageString = "0-\(document.pageCount - 1)"
  404. tBackground.pageString = pageString
  405. }
  406. tBackground.update()
  407. self.pdfView.document = document
  408. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.1) {
  409. self.isAllowReloadDocument = true
  410. }
  411. }
  412. // Other methods...
  413. @objc func controlTextDidEndEditing(_ notification: Notification) {
  414. guard let textField = notification.object as? NSTextField else { return }
  415. switch textField {
  416. case ratioTextField:
  417. let formatter = textField.formatter as? NumberFormatter
  418. if let floatValue = formatter?.number(from: textField.stringValue)?.floatValue {
  419. ratioStepper.doubleValue = Double(floatValue)
  420. background.scale = Double(floatValue)
  421. self.updatePDFView()
  422. }
  423. case angleTextField:
  424. if let integerValue = Int(textField.stringValue) {
  425. background.rotation = CGFloat(integerValue)
  426. angleStepper.doubleValue = Double(background.rotation)
  427. checkAngle()
  428. self.updatePDFView()
  429. }
  430. case alphaTextField:
  431. let floatValue = textField.stringValue.replacingOccurrences(of: "%", with: "").stringToCGFloat() * 0.01
  432. alphaSlider.doubleValue = Double(floatValue)
  433. alphaStepper.doubleValue = Double(floatValue)
  434. background.opacity = Double(floatValue)
  435. self.updatePDFView()
  436. case pageRangeComboBox:
  437. if pageRangeComboBox.indexOfSelectedItem == -1 {
  438. if !checkPageRangeValidate(pageRangeComboBox.stringValue) {
  439. let alert = NSAlert()
  440. alert.alertStyle = .critical
  441. alert.messageText = "\(pdfDocument!.documentURL.lastPathComponent) \(NSLocalizedString("Invalid page range or the page number is out of range. Please try again.", comment: ""))"
  442. alert.runModal()
  443. window?.makeFirstResponder(pageRangeComboBox)
  444. return
  445. } else {
  446. background.pagesString = pageRangeComboBox.stringValue
  447. self.updatePDFView()
  448. }
  449. }
  450. case verticalGapTextField:
  451. if let integerValue = Int(verticalGapTextField.stringValue) {
  452. background.verticalSpace = CGFloat(integerValue)
  453. verticalStepper.doubleValue = background.verticalSpace
  454. self.updatePDFView()
  455. }
  456. case horizontalGapTextField:
  457. if let integerValue = Int(horizontalGapTextField.stringValue) {
  458. background.horizontalSpace = CGFloat(integerValue)
  459. horizontalStepper.doubleValue = background.horizontalSpace
  460. self.updatePDFView()
  461. }
  462. case currentPageIndexTextField:
  463. if let pageIndex = Int(currentPageIndexTextField.stringValue), let page = pdfDocument!.page(at: UInt(pageIndex - 1)) {
  464. pdfView.go(to: page)
  465. }
  466. default:
  467. break
  468. }
  469. if filePathLabel.stringValue.count > 0 {
  470. doneButton.isEnabled = true
  471. }
  472. }
  473. @objc func comboBoxSelectionDidChange(_ notification: Notification) {
  474. guard notification.object as? NSComboBox == pageRangeComboBox else { return }
  475. pageRangeComboBox.isEditable = false
  476. switch pageRangeComboBox.indexOfSelectedItem {
  477. case 0:
  478. background.pageRangeType = .all
  479. case 1:
  480. background.pageRangeType = .odd
  481. case 2:
  482. background.pageRangeType = .even
  483. default:
  484. background.pageRangeType = .other
  485. pageRangeComboBox.stringValue = ""
  486. pageRangeComboBox.isEditable = true
  487. window?.makeFirstResponder(pageRangeComboBox)
  488. }
  489. if filePathLabel.stringValue.count > 0 {
  490. doneButton.isEnabled = true
  491. }
  492. }
  493. func checkPageRangeValidate(_ pageRangeString: String) -> Bool {
  494. var fileAttribute = self._fileAttri
  495. if fileAttribute == nil {
  496. fileAttribute = KMFileAttribute()
  497. fileAttribute?.password = self.pdfDocument?.password ?? ""
  498. fileAttribute?.filePath = self.pdfDocument?.documentURL.path ?? ""
  499. self._fileAttri = fileAttribute
  500. }
  501. // fileAttribute?.filePath = pdfDocument!.documentURL.path
  502. fileAttribute?.bAllPage = false
  503. fileAttribute?.pagesString = pageRangeComboBox.stringValue
  504. var pageRange: KMPageRange = .all
  505. let pageRangeType: KMWatermarkeModelPageRangeType = background.pageRangeType
  506. if pageRangeType == .all {
  507. pageRange = .all
  508. } else if pageRangeType == .even {
  509. pageRange = .even
  510. } else if pageRangeType == .odd {
  511. pageRange = .odd
  512. } else if pageRangeType == .other {
  513. pageRange = .custom
  514. }
  515. fileAttribute?.pagesType = pageRange
  516. return fileAttribute!.fetchSelectPages().count != 0
  517. }
  518. func saveAsPDF(with background: KMBackgroundModel, to path: String, autoOpen: Bool) {
  519. self.window?.makeFirstResponder(nil)
  520. DispatchQueue.global(qos: .default).async { [unowned self] in
  521. var filePath = self.pdfDocument!.documentURL?.path
  522. let password = self.password
  523. if filePath == nil {
  524. let writeSuccess = self.pdfDocument!.write(to: URL(fileURLWithPath: kNewDocumentTempSavePath(NSLocalizedString("Untitled", comment: ""))))
  525. if writeSuccess {
  526. self.pdfDocument = CPDFDocument(url: URL(fileURLWithPath: kNewDocumentTempSavePath(NSLocalizedString("Untitled", comment: ""))))!
  527. filePath = self.pdfDocument!.documentURL?.path
  528. }
  529. }
  530. let document = CPDFDocument(url: URL(fileURLWithPath: filePath!))!
  531. document.unlock(withPassword: password)
  532. let tBackground: CPDFBackground = document.background()
  533. tBackground.opacity = background.opacity
  534. tBackground.scale = background.scale
  535. tBackground.rotation = CGFloat(background.rotation)
  536. tBackground.horizontalAlignment = UInt(background.horizontalMode)
  537. tBackground.verticalAlignment = UInt(background.verticalMode)
  538. tBackground.xOffset = background.horizontalSpace
  539. tBackground.yOffset = background.verticalSpace
  540. if let color = background.color {
  541. tBackground.color = color
  542. tBackground.type = .color
  543. } else if background.imagePath.count != 0 {
  544. let image = NSImage(contentsOfFile: background.imagePath)!
  545. tBackground.setImage(image)
  546. tBackground.type = .image
  547. }
  548. if background.pagesString.count != 0 {
  549. tBackground.pageString = background.pagesString
  550. } else {
  551. let pageString = "0-\(document.pageCount - 1)"
  552. tBackground.pageString = pageString
  553. }
  554. tBackground.update()
  555. // Save to temporary path
  556. let documentPath = NSTemporaryDirectory()
  557. let tempPath = (documentPath as NSString).appendingPathComponent((path as NSString).lastPathComponent)
  558. try? FileManager.default.removeItem(atPath: tempPath)
  559. let result = document.write(to: URL(fileURLWithPath: tempPath))
  560. if result {
  561. if FileManager.default.fileExists(atPath: path) {
  562. try? FileManager.default.removeItem(atPath: path)
  563. }
  564. try? FileManager.default.moveItem(atPath: tempPath, toPath: path)
  565. } else {
  566. try? FileManager.default.removeItem(atPath: tempPath)
  567. }
  568. if result {
  569. DispatchQueue.main.async {
  570. let needSave = self.saveToTemplateButton.state == .on
  571. if needSave {
  572. if self.checkPageRangeValidate(self.pageRangeComboBox.stringValue) && self.pageRangeComboBox.indexOfSelectedItem == -1 {
  573. self.background.pagesString = self.pageRangeComboBox.stringValue
  574. }
  575. KMBackgroundManager.defaultManager.addTemplate(model: self.background)
  576. NotificationCenter.default.post(name: NSNotification.Name("KMBatchOperateWatermarksNotification"), object: self)
  577. }
  578. self.cancelAction?(self)
  579. if autoOpen {
  580. NSDocumentController.shared.openDocument(withContentsOf: URL(fileURLWithPath: path), display: true) { _, _, _ in }
  581. } else {
  582. NSWorkspace.shared.selectFile(path, inFileViewerRootedAtPath: "")
  583. }
  584. }
  585. }
  586. }
  587. }
  588. static func saveAsPDFRemoveAllBackground(PDFDocument: CPDFDocument, password: String?, toPath path: String, completionHandler handler: ((Int) -> Void)?) {
  589. DispatchQueue.global(qos: .default).async {
  590. guard let filePath = PDFDocument.documentURL?.path else {
  591. return
  592. }
  593. let document = CPDFDocument(url: URL(fileURLWithPath: filePath))
  594. if let password = password {
  595. document?.unlock(withPassword: password)
  596. }
  597. let tBackground: CPDFBackground = document!.background()
  598. tBackground.clear()
  599. // Save to a temporary path
  600. let documentPath = NSTemporaryDirectory()
  601. let tempPath = (documentPath as NSString).appendingPathComponent((path as NSString).lastPathComponent)
  602. if FileManager.default.fileExists(atPath: tempPath) {
  603. try? FileManager.default.removeItem(atPath: tempPath)
  604. }
  605. if let result = document?.write(to: URL(fileURLWithPath: tempPath)) {
  606. if FileManager.default.fileExists(atPath: path) {
  607. try? FileManager.default.removeItem(atPath: path)
  608. }
  609. try? FileManager.default.moveItem(atPath: tempPath, toPath: path)
  610. DispatchQueue.main.async {
  611. handler?(1)
  612. }
  613. } else {
  614. try? FileManager.default.removeItem(atPath: tempPath)
  615. DispatchQueue.main.async {
  616. handler?(0)
  617. }
  618. }
  619. }
  620. }
  621. func kNewDocumentTempSavePath(_ fileName: String) -> String {
  622. let searchPath = NSSearchPathForDirectoriesInDomains(.applicationSupportDirectory, .userDomainMask, true).last
  623. let append1 = searchPath?.stringByAppendingPathComponent(Bundle.main.bundleIdentifier!)
  624. let append2 = append1!.stringByAppendingPathComponent(String(format: "%@", fileName))
  625. return append2
  626. }
  627. func isDamageImage(_ image: NSImage, imagePath path: String) -> Bool {
  628. let addImageAnnotation = (NSSearchPathForDirectoriesInDomains(.applicationSupportDirectory, .userDomainMask, true).last! as NSString).appendingPathComponent(Bundle.main.bundleIdentifier!).stringByAppendingPathComponent("addImageAnnotation")
  629. if !FileManager.default.fileExists(atPath: addImageAnnotation) {
  630. try? FileManager.default.createDirectory(atPath: addImageAnnotation, withIntermediateDirectories: false, attributes: nil)
  631. }
  632. let data = image.tiffRepresentation!
  633. let imageRep = NSBitmapImageRep(data: data)!
  634. imageRep.size = image.size
  635. var imageData: Data?
  636. if path.lowercased() == "png" {
  637. imageData = imageRep.representation(using: .png, properties: [:])
  638. } else {
  639. imageData = imageRep.representation(using: .jpeg, properties: [:])
  640. }
  641. let rPath = (addImageAnnotation as NSString).appendingPathComponent(tagString() + ".png")
  642. return ((try? imageData?.write(to: URL(fileURLWithPath: rPath), options: .atomic)) == nil)
  643. }
  644. func tagString() -> String {
  645. let dateFormatter = DateFormatter()
  646. dateFormatter.dateFormat = "yyMMddHHmmss"
  647. return String(format: "%@%04d", dateFormatter.string(from: Date()), arc4random() % 10000)
  648. }
  649. // ... (remaining methods)
  650. @objc func pageChangeNotification(notification: NSNotification) {
  651. currentPageIndexTextField.stringValue = "\(pdfView.currentPage().pageIndex() + 1)"
  652. }
  653. }
  654. extension KMAddBackgroundView {
  655. @IBAction func cancelButtonAction(_ sender: Any) {
  656. guard let callBack = cancelAction else { return }
  657. callBack(self)
  658. }
  659. @IBAction func buttonClicked_SwitchBackgroundType(_ sender: Any) {
  660. changeTypeBoxState(sender as? NSObject == colorButton)
  661. }
  662. // @IBAction func colorWellChanged(_ sender: NSColorWell) {
  663. // background.color = sender.color
  664. // self.updatePDFView()
  665. // }
  666. @objc func colorWellDidChange(_ sender: NSColorWell) {
  667. if let color = sender.color.usingColorSpaceName(NSColorSpaceName.calibratedRGB) {
  668. background.color = color
  669. self.updatePDFView()
  670. }
  671. }
  672. @IBAction func radioStepperAction(_ sender: NSStepper) {
  673. ratioTextField.stringValue = "\(Int(sender.doubleValue * 100))%"
  674. background.scale = sender.doubleValue
  675. self.updatePDFView()
  676. if filePathLabel.stringValue.count > 0 {
  677. doneButton.isEnabled = true
  678. }
  679. }
  680. @IBAction func angleStepperAction(_ sender: NSStepper) {
  681. angleTextField.stringValue = "\(sender.doubleValue)"
  682. background.rotation = CGFloat(Int(sender.doubleValue))
  683. checkAngle()
  684. self.updatePDFView()
  685. if filePathLabel.stringValue.count > 0 {
  686. doneButton.isEnabled = true
  687. }
  688. }
  689. @IBAction func alphaSliderAction(_ sender: NSSlider) {
  690. background.opacity = sender.doubleValue
  691. alphaStepper.doubleValue = sender.doubleValue
  692. alphaTextField.stringValue = "\(Int(sender.doubleValue * 100))%"
  693. if filePathLabel.stringValue.count > 0 {
  694. doneButton.isEnabled = true
  695. }
  696. self.updatePDFView()
  697. }
  698. @IBAction func verticalStepperAction(_ sender: NSStepper) {
  699. verticalGapTextField.stringValue = "\(sender.doubleValue)"
  700. background.verticalSpace = sender.doubleValue
  701. self.updatePDFView()
  702. if filePathLabel.stringValue.count > 0 {
  703. doneButton.isEnabled = true
  704. }
  705. }
  706. @IBAction func horizentalStepperAction(_ sender: NSStepper) {
  707. horizontalGapTextField.stringValue = "\(sender.doubleValue)"
  708. background.horizontalSpace = sender.doubleValue
  709. self.updatePDFView()
  710. if filePathLabel.stringValue.count > 0 {
  711. doneButton.isEnabled = true
  712. }
  713. }
  714. @IBAction func alphaSteperAction(_ sender: NSStepper) {
  715. alphaTextField.stringValue = "\(Int(sender.doubleValue * 100))%"
  716. background.opacity = sender.doubleValue
  717. alphaSlider.doubleValue = sender.doubleValue
  718. self.updatePDFView()
  719. if filePathLabel.stringValue.count > 0 {
  720. doneButton.isEnabled = true
  721. }
  722. }
  723. @IBAction func goPrevious(_ sender: Any) {
  724. if pdfView.canGoToPreviousPage() {
  725. pdfView.goToPreviousPage(nil)
  726. }
  727. // let index = pdfDocument!.index(for: pdfView.currentPage())
  728. // currentPageIndexTextField.stringValue = "\(index + 1)"
  729. }
  730. @IBAction func goNext(_ sender: Any) {
  731. if pdfView.canGoToNextPage() {
  732. pdfView.goToNextPage(nil)
  733. }
  734. // let index = pdfDocument!.index(for: pdfView.currentPage())
  735. // currentPageIndexTextField.stringValue = "\(index + 1)"
  736. }
  737. @IBAction func buttonClicked_BrowserFile(_ sender: Any) {
  738. let openPanel = NSOpenPanel()
  739. openPanel.canChooseDirectories = false
  740. openPanel.canChooseFiles = true
  741. openPanel.allowsMultipleSelection = false
  742. openPanel.allowedFileTypes = ["jpg", "cur", "bmp", "jpeg", "gif", "png", "tiff", "tif", "ico", "icns", "tga", "psd", "eps", "hdr", "jp2", "jpc", "pict", "sgi", "pdf"]
  743. openPanel.beginSheetModal(for: window!) { (result) in
  744. if result == .OK {
  745. guard let url = openPanel.url else { return }
  746. let filePath: NSString = url.path as NSString
  747. if filePath.pathExtension.lowercased() == "pdf" {
  748. let pdf = CPDFDocument(url: url)
  749. if pdf?.isEncrypted == true {
  750. return
  751. }
  752. }
  753. if let image = NSImage(contentsOfFile: url.path), !self.isDamageImage(image, imagePath: url.path) {
  754. self.filePathLabel.stringValue = url.path
  755. self.doneButton.isEnabled = true
  756. self.background.imagePath = url.path
  757. self.background.image = image
  758. self.background.backgroundID = url.path.lastPathComponent.deletingPathExtension
  759. self.templateNameTextField.stringValue = url.path.lastPathComponent.deletingPathExtension
  760. self.self.updatePDFView()
  761. }
  762. }
  763. }
  764. }
  765. // ... (remaining IBActions)
  766. @IBAction func buttonClicked_Batch(_ sender: Any) {
  767. if background.type == .color {
  768. background.imagePath = ""
  769. } else {
  770. background.color = nil
  771. if background.image == nil {
  772. return
  773. }
  774. }
  775. if templateNameTextField.stringValue.count < 1 {
  776. background.backgroundID = initialID
  777. } else {
  778. background.backgroundID = templateNameTextField.stringValue
  779. }
  780. let needSave = saveToTemplateButton.state == .on
  781. if needSave {
  782. KMBackgroundManager.defaultManager.addTemplate(model: background)
  783. }
  784. operateCallBack?(background, currentType)
  785. }
  786. @IBAction func buttonClicked_Done(_ sender: Any) {
  787. guard let pdfDocument = pdfDocument else { return }
  788. if background.type == .color {
  789. background.imagePath = ""
  790. } else {
  791. background.color = nil
  792. if background.image == nil {
  793. return
  794. }
  795. }
  796. if templateNameTextField.stringValue.isEmpty {
  797. background.backgroundID = initialID
  798. } else {
  799. background.backgroundID = templateNameTextField.stringValue
  800. }
  801. // Avoid showing the page range alert twice
  802. if checkPageRangeValidate(pageRangeComboBox.stringValue) {
  803. background.pagesString = pageRangeComboBox.stringValue
  804. self.updatePDFView()
  805. window?.makeFirstResponder(self)
  806. }
  807. let needSave = saveToTemplateButton.state == .on
  808. var pages = [Int]()
  809. switch pageRangeComboBox.indexOfSelectedItem {
  810. case 0:
  811. pages = Array(0..<Int(pdfDocument.pageCount))
  812. case 1:
  813. pages = Array(stride(from: 0, to: Int(pdfDocument.pageCount), by: 2))
  814. case 2:
  815. pages = Array(stride(from: 1, to: Int(pdfDocument.pageCount), by: 2))
  816. default:
  817. var fileAttribute = self._fileAttri
  818. if fileAttribute == nil {
  819. fileAttribute = KMFileAttribute()
  820. fileAttribute?.password = self.pdfDocument?.password ?? ""
  821. fileAttribute?.filePath = self.pdfDocument?.documentURL.path ?? ""
  822. self._fileAttri = fileAttribute
  823. }
  824. // fileAttribute.filePath = pdfDocument.documentURL?.path ?? ""
  825. fileAttribute?.bAllPage = false
  826. fileAttribute?.pagesType = .custom
  827. // fileAttribute.password = pdfDocument.password ?? ""
  828. fileAttribute?.pagesString = pageRangeComboBox.stringValue
  829. let selectPages = fileAttribute?.fetchSelectPages() ?? []
  830. if selectPages.count != 0 {
  831. pages = selectPages.map { $0 - 1 }
  832. } else {
  833. let alert = NSAlert()
  834. alert.alertStyle = .critical
  835. alert.messageText = "\(fileAttribute?.filePath.lastPathComponent ?? "") \(NSLocalizedString("Invalid page range or the page number is out of range. Please try again.", comment: ""))"
  836. alert.runModal()
  837. return
  838. }
  839. }
  840. background.pagesString = pages.isEmpty ? "" : pages.map { "\($0)" }.joined(separator: ",")
  841. switch type {
  842. case .add:
  843. if needSave {
  844. if checkPageRangeValidate(pageRangeComboBox.stringValue) && pageRangeComboBox.indexOfSelectedItem == -1 {
  845. background.pagesString = pageRangeComboBox.stringValue
  846. }
  847. KMBackgroundManager.defaultManager.addTemplate(model: background)
  848. }
  849. operateCallBack?(background, currentType)
  850. case .edit:
  851. if needSave {
  852. if checkPageRangeValidate(pageRangeComboBox.stringValue) && pageRangeComboBox.indexOfSelectedItem == -1 {
  853. background.pagesString = pageRangeComboBox.stringValue
  854. }
  855. originalBackground = (background.copy() as? KMBackgroundModel)!
  856. KMBackgroundManager.defaultManager.updateTemplate(model: originalBackground)
  857. }
  858. operateCallBack?(originalBackground, currentType)
  859. case .use:
  860. let fileName = "\(pdfDocument.documentURL?.deletingPathExtension().lastPathComponent ?? NSLocalizedString("Untitled", comment: ""))_Background"
  861. let savePanelAccessoryViewController = KMSavePanelAccessoryController()
  862. let savePanel = NSSavePanel()
  863. savePanel.nameFieldStringValue = fileName
  864. savePanel.allowedFileTypes = ["pdf"]
  865. savePanel.accessoryView = savePanelAccessoryViewController.view
  866. savePanel.beginSheetModal(for: window!) { result in
  867. if result == .OK {
  868. self.saveAsPDF(with: self.background, to: savePanel.url?.path ?? "", autoOpen: savePanelAccessoryViewController.openAutomaticButton.state == .on ? true : false)
  869. }
  870. }
  871. }
  872. }
  873. }