KMAddBackgroundView.swift 39 KB

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