123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269 |
- //
- // KMWatermarkViewController.swift
- // PDF Reader Pro
- //
- // 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<Array<KMWatermarkAdjectiveTopBarItemModel>> = []
- for items in itemTitles {
- var array: Array<KMWatermarkAdjectiveTopBarItemModel> = []
- 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("PDFull cannot find a watermark in this file to remove.If you see a watermark,it was not added in PDFull,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.watermark = model
- self.topBarView.isCanApply(can: true)
- } else {
- preView.watermark = KMWatermarkModel()
- 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.watermark = 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 = .all
- 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
- }
-
- }
|