// // KMWatermarkController.swift // PDF Reader Pro // // Created by Niehaoyu on 2024/11/1. // import Cocoa import KMComponentLibrary @objc protocol KMWatermarkControllerDelegate: AnyObject { @objc optional func kmWatermarkControllerDidUpdateMode(_ view: KMWatermarkController) //水印数据发生变化时处理 @objc optional func kmWatermarkControllerDidWatermarkUpdated(_ view: KMWatermarkController) } class KMWatermarkController: NSViewController { @IBOutlet var contendView: NSView! @IBOutlet var preInfoView: NSView! @IBOutlet var propertyInfoView: NSView! var documentPreview: KMEditDocumentController = KMEditDocumentController.init() var propertyController: KMNWatermarkPropertyController = KMNWatermarkPropertyController.init() var templatesController: KMNWatermarkTemplateController = KMNWatermarkTemplateController() var saveTemplateMessage: ComponentMessage = ComponentMessage() var pdfDocument: CPDFDocument? var currentWatermarkData: KMPDFWatermarkData? weak open var delegate: KMWatermarkControllerDelegate? var editSubType: KMPDFEditSubModeType = .none //MARK: - func override func viewDidLoad() { super.viewDidLoad() // Do view setup here. documentPreview.view.frame = preInfoView.bounds documentPreview.view.autoresizingMask = [.width, .height] preInfoView.addSubview(documentPreview.view) propertyController.view.frame = propertyInfoView.bounds propertyController.view.autoresizingMask = [.width, .height] propertyController.delegate = self propertyController.view.isHidden = true propertyInfoView.addSubview(propertyController.view) templatesController.view.frame = propertyInfoView.bounds templatesController.view.autoresizingMask = [.width, .height] templatesController.delegate = self templatesController.view.isHidden = true propertyInfoView.addSubview(templatesController.view) setupProperty() } func setupProperty() { saveTemplateMessage.properties = ComponentMessageProperty(messageType: .success, title: KMLocalizedString("Saved Template")) } func resetUI() { propertyController.view.isHidden = true templatesController.view.isHidden = true if editSubType == .template { templatesController.view.isHidden = false } else if editSubType == .add { propertyController.view.isHidden = false propertyController.editSubType = editSubType } else if editSubType == .edit { propertyController.view.isHidden = false propertyController.editSubType = editSubType } } func reloadData() { documentPreview.pdfDocument = self.pdfDocument documentPreview.reloadData() if editSubType == .template { templatesController.reloadData() } else if editSubType == .add { propertyController.editSubType = editSubType propertyController.watermarkData = KMPDFWatermarkData.defaultData() propertyController.watermarkData.cPDFWatermark = CPDFWatermark(document: pdfDocument, type: .text) propertyController.reloadData() currentWatermarkData = propertyController.watermarkData } else if editSubType == .edit { propertyController.editSubType = editSubType propertyController.reloadData() } refreshPDFDocumentWaterMark() } func removeWatermark(_ watermark: CPDFWatermark?) { if let tempData = watermark { let document = documentPreview.pdfDocument document?.removeWatermark(tempData) } } func refreshPDFDocumentWaterMark() { guard let model = currentWatermarkData else { documentPreview.refreshUI() return } guard let pdfWatermark = model.cPDFWatermark else { return } let document = documentPreview.pdfDocument if (model.watermarkType == .text) { pdfWatermark.text = model.text pdfWatermark.textColor = model.textColor pdfWatermark.cFont = CPDFFont(familyName: model.fontName, fontStyle: model.fontStyle ?? "Regular") if model.isScale { pdfWatermark.setAutoSizeWithPercentage(model.scale) } else { pdfWatermark.fontSize = model.fontSize } } else if model.watermarkType == .image { pdfWatermark.image = model.image() if model.isScale { pdfWatermark.scale = model.scale } else { pdfWatermark.scale = 1 } } pdfWatermark.opacity = model.opacity pdfWatermark.rotation = model.rotation pdfWatermark.tx = model.tx pdfWatermark.ty = model.ty pdfWatermark.isFront = model.isFront pdfWatermark.horizontalPosition = model.horizontalPosition pdfWatermark.verticalPosition = model.verticalPosition pdfWatermark.isTilePage = model.isTilePage pdfWatermark.horizontalSpacing = model.horizontalSpacing pdfWatermark.verticalSpacing = model.verticalSpacing if let watermarks = document?.watermarks(){ document?.updateWatermark(pdfWatermark) } else { document?.addWatermark(pdfWatermark) } documentPreview.refreshUI() } } //MARK: - KMNWatermarkPropertyControllerDelegate extension KMWatermarkController: KMNWatermarkPropertyControllerDelegate { func watermarkPropertyControllerDidRemoveWatermark(_ controller: KMNWatermarkPropertyController, _ type: CPDFWatermarkType) { removeWatermark(controller.watermarkData.cPDFWatermark) if type == .text { controller.watermarkData.cPDFWatermark = nil controller.watermarkData.cPDFWatermark = CPDFWatermark(document: pdfDocument, type: .text) } else if type == .image { controller.watermarkData.cPDFWatermark = nil controller.watermarkData.cPDFWatermark = CPDFWatermark(document: pdfDocument, type: .image) } } func watermarkPropertyControllerDidUpdate(_ controller: KMNWatermarkPropertyController) { removeWatermark(currentWatermarkData?.cPDFWatermark) currentWatermarkData = controller.watermarkData refreshPDFDocumentWaterMark() delegate?.kmWatermarkControllerDidWatermarkUpdated?(self) } func watermarkPropertyControllerDidChangetoTemplate(_ controller: KMNWatermarkPropertyController) { editSubType = .template delegate?.kmWatermarkControllerDidUpdateMode?(self) resetUI() removeWatermark(currentWatermarkData?.cPDFWatermark) currentWatermarkData = nil delegate?.kmWatermarkControllerDidWatermarkUpdated?(self) documentPreview.layoutDocumentView() } func watermarkPropertyControllerSaveTemplateSuccess(_ controller: KMNWatermarkPropertyController, _ data: KMPDFWatermarkData) { saveTemplateMessage.frame = CGRectMake((CGRectGetWidth(self.view.frame) - saveTemplateMessage.properties.propertyInfo.viewWidth)/2, CGRectGetHeight(self.view.frame) - saveTemplateMessage.properties.propertyInfo.viewHeight - 8, saveTemplateMessage.properties.propertyInfo.viewWidth, saveTemplateMessage.properties.propertyInfo.viewHeight) saveTemplateMessage.reloadData() saveTemplateMessage.show(inView: self.view, autoHideSeconde: 2) editSubType = .template delegate?.kmWatermarkControllerDidUpdateMode?(self) resetUI() templatesController.selectedWatermark = data templatesController.reloadData() removeWatermark(currentWatermarkData?.cPDFWatermark) currentWatermarkData = templatesController.selectedWatermark refreshPDFDocumentWaterMark() } func watermarkPropertyControllerCancelTemplateEdit(_ controller: KMNWatermarkPropertyController) { removeWatermark(currentWatermarkData?.cPDFWatermark) currentWatermarkData = nil refreshPDFDocumentWaterMark() editSubType = .template delegate?.kmWatermarkControllerDidUpdateMode?(self) resetUI() templatesController.reloadData() } } //MARK: - KMNWatermarkTemplateControllerDelegate extension KMWatermarkController: KMNWatermarkTemplateControllerDelegate { func templateControllerDidAddWatermarkData(_ controller: KMNWatermarkTemplateController) { editSubType = .add resetUI() delegate?.kmWatermarkControllerDidUpdateMode?(self) propertyController.watermarkData = KMPDFWatermarkData.defaultData() propertyController.watermarkData.cPDFWatermark = CPDFWatermark(document: pdfDocument, type: .text) propertyController.reloadData() currentWatermarkData = propertyController.watermarkData refreshPDFDocumentWaterMark() delegate?.kmWatermarkControllerDidWatermarkUpdated?(self) } func templateControllerDidEditWatermarkData(_ controller: KMNWatermarkTemplateController, _ watermark: KMPDFWatermarkData) { removeWatermark(currentWatermarkData?.cPDFWatermark) editSubType = .edit delegate?.kmWatermarkControllerDidUpdateMode?(self) resetUI() propertyController.watermarkData = watermark propertyController.originalDataDict = KMWatermarkManager.defaultManager.parseWaterMark(model: watermark) propertyController.reloadData() currentWatermarkData = propertyController.watermarkData refreshPDFDocumentWaterMark() templatesController.selectedWatermark = nil delegate?.kmWatermarkControllerDidWatermarkUpdated?(self) } func templateControllerDidSelectedChanged(_ controller: KMNWatermarkTemplateController) { removeWatermark(currentWatermarkData?.cPDFWatermark) if let selectedWatermark = controller.selectedWatermark { if selectedWatermark.cPDFWatermark == nil { if selectedWatermark.watermarkType == .text { selectedWatermark.cPDFWatermark = CPDFWatermark(document: pdfDocument, type: .text) } else if controller.selectedWatermark?.watermarkType == .image { selectedWatermark.cPDFWatermark = CPDFWatermark(document: pdfDocument, type: .image) } } } currentWatermarkData = controller.selectedWatermark refreshPDFDocumentWaterMark() delegate?.kmWatermarkControllerDidWatermarkUpdated?(self) } }