Parcourir la source

【批量】- 添加水印

liujiajie il y a 1 an
Parent
commit
9a2fdf8892

+ 1 - 1
PDF Office/PDF Master/Class/Batch/Tools/KMImageToPDFMethod.swift

@@ -271,7 +271,7 @@ class KMImageToPDFMethod: NSObject, KMGOCRManagerDelegate {
                 if OCRResultString.count > 0 {
                     contents = self.OCRResultString
                 }
-                var str: String = results.firstObject as! String
+                let str: String = results.firstObject as? String ?? ""
                 contents = contents + "\n"
                 contents = contents + "Page" + "\(key + 1)"
                 contents = contents + "\n----------\n"

+ 50 - 3
PDF Office/PDF Master/Class/PDFTools/Watermark/Controller/KMBatchOperateAddWatermarkViewController.swift

@@ -654,6 +654,52 @@ extension KMBatchOperateAddWatermarkViewController {
         }
     }
     
+    func editWatermark(_ waterMark: KMWatermarkModel) {
+        if !isBackground {
+            var filePath: String = Bundle.main.path(forResource: "PDF Master User Guide", ofType: "pdf") ?? ""
+//            if let windowController = view.window?.windowController, let mainWindowController = windowController as? SKMainWindowController {
+//                filePath = mainWindowController.pdfView.document?.documentURL?.path
+//                password = (mainWindowController.pdfView.document as? SKPDFDocument)?.password
+//            }
+        
+            var cdocument = CPDFDocument(url: URL(fileURLWithPath: filePath))
+            let controller = KMWatermarkWindowController(windowNibName: "KMWatermarkWindowController")
+            controller.watermark = waterMark
+            controller.isBatch = isBatchOperation
+            controller.type = .edit
+            controller.pdfDocument = cdocument
+            controller.currentType = self.currentType.rawValue
+            controller.cancelAction = { [unowned self] wmWindowC in
+                self.km_endSheet()
+            }
+            controller.operateCallBack = { controller, watermark, countType in
+                self._loadData()
+                var haveWaters = false
+                if self.currentType == .txt {
+                    self._currentTextData = watermark
+                    if self._textTemplateArray?.count ?? 0 > 0 {
+                        haveWaters = true
+                    }
+                } else if self.currentType == .file {
+                    self._currentFileData = watermark
+                    if self._fileTemplateArray?.count ?? 0 > 0 {
+                        haveWaters = true
+                    }
+                }
+                if haveWaters {
+                    let indexPath = IndexPath(item: 0, section: 0)
+                    var set = Set<IndexPath>()
+                    set.insert(indexPath)
+                    self.collectionView.selectItems(at: set, scrollPosition: .bottom)
+                }
+                self._postNotification()
+            }
+            self.km_beginSheet(windowC: controller)
+        } else {
+            // Handle background case
+        }
+    }
+    
     private func _deleteAllWatermark() {
         if(!self.isBackground) {
             for waterMark in self._textTemplateArray ?? [] {
@@ -679,8 +725,9 @@ extension KMBatchOperateAddWatermarkViewController {
         self._postNotification()
     }
     
-    private func _deleteWatermark(_ waterMark: KMPDFWatermarkData) {
+    private func _deleteWatermark(_ waterMark: KMWatermarkModel) {
         //    [[KMWatermarkManager defaultManager] removeWatermarkWithData:waterMark];
+        let _ = KMWatermarkManager.defaultManager.removeWatermark(watermark: waterMark)
         self._loadData()
         self._postNotification()
     }
@@ -858,9 +905,9 @@ extension KMBatchOperateAddWatermarkViewController: NSCollectionViewDelegate, NS
                             self._currentFileData = waterMark
                         }
                         collectionView.reloadData()
-                        //                        [blockSelf editWatermark:watermark];
+                        self.editWatermark(waterMark!)
                     } else {
-                        //                        [blockSelf deleteWatermark:watermark];
+                        self._deleteWatermark(waterMark!)
                     }
                 }
             };

+ 2 - 2
PDF Office/PDF Master/Class/PDFTools/Watermark/Controller/KMWatermarkCollectionViewItem.swift

@@ -54,10 +54,10 @@ class KMWatermarkCollectionViewItem: NSCollectionViewItem {
         self._waterMark = waterMark;
         if waterMark.isFront {
             self.watermarkImageView.image = NSImage(named: "KMImageNameUXIconVipWatermarkImg")
-//            self.imageView.image = [KMPDFWatermarkObject drawImageAtpageRect:self.watermarkImageView.bounds watermarkDatas:waterMark];
+            self.imageView!.image = KMWatermarkModel.drawImageAtpageRect(rect: self.watermarkImageView.bounds, data: waterMark) // KMWatermarkManager.drawImageAtpageRect:self.watermarkImageView.bounds watermarkDatas:waterMark];
         } else {
             self.imageView?.image = NSImage(named: "KMImageNameUXIconVipWatermarkImg")
-//            self.watermarkImageView.image = [KMPDFWatermarkObject drawImageAtpageRect:self.watermarkImageView.bounds watermarkDatas:waterMark];
+            self.watermarkImageView.image = KMWatermarkModel.drawImageAtpageRect(rect: self.watermarkImageView.bounds, data: waterMark)//[KMPDFWatermarkObject drawImageAtpageRect:self.watermarkImageView.bounds watermarkDatas:waterMark];
         }
         self.watermarkNameLabel.stringValue = waterMark.watermarkID 
     }

+ 197 - 0
PDF Office/PDF Master/Class/PDFTools/Watermark/Model/KMWatermarkModel.swift

@@ -368,4 +368,201 @@ import Cocoa
         
         return image
     }
+    
+    class func drawImageAtpageRect(rect: NSRect, data: KMWatermarkModel) -> NSImage? {
+        var size = NSZeroSize
+        let text: String = data.text
+        if text.count > 0 {
+            var font = NSFont.systemFont(ofSize: 52)
+            if data.isTilePage {
+                font = NSFont.systemFont(ofSize: data.getTextFontSize())
+            }
+            let style = NSMutableParagraphStyle()
+            style.alignment = .center
+            style.lineBreakMode = .byCharWrapping
+            let attributes: [NSAttributedString.Key: Any] = [ .paragraphStyle: style, .font: font ]
+            size = text.boundingRect(with: CGSize(width: CGFloat.greatestFiniteMagnitude, height: CGFloat.greatestFiniteMagnitude), options: [.usesLineFragmentOrigin, .usesFontLeading], attributes: attributes).size
+        } else if let image = data.image {
+            size = image.size
+            size.width *= data.scale
+            size.height *= data.scale
+        }
+        let radian = data.rotation * (CGFloat.pi/180)
+        let t = CGAffineTransform(rotationAngle: radian)
+        var newRect = rect
+        if !data.isTilePage {
+            newRect = CGRectApplyAffineTransform(CGRect(x: 0, y: 0, width: size.width + 40, height: size.height + 40), t)
+        }
+        let image = NSImage(size: newRect.size)
+        image.lockFocus()
+        NSGraphicsContext.current?.imageInterpolation = .high
+        NSGraphicsContext.saveGraphicsState()
+        NSColor.clear.set()
+        rect.fill()
+        NSGraphicsContext.restoreGraphicsState()
+        guard let context = NSGraphicsContext.current?.cgContext else { return nil }
+        let imageSize = newRect.size
+        NSGraphicsContext.current = NSGraphicsContext(cgContext: context, flipped: false)
+        NSGraphicsContext.saveGraphicsState()
+
+        if text.count > 0 {
+            var font = NSFont.systemFont(ofSize: 52)
+            if data.isTilePage {
+                font = NSFont.systemFont(ofSize: data.getTextFontSize())
+            }
+            var color = data.getTextColor()
+            var red: CGFloat = 0, green: CGFloat = 0, blue: CGFloat = 0
+//            color.usingColorSpace(NSColorSpace.rg)?.getRed(&red, green: &green, blue: &blue, alpha: nil)
+            color.usingColorSpaceName(.calibratedRGB)?.getRed(&red, green: &green, blue: &blue, alpha: nil)
+            color = NSColor.init(calibratedRed: red, green: green, blue: blue, alpha: data.opacity)
+            
+            size = .zero
+            
+            let alpha = data.opacity
+            let tileHorizontalSpace = data.tileHorizontalSpace
+            let tileVerticalSpace = data.tileVerticalSpace
+            let horizontalSpace = data.horizontalSpace
+            let verticalSpace = data.verticalSpace
+            let style = NSMutableParagraphStyle()
+            style.alignment = .center
+            style.lineBreakMode = .byCharWrapping
+            let attributes: [NSAttributedString.Key: Any] = [ .paragraphStyle: style, .foregroundColor: NSColor(calibratedRed: red, green: green, blue: blue, alpha: alpha), .font: font ]
+            size = text.boundingRect(with: CGSize(width: CGFloat.greatestFiniteMagnitude, height: CGFloat.greatestFiniteMagnitude), options: [.usesLineFragmentOrigin, .usesFontLeading], attributes: attributes).size
+            let textRect = CGRect(x: 0, y: 0, width: size.width, height: size.height)
+            if data.isTilePage {
+                let width = sqrt(image.size.height * image.size.height + image.size.width * image.size.width)
+                let newRect = CGRect(x: -(width - image.size.width) / 2, y: -(width - image.size.height) / 2, width: width, height: width)
+                let new_w = newRect.size.width
+                let new_h = newRect.size.height
+                context.translateBy(x: image.size.width / 2, y: image.size.height / 2)
+                context.concatenate(CGAffineTransform(rotationAngle: radian))
+                context.translateBy(x: -(image.size.width / 2), y: -(image.size.height / 2))
+                let verticalWidth = size.width + tileHorizontalSpace / 3.0
+                let horizontalHeight = size.height + tileVerticalSpace / 3.0
+                let line: Int = Int((new_h - tileHorizontalSpace / 3.0) / horizontalHeight + 1)
+                let row: Int = Int((new_w - tileVerticalSpace / 3.0) / verticalWidth + 1)
+                let point = CGPoint(x: image.size.width / 2 - size.width / 2 + horizontalSpace / 3.0, y: image.size.height / 2 - size.height / 2 + verticalSpace / 3.0)
+                for i in 0..<line {
+                    for j in 0..<row {
+                        let drawRect = CGRect(x: point.x + CGFloat(j) * verticalWidth, y: point.y + CGFloat(i) * horizontalHeight, width: size.width, height: size.height)
+                        text.draw(in: drawRect, withAttributes: attributes)
+                    }
+                }
+                for i in 1..<line {
+                    for j in 0..<row {
+                        let drawRect = CGRect(x: point.x + CGFloat(j) * verticalWidth, y: point.y - CGFloat(i) * horizontalHeight, width: size.width, height: size.height)
+                        text.draw(in: drawRect, withAttributes: attributes)
+                    }
+                }
+                for i in 0..<line {
+                    for j in 1..<row {
+                        let drawRect = CGRect(x: point.x - CGFloat(j) * verticalWidth, y: point.y + CGFloat(i) * horizontalHeight, width: size.width, height: size.height)
+                        text.draw(in: drawRect, withAttributes: attributes)
+                    }
+                }
+                for i in 1..<line {
+                    for j in 1..<row {
+                        let drawRect = CGRect(x: point.x - CGFloat(j) * verticalWidth, y: point.y - CGFloat(i) * horizontalHeight, width: size.width, height: size.height)
+                        text.draw(in: drawRect, withAttributes: attributes)
+                    }
+                }
+            } else {
+                var textRect = textRect
+                if data.verticalMode == 0 {
+                    textRect.origin.y = imageSize.height - textRect.size.height
+                } else if data.verticalMode == 1 {
+                    textRect.origin.y = (imageSize.height - textRect.size.height) / 2.0
+                } else {
+                    textRect.origin.y = 0
+                }
+                if data.horizontalMode == 0 {
+                    textRect.origin.x = 0
+                } else if data.horizontalMode == 1{
+                    textRect.origin.x = (imageSize.width - textRect.size.width) / 2.0
+                } else {
+                    textRect.origin.x = imageSize.width - textRect.size.width
+                }
+                let contextCenter = CGPoint(x: textRect.midX, y: textRect.midY)
+                context.translateBy(x: contextCenter.x, y: contextCenter.y)
+                context.rotate(by: radian)
+                context.translateBy(x: -contextCenter.x, y: -contextCenter.y)
+                data.text.draw(in: CGRect(x: textRect.origin.x + (textRect.size.width - size.width) / 2.0, y: textRect.origin.y + (textRect.size.height - size.height) / 2.0, width: size.width, height: size.height), withAttributes: attributes)
+            }
+        }else if let image = data.image {
+            var size = image.size
+            size.width *= data.scale
+            size.height *= data.scale
+            let width = sqrt(image.size.height * image.size.height + image.size.width * image.size.width)
+            let newRect = CGRect(x: -(width - image.size.width)/2, y: -(width - image.size.height)/2, width: width, height: width)
+            let new_w = newRect.size.width
+            let new_h = newRect.size.height
+            let radian = data.rotation * (CGFloat.pi / 180)
+            let t = CGAffineTransform(rotationAngle: radian)
+            if data.isTilePage {
+                context.translateBy(x: image.size.width/2, y: image.size.height/2)
+                context.concatenate(t)
+                context.translateBy(x: -(image.size.width/2), y: -(image.size.height/2))
+                
+                let verticalWidth = size.width + data.tileHorizontalSpace * data.scale
+                let horizontalHeight = size.height + data.tileVerticalSpace * data.scale
+                let line: Int = Int((new_h - data.tileVerticalSpace * data.scale)/horizontalHeight + 1)
+                let row: Int = Int((new_w - data.tileHorizontalSpace * data.scale) / verticalWidth + 1)
+                let point = CGPoint(x: image.size.width/2 - size.width/2 + data.horizontalSpace*data.scale, y:image.size.height/2 - size.height/2 + data.verticalSpace * data.scale)
+                for i in 0..<line/2+1 {
+                    for j in 0..<row {
+                        let area = CGRect(x: point.x + CGFloat(j) * verticalWidth, y: point.y + CGFloat(i) * horizontalHeight, width: size.width, height: size.height)
+                        data.image.draw(in: area, from: NSZeroRect, operation: .sourceOver, fraction: data.opacity)
+                    }
+                }
+                for i in 1..<line/2+1 {
+                    for j in 0..<row {
+                        let area = CGRect(x: point.x + CGFloat(j) * verticalWidth, y: point.y - CGFloat(i) * horizontalHeight, width: size.width, height: size.height)
+                        data.image.draw(in: area, from: NSZeroRect, operation: .sourceOver, fraction: data.opacity)
+                    }
+                }
+                for i in 0..<line/2+1 {
+                    for j in 1..<row {
+                        let area = CGRect(x: point.x - CGFloat(j) * verticalWidth, y: point.y + CGFloat(i) * horizontalHeight, width: size.width, height: size.height)
+                        data.image.draw(in: area, from: NSZeroRect, operation: .sourceOver, fraction: data.opacity)
+                    }
+                }
+                for i in 1..<(line/2 + 1) {
+                    for j in 1..<row {
+                        let area = CGRect(x: point.x - CGFloat(j)*verticalWidth, y: point.y - CGFloat(i)*horizontalHeight, width: size.width, height: size.height)
+                        data.image.draw(in: area, from: NSZeroRect, operation: .sourceOver, fraction: data.opacity)
+                    }
+                }
+            }else {
+                var size = data.image.size
+                size.width *= data.scale
+                size.height *= data.scale
+                let radian = data.rotation * (CGFloat.pi / 180)
+                let t = CGAffineTransform(rotationAngle: radian)
+                var imageRect = CGRect(origin: .zero, size: size).applying(t)
+                if data.verticalMode == 0 {
+                    imageRect.origin.y = imageSize.height - imageRect.size.height
+                } else if data.verticalMode == 1 {
+                    imageRect.origin.y = (imageSize.height - imageRect.size.height) / 2.0
+                } else {
+                    imageRect.origin.y = 0
+                }
+                if data.horizontalMode == 0 {
+                    imageRect.origin.x = 0
+                } else if data.horizontalMode == 1 {
+                    imageRect.origin.x = (imageSize.width - imageRect.size.width) / 2.0
+                } else {
+                    imageRect.origin.x = imageSize.width - imageRect.size.width
+                }
+                let contextCenter = CGPoint(x: imageRect.midX, y: imageRect.midY)
+                context.translateBy(x: contextCenter.x, y: contextCenter.y)
+                context.rotate(by: radian)
+                context.translateBy(x: -contextCenter.x, y: -contextCenter.y)
+                data.image.draw(in: CGRect(x: imageRect.origin.x+(imageRect.size.width-size.width)/2.0, y: imageRect.origin.y+(imageRect.size.height-size.height)/2.0, width: size.width, height: size.height), from: NSZeroRect, operation: .sourceOver, fraction: data.opacity)
+            }
+        }
+        NSGraphicsContext.restoreGraphicsState()
+        NSGraphicsContext.current?.imageInterpolation = .default
+        image.unlockFocus()
+        return image
+    }
 }

+ 4 - 1
PDF Office/PDF Master/Class/PDFTools/Watermark/New/KMWatermarkWindowController.swift

@@ -13,7 +13,7 @@ typealias KMWatermarkWindowControllerOperateCallBack = (_ controller: NSWindowCo
 class KMWatermarkWindowController: KMBaseWindowController {
     var currentType: Int = 0
     @IBOutlet weak var watermarkView: KMWatermarkView!
-    
+    var watermark: KMWatermarkModel = KMWatermarkModel()
     var operateCallBack:KMWatermarkWindowControllerOperateCallBack?
     
     var type: KMWatermarkManagerType = .use
@@ -22,6 +22,9 @@ class KMWatermarkWindowController: KMBaseWindowController {
         super.windowDidLoad()
 
         // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
+        if type == .edit {
+            watermarkView.watermark = watermark
+        }
         watermarkView.isHiddenBatchBtn = isBatch
         watermarkView.type = type
         watermarkView.currentType = currentType

+ 1 - 1
PDF Office/PDF Master/Class/PDFTools/Watermark/New/View/KMWatermarkView.swift

@@ -293,7 +293,7 @@ class KMWatermarkView: KMBaseXibView, NSTextFieldDelegate, NSComboBoxDelegate, N
         ratioStepper.doubleValue = watermark.scale
         ratioTextField.stringValue = "\(Int(opacity * 100))%"
         fontComboBox.stringValue = "\(watermark.getTextFontSize() )"
-        filePathLabel.stringValue = watermark.imagePath ?? ""
+        filePathLabel.stringValue = watermark.imagePath 
         batchButton.isHidden = isHiddenBatchBtn
         if currentType == 0 {
             changeTypeBoxState(isTextWatermark: true)

+ 1 - 1
PDF Office/PDF Master/Class/PDFTools/Watermark/Tools/KMWatermarkManager.swift

@@ -129,7 +129,7 @@ class KMWatermarkManager: NSObject {
     
     func updateWatermark(watermark: KMWatermarkModel) -> Bool {
         if (!FileManager.default.fileExists(atPath: watermarkFolderPath!)) {
-            let create = try?FileManager.default.createDirectory(atPath: watermarkFolderPath!, withIntermediateDirectories: false)
+            let create: ()? = try?FileManager.default.createDirectory(atPath: watermarkFolderPath!, withIntermediateDirectories: false)
             if (create == nil) {
                 return false
             }

+ 2 - 2
PDF Office/PDF Master/Class/PDFWindowController/MainWindowController/MainWindowController.xib

@@ -1,8 +1,8 @@
 <?xml version="1.0" encoding="UTF-8"?>
-<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="21701" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct">
+<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="22155" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct">
     <dependencies>
         <deployment identifier="macosx"/>
-        <plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="21701"/>
+        <plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="22155"/>
         <capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
     </dependencies>
     <objects>