Browse Source

【编辑工具】自定义页面流程完善

lizhe 1 year ago
parent
commit
188d154763

+ 35 - 39
PDF Office/PDF Master/Class/PDFTools/AddHeaderFooter/New/View/KMHeaderFooterView.swift

@@ -931,20 +931,22 @@ class KMHeaderFooterView: KMBaseXibView, NSTextViewDelegate, NSComboBoxDelegate
         }
 
         if comboBox == pageRangeCombobox {
-            if !checkPageRangeValidate(pageRangeString: comboBox.stringValue) {
-                let alert = NSAlert()
-                alert.alertStyle = .critical
-                alert.messageText = "\(pdfDocument?.documentURL?.lastPathComponent ?? NSLocalizedString("Untitled", comment: "")) \(NSLocalizedString("Invalid page range or the page number is out of range. Please try again.", comment: ""))"
-                alert.runModal()
-
-                headerFooterObj.pageRangeType = .all
-                pageRangeCombobox.selectItem(at: 0)
-//                pageRangeCombobox.isEditable = false
-                pageRangeCombobox.isSelectable = false
-                return
-            } else {
-                headerFooterObj.pageRangeString = comboBox.stringValue
-                self.updatePDFView()
+            if pageRangeCombobox.indexOfSelectedItem == -1 {
+                if !checkPageRangeValidate(pageRangeString: comboBox.stringValue) {
+                    let alert = NSAlert()
+                    alert.alertStyle = .critical
+                    alert.messageText = "\(pdfDocument?.documentURL?.lastPathComponent ?? NSLocalizedString("Untitled", comment: "")) \(NSLocalizedString("Invalid page range or the page number is out of range. Please try again.", comment: ""))"
+                    alert.runModal()
+                    
+                    headerFooterObj.pageRangeType = .all
+                    pageRangeCombobox.selectItem(at: 0)
+                    //                pageRangeCombobox.isEditable = false
+                    pageRangeCombobox.isSelectable = false
+                    return
+                } else {
+                    headerFooterObj.pageRangeString = comboBox.stringValue
+                    self.updatePDFView()
+                }
             }
         }
     }
@@ -966,7 +968,7 @@ class KMHeaderFooterView: KMBaseXibView, NSTextViewDelegate, NSComboBoxDelegate
                 headerFooterObj.pageRangeType = .even
             default:
                 headerFooterObj.pageRangeType = .other
-                pageRangeCombobox.stringValue = ""
+//                pageRangeCombobox.stringValue = ""
                 pageRangeCombobox.isEditable = true
                 window?.makeFirstResponder(pageRangeCombobox)
             }
@@ -1116,45 +1118,39 @@ extension KMHeaderFooterView {
     }
 
     @IBAction func apply(_ sender: Any) {
+        guard let pdfDocument = pdfDocument else { return }
         let needSave = saveToTemplateButton.state == .on
         
-        var pages = [String]()
+        var pages = [Int]()
+
         switch pageRangeCombobox.indexOfSelectedItem {
         case 0:
-            for i in 0..<pdfDocument!.pageCount {
-                pages.append("\(i)")
-            }
+            pages = Array(0..<Int(pdfDocument.pageCount))
         case 1:
-            for i in 0..<pdfDocument!.pageCount where i % 2 == 0 {
-                pages.append("\(i)")
-            }
+            pages = Array(stride(from: 0, to: Int(pdfDocument.pageCount), by: 2))
         case 2:
-            for i in 0..<pdfDocument!.pageCount where i % 2 != 0 {
-                pages.append("\(i)")
-            }
+            pages = Array(stride(from: 1, to: Int(pdfDocument.pageCount), by: 2))
         default:
             let fileAttribute = KMFileAttribute()
-            fileAttribute.filePath = pdfDocument?.documentURL?.path ?? ""
+            fileAttribute.filePath = pdfDocument.documentURL?.path ?? ""
             fileAttribute.bAllPage = false
+            fileAttribute.pagesType = .custom
+            fileAttribute.password = pdfDocument.password ?? ""
             fileAttribute.pagesString = pageRangeCombobox.stringValue
-            
-            if fileAttribute.fetchSelectPages().count == 0 {
+
+            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
             }
-            for num in fileAttribute.fetchSelectPages() {
-                pages.append("\(num)")
-            }
-        }
-        
-        if pages.count > 0 {
-            headerFooterObj.pageRangeString = pages.joined(separator: ",")
-        } else {
-            headerFooterObj.pageRangeString = ""
         }
+
+        headerFooterObj.pageRangeString = pages.isEmpty ? "" : pages.map { "\($0)" }.joined(separator: ",")
         
         if type == .Add {
             let object = headerFooterObj
@@ -1191,9 +1187,9 @@ extension KMHeaderFooterView {
             
             var fileName: String?
             if isBates {
-                fileName = "\(pdfDocument?.documentURL?.lastPathComponent ?? NSLocalizedString("Untitled", comment: ""))_Bates"
+                fileName = "\(pdfDocument.documentURL?.lastPathComponent ?? NSLocalizedString("Untitled", comment: ""))_Bates"
             } else {
-                fileName = "\(pdfDocument?.documentURL?.lastPathComponent ?? NSLocalizedString("Untitled", comment: ""))_Header_Footer"
+                fileName = "\(pdfDocument.documentURL?.lastPathComponent ?? NSLocalizedString("Untitled", comment: ""))_Header_Footer"
             }
             
             let savePanelAccessoryViewController = KMSavePanelAccessoryController()

+ 2 - 0
PDF Office/PDF Master/Class/PDFTools/Background/New/View/KMAddBackgroundView.swift

@@ -865,6 +865,8 @@ extension KMAddBackgroundView {
             let fileAttribute = KMFileAttribute()
             fileAttribute.filePath = pdfDocument.documentURL?.path ?? ""
             fileAttribute.bAllPage = false
+            fileAttribute.pagesType = .custom
+            fileAttribute.password = pdfDocument.password ?? ""
             fileAttribute.pagesString = pageRangeComboBox.stringValue
 
             let selectPages = fileAttribute.fetchSelectPages()

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

@@ -743,47 +743,51 @@ extension KMWatermarkView {
     }
     
     @IBAction func buttonClicked_Done(_ sender: Any) {
-        if checkPageRangeValidate(pageRangeComboBox.stringValue) {
-            if watermark.pageRangeType == .other {
-                var pageString: String = ""
-                let fileAttribute = KMFileAttribute()
-                fileAttribute.filePath = (self.pdfDocument?.documentURL.path)!
-                fileAttribute.password = self.pdfDocument?.password ?? ""
-                fileAttribute.bAllPage = false
-                fileAttribute.pagesType = .custom
-                fileAttribute.pagesString = self.pageRangeComboBox.stringValue
-                let pages = fileAttribute.fetchSelectPages()
-                for i in 0 ..< pages.count {
-                    pageString.append("\(pages[i] - 1)")
-                    
-                    if (i != pages.count-1) {
-                        pageString.append(",")
-                    }
-                }
-                self.watermark.pagesString = pageString
-            } else {
-                self.watermark.pagesString = self.pageRangeComboBox.stringValue
+        guard let pdfDocument = pdfDocument else { return }
+        
+        if self.watermark.imagePath.count == 0 {
+            if self.watermark.text.isEmpty {
+                return
             }
-            updatePDFView()
         }
         
-        if self.watermark.pagesString.count == 0 {
-            let alert = NSAlert()
-            alert.alertStyle = .critical
-            alert.messageText = "\(self.pdfDocument?.documentURL.lastPathComponent) Invalid page range or the page number is out of range. Please try again."
-            alert.runModal()
-            return
-        } else {
-            window?.makeFirstResponder(nil)
+        if checkPageRangeValidate(pageRangeComboBox.stringValue) {
+            watermark.pagesString = pageRangeComboBox.stringValue
+            self.updatePDFView()
+            window?.makeFirstResponder(self)
         }
-        
+
         let needSave = saveToTemplateButton.state == .on
-        
-        if self.watermark.imagePath == nil {
-            if self.watermark.text.isEmpty {
+        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:
+            let fileAttribute = KMFileAttribute()
+            fileAttribute.filePath = pdfDocument.documentURL?.path ?? ""
+            fileAttribute.bAllPage = false
+            fileAttribute.pagesType = .custom
+            fileAttribute.password = pdfDocument.password ?? ""
+            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 self.type == .edit || self.type == .add {
             if needSave {
@@ -816,55 +820,16 @@ extension KMWatermarkView {
             
             savePanel.beginSheetModal(for: self.window!) { [unowned self] result in
                 if result == NSApplication.ModalResponse.OK {
-//                    let document = CPDFDocument(url: self.pdfDocument?.documentURL)
-//                    document?.unlock(withPassword: self.pdfDocument?.password)
-                    
                     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
-                                    // Handle the completion if needed
                                 }
                             } else {
                                 NSWorkspace.shared.activateFileViewerSelecting([savePanel.url!])
                             }
                         }
                     }
-
-//
-//                    let obj = KMPDFWatermarkObject(pdfDocument: document)
-//                    obj?.addWatermark(self.watermark, toPath: savePanel.url!.path, completionHandler: { (isSuccessfully) in
-//                        DispatchQueue.main.async {
-//                            savePanelAccessoryViewController.release()
-//
-//                            if isSuccessfully {
-//                                if savePanelAccessoryViewController.openAutomaticButton.state == .on {
-//                                    NSDocumentController.shared.openDocument(withContentsOf: savePanel.url!, display: true) { (document, documentWasAlreadyOpen, error) in
-//                                        // Handle the completion if needed
-//                                    }
-//                                } else {
-//                                    NSWorkspace.shared.activateFileViewer(selecting: [savePanel.url!])
-//                                }
-//                            }
-//                        }
-//                    })
-//
-//                    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) {
-//                            KMWatermarkManager.defaultManager.removeWatermark(withData: self.originalWatermark)
-//                            KMWatermarkManager.defaultManager.addWatermark(withData: self.watermark)
-//                        } else {
-//                            KMWatermarkManager.defaultManager.addWatermark(withData: self.watermark)
-//                        }
-//
-//                        NotificationCenter.default.post(name: NSNotification.Name("KMBatchOperateWatermarksNotification"), object: self)
-//                    }
                 }
             }
         }