KMAddBackgroundView.swift 39 KB

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