KMAddBackgroundView.swift 39 KB

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