Browse Source

Merge branch 'develop_PDFReaderProNew' of git.kdan.cc:Mac_PDF/PDF_Office into develop_PDFReaderProNew

tangchao 1 year ago
parent
commit
6e68c53b22

+ 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"

+ 4 - 0
PDF Office/PDF Master/Class/PDFTools/Print/Model/KMPrintPaperModel.swift

@@ -42,6 +42,10 @@ struct KMPrintPaperInfo {
         return KMPaperType.paperSize(type: type, px: true)
     }
     
+//    var showSize: CGSize {
+//        return KMPaperType.paperSize(type: type, px: false)
+//    }
+    
     enum KMPaperType: String, CaseIterable {
         case Actual = "Actual Size"
         case A3 = "A3"

+ 47 - 3
PDF Office/PDF Master/Class/PDFTools/Print/Presenter/KMPrintPresenter.swift

@@ -410,7 +410,6 @@ extension KMPrintPresenter: KMPrintPresenterDocument {
                     
                     page.page.draw(with: PDFDisplayBox.cropBox, to: context)
 //                    page.transform(context, for: PDFDisplayBox.cropBox)
-                    
                     if border {
                         var dirtyRect = rect
                         if rotate == 90 ||
@@ -428,6 +427,51 @@ extension KMPrintPresenter: KMPrintPresenterDocument {
             }
         }
     }
+    
+    func drawLabelTextContextSize(context: CGContext) {
+        let pageSize = CGSize(width: 0, height: 0)
+        let KBlankA4W = pageSize.width
+        let KBlankA4H = pageSize.height
+        
+        var contextString: String
+//        if let labelString = PDFPrint.labelString, !labelString.isEmpty {
+//            contextString = labelString
+//        } else {
+            let date = Date()
+            let formatter = DateFormatter()
+            formatter.dateFormat = "YYYY-MM-dd hh:mm:ss"
+        contextString = "(\("1"),\("1")) \("filePath.lastPathComponent") \(formatter.string(from: date))"
+//        }
+        
+        let fontSize = 12.0 * (max(KBlankA4W, KBlankA4H) / 842)
+        let font = NSFont.systemFont(ofSize: fontSize)
+        let color = NSColor.black
+        
+        var size = NSSize.zero
+        var style = NSMutableParagraphStyle()
+        style.alignment = .center
+        style.lineBreakMode = .byCharWrapping
+        var attributes = [NSAttributedString.Key: Any]()
+        attributes[.paragraphStyle] = style
+        attributes[.foregroundColor] = color
+        attributes[.font] = font
+        size = contextString.boundingRect(with: CGSize(width: CGFloat.greatestFiniteMagnitude, height: CGFloat.greatestFiniteMagnitude),
+                                          options: [.usesLineFragmentOrigin, .usesFontLeading],
+                                          attributes: attributes).size
+        
+//        if PDFPrint.splitType == .pageNumber {
+            contextString.draw(in: CGRect(x: 10 + 10,
+                                          y: KBlankA4H - 10 + size.height,
+                                          width: size.width, height: size.height),
+                                withAttributes: attributes)
+//        } else {
+//            contextString.draw(in: CGRect(x: PDFPrint.edgeInsets.left + 10,
+//                                          y: KBlankA4H - PDFPrint.edgeInsets.top + size.height,
+//                                          width: size.width, height: size.height),
+//                                withAttributes: attributes)
+//        }
+    }
+
 }
 
 protocol KMPrintPresenterPage {}
@@ -957,12 +1001,12 @@ extension KMPrintPresenter: KMPrintPresenterDraw {
 protocol KMPrintPresenterPrivate {}
 extension KMPrintPresenter: KMPrintPresenterPrivate {
     func isAnnoationStamp(type: String) -> Bool {
-        let annotationStamp: [String] = ["Square"]
+        let annotationStamp: [String] = ["Square", "Stamp"]
         return annotationStamp.contains(type)
     }
     
     func isAnnoationMarkup(type: String) -> Bool {
-        let annotationStamp: [String] = ["Widget", "Freehand"]
+        let annotationStamp: [String] = ["Widget", "Freehand", "Highlight", "Underline", "Squiggly", "Circle", "StrikeOut", "Ink"]
         return annotationStamp.contains(type)
     }
     

+ 1 - 1
PDF Office/PDF Master/Class/PDFTools/Print/View/Preview/KMPrintPreviewView.swift

@@ -94,7 +94,7 @@ class KMPrintPreviewView: KMBaseXibView {
             self.pageCountLabel.stringValue = self.previewView.document.pageCount.description
             
             if page != nil {
-                self.titleLabel.stringValue = NSLocalizedString("size", comment: "") + ":" + String(Int(page!.size.width / 28.3 * 10)) + NSLocalizedString("mm", comment: "") + "*" + String(Int(page!.size.height / 28.3 * 10)) + NSLocalizedString("mm", comment: "")
+                self.titleLabel.stringValue = NSLocalizedString("size", comment: "") + ":" + page!.size.width.description + NSLocalizedString("mm", comment: "") + "*" + String(page!.size.height.description) + NSLocalizedString("mm", comment: "")
             }
         }
     }

+ 8 - 0
PDF Office/PDF Master/Class/PDFTools/Print/View/PrintSettingView/KMPrintSettingView.swift

@@ -31,6 +31,14 @@ class KMPrintSettingView: KMBaseXibView {
         defaultView.printSettingChange = { [unowned self] view, model in
             self.printSettingChange?(self, model)
         }
+        
+        posterView.pageSettingChangeAction = { [unowned self] view, model in
+            self.pageSettingChange?(self, model)
+        }
+        
+        posterView.printSettingChangeAction = { [unowned self] view, model in
+            self.printSettingChange?(self, model)
+        }
     }
     
     override func reloadData() {

+ 111 - 58
PDF Office/PDF Master/Class/PDFTools/Print/View/PrintSettingView/View/Poster/KMPrintPosterView.swift

@@ -7,6 +7,10 @@
 
 import Cocoa
 
+typealias KMPrintPosterViewPageSettingChange = (_ view: KMPrintPosterView, _ model: KMPrintModel) -> Void
+typealias KMPrintPosterViewPrintSettingChange = (_ view: KMPrintPosterView, _ model: KMPrintModel) -> Void
+
+
 class KMPrintPosterView: KMBaseXibView {
 
     @IBOutlet weak var scaleTextField: NSTextField!
@@ -51,11 +55,14 @@ class KMPrintPosterView: KMBaseXibView {
     
     var preview: KMPDFPosterPrintContentView?
     
-    lazy var viewModel: KMPrintViewModel = KMPrintViewModel()
+    var pageSettingChangeAction: KMPrintPosterViewPageSettingChange?
+    var printSettingChangeAction: KMPrintPosterViewPrintSettingChange?
     
+    lazy var viewModel: KMPrintViewModel = KMPrintViewModel()
     
     override func setup() {
-
+        viewModel.model.page.operation.type = .poster
+        
         scaleTextField.stringValue = "100"
         scaleTextField.formatter = TextFieldFormatter()
 
@@ -83,7 +90,7 @@ class KMPrintPosterView: KMBaseXibView {
         
         unitComboBox.selectItem(at: 1)
 
-        var paperArray = KMPrintPaperInfo.KMPaperType.allValues()
+        let paperArray = KMPrintPaperInfo.KMPaperType.allValues()
         var tPaperArray = paperArray
         tPaperArray.removeFirst()
         
@@ -93,7 +100,7 @@ class KMPrintPosterView: KMBaseXibView {
         pageWidthTextField.isEnabled = false
         pageHeightTextField.isEnabled = false
 
-        var tFileArray = paperArray
+        let tFileArray = paperArray
         filePageSizeBox.addItems(withObjectValues: tFileArray)
         filePageSizeBox.selectItem(at: 0)
         filePageSizeBox.isEditable = false
@@ -152,14 +159,15 @@ class KMPrintPosterView: KMBaseXibView {
 
         tableView.tabViewItem(at: 0).label = NSLocalizedString("Tile", comment: "")
         tableView.tabViewItem(at: 1).label = NSLocalizedString("Split", comment: "")
+        tableView.delegate = self
 
-        var paperArray = KMPrintPaperInfo.KMPaperType.allValues() /*KMPageSizeTool.paperSize()*/
+        let paperArray = KMPrintPaperInfo.KMPaperType.allValues() /*KMPageSizeTool.paperSize()*/
         var tPaperArray = paperArray
         tPaperArray.removeFirst()
         splitSizeComboBox.removeAllItems()
         splitSizeComboBox.addItems(withObjectValues: tPaperArray)
 
-        var tFileArray = paperArray
+        let tFileArray = paperArray
         filePageSizeBox.removeAllItems()
         filePageSizeBox.addItems(withObjectValues: tFileArray)
 
@@ -168,23 +176,98 @@ class KMPrintPosterView: KMBaseXibView {
     }
     
     override func reloadData() {
+        let model = viewModel.model
+        
         let poster: KMPrintPageOperation.Poster = viewModel.model.page.operation.poster
         
         columnCountText.stringValue = "\(poster.pageOfPaper.point.x)"
         lineCountText.stringValue = "\(poster.pageOfPaper.point.y)"
+        
+        //pageRange
+        var range: KMPrintPageRange = self.viewModel.model.page.range
+        if range.type == .allPage {
+            pageRangeComboBox.isEditable = false
+            pageRangeComboBox.selectItem(at: 0)
+        } else if range.type == .oddPage {
+            pageRangeComboBox.isEditable = false
+            pageRangeComboBox.selectItem(at: 1)
+        } else if range.type == .evenPage {
+            pageRangeComboBox.isEditable = false
+            pageRangeComboBox.selectItem(at: 2)
+        } else if range.type == .custom {
+            pageRangeComboBox.isEditable = true
+            pageRangeComboBox.selectItem(at: 3)
+            pageRangeComboBox.stringValue = range.pageString
+        }
+        
+        //paper Type
+        if model.paper.info.type == .Actual {
+            filePageSizeBox.selectItem(at: 0)
+            paperWidthTextField.isEnabled = false
+            paperHeightTextField.isEnabled = false
+        } else if model.paper.info.type == .custom {
+            splitSizeComboBox.selectItem(at: splitSizeComboBox.numberOfItems)
+            pageWidthTextField.isEnabled = true
+            pageHeightTextField.isEnabled = true
+            
+            filePageSizeBox.selectItem(at: filePageSizeBox.numberOfItems)
+            paperWidthTextField.isEnabled = true
+            paperHeightTextField.isEnabled = true
+        } else {
+            splitSizeComboBox.selectItem(withObjectValue: model.paper.info.type.rawValue)
+            pageWidthTextField.isEnabled = false
+            pageHeightTextField.isEnabled = false
+            
+            filePageSizeBox.selectItem(withObjectValue: model.paper.info.type.rawValue)
+            paperWidthTextField.isEnabled = false
+            paperHeightTextField.isEnabled = false
+            
+            paperWidthTextField.stringValue = model.paper.info.size.width.description
+            paperHeightTextField.stringValue = model.paper.info.size.height.description
+            
+            pageWidthTextField.stringValue = model.paper.info.size.width.description
+            pageHeightTextField.stringValue = model.paper.info.size.height.description
+        }
+        
+        //page per
+        if model.page.operation.poster.pageOfPaper.type == .custom {
+            columnCountText.isEditable = true
+            lineCountText.isEditable = true
+            columnCountText.isSelectable = true
+            lineCountText.isSelectable = true
+        } else {
+            columnCountText.isEditable = false
+            lineCountText.isEditable = false
+            columnCountText.isSelectable = false
+            lineCountText.isSelectable = false
+            
+            lineCountText.stringValue = model.page.operation.poster.pageOfPaper.point.x.description
+            columnCountText.stringValue = model.page.operation.poster.pageOfPaper.point.y.description
+        }
     }
     
-    
+    func updatePDFView(isPageChange: Bool = true) {
+        debugPrint("需要刷新PDFView")
+        self.reloadData()
+        if isPageChange {
+            self.pageSettingChangeAction?(self, self.viewModel.model)
+        } else {
+            self.printSettingChangeAction?(self, self.viewModel.model)
+        }
+    }
+}
+
+extension KMPrintPosterView {
     //MARK: Action
     @IBAction func buttonItemClickCutMarks(_ sender: NSButton) {
         self.viewModel.posterCutMarkAction(isSelect: cutMarksButton.state == .on, completion: { success, model in
-                
+            self.updatePDFView()
         })
     }
 
     @IBAction func buttonItemClickLabels(_ sender: NSButton) {
         self.viewModel.posterTagsAction(isSelect: cutMarksButton.state == .on, tags:[labelTextField.stringValue]) { success, model in
-            
+            self.updatePDFView(isPageChange: false)
         }
     }
 
@@ -197,7 +280,7 @@ class KMPrintPosterView: KMBaseXibView {
         }
         
         self.viewModel.printDirectionAction(type: direction, completion: { success, model in
-            
+            self.updatePDFView(isPageChange: false)
         })
     }
 
@@ -205,11 +288,8 @@ class KMPrintPosterView: KMBaseXibView {
     @IBAction func comboBoxItemClickPageRange(_ sender: NSComboBox) {
         var range: KMPrintPageRange = KMPrintPageRange()
         if sender.indexOfSelectedItem == 0 ||
-            sender.indexOfSelectedItem == 1 || 
+            sender.indexOfSelectedItem == 1 ||
             sender.indexOfSelectedItem == 2 {
-            window?.makeFirstResponder(self)
-            sender.isEditable = false
-            
             if sender.indexOfSelectedItem == 0 {
                 range.type = .allPage
             } else if sender.indexOfSelectedItem == 1 {
@@ -219,59 +299,41 @@ class KMPrintPosterView: KMBaseXibView {
             }
         } else if sender.indexOfSelectedItem == 3 {
             sender.stringValue = ""
-            sender.isEditable = true
             window?.makeFirstResponder(sender)
             
             range.type = .custom
             range.pageString = sender.stringValue
         }
         self.viewModel.pageRangeAction(range: range) { success, model in
-            
+            self.updatePDFView()
         }
     }
 
     @IBAction func comboBoxItemClickSplitSize(_ sender: NSComboBox) {
         overlapTextField.stringValue = "0"
-        var customSize = CGSize(width: 595, height: 842)
+        let customSize = CGSize(width: 595, height: 842)
 //        self.PDFPrint.splitSize = CGSizeMake(tSize.width * 595.0/210, tSize.height * 842.0/297.0);
-        if splitSizeComboBox.indexOfSelectedItem == splitSizeComboBox.numberOfItems - 1 {
-            pageWidthTextField.isEnabled = true
-            pageHeightTextField.isEnabled = true
-        } else {
-            pageWidthTextField.isEnabled = false
-            pageHeightTextField.isEnabled = false
-        }
 
         let type = KMPrintPaperInfo.KMPaperType(rawValue: sender.stringValue) ?? .A4
         self.viewModel.printPaperTypeAction(type: type, customSize: customSize, completion: { success, model in
-            
+            self.updatePDFView()
         })
         
     }
 
     @IBAction func comboBoxItemClickFullPageSize(_ sender: NSComboBox) {
         var customSize = CGSize(width: 595, height: 842)
-        if filePageSizeBox.indexOfSelectedItem == 0 {
-            paperWidthTextField.isEnabled = false
-            paperHeightTextField.isEnabled = false
-        } else if filePageSizeBox.indexOfSelectedItem == filePageSizeBox.numberOfItems - 1 {
-            paperWidthTextField.isEnabled = true
-            paperHeightTextField.isEnabled = true
-        } else {
-            paperWidthTextField.isEnabled = false
-            paperHeightTextField.isEnabled = false
-        }
 
         overlapTextField.stringValue = "0"
         let type = KMPrintPaperInfo.KMPaperType(rawValue: sender.stringValue) ?? .A4
         self.viewModel.printPaperTypeAction(type: type, posterType: .breakUp, customSize: customSize, completion: { success, model in
-            
+            self.updatePDFView()
         })
     }
 
     @IBAction func buttonItemClickReversePage(_ sender: NSButton) {
         self.viewModel.printReverseAction(isSelect: reverseButton.state == .on) { success, model in
-            
+            self.updatePDFView()
         }
     }
 
@@ -292,7 +354,7 @@ class KMPrintPosterView: KMBaseXibView {
         
         overlapTextField.stringValue = "0"
         self.viewModel.printOverlapChangeAction(value: overlapTextField.floatValue, completion: { success, model in
-            
+            self.reloadData()
         })
     }
 
@@ -307,28 +369,18 @@ class KMPrintPosterView: KMBaseXibView {
         }
         
         self.viewModel.printContentAction(type: type, completion: { success, model in
-            
+            self.updatePDFView()
         })
     }
 
     @IBAction func comboBoxItemClickPagesPer(_ sender: NSComboBox) {
-        if selectPerComboBox.indexOfSelectedItem != 5 {
-            columnCountText.isEditable = false
-            lineCountText.isEditable = false
-            columnCountText.isSelectable = false
-            lineCountText.isSelectable = false
-        } else {
-            columnCountText.isEditable = true
-            lineCountText.isEditable = true
-            columnCountText.isSelectable = true
-            lineCountText.isSelectable = true
-        }
-        
         self.viewModel.printPagePerChangeAction(type: KMPrintPageOperation.PageOfPaper.PageType(rawValue: selectPerComboBox.stringValue) ?? .page, completion: { success, model in
-            
+            self.updatePDFView()
         })
     }
+}
 
+extension KMPrintPosterView: NSTabViewDelegate {
     func tabView(_ tabView: NSTabView, willSelect tabViewItem: NSTabViewItem?) {
         overlapTextField.stringValue = "0"
         self.viewModel.printOverlapChangeAction(value: overlapTextField.floatValue, completion: { success, model in
@@ -338,16 +390,18 @@ class KMPrintPosterView: KMBaseXibView {
         if let identifier = tabViewItem?.identifier as? String {
             if identifier == "1" {
                 self.viewModel.posterTypeAction(type: .tile) { success, model in
-                    
+                    self.updatePDFView()
                 }
             } else {
                 self.viewModel.posterTypeAction(type: .breakUp) { success, model in
-                    
+                    self.updatePDFView()
                 }
             }
         }
     }
-    
+}
+
+extension KMPrintPosterView  {
     @objc func controlTextDidEndEditing(_ notification: Notification) {
         guard let textField = notification.object as? NSTextField else {
             return
@@ -411,7 +465,7 @@ class KMPrintPosterView: KMBaseXibView {
             var size = self.viewModel.model.paper.info.size
             size.width = over
             self.viewModel.printPaperTypeAction(type: .custom, customSize: size, completion: { success, model in
-                
+                self.updatePDFView()
             })
             
         } else if textField == pageHeightTextField || textField == paperHeightTextField {
@@ -430,7 +484,7 @@ class KMPrintPosterView: KMBaseXibView {
             var size = self.viewModel.model.paper.info.size
             size.width = over
             self.viewModel.printPaperTypeAction(type: .custom, customSize: size, completion: { success, model in
-                
+                self.updatePDFView()
             })
         }
     }
@@ -468,7 +522,6 @@ class KMPrintPosterView: KMBaseXibView {
             }
         }
     }
-
 }
 
 

+ 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

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

@@ -209,7 +209,6 @@ class KMWatermarkView: KMBaseXibView, NSTextFieldDelegate, NSComboBoxDelegate, N
         fontSizeCombobox.addItems(withObjectValues: ["9","10","11","12","13","14","18","24","36","48","64","72","96","144","288"])
         fontSizeCombobox.formatter = TextFieldFormatter()
         
-        watermarkStringTextView.string = watermark.text 
         watermarkStringTextView.delegate = self
         
         pageRangeComboBox.removeAllItems()
@@ -246,7 +245,6 @@ class KMWatermarkView: KMBaseXibView, NSTextFieldDelegate, NSComboBoxDelegate, N
         verticalIconButton.toolTip = NSLocalizedString("Vertical Distance", comment: "")
         fontSizeLabel.stringValue = "\(NSLocalizedString("Font Size", comment: "")):"
         colorLabel.stringValue = "\(NSLocalizedString("Color", comment: "")):"
-        colorWell.color = watermark.getTextColor()
         templateNameTextField.stringValue = watermark.watermarkID ?? ""
         
         appearanceBox.title = NSLocalizedString("Appearance", comment: "")
@@ -278,6 +276,9 @@ class KMWatermarkView: KMBaseXibView, NSTextFieldDelegate, NSComboBoxDelegate, N
         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 = "\(watermark.getTextFontSize())"
@@ -293,7 +294,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>