12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022 |
- //
- // KMAddBackgroundView.swift
- // PDF Reader Pro
- //
- // Created by lizhe on 2023/11/14.
- //
- import Cocoa
- enum KMBackgroundManagerType: Int {
- case add = 0
- case edit
- case use
- }
- typealias KMAddBackgroundViewOperateCallBack = (_ background: KMBackgroundModel, _ countType: Int) -> ()
- typealias KMAddBackgroundViewBatchAction = (_ view: KMAddBackgroundView, _ files: [KMFileAttribute]) -> Void
- typealias KMAddBackgroundViewCancelAction = (_ view: KMAddBackgroundView) -> Void
- class KMAddBackgroundView: KMBaseXibView, NSComboBoxDelegate {
-
- @IBOutlet weak var pdfView: KMWatermarkPDFView!
- @IBOutlet weak var previousButton: NSButton!
- @IBOutlet weak var nextButton: NSButton!
- @IBOutlet weak var currentPageIndexTextField: NSTextField!
- @IBOutlet weak var totalPageCountlabel: NSTextField!
-
- @IBOutlet weak var typeBox: NSBox!
- @IBOutlet weak var colorButton: NSButton!
- @IBOutlet weak var colorWell: NSColorWell!
- @IBOutlet weak var fileButton: NSButton!
-
- @IBOutlet weak var filePathLabel: NSTextField!
- @IBOutlet weak var browseButton: NSButton!
- @IBOutlet weak var ratioLabel: NSTextField!
- @IBOutlet weak var ratioTextField: NSTextField!
- @IBOutlet weak var ratioStepper: NSStepper!
-
- @IBOutlet weak var appearanceBox: NSBox!
- @IBOutlet weak var angleLabel: NSTextField!
- @IBOutlet weak var angleTextField: NSTextField!
- @IBOutlet weak var angleStepper: NSStepper!
- @IBOutlet weak var left45IndicateView: KMAngleIndicateView!
- @IBOutlet weak var horizontalIndicateView: KMAngleIndicateView!
- @IBOutlet weak var right45IndicateView: KMAngleIndicateView!
- @IBOutlet weak var alphaLabel: NSTextField!
- @IBOutlet weak var alphaSlider: NSSlider!
- @IBOutlet weak var alphaTextField: NSTextField!
- @IBOutlet weak var alphaStepper: NSStepper!
- @IBOutlet weak var postionView: KMPostionIndicateView!
- @IBOutlet weak var verticalGapLabel: NSTextField!
- @IBOutlet weak var verticalGapTextField: NSTextField!
- @IBOutlet weak var verticalStepper: NSStepper!
- @IBOutlet weak var horizontalGapLabel: NSTextField!
- @IBOutlet weak var horizontalGapTextField: NSTextField!
- @IBOutlet weak var horizontalStepper: NSStepper!
- @IBOutlet weak var pageRangeComboBox: NSComboBox!
- @IBOutlet weak var pageRangeLabel: NSTextField!
-
- @IBOutlet weak var saveToTemplateButton: NSButton!
- @IBOutlet weak var templateNameLabel: NSTextField!
- @IBOutlet weak var templateNameTextField: NSTextField!
-
- @IBOutlet weak var doneButton: NSButton!
- @IBOutlet weak var cancelButton: NSButton!
- @IBOutlet weak var batchButton: NSButton!
-
- var isHiddenBatchBtn: Bool = false
-
- private var workItem: DispatchWorkItem?
-
- var isAllowReloadDocument = true
-
- lazy var background: KMBackgroundModel = {
- var bg = KMBackgroundModel()
- bg.type = .color
- bg.color = .red
- bg.opacity = 1.0
- bg.scale = 1.0
- bg.verticalMode = 1
- bg.horizontalMode = 1
- bg.backgroundID = KMBackgroundManager.defaultManager.fetchAvailableName()
- self.initialID = bg.backgroundID
- bg.pagesString = ""
- bg.pageRangeType = .all
- return bg
- }()
- var originalBackground: KMBackgroundModel = KMBackgroundModel()
- var filePath: String = Bundle.main.path(forResource: NSLocalizedString("Quick Start Guide.pdf", comment: ""), ofType: "") ?? ""
- var password: String = ""
- var type: KMBackgroundManagerType = .use
- var pdfDocument: CPDFDocument? {
- didSet {
- self._fileAttri = KMFileAttribute()
- self._fileAttri?.password = self.pdfDocument?.password ?? ""
- self._fileAttri?.filePath = self.pdfDocument?.documentURL.path ?? ""
-
- self.password = pdfDocument?.password ?? ""
- self.pdfView.document = pdfDocument
- self.reloadData()
- }
- }
-
- var backgroundType: KMBackgroundType = .color
-
- var initialID: String!
- var currentType: Int = 0
-
- var cancelAction: KMAddBackgroundViewCancelAction?
- var batchAction: KMAddBackgroundViewCancelAction?
- var operateCallBack: KMAddBackgroundViewOperateCallBack?
- var onlyManagerTemplate: Bool = true
-
- private var _fileAttri: KMFileAttribute?
- // MARK: - Dealloc
- deinit {
- NotificationCenter.default.removeObserver(self)
- }
- // MARK: - Init Methods
- convenience init?(baseFile filePath: String, background backgroundObject: KMBackgroundModel, password: String, type: KMBackgroundManagerType, fileType countType: Int) {
- self.init()
- self.filePath = filePath
- self.password = password
- self.background = backgroundObject
- self.originalBackground = backgroundObject
- self.initialID = backgroundObject.backgroundID
- self.type = type
- self.pdfDocument = CPDFDocument(url: URL(fileURLWithPath: self.filePath))
- if pdfDocument!.isLocked {
- pdfDocument!.unlock(withPassword: password)
- }
- if pdfDocument!.isLocked {
- return nil
- }
- }
- override func setup() {
- // pdfView.background = background
- // pdfView.document = pdfDocument
- pdfView.autoScales = true
- pdfView.setDisplay(.singlePage)
- // pdfView.documentView?.enclosingScrollView?.hasVerticalScroller = false
- // pdfView.documentView?.enclosingScrollView?.hasHorizontalScroller = false
- for i in 0..<3 {
- for j in 0..<3 {
- if i == Int(background.horizontalMode) && j == Int(background.verticalMode) {
- postionView.style = KMPositionIndicateViewStyle(rawValue: i + 3 * j)!
- }
- }
- }
- postionView.styleChangedCallBack = { [weak self] in
- guard let self = self else { return }
- self.background.horizontalMode = self.postionView.style.rawValue % 3
- self.background.verticalMode = self.postionView.style.rawValue / 3
- self.self.updatePDFView()
- if self.filePathLabel.stringValue.count > 0 {
- self.doneButton.isEnabled = true
- }
- }
- currentPageIndexTextField.stringValue = "1"
- // let numberFormatter = currentPageIndexTextField.formatter as! NumberFormatter
- // numberFormatter.maximum = NSNumber(value: pdfDocument.pageCount)
- let countFormatter = TextFieldFormatter.init()
- countFormatter.allowedCharacterSet = "-"
- self.angleTextField.formatter = countFormatter
- angleTextField.delegate = self
-
-
- let formatter = TextFieldFormatter.init()
- formatter.allowedCharacterSet = "-."
- self.verticalGapTextField.formatter = formatter
- self.verticalGapTextField.delegate = self
-
- let formatter2 = TextFieldFormatter.init()
- formatter2.allowedCharacterSet = "-."
- self.horizontalGapTextField.formatter = formatter2
- self.horizontalGapTextField.delegate = self
-
- let formatter3 = TextFieldFormatter.init()
- formatter3.allowedCharacterSet = "%"
- self.alphaTextField.formatter = formatter3
- alphaTextField.delegate = self
- left45IndicateView.style = .left45
- left45IndicateView.touchCallBack = { [weak self] in
- guard let self = self else { return }
- self.background.rotation = -45
- self.angleStepper.doubleValue = -45
- self.angleTextField.stringValue = "\(-45)"
- self.checkAngle()
- if self.filePathLabel.stringValue.count > 0 {
- self.doneButton.isEnabled = true
- }
- self.updatePDFView()
- }
- horizontalIndicateView.style = .horizontal
- horizontalIndicateView.touchCallBack = { [weak self] in
- guard let self = self else { return }
- self.background.rotation = 0
- self.angleStepper.doubleValue = 0
- self.angleTextField.stringValue = "\(0)"
- self.checkAngle()
- if self.filePathLabel.stringValue.count > 0 {
- self.doneButton.isEnabled = true
- }
- self.updatePDFView()
- }
- right45IndicateView.style = .right45
- right45IndicateView.touchCallBack = { [weak self] in
- guard let self = self else { return }
- self.background.rotation = 45
- self.angleStepper.doubleValue = 45
- self.angleTextField.stringValue = "\(45)"
- self.checkAngle()
- if self.filePathLabel.stringValue.count > 0 {
- self.doneButton.isEnabled = true
- }
- self.updatePDFView()
- }
- checkAngle()
- typeBox.titleFont = NSFont.systemFont(ofSize: 13)
- colorWell.color = background.color ?? NSColor.red
- templateNameTextField.stringValue = background.backgroundID
- appearanceBox.titleFont = NSFont.systemFont(ofSize: 13)
- saveToTemplateButton.isEnabled = onlyManagerTemplate
- // if type == .use {
- // saveToTemplateButton.isHidden = true
- // saveToTemplateButton.state = .off
- // } else {
- // saveToTemplateButton.isHidden = false
- // saveToTemplateButton.state = .on
- // }
- pageRangeComboBox.removeAllItems()
- pageRangeComboBox.addItems(withObjectValues: [
- NSLocalizedString("All Pages", comment: ""),
- NSLocalizedString("Odd Pages Only", comment: ""),
- NSLocalizedString("Even Pages Only", comment: ""),
- NSLocalizedString("e.g. 1,3-5,10", comment: "")
- ])
- pageRangeComboBox.placeholderString = NSLocalizedString("e.g. 1,3-5,10", comment: "")
- pageRangeComboBox.delegate = nil
- pageRangeComboBox.selectItem(at: 0)
- pageRangeComboBox.isEditable = false
- pageRangeComboBox.delegate = self
-
-
- colorWell.target = self
- colorWell.action = #selector(colorWellDidChange)
- }
-
- override func addNotification() {
- NotificationCenter.default.addObserver(self, selector: #selector(pageChangeNotification), name: NSNotification.Name.CPDFViewPageChanged, object: self.pdfView)
- }
- override func updateLanguage() {
- typeBox.title = NSLocalizedString("Source", comment: "")
- self.templateNameLabel.stringValue = NSLocalizedString("Name:", comment: "")
- batchButton.title = NSLocalizedString("Batch", comment: "");
- cancelButton.title = NSLocalizedString("Cancel", comment: "");
-
- colorButton.title = NSLocalizedString("Color", comment: "")
- fileButton.title = NSLocalizedString("File", comment: "")
- browseButton.title = NSLocalizedString("Choose...", comment: "")
- ratioLabel.stringValue = "\(NSLocalizedString("Ratio", comment: "")):"
- appearanceBox.title = NSLocalizedString("Appearance", comment: "")
- angleLabel.stringValue = "\(NSLocalizedString("Rotation", comment: "")):"
- alphaLabel.stringValue = "\(NSLocalizedString("Opacity", comment: "")):"
- pageRangeLabel.stringValue = "\(NSLocalizedString("Page Range", comment: "")):"
- horizontalGapLabel.stringValue = "X:"
- verticalGapLabel.stringValue = "Y:"
-
- templateNameLabel.stringValue = NSLocalizedString("Label", comment: "")
-
- saveToTemplateButton.title = NSLocalizedString("Add to Template", comment: "")
-
- if (self.type == .add) {
- self.doneButton.title = NSLocalizedString("Apply", comment: "");
- self.batchButton.isHidden = true
- } else if (self.type == .edit) {
- self.doneButton.title = NSLocalizedString("Apply", comment: "");
- self.batchButton.isHidden = true
- } else if (self.type == .use) {
- self.doneButton.title = NSLocalizedString("Save & Apply", comment: "");
- }
- }
- private func checkAngle() {
- left45IndicateView.isSelcted = false
- horizontalIndicateView.isSelcted = false
- right45IndicateView.isSelcted = false
- if background.rotation == 45 {
- right45IndicateView.isSelcted = true
- } else if background.rotation == 0 {
- horizontalIndicateView.isSelcted = true
- } else if background.rotation == -45 {
- left45IndicateView.isSelcted = true
- }
- }
- override func reloadData() {
- guard let pdfDocument = pdfDocument else { return }
-
- totalPageCountlabel.stringValue = "/ \(pdfDocument.pageCount)"
- templateNameTextField.stringValue = background.backgroundID
- colorWell.color = background.color ?? NSColor.red
- filePathLabel.stringValue = background.imagePath
- filePathLabel.placeholderString = NSLocalizedString("Select a File", comment: "")
- angleStepper.doubleValue = Double(background.rotation)
- angleTextField.stringValue = "\(angleStepper.intValue)"
- alphaSlider.doubleValue = background.opacity
- alphaStepper.doubleValue = background.opacity
- let opacity = round(background.opacity * 100) / 100
- alphaTextField.stringValue = "\(Int(opacity * 100))%"
- ratioStepper.doubleValue = background.scale
- ratioTextField.stringValue = "\(Int(background.scale * 100))%"
- if currentType == 0 {
- changeTypeBoxState(true)
- } else {
- changeTypeBoxState(false)
- }
- pageRangeComboBox.delegate = nil
- switch background.pageRangeType {
- case .all:
- pageRangeComboBox.isEditable = false
- pageRangeComboBox.selectItem(at: 0)
- case .odd:
- pageRangeComboBox.isEditable = false
- pageRangeComboBox.selectItem(at: 1)
- case .even:
- pageRangeComboBox.isEditable = false
- pageRangeComboBox.selectItem(at: 2)
- case .other:
- pageRangeComboBox.isEditable = true
- pageRangeComboBox.selectItem(at: 3)
- pageRangeComboBox.stringValue = background.pagesString
- window?.makeFirstResponder(pageRangeComboBox)
- }
- pageRangeComboBox.delegate = self
- verticalStepper.doubleValue = background.verticalSpace
- verticalGapTextField.stringValue = "\(verticalStepper.doubleValue)"
- horizontalStepper.doubleValue = background.horizontalSpace
- horizontalGapTextField.stringValue = "\(horizontalStepper.doubleValue)"
- batchButton.isHidden = isHiddenBatchBtn
- if !isHiddenBatchBtn {
- self.batchButton.isHidden = type != .use
- }
- }
- private func changeTypeBoxState(_ isColor: Bool) {
- if isColor {
- currentType = 0
- colorButton.state = .on
- colorWell.isEnabled = true
- fileButton.state = .off
- browseButton.isEnabled = false
- ratioTextField.isEnabled = false
- ratioStepper.isEnabled = false
- background.color = colorWell.color
- background.type = .color
- background.scale = 1
- doneButton.isEnabled = true
- } else {
- currentType = 1
- colorButton.state = .off
- colorWell.isEnabled = false
- fileButton.state = .on
- browseButton.isEnabled = true
- ratioTextField.isEnabled = true
- ratioStepper.isEnabled = true
- background.color = NSColor.white
- background.type = .file
- background.scale = ratioStepper.doubleValue
- doneButton.isEnabled = filePathLabel.stringValue.count > 0
- }
- self.updatePDFView()
- }
-
- func updatePDFView() {
- // pdfView.needsDisplay = true
- // pdfView.layoutDocumentView()
- // pdfView.setNeedsDisplayForVisiblePages()
-
- // // Save to temporary path
- // let documentPath = NSTemporaryDirectory()
- // let tempPath = (documentPath as NSString).appendingPathComponent((path as NSString).lastPathComponent)
- // try? FileManager.default.removeItem(atPath: tempPath)
- //
- // let result = document.write(to: URL(fileURLWithPath: tempPath))
- // if result {
- // if FileManager.default.fileExists(atPath: path) {
- // try? FileManager.default.removeItem(atPath: path)
- // }
- // try? FileManager.default.moveItem(atPath: tempPath, toPath: path)
- // } else {
- // try? FileManager.default.removeItem(atPath: tempPath)
- // }
-
- // 如果前一次调用还在执行,则取消它
- workItem?.cancel()
- // 创建一个新的 DispatchWorkItem
- let newWorkItem = DispatchWorkItem {
- // 在这里执行你的方法的实际逻辑
- self.reloadDocument()
- }
- // 将新的 DispatchWorkItem 分配给实例变量
- workItem = newWorkItem
- // 在队列中执行 DispatchWorkItem
- DispatchQueue.main.async(execute: newWorkItem)
- }
-
- func reloadDocument() {
- if !self.isAllowReloadDocument {
- return
- }
- self.isAllowReloadDocument = false
-
- let document = CPDFDocument(url: self.pdfDocument?.documentURL)
- guard let document = document else { return }
- document.unlock(withPassword: self.pdfDocument?.password)
-
- let tBackground: CPDFBackground = document.background()
- tBackground.opacity = background.opacity
- tBackground.scale = background.scale
- tBackground.rotation = CGFloat(background.rotation)
- tBackground.horizontalAlignment = UInt(background.horizontalMode)
- tBackground.verticalAlignment = UInt(background.verticalMode)
- tBackground.xOffset = background.horizontalSpace
- tBackground.yOffset = background.verticalSpace
- if let color = background.color {
- tBackground.color = color
- tBackground.type = .color
- }
- if background.imagePath.count != 0 {
- if let data = background.image {
- tBackground.setImage(data)
- } else {
- if let image = NSImage(contentsOfFile: background.imagePath) {
- tBackground.setImage(image)
- }
- }
- tBackground.type = .image
- }
- if background.pagesString.count != 0 {
- tBackground.pageString = background.pagesString
- } else {
- let pageString = "0-\(document.pageCount - 1)"
- tBackground.pageString = pageString
- }
- tBackground.update()
- self.pdfView.document = document
-
- DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.1) {
- self.pdfView.layoutDocumentView()
- self.isAllowReloadDocument = true
- }
- }
- // Other methods...
-
- @objc func controlTextDidEndEditing(_ notification: Notification) {
- guard let textField = notification.object as? NSTextField else { return }
- switch textField {
- case ratioTextField:
- let formatter = textField.formatter as? NumberFormatter
- if let floatValue = formatter?.number(from: textField.stringValue)?.floatValue {
- ratioStepper.doubleValue = Double(floatValue)
- background.scale = Double(floatValue)
- self.updatePDFView()
- }
- case angleTextField:
- if let integerValue = Int(textField.stringValue) {
- background.rotation = CGFloat(integerValue)
- angleStepper.doubleValue = Double(background.rotation)
- checkAngle()
- self.updatePDFView()
- }
- case alphaTextField:
- let floatValue = textField.stringValue.replacingOccurrences(of: "%", with: "").stringToCGFloat() * 0.01
- alphaSlider.doubleValue = Double(floatValue)
- alphaStepper.doubleValue = Double(floatValue)
- background.opacity = Double(floatValue)
- self.updatePDFView()
- case pageRangeComboBox:
- if pageRangeComboBox.indexOfSelectedItem == -1 {
- if !checkPageRangeValidate(pageRangeComboBox.stringValue) {
- let alert = NSAlert()
- alert.alertStyle = .critical
- alert.messageText = "\(pdfDocument!.documentURL.lastPathComponent) \(NSLocalizedString("Invalid page range or the page number is out of range. Please try again.", comment: ""))"
- alert.runModal()
- window?.makeFirstResponder(pageRangeComboBox)
- return
- } else {
- background.pagesString = pageRangeComboBox.stringValue
- self.updatePDFView()
- }
- }
- case verticalGapTextField:
- if let integerValue = Int(verticalGapTextField.stringValue) {
- background.verticalSpace = CGFloat(integerValue)
- verticalStepper.doubleValue = background.verticalSpace
- self.updatePDFView()
- }
- case horizontalGapTextField:
- if let integerValue = Int(horizontalGapTextField.stringValue) {
- background.horizontalSpace = CGFloat(integerValue)
- horizontalStepper.doubleValue = background.horizontalSpace
- self.updatePDFView()
- }
- case currentPageIndexTextField:
- if let pageIndex = Int(currentPageIndexTextField.stringValue), let page = pdfDocument!.page(at: UInt(pageIndex - 1)) {
- pdfView.go(to: page)
- }
- default:
- break
- }
- if filePathLabel.stringValue.count > 0 {
- doneButton.isEnabled = true
- }
- }
- @objc func comboBoxSelectionDidChange(_ notification: Notification) {
- guard notification.object as? NSComboBox == pageRangeComboBox else { return }
- pageRangeComboBox.isEditable = false
- switch pageRangeComboBox.indexOfSelectedItem {
- case 0:
- background.pageRangeType = .all
- case 1:
- background.pageRangeType = .odd
- case 2:
- background.pageRangeType = .even
- default:
- background.pageRangeType = .other
- pageRangeComboBox.stringValue = ""
- pageRangeComboBox.isEditable = true
- window?.makeFirstResponder(pageRangeComboBox)
- }
- if filePathLabel.stringValue.count > 0 {
- doneButton.isEnabled = true
- }
- }
- func checkPageRangeValidate(_ pageRangeString: String) -> Bool {
- var fileAttribute = self._fileAttri
- if fileAttribute == nil {
- fileAttribute = KMFileAttribute()
- fileAttribute?.password = self.pdfDocument?.password ?? ""
- fileAttribute?.filePath = self.pdfDocument?.documentURL.path ?? ""
- self._fileAttri = fileAttribute
- }
- // fileAttribute?.filePath = pdfDocument!.documentURL.path
- fileAttribute?.bAllPage = false
- fileAttribute?.pagesString = pageRangeComboBox.stringValue
- var pageRange: KMPageRange = .all
- let pageRangeType: KMWatermarkeModelPageRangeType = background.pageRangeType
- if pageRangeType == .all {
- pageRange = .all
- } else if pageRangeType == .even {
- pageRange = .even
- } else if pageRangeType == .odd {
- pageRange = .odd
- } else if pageRangeType == .other {
- pageRange = .custom
- }
- fileAttribute?.pagesType = pageRange
-
- return fileAttribute!.fetchSelectPages().count != 0
- }
- func saveAsPDF(with background: KMBackgroundModel, to path: String, autoOpen: Bool) {
- self.window?.makeFirstResponder(nil)
-
- DispatchQueue.global(qos: .default).async { [unowned self] in
- var filePath = self.pdfDocument!.documentURL?.path
- let password = self.password
- if filePath == nil {
- let writeSuccess = self.pdfDocument!.write(to: URL(fileURLWithPath: kNewDocumentTempSavePath(NSLocalizedString("Untitled", comment: ""))))
- if writeSuccess {
- self.pdfDocument = CPDFDocument(url: URL(fileURLWithPath: kNewDocumentTempSavePath(NSLocalizedString("Untitled", comment: ""))))!
- filePath = self.pdfDocument!.documentURL?.path
- }
- }
- let document = CPDFDocument(url: URL(fileURLWithPath: filePath!))!
- document.unlock(withPassword: password)
-
- let tBackground: CPDFBackground = document.background()
- tBackground.opacity = background.opacity
- tBackground.scale = background.scale
- tBackground.rotation = CGFloat(background.rotation)
- tBackground.horizontalAlignment = UInt(background.horizontalMode)
- tBackground.verticalAlignment = UInt(background.verticalMode)
- tBackground.xOffset = background.horizontalSpace
- tBackground.yOffset = background.verticalSpace
- if let color = background.color {
- tBackground.color = color
- tBackground.type = .color
- } else if background.imagePath.count != 0 {
- let image = NSImage(contentsOfFile: background.imagePath)!
- tBackground.setImage(image)
- tBackground.type = .image
- }
- if background.pagesString.count != 0 {
- tBackground.pageString = background.pagesString
- } else {
- let pageString = "0-\(document.pageCount - 1)"
- tBackground.pageString = pageString
- }
- tBackground.update()
- // Save to temporary path
- let documentPath = NSTemporaryDirectory()
- let tempPath = (documentPath as NSString).appendingPathComponent((path as NSString).lastPathComponent)
- try? FileManager.default.removeItem(atPath: tempPath)
- let result = document.write(to: URL(fileURLWithPath: tempPath))
- if result {
- if FileManager.default.fileExists(atPath: path) {
- try? FileManager.default.removeItem(atPath: path)
- }
- try? FileManager.default.moveItem(atPath: tempPath, toPath: path)
- } else {
- try? FileManager.default.removeItem(atPath: tempPath)
- }
- if result {
- DispatchQueue.main.async {
- let needSave = self.saveToTemplateButton.state == .on
- if needSave {
- if self.checkPageRangeValidate(self.pageRangeComboBox.stringValue) && self.pageRangeComboBox.indexOfSelectedItem == -1 {
- self.background.pagesString = self.pageRangeComboBox.stringValue
- }
- KMBackgroundManager.defaultManager.addTemplate(model: self.background)
- NotificationCenter.default.post(name: NSNotification.Name("KMBatchOperateWatermarksNotification"), object: self)
- }
- self.cancelAction?(self)
- if autoOpen {
- NSDocumentController.shared.openDocument(withContentsOf: URL(fileURLWithPath: path), display: true) { _, _, _ in }
- } else {
- NSWorkspace.shared.selectFile(path, inFileViewerRootedAtPath: "")
- }
- }
- }
- }
- }
-
- static func saveAsPDFRemoveAllBackground(PDFDocument: CPDFDocument, password: String?, toPath path: String, completionHandler handler: ((Int) -> Void)?) {
- DispatchQueue.global(qos: .default).async {
- guard let filePath = PDFDocument.documentURL?.path else {
- return
- }
- let document = CPDFDocument(url: URL(fileURLWithPath: filePath))
- if let password = password {
- document?.unlock(withPassword: password)
- }
- let tBackground: CPDFBackground = document!.background()
- tBackground.clear()
- // Save to a temporary path
- let documentPath = NSTemporaryDirectory()
- let tempPath = (documentPath as NSString).appendingPathComponent((path as NSString).lastPathComponent)
- if FileManager.default.fileExists(atPath: tempPath) {
- try? FileManager.default.removeItem(atPath: tempPath)
- }
- if let result = document?.write(to: URL(fileURLWithPath: tempPath)) {
- if FileManager.default.fileExists(atPath: path) {
- try? FileManager.default.removeItem(atPath: path)
- }
- try? FileManager.default.moveItem(atPath: tempPath, toPath: path)
- DispatchQueue.main.async {
- handler?(1)
- }
- } else {
- try? FileManager.default.removeItem(atPath: tempPath)
- DispatchQueue.main.async {
- handler?(0)
- }
- }
- }
- }
-
- func kNewDocumentTempSavePath(_ fileName: String) -> String {
- let searchPath = NSSearchPathForDirectoriesInDomains(.applicationSupportDirectory, .userDomainMask, true).last
- let append1 = searchPath?.stringByAppendingPathComponent(Bundle.main.bundleIdentifier!)
- let append2 = append1!.stringByAppendingPathComponent(String(format: "%@", fileName))
- return append2
- }
- func isDamageImage(_ image: NSImage, imagePath path: String) -> Bool {
- let addImageAnnotation = (NSSearchPathForDirectoriesInDomains(.applicationSupportDirectory, .userDomainMask, true).last! as NSString).appendingPathComponent(Bundle.main.bundleIdentifier!).stringByAppendingPathComponent("addImageAnnotation")
- if !FileManager.default.fileExists(atPath: addImageAnnotation) {
- try? FileManager.default.createDirectory(atPath: addImageAnnotation, withIntermediateDirectories: false, attributes: nil)
- }
- let data = image.tiffRepresentation!
- let imageRep = NSBitmapImageRep(data: data)!
- imageRep.size = image.size
- var imageData: Data?
- if path.lowercased() == "png" {
- imageData = imageRep.representation(using: .png, properties: [:])
- } else {
- imageData = imageRep.representation(using: .jpeg, properties: [:])
- }
- let rPath = (addImageAnnotation as NSString).appendingPathComponent(tagString() + ".png")
- return ((try? imageData?.write(to: URL(fileURLWithPath: rPath), options: .atomic)) == nil)
- }
- func tagString() -> String {
- let dateFormatter = DateFormatter()
- dateFormatter.dateFormat = "yyMMddHHmmss"
- return String(format: "%@%04d", dateFormatter.string(from: Date()), arc4random() % 10000)
- }
- // ... (remaining methods)
- @objc func pageChangeNotification(notification: NSNotification) {
- currentPageIndexTextField.stringValue = "\(pdfView.currentPage().pageIndex() + 1)"
- }
- }
- extension KMAddBackgroundView {
- @IBAction func cancelButtonAction(_ sender: Any) {
- guard let callBack = cancelAction else { return }
-
- callBack(self)
- }
-
- @IBAction func buttonClicked_SwitchBackgroundType(_ sender: Any) {
- changeTypeBoxState(sender as? NSObject == colorButton)
- }
- // @IBAction func colorWellChanged(_ sender: NSColorWell) {
- // background.color = sender.color
- // self.updatePDFView()
- // }
-
- @objc func colorWellDidChange(_ sender: NSColorWell) {
- if let color = sender.color.usingColorSpaceName(NSColorSpaceName.calibratedRGB) {
- background.color = color
- self.updatePDFView()
- }
- }
- @IBAction func radioStepperAction(_ sender: NSStepper) {
- ratioTextField.stringValue = "\(Int(sender.doubleValue * 100))%"
- background.scale = sender.doubleValue
- self.updatePDFView()
- if filePathLabel.stringValue.count > 0 {
- doneButton.isEnabled = true
- }
- }
- @IBAction func angleStepperAction(_ sender: NSStepper) {
- angleTextField.stringValue = "\(sender.doubleValue)"
- background.rotation = CGFloat(Int(sender.doubleValue))
- checkAngle()
- self.updatePDFView()
- if filePathLabel.stringValue.count > 0 {
- doneButton.isEnabled = true
- }
- }
- @IBAction func alphaSliderAction(_ sender: NSSlider) {
- background.opacity = sender.doubleValue
- alphaStepper.doubleValue = sender.doubleValue
- alphaTextField.stringValue = "\(Int(sender.doubleValue * 100))%"
- if filePathLabel.stringValue.count > 0 {
- doneButton.isEnabled = true
- }
- self.updatePDFView()
- }
- @IBAction func verticalStepperAction(_ sender: NSStepper) {
- verticalGapTextField.stringValue = "\(sender.doubleValue)"
- background.verticalSpace = sender.doubleValue
- self.updatePDFView()
- if filePathLabel.stringValue.count > 0 {
- doneButton.isEnabled = true
- }
- }
- @IBAction func horizentalStepperAction(_ sender: NSStepper) {
- horizontalGapTextField.stringValue = "\(sender.doubleValue)"
- background.horizontalSpace = sender.doubleValue
- self.updatePDFView()
- if filePathLabel.stringValue.count > 0 {
- doneButton.isEnabled = true
- }
- }
- @IBAction func alphaSteperAction(_ sender: NSStepper) {
- alphaTextField.stringValue = "\(Int(sender.doubleValue * 100))%"
- background.opacity = sender.doubleValue
- alphaSlider.doubleValue = sender.doubleValue
- self.updatePDFView()
- if filePathLabel.stringValue.count > 0 {
- doneButton.isEnabled = true
- }
- }
- @IBAction func goPrevious(_ sender: Any) {
- if pdfView.canGoToPreviousPage() {
- pdfView.goToPreviousPage(nil)
- }
- // let index = pdfDocument!.index(for: pdfView.currentPage())
- // currentPageIndexTextField.stringValue = "\(index + 1)"
- }
- @IBAction func goNext(_ sender: Any) {
- if pdfView.canGoToNextPage() {
- pdfView.goToNextPage(nil)
- }
- // let index = pdfDocument!.index(for: pdfView.currentPage())
- // currentPageIndexTextField.stringValue = "\(index + 1)"
- }
- @IBAction func buttonClicked_BrowserFile(_ sender: Any) {
- let openPanel = NSOpenPanel()
- openPanel.canChooseDirectories = false
- openPanel.canChooseFiles = true
- openPanel.allowsMultipleSelection = false
- openPanel.allowedFileTypes = ["jpg", "cur", "bmp", "jpeg", "gif", "png", "tiff", "tif", "ico", "icns", "tga", "psd", "eps", "hdr", "jp2", "jpc", "pict", "sgi", "pdf"]
- openPanel.beginSheetModal(for: window!) { (result) in
- if result == .OK {
- guard let url = openPanel.url else { return }
- let filePath: NSString = url.path as NSString
- if filePath.pathExtension.lowercased() == "pdf" {
- let pdf = CPDFDocument(url: url)
- if pdf?.isEncrypted == true {
- return
- }
- }
- if let image = NSImage(contentsOfFile: url.path), !self.isDamageImage(image, imagePath: url.path) {
- self.filePathLabel.stringValue = url.path
- self.doneButton.isEnabled = true
- self.background.imagePath = url.path
- self.background.image = image
- self.background.backgroundID = url.path.lastPathComponent.deletingPathExtension
- self.templateNameTextField.stringValue = url.path.lastPathComponent.deletingPathExtension
- self.self.updatePDFView()
- }
- }
- }
- }
- // ... (remaining IBActions)
- @IBAction func buttonClicked_Batch(_ sender: Any) {
- if background.type == .color {
- background.imagePath = ""
- } else {
- background.color = nil
- if background.image == nil {
- return
- }
- }
- if templateNameTextField.stringValue.count < 1 {
- background.backgroundID = initialID
- } else {
- background.backgroundID = templateNameTextField.stringValue
- }
- let needSave = saveToTemplateButton.state == .on
- if needSave {
- KMBackgroundManager.defaultManager.addTemplate(model: background)
- }
- operateCallBack?(background, currentType)
- }
- @IBAction func buttonClicked_Done(_ sender: Any) {
- guard let pdfDocument = pdfDocument else { return }
- if background.type == .color {
- background.imagePath = ""
- } else {
- background.color = nil
- if background.image == nil {
- return
- }
- }
- if templateNameTextField.stringValue.isEmpty {
- background.backgroundID = initialID
- } else {
- background.backgroundID = templateNameTextField.stringValue
- }
- // Avoid showing the page range alert twice
- if checkPageRangeValidate(pageRangeComboBox.stringValue) {
- background.pagesString = pageRangeComboBox.stringValue
- self.updatePDFView()
- window?.makeFirstResponder(self)
- }
- let needSave = saveToTemplateButton.state == .on
- var pages = [Int]()
- switch pageRangeComboBox.indexOfSelectedItem {
- case 0:
- pages = Array(0..<Int(pdfDocument.pageCount))
- case 1:
- pages = Array(stride(from: 0, to: Int(pdfDocument.pageCount), by: 2))
- case 2:
- pages = Array(stride(from: 1, to: Int(pdfDocument.pageCount), by: 2))
- default:
- var fileAttribute = self._fileAttri
- if fileAttribute == nil {
- fileAttribute = KMFileAttribute()
- fileAttribute?.password = self.pdfDocument?.password ?? ""
- fileAttribute?.filePath = self.pdfDocument?.documentURL.path ?? ""
- self._fileAttri = fileAttribute
- }
- // fileAttribute.filePath = pdfDocument.documentURL?.path ?? ""
- fileAttribute?.bAllPage = false
- fileAttribute?.pagesType = .custom
- // fileAttribute.password = pdfDocument.password ?? ""
- fileAttribute?.pagesString = pageRangeComboBox.stringValue
- let selectPages = fileAttribute?.fetchSelectPages() ?? []
- if selectPages.count != 0 {
- pages = selectPages.map { $0 - 1 }
- } else {
- let alert = NSAlert()
- alert.alertStyle = .critical
- alert.messageText = "\(fileAttribute?.filePath.lastPathComponent ?? "") \(NSLocalizedString("Invalid page range or the page number is out of range. Please try again.", comment: ""))"
- alert.runModal()
- return
- }
- }
- background.pagesString = pages.isEmpty ? "" : pages.map { "\($0)" }.joined(separator: ",")
- switch type {
- case .add:
- if needSave {
- if checkPageRangeValidate(pageRangeComboBox.stringValue) && pageRangeComboBox.indexOfSelectedItem == -1 {
- background.pagesString = pageRangeComboBox.stringValue
- }
- KMBackgroundManager.defaultManager.addTemplate(model: background)
- }
- operateCallBack?(background, currentType)
- case .edit:
- if needSave {
- if checkPageRangeValidate(pageRangeComboBox.stringValue) && pageRangeComboBox.indexOfSelectedItem == -1 {
- background.pagesString = pageRangeComboBox.stringValue
- }
- originalBackground = (background.copy() as? KMBackgroundModel)!
- KMBackgroundManager.defaultManager.updateTemplate(model: originalBackground)
- }
- operateCallBack?(originalBackground, currentType)
- case .use:
- let fileName = "\(pdfDocument.documentURL?.deletingPathExtension().lastPathComponent ?? NSLocalizedString("Untitled", comment: ""))_Background"
- let savePanelAccessoryViewController = KMSavePanelAccessoryController()
- let savePanel = NSSavePanel()
- savePanel.nameFieldStringValue = fileName
- savePanel.allowedFileTypes = ["pdf"]
- savePanel.accessoryView = savePanelAccessoryViewController.view
- savePanel.beginSheetModal(for: window!) { result in
- if result == .OK {
- self.saveAsPDF(with: self.background, to: savePanel.url?.path ?? "", autoOpen: savePanelAccessoryViewController.openAutomaticButton.state == .on ? true : false)
- }
- }
- }
- }
- }
|