KMAddBackgroundView.swift 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022
  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: NSLocalizedString("Quick Start Guide.pdf", comment: ""), ofType: "") ?? ""
  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.pdfView.layoutDocumentView()
  410. self.isAllowReloadDocument = true
  411. }
  412. }
  413. // Other methods...
  414. @objc func controlTextDidEndEditing(_ notification: Notification) {
  415. guard let textField = notification.object as? NSTextField else { return }
  416. switch textField {
  417. case ratioTextField:
  418. let formatter = textField.formatter as? NumberFormatter
  419. if let floatValue = formatter?.number(from: textField.stringValue)?.floatValue {
  420. ratioStepper.doubleValue = Double(floatValue)
  421. background.scale = Double(floatValue)
  422. self.updatePDFView()
  423. }
  424. case angleTextField:
  425. if let integerValue = Int(textField.stringValue) {
  426. background.rotation = CGFloat(integerValue)
  427. angleStepper.doubleValue = Double(background.rotation)
  428. checkAngle()
  429. self.updatePDFView()
  430. }
  431. case alphaTextField:
  432. let floatValue = textField.stringValue.replacingOccurrences(of: "%", with: "").stringToCGFloat() * 0.01
  433. alphaSlider.doubleValue = Double(floatValue)
  434. alphaStepper.doubleValue = Double(floatValue)
  435. background.opacity = Double(floatValue)
  436. self.updatePDFView()
  437. case pageRangeComboBox:
  438. if pageRangeComboBox.indexOfSelectedItem == -1 {
  439. if !checkPageRangeValidate(pageRangeComboBox.stringValue) {
  440. let alert = NSAlert()
  441. alert.alertStyle = .critical
  442. alert.messageText = "\(pdfDocument!.documentURL.lastPathComponent) \(NSLocalizedString("Invalid page range or the page number is out of range. Please try again.", comment: ""))"
  443. alert.runModal()
  444. window?.makeFirstResponder(pageRangeComboBox)
  445. return
  446. } else {
  447. background.pagesString = pageRangeComboBox.stringValue
  448. self.updatePDFView()
  449. }
  450. }
  451. case verticalGapTextField:
  452. if let integerValue = Int(verticalGapTextField.stringValue) {
  453. background.verticalSpace = CGFloat(integerValue)
  454. verticalStepper.doubleValue = background.verticalSpace
  455. self.updatePDFView()
  456. }
  457. case horizontalGapTextField:
  458. if let integerValue = Int(horizontalGapTextField.stringValue) {
  459. background.horizontalSpace = CGFloat(integerValue)
  460. horizontalStepper.doubleValue = background.horizontalSpace
  461. self.updatePDFView()
  462. }
  463. case currentPageIndexTextField:
  464. if let pageIndex = Int(currentPageIndexTextField.stringValue), let page = pdfDocument!.page(at: UInt(pageIndex - 1)) {
  465. pdfView.go(to: page)
  466. }
  467. default:
  468. break
  469. }
  470. if filePathLabel.stringValue.count > 0 {
  471. doneButton.isEnabled = true
  472. }
  473. }
  474. @objc func comboBoxSelectionDidChange(_ notification: Notification) {
  475. guard notification.object as? NSComboBox == pageRangeComboBox else { return }
  476. pageRangeComboBox.isEditable = false
  477. switch pageRangeComboBox.indexOfSelectedItem {
  478. case 0:
  479. background.pageRangeType = .all
  480. case 1:
  481. background.pageRangeType = .odd
  482. case 2:
  483. background.pageRangeType = .even
  484. default:
  485. background.pageRangeType = .other
  486. pageRangeComboBox.stringValue = ""
  487. pageRangeComboBox.isEditable = true
  488. window?.makeFirstResponder(pageRangeComboBox)
  489. }
  490. if filePathLabel.stringValue.count > 0 {
  491. doneButton.isEnabled = true
  492. }
  493. }
  494. func checkPageRangeValidate(_ pageRangeString: String) -> Bool {
  495. var fileAttribute = self._fileAttri
  496. if fileAttribute == nil {
  497. fileAttribute = KMFileAttribute()
  498. fileAttribute?.password = self.pdfDocument?.password ?? ""
  499. fileAttribute?.filePath = self.pdfDocument?.documentURL.path ?? ""
  500. self._fileAttri = fileAttribute
  501. }
  502. // fileAttribute?.filePath = pdfDocument!.documentURL.path
  503. fileAttribute?.bAllPage = false
  504. fileAttribute?.pagesString = pageRangeComboBox.stringValue
  505. var pageRange: KMPageRange = .all
  506. let pageRangeType: KMWatermarkeModelPageRangeType = background.pageRangeType
  507. if pageRangeType == .all {
  508. pageRange = .all
  509. } else if pageRangeType == .even {
  510. pageRange = .even
  511. } else if pageRangeType == .odd {
  512. pageRange = .odd
  513. } else if pageRangeType == .other {
  514. pageRange = .custom
  515. }
  516. fileAttribute?.pagesType = pageRange
  517. return fileAttribute!.fetchSelectPages().count != 0
  518. }
  519. func saveAsPDF(with background: KMBackgroundModel, to path: String, autoOpen: Bool) {
  520. self.window?.makeFirstResponder(nil)
  521. DispatchQueue.global(qos: .default).async { [unowned self] in
  522. var filePath = self.pdfDocument!.documentURL?.path
  523. let password = self.password
  524. if filePath == nil {
  525. let writeSuccess = self.pdfDocument!.write(to: URL(fileURLWithPath: kNewDocumentTempSavePath(NSLocalizedString("Untitled", comment: ""))))
  526. if writeSuccess {
  527. self.pdfDocument = CPDFDocument(url: URL(fileURLWithPath: kNewDocumentTempSavePath(NSLocalizedString("Untitled", comment: ""))))!
  528. filePath = self.pdfDocument!.documentURL?.path
  529. }
  530. }
  531. let document = CPDFDocument(url: URL(fileURLWithPath: filePath!))!
  532. document.unlock(withPassword: password)
  533. let tBackground: CPDFBackground = document.background()
  534. tBackground.opacity = background.opacity
  535. tBackground.scale = background.scale
  536. tBackground.rotation = CGFloat(background.rotation)
  537. tBackground.horizontalAlignment = UInt(background.horizontalMode)
  538. tBackground.verticalAlignment = UInt(background.verticalMode)
  539. tBackground.xOffset = background.horizontalSpace
  540. tBackground.yOffset = background.verticalSpace
  541. if let color = background.color {
  542. tBackground.color = color
  543. tBackground.type = .color
  544. } else if background.imagePath.count != 0 {
  545. let image = NSImage(contentsOfFile: background.imagePath)!
  546. tBackground.setImage(image)
  547. tBackground.type = .image
  548. }
  549. if background.pagesString.count != 0 {
  550. tBackground.pageString = background.pagesString
  551. } else {
  552. let pageString = "0-\(document.pageCount - 1)"
  553. tBackground.pageString = pageString
  554. }
  555. tBackground.update()
  556. // Save to temporary path
  557. let documentPath = NSTemporaryDirectory()
  558. let tempPath = (documentPath as NSString).appendingPathComponent((path as NSString).lastPathComponent)
  559. try? FileManager.default.removeItem(atPath: tempPath)
  560. let result = document.write(to: URL(fileURLWithPath: tempPath))
  561. if result {
  562. if FileManager.default.fileExists(atPath: path) {
  563. try? FileManager.default.removeItem(atPath: path)
  564. }
  565. try? FileManager.default.moveItem(atPath: tempPath, toPath: path)
  566. } else {
  567. try? FileManager.default.removeItem(atPath: tempPath)
  568. }
  569. if result {
  570. DispatchQueue.main.async {
  571. let needSave = self.saveToTemplateButton.state == .on
  572. if needSave {
  573. if self.checkPageRangeValidate(self.pageRangeComboBox.stringValue) && self.pageRangeComboBox.indexOfSelectedItem == -1 {
  574. self.background.pagesString = self.pageRangeComboBox.stringValue
  575. }
  576. KMBackgroundManager.defaultManager.addTemplate(model: self.background)
  577. NotificationCenter.default.post(name: NSNotification.Name("KMBatchOperateWatermarksNotification"), object: self)
  578. }
  579. self.cancelAction?(self)
  580. if autoOpen {
  581. NSDocumentController.shared.openDocument(withContentsOf: URL(fileURLWithPath: path), display: true) { _, _, _ in }
  582. } else {
  583. NSWorkspace.shared.selectFile(path, inFileViewerRootedAtPath: "")
  584. }
  585. }
  586. }
  587. }
  588. }
  589. static func saveAsPDFRemoveAllBackground(PDFDocument: CPDFDocument, password: String?, toPath path: String, completionHandler handler: ((Int) -> Void)?) {
  590. DispatchQueue.global(qos: .default).async {
  591. guard let filePath = PDFDocument.documentURL?.path else {
  592. return
  593. }
  594. let document = CPDFDocument(url: URL(fileURLWithPath: filePath))
  595. if let password = password {
  596. document?.unlock(withPassword: password)
  597. }
  598. let tBackground: CPDFBackground = document!.background()
  599. tBackground.clear()
  600. // Save to a temporary path
  601. let documentPath = NSTemporaryDirectory()
  602. let tempPath = (documentPath as NSString).appendingPathComponent((path as NSString).lastPathComponent)
  603. if FileManager.default.fileExists(atPath: tempPath) {
  604. try? FileManager.default.removeItem(atPath: tempPath)
  605. }
  606. if let result = document?.write(to: URL(fileURLWithPath: tempPath)) {
  607. if FileManager.default.fileExists(atPath: path) {
  608. try? FileManager.default.removeItem(atPath: path)
  609. }
  610. try? FileManager.default.moveItem(atPath: tempPath, toPath: path)
  611. DispatchQueue.main.async {
  612. handler?(1)
  613. }
  614. } else {
  615. try? FileManager.default.removeItem(atPath: tempPath)
  616. DispatchQueue.main.async {
  617. handler?(0)
  618. }
  619. }
  620. }
  621. }
  622. func kNewDocumentTempSavePath(_ fileName: String) -> String {
  623. let searchPath = NSSearchPathForDirectoriesInDomains(.applicationSupportDirectory, .userDomainMask, true).last
  624. let append1 = searchPath?.stringByAppendingPathComponent(Bundle.main.bundleIdentifier!)
  625. let append2 = append1!.stringByAppendingPathComponent(String(format: "%@", fileName))
  626. return append2
  627. }
  628. func isDamageImage(_ image: NSImage, imagePath path: String) -> Bool {
  629. let addImageAnnotation = (NSSearchPathForDirectoriesInDomains(.applicationSupportDirectory, .userDomainMask, true).last! as NSString).appendingPathComponent(Bundle.main.bundleIdentifier!).stringByAppendingPathComponent("addImageAnnotation")
  630. if !FileManager.default.fileExists(atPath: addImageAnnotation) {
  631. try? FileManager.default.createDirectory(atPath: addImageAnnotation, withIntermediateDirectories: false, attributes: nil)
  632. }
  633. let data = image.tiffRepresentation!
  634. let imageRep = NSBitmapImageRep(data: data)!
  635. imageRep.size = image.size
  636. var imageData: Data?
  637. if path.lowercased() == "png" {
  638. imageData = imageRep.representation(using: .png, properties: [:])
  639. } else {
  640. imageData = imageRep.representation(using: .jpeg, properties: [:])
  641. }
  642. let rPath = (addImageAnnotation as NSString).appendingPathComponent(tagString() + ".png")
  643. return ((try? imageData?.write(to: URL(fileURLWithPath: rPath), options: .atomic)) == nil)
  644. }
  645. func tagString() -> String {
  646. let dateFormatter = DateFormatter()
  647. dateFormatter.dateFormat = "yyMMddHHmmss"
  648. return String(format: "%@%04d", dateFormatter.string(from: Date()), arc4random() % 10000)
  649. }
  650. // ... (remaining methods)
  651. @objc func pageChangeNotification(notification: NSNotification) {
  652. currentPageIndexTextField.stringValue = "\(pdfView.currentPage().pageIndex() + 1)"
  653. }
  654. }
  655. extension KMAddBackgroundView {
  656. @IBAction func cancelButtonAction(_ sender: Any) {
  657. guard let callBack = cancelAction else { return }
  658. callBack(self)
  659. }
  660. @IBAction func buttonClicked_SwitchBackgroundType(_ sender: Any) {
  661. changeTypeBoxState(sender as? NSObject == colorButton)
  662. }
  663. // @IBAction func colorWellChanged(_ sender: NSColorWell) {
  664. // background.color = sender.color
  665. // self.updatePDFView()
  666. // }
  667. @objc func colorWellDidChange(_ sender: NSColorWell) {
  668. if let color = sender.color.usingColorSpaceName(NSColorSpaceName.calibratedRGB) {
  669. background.color = color
  670. self.updatePDFView()
  671. }
  672. }
  673. @IBAction func radioStepperAction(_ sender: NSStepper) {
  674. ratioTextField.stringValue = "\(Int(sender.doubleValue * 100))%"
  675. background.scale = sender.doubleValue
  676. self.updatePDFView()
  677. if filePathLabel.stringValue.count > 0 {
  678. doneButton.isEnabled = true
  679. }
  680. }
  681. @IBAction func angleStepperAction(_ sender: NSStepper) {
  682. angleTextField.stringValue = "\(sender.doubleValue)"
  683. background.rotation = CGFloat(Int(sender.doubleValue))
  684. checkAngle()
  685. self.updatePDFView()
  686. if filePathLabel.stringValue.count > 0 {
  687. doneButton.isEnabled = true
  688. }
  689. }
  690. @IBAction func alphaSliderAction(_ sender: NSSlider) {
  691. background.opacity = sender.doubleValue
  692. alphaStepper.doubleValue = sender.doubleValue
  693. alphaTextField.stringValue = "\(Int(sender.doubleValue * 100))%"
  694. if filePathLabel.stringValue.count > 0 {
  695. doneButton.isEnabled = true
  696. }
  697. self.updatePDFView()
  698. }
  699. @IBAction func verticalStepperAction(_ sender: NSStepper) {
  700. verticalGapTextField.stringValue = "\(sender.doubleValue)"
  701. background.verticalSpace = sender.doubleValue
  702. self.updatePDFView()
  703. if filePathLabel.stringValue.count > 0 {
  704. doneButton.isEnabled = true
  705. }
  706. }
  707. @IBAction func horizentalStepperAction(_ sender: NSStepper) {
  708. horizontalGapTextField.stringValue = "\(sender.doubleValue)"
  709. background.horizontalSpace = sender.doubleValue
  710. self.updatePDFView()
  711. if filePathLabel.stringValue.count > 0 {
  712. doneButton.isEnabled = true
  713. }
  714. }
  715. @IBAction func alphaSteperAction(_ sender: NSStepper) {
  716. alphaTextField.stringValue = "\(Int(sender.doubleValue * 100))%"
  717. background.opacity = sender.doubleValue
  718. alphaSlider.doubleValue = sender.doubleValue
  719. self.updatePDFView()
  720. if filePathLabel.stringValue.count > 0 {
  721. doneButton.isEnabled = true
  722. }
  723. }
  724. @IBAction func goPrevious(_ sender: Any) {
  725. if pdfView.canGoToPreviousPage() {
  726. pdfView.goToPreviousPage(nil)
  727. }
  728. // let index = pdfDocument!.index(for: pdfView.currentPage())
  729. // currentPageIndexTextField.stringValue = "\(index + 1)"
  730. }
  731. @IBAction func goNext(_ sender: Any) {
  732. if pdfView.canGoToNextPage() {
  733. pdfView.goToNextPage(nil)
  734. }
  735. // let index = pdfDocument!.index(for: pdfView.currentPage())
  736. // currentPageIndexTextField.stringValue = "\(index + 1)"
  737. }
  738. @IBAction func buttonClicked_BrowserFile(_ sender: Any) {
  739. let openPanel = NSOpenPanel()
  740. openPanel.canChooseDirectories = false
  741. openPanel.canChooseFiles = true
  742. openPanel.allowsMultipleSelection = false
  743. openPanel.allowedFileTypes = ["jpg", "cur", "bmp", "jpeg", "gif", "png", "tiff", "tif", "ico", "icns", "tga", "psd", "eps", "hdr", "jp2", "jpc", "pict", "sgi", "pdf"]
  744. openPanel.beginSheetModal(for: window!) { (result) in
  745. if result == .OK {
  746. guard let url = openPanel.url else { return }
  747. let filePath: NSString = url.path as NSString
  748. if filePath.pathExtension.lowercased() == "pdf" {
  749. let pdf = CPDFDocument(url: url)
  750. if pdf?.isEncrypted == true {
  751. return
  752. }
  753. }
  754. if let image = NSImage(contentsOfFile: url.path), !self.isDamageImage(image, imagePath: url.path) {
  755. self.filePathLabel.stringValue = url.path
  756. self.doneButton.isEnabled = true
  757. self.background.imagePath = url.path
  758. self.background.image = image
  759. self.background.backgroundID = url.path.lastPathComponent.deletingPathExtension
  760. self.templateNameTextField.stringValue = url.path.lastPathComponent.deletingPathExtension
  761. self.self.updatePDFView()
  762. }
  763. }
  764. }
  765. }
  766. // ... (remaining IBActions)
  767. @IBAction func buttonClicked_Batch(_ sender: Any) {
  768. if background.type == .color {
  769. background.imagePath = ""
  770. } else {
  771. background.color = nil
  772. if background.image == nil {
  773. return
  774. }
  775. }
  776. if templateNameTextField.stringValue.count < 1 {
  777. background.backgroundID = initialID
  778. } else {
  779. background.backgroundID = templateNameTextField.stringValue
  780. }
  781. let needSave = saveToTemplateButton.state == .on
  782. if needSave {
  783. KMBackgroundManager.defaultManager.addTemplate(model: background)
  784. }
  785. operateCallBack?(background, currentType)
  786. }
  787. @IBAction func buttonClicked_Done(_ sender: Any) {
  788. guard let pdfDocument = pdfDocument else { return }
  789. if background.type == .color {
  790. background.imagePath = ""
  791. } else {
  792. background.color = nil
  793. if background.image == nil {
  794. return
  795. }
  796. }
  797. if templateNameTextField.stringValue.isEmpty {
  798. background.backgroundID = initialID
  799. } else {
  800. background.backgroundID = templateNameTextField.stringValue
  801. }
  802. // Avoid showing the page range alert twice
  803. if checkPageRangeValidate(pageRangeComboBox.stringValue) {
  804. background.pagesString = pageRangeComboBox.stringValue
  805. self.updatePDFView()
  806. window?.makeFirstResponder(self)
  807. }
  808. let needSave = saveToTemplateButton.state == .on
  809. var pages = [Int]()
  810. switch pageRangeComboBox.indexOfSelectedItem {
  811. case 0:
  812. pages = Array(0..<Int(pdfDocument.pageCount))
  813. case 1:
  814. pages = Array(stride(from: 0, to: Int(pdfDocument.pageCount), by: 2))
  815. case 2:
  816. pages = Array(stride(from: 1, to: Int(pdfDocument.pageCount), by: 2))
  817. default:
  818. var fileAttribute = self._fileAttri
  819. if fileAttribute == nil {
  820. fileAttribute = KMFileAttribute()
  821. fileAttribute?.password = self.pdfDocument?.password ?? ""
  822. fileAttribute?.filePath = self.pdfDocument?.documentURL.path ?? ""
  823. self._fileAttri = fileAttribute
  824. }
  825. // fileAttribute.filePath = pdfDocument.documentURL?.path ?? ""
  826. fileAttribute?.bAllPage = false
  827. fileAttribute?.pagesType = .custom
  828. // fileAttribute.password = pdfDocument.password ?? ""
  829. fileAttribute?.pagesString = pageRangeComboBox.stringValue
  830. let selectPages = fileAttribute?.fetchSelectPages() ?? []
  831. if selectPages.count != 0 {
  832. pages = selectPages.map { $0 - 1 }
  833. } else {
  834. let alert = NSAlert()
  835. alert.alertStyle = .critical
  836. alert.messageText = "\(fileAttribute?.filePath.lastPathComponent ?? "") \(NSLocalizedString("Invalid page range or the page number is out of range. Please try again.", comment: ""))"
  837. alert.runModal()
  838. return
  839. }
  840. }
  841. background.pagesString = pages.isEmpty ? "" : pages.map { "\($0)" }.joined(separator: ",")
  842. switch type {
  843. case .add:
  844. if needSave {
  845. if checkPageRangeValidate(pageRangeComboBox.stringValue) && pageRangeComboBox.indexOfSelectedItem == -1 {
  846. background.pagesString = pageRangeComboBox.stringValue
  847. }
  848. KMBackgroundManager.defaultManager.addTemplate(model: background)
  849. }
  850. operateCallBack?(background, currentType)
  851. case .edit:
  852. if needSave {
  853. if checkPageRangeValidate(pageRangeComboBox.stringValue) && pageRangeComboBox.indexOfSelectedItem == -1 {
  854. background.pagesString = pageRangeComboBox.stringValue
  855. }
  856. originalBackground = (background.copy() as? KMBackgroundModel)!
  857. KMBackgroundManager.defaultManager.updateTemplate(model: originalBackground)
  858. }
  859. operateCallBack?(originalBackground, currentType)
  860. case .use:
  861. let fileName = "\(pdfDocument.documentURL?.deletingPathExtension().lastPathComponent ?? NSLocalizedString("Untitled", comment: ""))_Background"
  862. let savePanelAccessoryViewController = KMSavePanelAccessoryController()
  863. let savePanel = NSSavePanel()
  864. savePanel.nameFieldStringValue = fileName
  865. savePanel.allowedFileTypes = ["pdf"]
  866. savePanel.accessoryView = savePanelAccessoryViewController.view
  867. savePanel.beginSheetModal(for: window!) { result in
  868. if result == .OK {
  869. self.saveAsPDF(with: self.background, to: savePanel.url?.path ?? "", autoOpen: savePanelAccessoryViewController.openAutomaticButton.state == .on ? true : false)
  870. }
  871. }
  872. }
  873. }
  874. }