KMAddBackgroundView.swift 39 KB

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