// // KMWatermarkViewController.swift // PDF Master // // Created by tangchao on 2022/12/15. // import Cocoa /// 水印 预览控制器 class KMWatermarkViewController: KMWatermarkAdjectivePreViewBaseController { var createController: KMWatermarkPropertyCreateController! var propertyController: KMWatermarkPropertyController! var editController: KMWatermarkPropertyCreateController! override func viewDidLoad() { super.viewDidLoad() let itemTitles = [[NSLocalizedString("Add Watermark", comment: ""), NSLocalizedString("Delete Watermark", comment: "")], [NSLocalizedString("Batch", comment: "")]] var itemModels: Array> = [] for items in itemTitles { var array: Array = [] for title in items { let model = KMWatermarkAdjectiveTopBarItemModel() model.iconName = "" model.itemTitle = title array.append(model) } itemModels.append(array) } self.topBarView.initItemData(itemArrays: itemModels) self.topBarView.selectTopItem(index: 0) let preView: KMWatermarkPDFView_OC = KMWatermarkPDFView_OC() self.preView = preView self.preView.frame = self.preViewBox.contentView!.bounds self.preView.autoresizingMask = [.width, .height] self.preViewBox.contentView?.addSubview(self.preView) self.preView.setDisplay(.singlePage) let controller = KMWatermarkPropertyHomeController() controller.preView = self.preView controller.view.frame = self.rightBox.contentView!.bounds controller.view.autoresizingMask = [.width, .height] self.right_gotoViewController(viewController: controller) controller.modelDidChange = { [weak self] model in if (model == nil) { self!.topBarView.isCanApply(can: false) } else { self!.topBarView.isCanApply(can: true) } self!.model = model (self?.preView as! KMWatermarkPDFView_OC).model = model as Any } } override func viewDidAppear() { super.viewDidAppear() (self.rightViewController as! KMWatermarkPropertyHomeController).pageCount = Int(self.preView.document.pageCount) } override func applyAction() { super.applyAction() self.beginLoading() let preView: KMWatermarkPDFView_OC = self.preView as! KMWatermarkPDFView_OC self.addWatermark(model: preView.model as! KMWatermarkModel, toPath: self.preView.document.documentURL.path) { result in self.endLoading() DispatchQueue.main.async { self.preView.layoutDocumentView() self.preView.setNeedsDisplayForVisiblePages() if (result) { guard let callback = self.itemClick else { return } callback(1, nil) } else { let alert = NSAlert() alert.alertStyle = .critical alert.messageText = NSLocalizedString("Failure", comment: "") alert.runModal() } } } } override func topItemClick(index: Int) { super.topItemClick(index: index) if (index == 0) { /// 添加 return } if (index == 2) { /// 批量 KMBatchWindowController.openFile(nil, .Watermark) return } /// 移除水印 let watermarks = self.preView.document.watermarks() if (watermarks == nil || watermarks!.count <= 0) { let alert = NSAlert() alert.alertStyle = .warning alert.messageText = NSLocalizedString("PDF Master cannot find a watermark in this file to remove.If you see a watermark,it was not added in PDF Master,and cannot be detected as such?", comment: "") alert.addButton(withTitle: NSLocalizedString("OK", comment: "")) alert.runModal() return } let alert = NSAlert() alert.alertStyle = .warning alert.messageText = NSLocalizedString("Are you sure you want to remove the Watermark for”” documents?", comment: "") alert.addButton(withTitle: NSLocalizedString("Delete", comment: "")) alert.addButton(withTitle: NSLocalizedString("Cancel", comment: "")) let result = alert.runModal() if (result != .alertFirstButtonReturn) { return } self.beginLoading() self.deleteWatermarks(toPath: self.preView.document.documentURL.path) { result in DispatchQueue.main.async { self.preView.layoutDocumentView() self.preView.setNeedsDisplayForVisiblePages() } self.endLoading() } } func gotoPropertyController() { let propertyController = KMWatermarkPropertyController() propertyController.view.frame = self.rightBox.contentView!.bounds propertyController.view.autoresizingMask = NSView.AutoresizingMask(rawValue: 18) self.right_gotoViewController(viewController: propertyController) propertyController.preView = self.preView self.propertyController = propertyController self.propertyController.rightButtonClick = { [self] (index: Int)->() in /// 创建水印 self.gotoPropertyCreateController() } self.propertyController.templateModelDidSelected = { (model: KMWatermarkModel) in let preView: KMWatermarkPDFView = self.preView as! KMWatermarkPDFView let templateModel: KMWatermarkTemplateModel = model as! KMWatermarkTemplateModel if (templateModel.isSelected) { preView.watermarkModel = model self.topBarView.isCanApply(can: true) } else { preView.watermarkModel = nil self.topBarView.isCanApply(can: false) } preView.setNeedsDisplay(preView.bounds) } self.propertyController.templateMenuItemClick = { (itemIndex: Int, model: KMWatermarkModel) in if (itemIndex == 1) { self.gotoPropertyEditController() let index: Int = self.propertyController.tabView.indexOfTabViewItem(self.propertyController.tabView.selectedTabViewItem!) self.editController.updateModel(model: model, type: index) } } } func gotoPropertyCreateController() { let watermarks = self.preView.document.watermarks() if (watermarks != nil && watermarks!.count > 0) { /// /// 替换水印 let alert = NSAlert() alert.alertStyle = .warning alert.messageText = NSLocalizedString("文件中已添加水印,您要替换现有的水印吗?", comment: "") alert.addButton(withTitle: NSLocalizedString("替换", comment: "")) alert.addButton(withTitle: NSLocalizedString("Cancel", comment: "")) let result = alert.runModal() if (result != .alertFirstButtonReturn) { return } /// 替换 for i in 0 ..< watermarks!.count { let model = watermarks![i] self.preView.document.removeWatermark(model) } self.preView.setNeedsDisplayForVisiblePages() } self.topBarView.isCanApply(can: true) let preView: KMWatermarkPDFView = self.preView as! KMWatermarkPDFView preView.watermarkModel = self.createWatermarkTemplate() preView.setNeedsDisplay(preView.bounds) let createController = KMWatermarkPropertyCreateController() createController.preView = preView createController.model = self.createWatermarkTemplate() createController.view.frame = self.rightBox.contentView!.bounds createController.view.autoresizingMask = NSView.AutoresizingMask(rawValue: 18) self.right_gotoViewController(viewController: createController) self.createController = createController self.createController.tabView.selectTabViewItem(at: 0) self.createController.backClick = { ()->() in self.gotoPropertyController() } self.createController.rightButtonClick = { (index: Int)->() in /// 保存模块 let result = KMWatermarkManager.defaultManager.addWatermark(watermark: self.createController.model as! KMWatermarkModel) if (result) { self.gotoPropertyController() } } } func gotoPropertyEditController() { let editController = KMWatermarkPropertyCreateController() editController.isEdit = true editController.preView = self.preView editController.model = self.model editController.view.frame = self.rightBox.contentView!.bounds editController.view.autoresizingMask = NSView.AutoresizingMask(rawValue: 18) self.right_gotoViewController(viewController: editController) self.editController = editController self.editController.backClick = { self.gotoPropertyController() let result = KMWatermarkManager.defaultManager.updateWatermark(watermark: self.editController.model as! KMWatermarkModel) if (result) { self.propertyController.updateDatas() } } } func createWatermarkTemplate() -> KMWatermarkModel { let model = KMWatermarkModel() model.textFont = .font(name: "Helvetica", size: 48) model.textColor = .color(red: 0, green: 0, blue: 0, alpha: 1.0) model.opacity = 1 model.pageRangeType = 0 model.scale = 0.5 model.verticalMode = 1 model.horizontalMode = 1 model.isFront = true model.tileVerticalSpace = 6 model.tileHorizontalSpace = 6 model.isTilePage = false model.pagesString = "" model.isFront = true model.text = NSLocalizedString("Watermark", comment: "") model.watermarkID = KMWatermarkManager.defaultManager.fetchAvailableName() return model } }