KMAddBackgroundView.swift 38 KB

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