1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285 |
- //
- // KMWatermarkView.swift
- // PDF Reader Pro
- //
- // Created by lizhe on 2023/11/14.
- //
- import Cocoa
- enum KMWatermarkManagerType {
- case add
- case edit
- case use
- }
- typealias KMWatermarkViewOperateCallBack = (_ watermark: KMWatermarkModel, _ countType: Int) -> ()
- typealias KMWatermarkViewCancelAction = (_ view: KMWatermarkView) -> Void
- class KMWatermarkView: KMBaseXibView, NSTextFieldDelegate, NSComboBoxDelegate, NSTextViewDelegate, CPDFViewDelegate {
- //顶部box
- @IBOutlet var typeBox: NSBox!
- @IBOutlet var txtButton: NSButton!
- @IBOutlet var fileButton: NSButton!
- @IBOutlet var filePathLabel: NSTextField!
- @IBOutlet var browsebutton: NSButton!
- @IBOutlet var ratioLabel: NSTextField!
- @IBOutlet var ratioTextField: NSTextField!
- @IBOutlet var ratioStepper: NSStepper!
- @IBOutlet var fontLabel: NSTextField!
- @IBOutlet var fontComboBox: NSComboBox!
- @IBOutlet var fontSizeLabel: NSTextField!
- @IBOutlet var fontSizeCombobox: NSComboBox!
- @IBOutlet var colorLabel: NSTextField!
- @IBOutlet var colorWell: NSColorWell!
- @IBOutlet var watermarkStringTextView: NSTextView!
- var maskView: KMBookletMaskView?
-
- //底部box
- @IBOutlet var appearanceBox: NSBox!
- @IBOutlet var angleLabel: NSTextField!
- @IBOutlet var angleTextF: NSTextField!
- @IBOutlet var angleStepper: NSStepper!
- @IBOutlet var left45IndicateView: KMAngleIndicateView!
- @IBOutlet var horizontalIndicateView: KMAngleIndicateView!
- @IBOutlet var right45IndicateView: KMAngleIndicateView!
-
- @IBOutlet var alphaLabel: NSTextField!
- @IBOutlet var alphaSlider: NSSlider!
- @IBOutlet var alphaTextField: NSTextField!
- @IBOutlet var alphaStepper: NSStepper!
- @IBOutlet var watermarkPositionLabel: NSTextField!
- @IBOutlet var OnPageButton: NSButton!
- @IBOutlet var backPageButton: NSButton!
- @IBOutlet var positionView: KMPostionIndicateView!
- @IBOutlet var horizentalGapLabel: NSTextField!
- @IBOutlet var verticalGapLabel: NSTextField!
- @IBOutlet var horizentalGapTextField: NSTextField!
- @IBOutlet var verticalFapTextField: NSTextField!
- @IBOutlet var horizentalGapStepper: NSStepper!
- @IBOutlet var verticalGapStepper: NSStepper!
- @IBOutlet var pageRangeLabel: NSTextField!
- @IBOutlet var pageRangeComboBox: NSComboBox!
-
- //保存到模版按钮
- @IBOutlet var saveToTemplateButton: NSButton!
- @IBOutlet var templateNameLabel: NSTextField!
- @IBOutlet var templateNameTextField: NSTextField!
-
- @IBOutlet var isTileWater: NSButton!
- @IBOutlet var spacingHorTextField: NSTextField!
- @IBOutlet var spacingHorStepper: NSStepper!
- @IBOutlet var horizentalIconButton: NSButton!
- @IBOutlet var verticalIconButton: NSButton!
- @IBOutlet var spacingVerTextField: NSTextField!
- @IBOutlet var spacingStepper: NSStepper!
-
- //底部按钮
- @IBOutlet var batchButton: NSButton!
- @IBOutlet var cancelButton: NSButton!
- @IBOutlet var applyButton: NSButton!
-
- //左侧
- @IBOutlet var previewPageButton: NSButton!
- @IBOutlet var nextPageButton: NSButton!
- @IBOutlet var currentPageIndexTextF: NSTextField!
- @IBOutlet var totalPageCountLabel: NSTextField!
- @IBOutlet var pdfView: KMWatermarkPDFView!
- var fontModified: Bool = false
-
- var currentType: Int = 0
-
- var filePath: String = Bundle.main.path(forResource: "Quick Start Guide", ofType: "pdf") ?? ""
- var password: String = ""
- var originalWatermark: KMWatermarkModel = KMWatermarkModel()
- var watermark: KMWatermarkModel = KMWatermarkModel()
- var type: KMWatermarkManagerType = .use {
- didSet {
- if type == .edit {
-
- } else {
- watermark.watermarkID = KMWatermarkManager.defaultManager.fetchAvailableName()
- }
- self.templateNameTextField.stringValue = self.watermark.watermarkID
- }
- }
- var pdfDocument: CPDFDocument? {
- didSet {
- self._fileAttri = KMFileAttribute()
- self._fileAttri?.password = self.pdfDocument?.password ?? ""
- self._fileAttri?.filePath = self.pdfDocument?.documentURL?.path ?? ""
- self.password = pdfDocument?.password ?? ""
- DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.3) {
- self.pdfView.watermark = self.watermark
- self.pdfView.document = self.pdfDocument
-
- self.pdfView.autoScales = true
- self.pdfView.documentView().enclosingScrollView?.scrollerStyle = .overlay
- self.pdfView.documentView().enclosingScrollView?.hasVerticalScroller = false
- self.pdfView.documentView().enclosingScrollView?.hasHorizontalScroller = false
- }
- // self.reloadData()
- }
- }
-
- var onlyManagerTemplate: Bool = true
- var isHiddenBatchBtn: Bool = false
- private var _fileAttri: KMFileAttribute?
-
- var cancelAction: KMWatermarkViewCancelAction?
- var operateCallBack: KMWatermarkViewOperateCallBack?
-
- convenience init(baseFile filePath: String, watermark: KMWatermarkModel, password: String, type: KMWatermarkManagerType, fileType countType: Int) {
- self.init()
-
- self.filePath = filePath
- self.password = password
- self.originalWatermark = watermark.copy() as! KMWatermarkModel
- self.watermark = watermark.copy() as! KMWatermarkModel
- self.type = type
- self.pdfDocument = CPDFDocument(url: URL(fileURLWithPath: self.filePath))
- self.currentType = countType
-
- if pdfDocument?.isLocked ?? false {
- pdfDocument?.unlock(withPassword: password)
- }
- }
-
- override func setup() {
- pdfView.setDisplay(.singlePage)
-
- // if (pdfView.documentView() != nil) {
- // pdfView.setDisplay(.singlePage)
- // pdfView.documentView().enclosingScrollView?.scrollerStyle = .overlay
- // pdfView.documentView().enclosingScrollView?.hasVerticalScroller = false
- // pdfView.documentView().enclosingScrollView?.hasHorizontalScroller = false
- // }
-
- for i in 0..<3 {
- for j in 0..<3 {
- if i == Int(watermark.horizontalMode) && j == Int(watermark.verticalMode) {
- positionView.style = KMPositionIndicateViewStyle(rawValue: i + 3 * j)!
- }
- }
- }
-
- positionView.styleChangedCallBack = { [weak self] in
- guard let self = self else { return }
- self.watermark.horizontalMode = self.positionView.style.rawValue % 3
- self.watermark.verticalMode = self.positionView.style.rawValue / 3
- self.updatePDFView()
- }
-
- currentPageIndexTextF.stringValue = "1"
- // let numberFormatter = currentPageIndexTextF.formatter as! NumberFormatter
- // numberFormatter.maximum = NSNumber(value: pdfDocument!.pageCount)
- pdfView.delegate = self
-
- let formatter = TextFieldFormatter.init()
- formatter.allowedCharacterSet = "-"
- self.angleTextF.formatter = formatter
- angleTextF.delegate = self
-
- let formatter2 = TextFieldFormatter.init()
- formatter2.allowedCharacterSet = "%"
- self.alphaTextField.formatter = formatter2
- alphaTextField.delegate = self
-
- let formatter3 = TextFieldFormatter.init()
- formatter3.allowedCharacterSet = ".-"
- self.horizentalGapTextField.formatter = formatter3
- horizentalGapTextField.delegate = self
-
- let formatter4 = TextFieldFormatter.init()
- formatter4.allowedCharacterSet = ".-"
- self.verticalFapTextField.formatter = formatter4
- verticalFapTextField.delegate = self
-
- let formatter5 = TextFieldFormatter.init()
- formatter5.allowedCharacterSet = ""
- self.spacingHorTextField.formatter = formatter5
- spacingHorTextField.delegate = self
-
- let formatter6 = TextFieldFormatter.init()
- formatter6.allowedCharacterSet = ""
- self.spacingVerTextField.formatter = formatter6
- spacingVerTextField.delegate = self
-
- left45IndicateView.style = .left45
- left45IndicateView.touchCallBack = { [weak self] in
- guard let self = self else { return }
- self.watermark.rotation = -45
- self.angleStepper.doubleValue = self.watermark.rotation
- self.angleTextF.stringValue = "\(self.angleStepper.intValue)"
- self.checkAngle()
- self.updatePDFView()
- }
-
- horizontalIndicateView.style = .horizontal
- horizontalIndicateView.touchCallBack = { [weak self] in
- guard let self = self else { return }
- self.watermark.rotation = 0
- self.angleStepper.doubleValue = 0
- self.angleTextF.stringValue = "\(0)"
- self.checkAngle()
- self.updatePDFView()
- }
-
- right45IndicateView.style = .right45
- right45IndicateView.touchCallBack = { [weak self] in
- guard let self = self else { return }
- self.watermark.rotation = 45
- self.angleStepper.doubleValue = self.watermark.rotation
- self.angleTextF.stringValue = "\(self.angleStepper.intValue)"
- self.checkAngle()
- self.updatePDFView()
- }
-
- saveToTemplateButton.isEnabled = onlyManagerTemplate
-
- // if type == .use {
- // saveToTemplateButton.isHidden = true
- // saveToTemplateButton.state = .off
- // } else {
- // saveToTemplateButton.isHidden = false
- // saveToTemplateButton.state = .on
- // }
-
- if type == .add {
- applyButton.title = NSLocalizedString("Apply", comment: "")
- batchButton.isHidden = true
- } else if type == .edit {
- applyButton.title = NSLocalizedString("Apply", comment: "")
- batchButton.isHidden = true
- } else if type == .use {
- applyButton.title = NSLocalizedString("Save & Apply", comment: "")
- }
-
- filePathLabel.stringValue = ""
- filePathLabel.placeholderString = NSLocalizedString("Select a File", comment: "")
-
- fontSizeCombobox.removeAllItems()
- fontSizeCombobox.addItems(withObjectValues: ["9","10","11","12","13","14","18","24","36","48","64","72","96","144","288"])
- fontSizeCombobox.formatter = TextFieldFormatter()
-
- watermarkStringTextView.delegate = self
- self.watermarkStringTextView.insertionPointColor = .labelColor
-
- 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
-
- fontSizeCombobox.wantsLayer = true
- pageRangeComboBox.wantsLayer = true
-
- fontSizeCombobox.layer?.cornerRadius = 3.0
- pageRangeComboBox.layer?.cornerRadius = 3.0
-
- self.verticalFapTextField.delegate = self
- self.horizentalGapTextField.delegate = self
- self.spacingHorTextField.delegate = self
- self.spacingVerTextField.delegate = self
- }
-
- override func updateLanguage() {
- typeBox.title = NSLocalizedString("Source", comment: "")
- typeBox.titleFont = NSFont.systemFont(ofSize: 13)
- txtButton.title = NSLocalizedString("Text", comment: "")
- fileButton.title = NSLocalizedString("File", comment: "")
- browsebutton.title = NSLocalizedString("Choose...", comment: "")
- ratioLabel.stringValue = "\(NSLocalizedString("Ratio", comment: "")):"
- fontLabel.stringValue = NSLocalizedString("Font", comment: "")
- horizentalIconButton.toolTip = NSLocalizedString("Horizontal Distance", comment: "")
- verticalIconButton.toolTip = NSLocalizedString("Vertical Distance", comment: "")
- fontSizeLabel.stringValue = "\(NSLocalizedString("Font Size", comment: "")):"
- colorLabel.stringValue = "\(NSLocalizedString("Color", comment: "")):"
- templateNameTextField.stringValue = watermark.watermarkID ?? ""
-
- appearanceBox.title = NSLocalizedString("Appearance", comment: "")
- appearanceBox.titleFont = NSFont.systemFont(ofSize: 13)
- angleLabel.stringValue = "\(NSLocalizedString("Rotation", comment: "")):"
- alphaLabel.stringValue = "\(NSLocalizedString("Opacity", comment: "")):"
- watermarkPositionLabel.stringValue = "\(NSLocalizedString("Position", comment: "")):"
- OnPageButton.title = NSLocalizedString("Appear on top of page", comment: "")
- backPageButton.title = NSLocalizedString("Appear behind page", comment: "")
- pageRangeLabel.stringValue = "\(NSLocalizedString("Page Range", comment: "")):"
- isTileWater.title = NSLocalizedString("Tile", comment: "")
-
- OnPageButton.toolTip = NSLocalizedString("Appear on top of page", comment: "")
- backPageButton.toolTip = NSLocalizedString("Appear behind page", comment: "")
- pageRangeLabel.toolTip = "\(NSLocalizedString("Page Range", comment: "")):"
-
- horizentalGapLabel.stringValue = "X:"
- verticalGapLabel.stringValue = "Y:"
-
- saveToTemplateButton.title = NSLocalizedString("Add to Template", comment: "")
-
- templateNameLabel.stringValue = NSLocalizedString("Name:", comment: "")
-
- batchButton.title = NSLocalizedString("Batch", comment: "")
- cancelButton.title = NSLocalizedString("Cancel", comment: "")
- }
-
- override func reloadData() {
- guard let pdfDocument = pdfDocument else { return }
- let opacity = round(watermark.opacity * 100) / 100
-
- watermarkStringTextView.string = watermark.text
- colorWell.color = watermark.getTextColor()
-
- totalPageCountLabel.stringValue = "/ \(pdfDocument.pageCount)"
-
- fontSizeCombobox.stringValue = "\(Int(watermark.getTextFontSize()))"
- angleStepper.doubleValue = Double(watermark.rotation)
- angleTextF.stringValue = "\(angleStepper.intValue)"
-
- OnPageButton.state = watermark.isFront ? .on : .off
- backPageButton.state = watermark.isFront ? .off : .on
-
- alphaSlider.doubleValue = watermark.opacity
- alphaStepper.doubleValue = watermark.opacity
- alphaTextField.stringValue = "\(Int(opacity * 100))%"
- ratioStepper.doubleValue = watermark.scale
- ratioTextField.stringValue = "\(Int(opacity * 100))%"
- fontComboBox.stringValue = "\(watermark.getTextFontSize() )"
- filePathLabel.stringValue = watermark.imagePath
- batchButton.isHidden = isHiddenBatchBtn
- if !isHiddenBatchBtn {
- self.batchButton.isHidden = type != .use
- }
- if currentType == 0 {
- changeTypeBoxState(isTextWatermark: true)
- } else {
- changeTypeBoxState(isTextWatermark: false)
- }
-
- switch watermark.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 = watermark.pagesString
- }
-
- for i in 0..<3 {
- for j in 0..<3 {
- if i == Int(watermark.horizontalMode) && j == Int(watermark.verticalMode) {
- self.positionView.style = KMPositionIndicateViewStyle(rawValue: i + 3 * j)!
- }
- }
- }
-
- verticalGapStepper.doubleValue = Double(watermark.verticalSpace)
- verticalFapTextField.stringValue = "\(verticalGapStepper.doubleValue)"
- horizentalGapStepper.doubleValue = Double(watermark.horizontalSpace)
- horizentalGapTextField.stringValue = "\(horizentalGapStepper.doubleValue)"
-
- spacingHorTextField.stringValue = "\(watermark.tileHorizontalSpace)"
- spacingVerTextField.stringValue = "\(watermark.tileVerticalSpace)"
- spacingHorStepper.doubleValue = Double(watermark.tileHorizontalSpace)
- spacingStepper.doubleValue = Double(watermark.tileVerticalSpace)
-
- if watermark.isTilePage {
- isTileWater.state = .on
- spacingHorTextField.isEnabled = true
- spacingHorStepper.isEnabled = true
- spacingVerTextField.isEnabled = true
- spacingStepper.isEnabled = true
- } else {
- isTileWater.state = .off
- spacingHorTextField.isEnabled = false
- spacingHorStepper.isEnabled = false
- spacingVerTextField.isEnabled = false
- spacingStepper.isEnabled = false
- }
-
- pageRangeComboxAction(Any.self)
- checkAngle()
- }
-
- override func addNotification() {
- NotificationCenter.default.addObserver(self, selector: #selector(pageChangeNotification), name: NSNotification.Name.CPDFViewPageChanged, object: self.pdfView)
- }
- func updatePDFView() {
- // pdfView.needsDisplay = true
- km_synchronized(self) {
- self.pdfView.setNeedsDisplayForVisiblePages()
- }
- }
-
- func checkAngle() {
- left45IndicateView.isSelcted = false
- horizontalIndicateView.isSelcted = false
- right45IndicateView.isSelcted = false
-
- switch watermark.rotation {
- case -45:
- left45IndicateView.isSelcted = true
- case 0:
- horizontalIndicateView.isSelcted = true
- case 45:
- right45IndicateView.isSelcted = true
- default:
- break
- }
- }
-
- func changeTypeBoxState(isTextWatermark: Bool) {
- if isTextWatermark {
- txtButton.state = .on
- watermarkStringTextView.isEditable = true
- watermarkStringTextView.isSelectable = true
- fontSizeCombobox.isEnabled = true
- colorWell.isEnabled = true
-
- fileButton.state = .off
- browsebutton.isEnabled = false
- ratioTextField.isEnabled = false
- ratioStepper.isEnabled = false
-
- watermark.text = watermarkStringTextView.string
- watermarkStringTextView.string = watermarkStringTextView.string
- watermark.image = nil
-
- if watermarkStringTextView.string.isEmpty == false {
- applyButton.isEnabled = true
- batchButton.isEnabled = true
- } else {
- applyButton.isEnabled = false
- batchButton.isEnabled = false
- }
- } else {
- txtButton.state = .off
- watermarkStringTextView.isEditable = false
- watermarkStringTextView.isSelectable = false
- fontSizeCombobox.isEnabled = false
- colorWell.isEnabled = false
-
- fileButton.state = .on
- browsebutton.isEnabled = true
- ratioTextField.isEnabled = true
- ratioStepper.isEnabled = true
-
- watermark.text = ""
- if let _image = NSImage(contentsOfFile: watermark.imagePath ) {
- watermark.image = _image
- }
-
- if filePathLabel.stringValue.isEmpty == false {
- applyButton.isEnabled = true
- batchButton.isEnabled = true
- } else {
- applyButton.isEnabled = false
- batchButton.isEnabled = false
- }
- }
- updatePDFView()
- }
-
- 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?.bAllPage = false
- fileAttribute?.pagesString = self.pageRangeComboBox.stringValue
-
- var pageRange: KMPageRange = .all
- let pageRangeType: KMWatermarkeModelPageRangeType = watermark.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
-
- if let cnt = fileAttribute?.fetchSelectPages().count, cnt == 0 {
- return false
- }
- return true
- }
-
- @objc func controlTextDidEndEditing(_ obj: Notification) {
- if obj.object as? NSComboBox == self.fontSizeCombobox {
- var textSize = CGFloat(Int(self.fontSizeCombobox.stringValue) ?? 0)
- if textSize == 0 {
- textSize = 48
- if self.watermark.isTilePage {
- textSize = 18
- }
- }
- self.watermark.textFont = KMWatermarkAdjectiveText.font(name: "Helvetica", size: textSize)
- self.fontSizeCombobox.stringValue = "\(Int(textSize))"
- self.updatePDFView()
- } else if obj.object as? NSTextField == self.ratioTextField {
- let formatter = self.ratioTextField.formatter as? NumberFormatter
- _ = formatter?.number(from: self.ratioTextField.stringValue)?.floatValue ?? 0
- self.ratioStepper.doubleValue = Double(self.watermark.scale)
- self.updatePDFView()
- } else if obj.object as? NSTextField == self.angleTextF {
- self.watermark.rotation = CGFloat(Int(self.angleTextF.stringValue) ?? 0)
- self.angleStepper.doubleValue = self.watermark.rotation
- self.checkAngle()
- self.updatePDFView()
- } else if obj.object as? NSTextField == self.alphaTextField {
- let floatValue = self.alphaTextField.stringValue.replacingOccurrences(of: "%", with: "").stringToCGFloat() * 0.01
-
- self.alphaSlider.doubleValue = Double(floatValue)
- self.alphaStepper.doubleValue = Double(floatValue)
- self.watermark.opacity = Double(floatValue)
- self.updatePDFView()
- } else if obj.object as? NSComboBox == self.pageRangeComboBox {
- if self.pageRangeComboBox.indexOfSelectedItem == -1 {
- if !self.checkPageRangeValidate(self.pageRangeComboBox.stringValue) {
- let alert = NSAlert()
- alert.alertStyle = .critical
- alert.messageText = "\(String(describing: self.pdfDocument?.documentURL.lastPathComponent)) \(NSLocalizedString("Invalid page range or the page number is out of range. Please try again.", comment: ""))"
- alert.runModal()
- self.window?.makeFirstResponder(self.pageRangeComboBox)
- return
- } else {
- self.watermark.pagesString = self.pageRangeComboBox.stringValue
- self.updatePDFView()
- }
- }
- } else if obj.object as? NSTextField == self.verticalFapTextField {
- self.watermark.verticalSpace = CGFloat(Int(self.verticalFapTextField.stringValue) ?? 0)
- self.verticalGapStepper.doubleValue = Double(self.watermark.verticalSpace)
- self.updatePDFView()
- } else if obj.object as? NSTextField == self.horizentalGapTextField {
- self.watermark.horizontalSpace = CGFloat(Int(self.horizentalGapTextField.stringValue) ?? 0)
- self.horizentalGapStepper.doubleValue = Double(self.watermark.horizontalSpace)
- self.updatePDFView()
- } else if obj.object as? NSTextField == self.currentPageIndexTextF {
- self.pdfView.go(toPageIndex: (Int(self.currentPageIndexTextF.stringValue) ?? 0 - 1), animated: false)
- } else if obj.object as? NSTextField == self.spacingHorTextField {
- self.watermark.tileHorizontalSpace = CGFloat(Int(self.spacingHorTextField.stringValue) ?? 0)
- self.spacingHorStepper.doubleValue = Double(self.watermark.tileHorizontalSpace)
- self.updatePDFView()
- } else if obj.object as? NSTextField == self.spacingVerTextField {
- self.watermark.tileVerticalSpace = CGFloat(Int(self.spacingVerTextField.stringValue) ?? 0)
- self.spacingStepper.doubleValue = Double(self.watermark.tileVerticalSpace)
- self.updatePDFView()
- }
- }
-
- @objc func comboBoxSelectionDidChange(_ notification: Notification) {
- if notification.object as? NSComboBox == self.pageRangeComboBox {
- self.pageRangeComboBox.isEditable = false
- if self.pageRangeComboBox.indexOfSelectedItem == 0 {
- self.watermark.pageRangeType = .all
- } else if self.pageRangeComboBox.indexOfSelectedItem == 1 {
- self.watermark.pageRangeType = .odd
- } else if self.pageRangeComboBox.indexOfSelectedItem == 2 {
- self.watermark.pageRangeType = .even
- } else {
- self.watermark.pageRangeType = .other
- self.pageRangeComboBox.stringValue = ""
- self.pageRangeComboBox.isEditable = true
- self.window?.makeFirstResponder(self.pageRangeComboBox)
- }
- }
- }
-
- @objc func textDidChange(_ notification: Notification) {
- if notification.object as? NSTextView == self.watermarkStringTextView {
- self.watermark.text = self.watermarkStringTextView.string
- self.applyButton.isEnabled = !self.watermark.text.isEmpty
- self.batchButton.isEnabled = !self.watermark.text.isEmpty
- self.updatePDFView()
- }
- }
-
- func isDamageImage(_ image: NSImage?, imagePath path: String) -> Bool {
- let addImageAnnotation = (NSSearchPathForDirectoriesInDomains(.applicationSupportDirectory, .userDomainMask, true).last! as NSString).appendingPathComponent(Bundle.main.bundleIdentifier!)
- 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 ?? NSSize.zero
- 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("waterAnnotation.png")
- if ((try? imageData?.write(to: URL(fileURLWithPath: rPath), options: .atomic)) == nil) {
- return true
- } else {
- return false
- }
- }
-
- func tagString() -> String {
- let dateFormatter = DateFormatter()
- dateFormatter.dateFormat = "yyMMddHHmmss"
- return "\(dateFormatter.string(from: Date()))\(arc4random() % 10000)"
- }
-
- // MARK: - Action
-
- // NSNotification
- @objc func pageChangeNotification(_ notification: Notification) {
- currentPageIndexTextF.stringValue = "\(pdfView.currentPage().pageIndex() + 1)"
- }
-
- // 开始转圈,隐藏转圈提示
- func showWaitting() {
- if maskView == nil {
- maskView = KMBookletMaskView(frame: NSRect(x: 0, y: 0, width: window?.frame.size.width ?? 0, height: window?.frame.size.height ?? 0))
- }
- window?.contentView?.addSubview(maskView!)
- }
-
- func hideWaitting() {
- maskView?.removeFromSuperview()
- }
-
- override func mouseDown(with event: NSEvent) {
- super.mouseDown(with: event)
- window?.makeFirstResponder(nil)
- }
- }
- extension KMWatermarkView {
- @IBAction func buttonClicked_SwitchTextWatermark(_ sender: Any) {
- currentType = 0
- changeTypeBoxState(isTextWatermark: true)
- }
-
- @IBAction func buttonClicked_SwitchFileWaterMark(_ sender: Any) {
- currentType = 1
- changeTypeBoxState(isTextWatermark: false)
- }
-
- @IBAction func buttonClicked_BrowseFile(_ sender: Any) {
- let openPanel = NSOpenPanel()
- openPanel.canChooseDirectories = false
- openPanel.canChooseFiles = true
- openPanel.allowsMultipleSelection = false
- openPanel.allowedFileTypes = ["jpg", "jpeg", "png", "pdf"]
-
- openPanel.beginSheetModal(for: window!) { (result) in
- if result == NSApplication.ModalResponse.OK {
- guard let url = openPanel.url else { return }
- let filePath = url.path
-
- if filePath.extension.lowercased() == ".pdf" {
- let pdf = CPDFDocument(url: url)
- guard !pdf!.isEncrypted else { return }
-
- self.showWaitting()
-
- let convert = KMPDFConvert()
- convert.convertType = .png
- convert.filePath = url.path
- convert.pages = [1]
- convert.outputFolderPath = (NSSearchPathForDirectoriesInDomains(.applicationSupportDirectory, .userDomainMask, true).last! as NSString).appendingPathComponent(Bundle.main.bundleIdentifier!)
- convert.outputFileName = "pdfConvertPng"
-
- KMPDFConvertManager.defaultManager.convert(convert: convert, completion: { (finished, error) in
- self.hideWaitting()
- if finished {
- let outputFilePath = convert.outputFilePath
- if FileManager.default.fileExists(atPath: outputFilePath) {
- let fimagePath = (try? FileManager.default.subpaths(atPath: outputFilePath))?.first
- let pdfPath = convert.outputFilePath + "/" + fimagePath!
- var newPath = (convert.outputFolderPath as NSString).appendingPathComponent(self.tagString())
- newPath = (newPath as NSString).appendingPathComponent(((url.path as NSString).lastPathComponent as NSString).deletingPathExtension + ".png")
-
- do {
- try FileManager.default.copyItem(atPath: pdfPath, toPath: newPath)
- try FileManager.default.removeItem(atPath: convert.outputFilePath)
- } catch {
- print(error.localizedDescription)
- }
-
- let image = NSImage(contentsOfFile: pdfPath)
- if self.isDamageImage(image, imagePath: pdfPath) {
- let alert = NSAlert()
- alert.alertStyle = .critical
- alert.messageText = "The file \"\(url.lastPathComponent)\" could not be opened."
- alert.informativeText = "It may be damaged or use a file format that LynxPDF Editor doesn’t recognize."
- alert.addButton(withTitle: "Cancel")
- alert.beginSheetModal(for: NSApp.mainWindow!) { (response) in
- if response == NSApplication.ModalResponse.alertFirstButtonReturn {
- // Cancel button clicked
- }
- }
- return
- }
-
- self.filePathLabel.stringValue = url.path
- self.watermark.imagePath = pdfPath
- self.watermark.image = NSImage(contentsOfFile: pdfPath)
- self.watermark.text = ""
- self.templateNameTextField.stringValue = self.watermark.watermarkID
- self.applyButton.isEnabled = true
- self.batchButton.isEnabled = true
- self.pdfView.setNeedsDisplay(NSRect.zero)
- }
- }
- })
- } else {
- let image = NSImage(contentsOfFile: url.path)
- if image == nil || self.isDamageImage(image, imagePath: url.path) {
- let alert = NSAlert()
- alert.alertStyle = .critical
- alert.messageText = "The file \"\(url.lastPathComponent)\" could not be opened."
- alert.informativeText = "It may be damaged or use a file format that LynxPDF Editor doesn’t recognize."
- alert.addButton(withTitle: "Cancel")
- alert.beginSheetModal(for: NSApp.mainWindow!) { (response) in
- if response == NSApplication.ModalResponse.alertFirstButtonReturn {
- // Cancel button clicked
- }
- }
- return
- }
-
- self.filePathLabel.stringValue = url.path
- self.watermark.imagePath = url.path
- self.watermark.image = NSImage(contentsOfFile: url.path)
- self.watermark.text = ""
- self.templateNameTextField.stringValue = self.watermark.watermarkID
- self.applyButton.isEnabled = true
- self.batchButton.isEnabled = true
- self.pdfView.setNeedsDisplay(NSRect.zero)
- }
- }
- }
- }
-
- @IBAction func buttonClicked_Batch(_ sender: Any) {
- let needSave = saveToTemplateButton.state == .on
-
- if needSave {
- if templateNameTextField.stringValue.isEmpty {
- self.watermark.watermarkID = templateNameTextField.stringValue
- } else {
- self.watermark.watermarkID = KMWatermarkManager.defaultManager.fetchAvailableName()
- }
-
- let result = KMWatermarkManager.defaultManager.addWatermark(watermark: self.watermark)
- if !result {
- return
- }
- }
-
- if let operateCallBack = self.operateCallBack {
- operateCallBack(self.watermark, currentType)
- }
-
- // self.cancelAction?(self)
- }
-
- @IBAction func buttonClicked_Cancel(_ sender: Any) {
- self.pdfView.watermark = nil
- self.pdfView.background = nil
- self.pdfView.headerFooter = nil
-
- self.cancelAction?(self)
- }
-
- @IBAction func buttonClicked_Done(_ sender: Any) {
- guard let pdfDocument = pdfDocument else { return }
-
- if self.watermark.imagePath.count == 0 {
- if self.watermark.text.isEmpty {
- return
- }
- }
-
- if checkPageRangeValidate(pageRangeComboBox.stringValue) {
- watermark.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 = pdfDocument.password ?? ""
- fileAttribute?.filePath = pdfDocument.documentURL?.path ?? ""
- self._fileAttri = fileAttribute
- }
- fileAttribute?.bAllPage = false
- fileAttribute?.pagesType = .custom
-
- 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
- }
- }
- watermark.pagesString = pages.isEmpty ? "" : pages.map { "\($0)" }.joined(separator: ",")
-
-
- if needSave {
- if self.templateNameTextField.stringValue.isEmpty {
- self.watermark.watermarkID = KMWatermarkManager.defaultManager.fetchAvailableName()
- } else {
- self.watermark.watermarkID = self.templateNameTextField.stringValue
- }
-
- if KMWatermarkManager.defaultManager.watermarks.contains(self.originalWatermark) {
- let _ = KMWatermarkManager.defaultManager.removeWatermark(watermark: self.originalWatermark)
- let _ = KMWatermarkManager.defaultManager.addWatermark(watermark: self.watermark)
- } else {
- let _ = KMWatermarkManager.defaultManager.addWatermark(watermark: self.watermark)
- }
- }
-
- if self.type == .edit || self.type == .add {
- if let operateCallBack = self.operateCallBack {
- operateCallBack(self.watermark, currentType)
- }
-
- self.cancelAction?(self)
- } else if self.type == .use {
- let fileName = "\(pdfDocument.documentURL?.deletingPathExtension().lastPathComponent ?? NSLocalizedString("Untitled", comment: ""))_Watermark"
- let savePanelAccessoryViewController = KMSavePanelAccessoryController()
- let savePanel = NSSavePanel()
- savePanel.nameFieldStringValue = fileName
- savePanel.allowedFileTypes = ["pdf"]
- savePanel.accessoryView = savePanelAccessoryViewController.view
-
- savePanel.beginSheetModal(for: self.window!) { [unowned self] result in
- if result == NSApplication.ModalResponse.OK {
- self.addWatermark(model: self.watermark, toPath: savePanel.url!.path) { success in
- if success {
- if savePanelAccessoryViewController.openAutomaticButton.state == .on {
- NSDocumentController.shared.openDocument(withContentsOf: savePanel.url!, display: true) { (document, documentWasAlreadyOpen, error) in
- }
- } else {
- NSWorkspace.shared.activateFileViewerSelecting([savePanel.url!])
- }
- self.cancelAction?(self)
- }
- }
- }
- }
- }
- }
-
- @IBAction func colorWellColorChanged(_ sender: NSColorWell) {
- let convertedColor = sender.color.usingColorSpaceName(NSColorSpaceName.deviceRGB)
- if let rgbColor = convertedColor {
- self.watermark.textColor = KMWatermarkAdjectiveText.color(red: rgbColor.redComponent, green: rgbColor.greenComponent, blue: rgbColor.blueComponent, alpha: rgbColor.alphaComponent)
- updatePDFView()
- }
- }
-
- @IBAction func radioStepperAction(_ sender: NSStepper) {
- self.ratioTextField.stringValue = "\(Int(sender.doubleValue * 100))%"
- self.watermark.scale = sender.doubleValue
- updatePDFView()
- }
-
- @IBAction func angleStepperAction(_ sender: NSStepper) {
- self.angleTextF.stringValue = "\(sender.doubleValue)"
- self.watermark.rotation = sender.doubleValue
- checkAngle()
- updatePDFView()
- }
-
- @IBAction func alphaSliderAction(_ sender: NSSlider) {
- self.watermark.opacity = sender.doubleValue
- self.alphaStepper.doubleValue = sender.doubleValue
- self.alphaTextField.stringValue = "\(Int(sender.doubleValue * 100))%"
- updatePDFView()
- }
-
- @IBAction func verticalStepperAction(_ sender: NSStepper) {
- self.verticalFapTextField.stringValue = "\(sender.doubleValue)"
- self.watermark.verticalSpace = sender.doubleValue
- updatePDFView()
- }
-
- @IBAction func tileVerticalStepperAction(_ sender: NSStepper) {
- self.watermark.tileVerticalSpace = sender.doubleValue
- self.spacingVerTextField.stringValue = "\(sender.doubleValue)"
- self.watermark.tileVerticalSpace = sender.doubleValue
- updatePDFView()
- }
-
- @IBAction func horizentalStepperAction(_ sender: NSStepper) {
- self.watermark.horizontalSpace = sender.doubleValue
- self.horizentalGapTextField.stringValue = "\(sender.doubleValue)"
- self.watermark.horizontalSpace = sender.doubleValue
- updatePDFView()
- }
-
- @IBAction func tileHorizentalStepperAction(_ sender: NSStepper) {
- self.watermark.tileHorizontalSpace = sender.doubleValue
- self.spacingHorTextField.stringValue = "\(sender.doubleValue)"
- self.watermark.tileHorizontalSpace = sender.doubleValue
- updatePDFView()
- }
-
- @IBAction func alphaSteperAction(_ sender: NSStepper) {
- self.alphaTextField.stringValue = "\(Int(sender.doubleValue * 100))%"
- self.watermark.opacity = sender.doubleValue
- self.alphaSlider.doubleValue = sender.doubleValue
- updatePDFView()
- }
-
- @IBAction func onPageButtonAction(_ sender: Any) {
- self.backPageButton.state = .off
- self.watermark.isFront = true
- updatePDFView()
- }
-
- @IBAction func backPageButtonAction(_ sender: Any) {
- self.OnPageButton.state = .off
- self.watermark.isFront = false
- updatePDFView()
- }
-
- @IBAction func onPageTileButtonAction(_ sender: Any) {
- self.watermark.isTilePage = !self.watermark.isTilePage
-
- if self.type != .edit && !self.fontModified && self.txtButton.state == .on {
- if self.watermark.isTilePage {
- let textSize = self.watermark.getTextFontSize() == 48 ? 18 : self.watermark.getTextFontSize()
- self.isTileWater.state = .on
- self.fontSizeCombobox.stringValue = "\(Int(textSize))"
- self.watermark.textFont = KMWatermarkAdjectiveText.font(name: "Helvetica", size: textSize)
- } else {
- let textSize = self.watermark.getTextFontSize() == 18 ? 48 : self.watermark.getTextFontSize()
- self.fontSizeCombobox.stringValue = "\(Int(textSize))"
- self.watermark.textFont = KMWatermarkAdjectiveText.font(name: "Helvetica", size: textSize)
- }
- }
-
- self.spacingHorTextField.isEnabled = self.watermark.isTilePage
- self.spacingHorStepper.isEnabled = self.watermark.isTilePage
- self.spacingVerTextField.isEnabled = self.watermark.isTilePage
- self.spacingStepper.isEnabled = self.watermark.isTilePage
-
- updatePDFView()
- }
-
- @IBAction func goPrevious(_ sender: Any) {
- if pdfView.canGoToPreviousPage() {
- pdfView.goToPreviousPage(sender)
- }
- let index = pdfDocument?.index(for: pdfView.currentPage())
- // currentPageIndexTextF.stringValue = "\(index! + 1)"
- }
-
- @IBAction func goNext(_ sender: Any) {
- if pdfView.canGoToNextPage() {
- pdfView.goToNextPage(sender)
- }
-
- let index = pdfDocument!.index(for: pdfView.currentPage())
- // currentPageIndexTextF.stringValue = "\(index + 1)"
- }
-
- @IBAction func fontSizeComboxAction(_ sender: NSComboBox) {
- let textSize = CGFloat(Int(fontSizeCombobox.stringValue) ?? 0)
- self.watermark.textFont = KMWatermarkAdjectiveText.font(name: "Helvetica", size: textSize)
- fontModified = true
- updatePDFView()
- }
-
- @IBAction func pageRangeComboxAction(_ sender: Any) {
- pageRangeComboBox.isEditable = false
- var pages = [Int]()
-
- switch pageRangeComboBox.indexOfSelectedItem {
- case 0:
- for i in 0..<pdfDocument!.pageCount {
- pages.append(Int(i))
- }
- watermark.pageRangeType = .all
- case 1:
- for i in 0..<pdfDocument!.pageCount where i % 2 == 0 {
- pages.append(Int(i))
- }
- watermark.pageRangeType = .odd
- case 2:
- for i in 0..<pdfDocument!.pageCount where i % 2 != 0 {
- pages.append(Int(i))
- }
- watermark.pageRangeType = .even
- default:
- var fileAttribute = self._fileAttri
- if fileAttribute == nil {
- fileAttribute = KMFileAttribute()
- fileAttribute?.filePath = pdfDocument?.documentURL?.path ?? ""
- self._fileAttri = fileAttribute
- }
-
- fileAttribute?.bAllPage = false
- fileAttribute?.pagesString = pageRangeComboBox.stringValue
- watermark.pageRangeType = .other
- pageRangeComboBox.isEditable = true
- window?.makeFirstResponder(pageRangeComboBox)
-
- let selectPages = fileAttribute?.fetchSelectPages() ?? []
- pages = selectPages.map { $0 - 1 }
- }
-
- if !pages.isEmpty {
- watermark.pagesString = pages.map { "\($0)" }.joined(separator: ",")
- } else {
- watermark.pagesString = ""
- }
-
- updatePDFView()
- }
- }
- extension KMWatermarkView {
- class func addWatermarkForOutSide(document: CPDFDocument, model: KMWatermarkModel, toPath: String, completion: @escaping (_ result: Bool) -> ()) {
- DispatchQueue.main.async {
- let wm = KMWatermarkView()
- wm.pdfView = KMWatermarkPDFView()
- wm.pdfView.document = document
- wm.password = document.password ?? ""
- wm.addWatermark(model: model, toPath: toPath, completion: completion)
- }
- }
- func addWatermark(model: KMWatermarkModel, toPath: String, completion: @escaping (_ result: Bool) -> ()) {
- DispatchQueue.global().async {
- let waterDocument = CPDFDocument(url: self.pdfView.document.documentURL)
- guard let waterDocumentT = waterDocument else { return }
- if self.password.isEmpty == false {
- waterDocumentT.unlock(withPassword: self.password)
- }
- var property: CPDFWatermark!
- var scale: CGFloat = model.scale
- if model.image != nil {
- property = CPDFWatermark(document: waterDocument, type: .image)
- property.image = model.image
- } else {
- property = CPDFWatermark(document: waterDocument, type: .text)
- property.text = model.text
- property.textColor = model.getTextColor()
- scale = model.getTextFontSize() / 24.0
- }
-
- property.scale = scale
- property.rotation = -model.rotation
- property.opacity = model.opacity
- property.tx = model.horizontalSpace
- property.ty = model.verticalSpace
- property.isFront = model.isFront
- var pageString: String = ""
- if (model.pageRangeType == .all) {
- for i in 0..<waterDocumentT.pageCount {
- pageString.append("\(i)")
-
- if (i != waterDocumentT.pageCount-1) {
- pageString.append(",")
- }
- }
- } else if (model.pageRangeType == .odd) {
- for i in 0 ..< waterDocumentT.pageCount {
- if (i % 2 == 0) {
- pageString.append("\(i)")
- } else {
- continue
- }
-
- if (i != waterDocumentT.pageCount-1) {
- pageString.append(",")
- }
- }
- } else if (model.pageRangeType == .even) {
- for i in 0 ..< waterDocumentT.pageCount {
- if (i % 2 == 1) {
- pageString.append("\(i)")
- } else {
- continue
- }
-
- if (i != waterDocumentT.pageCount-1) {
- pageString.append(",")
- }
- }
- } else {
- pageString = model.pagesString
- }
- property.pageString = pageString
- property.isTilePage = model.isTilePage
- property.horizontalSpacing = model.tileHorizontalSpace / scale
- property.verticalSpacing = model.tileVerticalSpace / scale
- if (model.verticalMode == 0) {
- property.verticalPosition = .top
- } else if (model.verticalMode == 1) {
- property.verticalPosition = .center
- } else if (model.verticalMode == 2) {
- property.verticalPosition = .bottom
- }
-
- if (model.horizontalMode == 0) {
- property.horizontalPosition = .left
- } else if (model.horizontalMode == 1) {
- property.horizontalPosition = .center
- } else if (model.horizontalMode == 2) {
- property.horizontalPosition = .right
- }
- model.watermark = property
-
- waterDocumentT.addWatermark(property)
-
- /// 保存到临时路径
- let documentPath = NSTemporaryDirectory()
- let tempPath: String = "\(documentPath)/\(toPath.lastPathComponent)"
- if (FileManager.default.fileExists(atPath: tempPath)) {
- try?FileManager.default.removeItem(atPath: tempPath)
- }
-
- let result = waterDocumentT.write(to: URL(fileURLWithPath: tempPath))
- if (result) {
- if (FileManager.default.fileExists(atPath: toPath)) {
- try?FileManager.default.removeItem(atPath: toPath)
- }
-
- try?FileManager.default.moveItem(atPath: tempPath, toPath: toPath)
- } else {
- try?FileManager.default.removeItem(atPath: tempPath)
- }
-
- DispatchQueue.main.async {
- completion(result)
- }
- }
-
- // static func loadAllWatermarks() {
- // self.watermarkArray.removeAll()
- // let watermarks: Array<CPDFWatermark> = self.preView.document.watermarks()
- // for watermark in watermarks {
- // let model = KMWatermarkModel()
- // model.scale = watermark.scale
- // model.rotation = -watermark.rotation
- // model.opacity = watermark.opacity
- // model.verticalSpace = watermark.tx
- // model.horizontalSpace = watermark.ty
- // model.pagesString = watermark.pageString
- //// model.textColor = watermark.textColor
- // model.isFront = watermark.isFront
- // model.isTilePage = watermark.isTilePage
- // model.tileVerticalSpace = watermark.verticalSpacing
- // model.tileHorizontalSpace = watermark.horizontalSpacing
- //
- // if (watermark.type == .text) {
- // model.text = watermark.text
- //// model.textFont
- // model.scale = watermark.scale * 24
- // } else if (watermark.type == .image) {
- // model.image = watermark.image
- // }
- //
- // if (watermark.verticalPosition == .top) {
- // model.verticalMode = 0
- // } else if (watermark.verticalPosition == .center) {
- // model.verticalMode = 1
- // } else if (watermark.verticalPosition == .bottom) {
- // model.verticalMode = 2
- // }
- //
- // if (watermark.horizontalPosition == .left) {
- // model.horizontalMode = 0
- // } else if (watermark.horizontalPosition == .center) {
- // model.horizontalMode = 1
- // } else if (watermark.horizontalPosition == .right) {
- // model.horizontalMode = 2
- // }
- //
- // model.watermark = watermark
- // self.watermarkArray.append(model)
- // }
- // }
- }
-
- func deleteWatermarks(toPath: String, completion: @escaping (_ result: Bool) -> ()) -> () {
- if (self.pdfView.document.watermarks().count <= 0) {
- completion(false)
- }
- DispatchQueue.global().async {
- let array: Array<CPDFWatermark> = self.pdfView.document.watermarks()
- for model in array {
- self.pdfView.document.removeWatermark(model)
- }
- /// 保存到临时路径
- let documentPath = NSTemporaryDirectory()
- let tempPath: String = "\(documentPath)/\(toPath.lastPathComponent)"
- if (FileManager.default.fileExists(atPath: tempPath)) {
- try?FileManager.default.removeItem(atPath: tempPath)
- }
- let result = self.pdfView.document.write(to: URL(fileURLWithPath: tempPath))
- if (result) {
- if (FileManager.default.fileExists(atPath: toPath)) {
- try?FileManager.default.removeItem(atPath: toPath)
- }
- try?FileManager.default.moveItem(atPath: tempPath, toPath: toPath)
- } else {
- try?FileManager.default.removeItem(atPath: tempPath)
- }
- DispatchQueue.main.async {
- completion(result)
- }
- }
- }
- }
|