KMAddBackgroundView.swift 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019
  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. background.color = sender.color
  668. self.updatePDFView()
  669. }
  670. @IBAction func radioStepperAction(_ sender: NSStepper) {
  671. ratioTextField.stringValue = "\(Int(sender.doubleValue * 100))%"
  672. background.scale = sender.doubleValue
  673. self.updatePDFView()
  674. if filePathLabel.stringValue.count > 0 {
  675. doneButton.isEnabled = true
  676. }
  677. }
  678. @IBAction func angleStepperAction(_ sender: NSStepper) {
  679. angleTextField.stringValue = "\(sender.doubleValue)"
  680. background.rotation = CGFloat(Int(sender.doubleValue))
  681. checkAngle()
  682. self.updatePDFView()
  683. if filePathLabel.stringValue.count > 0 {
  684. doneButton.isEnabled = true
  685. }
  686. }
  687. @IBAction func alphaSliderAction(_ sender: NSSlider) {
  688. background.opacity = sender.doubleValue
  689. alphaStepper.doubleValue = sender.doubleValue
  690. alphaTextField.stringValue = "\(Int(sender.doubleValue * 100))%"
  691. if filePathLabel.stringValue.count > 0 {
  692. doneButton.isEnabled = true
  693. }
  694. self.updatePDFView()
  695. }
  696. @IBAction func verticalStepperAction(_ sender: NSStepper) {
  697. verticalGapTextField.stringValue = "\(sender.doubleValue)"
  698. background.verticalSpace = sender.doubleValue
  699. self.updatePDFView()
  700. if filePathLabel.stringValue.count > 0 {
  701. doneButton.isEnabled = true
  702. }
  703. }
  704. @IBAction func horizentalStepperAction(_ sender: NSStepper) {
  705. horizontalGapTextField.stringValue = "\(sender.doubleValue)"
  706. background.horizontalSpace = sender.doubleValue
  707. self.updatePDFView()
  708. if filePathLabel.stringValue.count > 0 {
  709. doneButton.isEnabled = true
  710. }
  711. }
  712. @IBAction func alphaSteperAction(_ sender: NSStepper) {
  713. alphaTextField.stringValue = "\(Int(sender.doubleValue * 100))%"
  714. background.opacity = sender.doubleValue
  715. alphaSlider.doubleValue = sender.doubleValue
  716. self.updatePDFView()
  717. if filePathLabel.stringValue.count > 0 {
  718. doneButton.isEnabled = true
  719. }
  720. }
  721. @IBAction func goPrevious(_ sender: Any) {
  722. if pdfView.canGoToPreviousPage() {
  723. pdfView.goToPreviousPage(nil)
  724. }
  725. // let index = pdfDocument!.index(for: pdfView.currentPage())
  726. // currentPageIndexTextField.stringValue = "\(index + 1)"
  727. }
  728. @IBAction func goNext(_ sender: Any) {
  729. if pdfView.canGoToNextPage() {
  730. pdfView.goToNextPage(nil)
  731. }
  732. // let index = pdfDocument!.index(for: pdfView.currentPage())
  733. // currentPageIndexTextField.stringValue = "\(index + 1)"
  734. }
  735. @IBAction func buttonClicked_BrowserFile(_ sender: Any) {
  736. let openPanel = NSOpenPanel()
  737. openPanel.canChooseDirectories = false
  738. openPanel.canChooseFiles = true
  739. openPanel.allowsMultipleSelection = false
  740. openPanel.allowedFileTypes = ["jpg", "cur", "bmp", "jpeg", "gif", "png", "tiff", "tif", "ico", "icns", "tga", "psd", "eps", "hdr", "jp2", "jpc", "pict", "sgi", "pdf"]
  741. openPanel.beginSheetModal(for: window!) { (result) in
  742. if result == .OK {
  743. guard let url = openPanel.url else { return }
  744. let filePath: NSString = url.path as NSString
  745. if filePath.pathExtension.lowercased() == "pdf" {
  746. let pdf = CPDFDocument(url: url)
  747. if pdf?.isEncrypted == true {
  748. return
  749. }
  750. }
  751. if let image = NSImage(contentsOfFile: url.path), !self.isDamageImage(image, imagePath: url.path) {
  752. self.filePathLabel.stringValue = url.path
  753. self.doneButton.isEnabled = true
  754. self.background.imagePath = url.path
  755. self.background.image = image
  756. self.background.backgroundID = url.path.lastPathComponent.deletingPathExtension
  757. self.templateNameTextField.stringValue = url.path.lastPathComponent.deletingPathExtension
  758. self.self.updatePDFView()
  759. }
  760. }
  761. }
  762. }
  763. // ... (remaining IBActions)
  764. @IBAction func buttonClicked_Batch(_ sender: Any) {
  765. if background.type == .color {
  766. background.imagePath = ""
  767. } else {
  768. background.color = nil
  769. if background.image == nil {
  770. return
  771. }
  772. }
  773. if templateNameTextField.stringValue.count < 1 {
  774. background.backgroundID = initialID
  775. } else {
  776. background.backgroundID = templateNameTextField.stringValue
  777. }
  778. let needSave = saveToTemplateButton.state == .on
  779. if needSave {
  780. KMBackgroundManager.defaultManager.addTemplate(model: background)
  781. }
  782. operateCallBack?(background, currentType)
  783. }
  784. @IBAction func buttonClicked_Done(_ sender: Any) {
  785. guard let pdfDocument = pdfDocument else { return }
  786. if background.type == .color {
  787. background.imagePath = ""
  788. } else {
  789. background.color = nil
  790. if background.image == nil {
  791. return
  792. }
  793. }
  794. if templateNameTextField.stringValue.isEmpty {
  795. background.backgroundID = initialID
  796. } else {
  797. background.backgroundID = templateNameTextField.stringValue
  798. }
  799. // Avoid showing the page range alert twice
  800. if checkPageRangeValidate(pageRangeComboBox.stringValue) {
  801. background.pagesString = pageRangeComboBox.stringValue
  802. self.updatePDFView()
  803. window?.makeFirstResponder(self)
  804. }
  805. let needSave = saveToTemplateButton.state == .on
  806. var pages = [Int]()
  807. switch pageRangeComboBox.indexOfSelectedItem {
  808. case 0:
  809. pages = Array(0..<Int(pdfDocument.pageCount))
  810. case 1:
  811. pages = Array(stride(from: 0, to: Int(pdfDocument.pageCount), by: 2))
  812. case 2:
  813. pages = Array(stride(from: 1, to: Int(pdfDocument.pageCount), by: 2))
  814. default:
  815. var fileAttribute = self._fileAttri
  816. if fileAttribute == nil {
  817. fileAttribute = KMFileAttribute()
  818. fileAttribute?.password = self.pdfDocument?.password ?? ""
  819. fileAttribute?.filePath = self.pdfDocument?.documentURL.path ?? ""
  820. self._fileAttri = fileAttribute
  821. }
  822. // fileAttribute.filePath = pdfDocument.documentURL?.path ?? ""
  823. fileAttribute?.bAllPage = false
  824. fileAttribute?.pagesType = .custom
  825. // fileAttribute.password = pdfDocument.password ?? ""
  826. fileAttribute?.pagesString = pageRangeComboBox.stringValue
  827. let selectPages = fileAttribute?.fetchSelectPages() ?? []
  828. if selectPages.count != 0 {
  829. pages = selectPages.map { $0 - 1 }
  830. } else {
  831. let alert = NSAlert()
  832. alert.alertStyle = .critical
  833. alert.messageText = "\(fileAttribute?.filePath.lastPathComponent ?? "") \(NSLocalizedString("Invalid page range or the page number is out of range. Please try again.", comment: ""))"
  834. alert.runModal()
  835. return
  836. }
  837. }
  838. background.pagesString = pages.isEmpty ? "" : pages.map { "\($0)" }.joined(separator: ",")
  839. switch type {
  840. case .add:
  841. if needSave {
  842. if checkPageRangeValidate(pageRangeComboBox.stringValue) && pageRangeComboBox.indexOfSelectedItem == -1 {
  843. background.pagesString = pageRangeComboBox.stringValue
  844. }
  845. KMBackgroundManager.defaultManager.addTemplate(model: background)
  846. }
  847. operateCallBack?(background, currentType)
  848. case .edit:
  849. if needSave {
  850. if checkPageRangeValidate(pageRangeComboBox.stringValue) && pageRangeComboBox.indexOfSelectedItem == -1 {
  851. background.pagesString = pageRangeComboBox.stringValue
  852. }
  853. originalBackground = (background.copy() as? KMBackgroundModel)!
  854. KMBackgroundManager.defaultManager.updateTemplate(model: originalBackground)
  855. }
  856. operateCallBack?(originalBackground, currentType)
  857. case .use:
  858. let fileName = "\(pdfDocument.documentURL?.deletingPathExtension().lastPathComponent ?? NSLocalizedString("Untitled", comment: ""))_Background"
  859. let savePanelAccessoryViewController = KMSavePanelAccessoryController()
  860. let savePanel = NSSavePanel()
  861. savePanel.nameFieldStringValue = fileName
  862. savePanel.allowedFileTypes = ["pdf"]
  863. savePanel.accessoryView = savePanelAccessoryViewController.view
  864. savePanel.beginSheetModal(for: window!) { result in
  865. if result == .OK {
  866. self.saveAsPDF(with: self.background, to: savePanel.url?.path ?? "", autoOpen: savePanelAccessoryViewController.openAutomaticButton.state == .on ? true : false)
  867. }
  868. }
  869. }
  870. }
  871. }