Browse Source

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

tangchao 1 year ago
parent
commit
0a3d8cdef3

+ 1 - 1
PDF Office/PDF Master/Class/Document/KMMainDocument.swift

@@ -557,7 +557,7 @@ typealias KMMainDocumentCloudUploadHanddler = (@escaping(Bool, String)->()) -> (
     }
     @IBAction func printPDFDocument(_ sender: Any?) {
         KMPrint("printPDFDocument")
-        KMPrintWindowController.showNewPrintWindowControll(inputData: self.mainViewController?.document?.documentURL, inputDocument: nil, inputPageRange: KMPrintPageRange())
+        KMPrintWindowController.showNewPrintWindowControll(inputDocument: self.mainViewController?.document, inputPageRange: KMPrintPageRange())
     }
     @IBAction func performFindPanelAction(_ sender: Any?) {
         KMPrint("performFindPanelAction")

+ 6 - 3
PDF Office/PDF Master/Class/Home/ViewController/KMHomeViewController+Action.swift

@@ -1693,7 +1693,8 @@ extension KMHomeViewController {
             }
             
             DispatchQueue.main.asyncAfter(deadline: .now() + 0.25) {
-                KMPrintWindowController.showNewPrintWindowControll(inputData: openPanel.url, inputDocument: nil, inputPageRange: KMPrintPageRange(), printType: .pamphlet)
+                let pdfDocument = CPDFDocument(url: openPanel.url!)
+                KMPrintWindowController.showNewPrintWindowControll(inputDocument: pdfDocument, inputPageRange: KMPrintPageRange(), printType: .pamphlet)
             }
         }
     }
@@ -1725,7 +1726,8 @@ extension KMHomeViewController {
             }
             
             DispatchQueue.main.asyncAfter(deadline: .now() + 0.25) {
-                KMPrintWindowController.showNewPrintWindowControll(inputData: openPanel.url, inputDocument: nil, inputPageRange: KMPrintPageRange(), printType: .poster)
+                let pdfDocument = CPDFDocument(url: openPanel.url!)
+                KMPrintWindowController.showNewPrintWindowControll(inputDocument: pdfDocument, inputPageRange: KMPrintPageRange(), printType: .poster)
             }
         }
     }
@@ -1757,7 +1759,8 @@ extension KMHomeViewController {
             }
             
             DispatchQueue.main.asyncAfter(deadline: .now() + 0.25) {
-                KMPrintWindowController.showNewPrintWindowControll(inputData: openPanel.url, inputDocument: nil, inputPageRange: KMPrintPageRange(), printType: .multipage)
+                let pdfDocument = CPDFDocument(url: openPanel.url!)
+                KMPrintWindowController.showNewPrintWindowControll(inputDocument: pdfDocument, inputPageRange: KMPrintPageRange(), printType: .multipage)
             }
         }
     }

+ 2 - 0
PDF Office/PDF Master/Class/PDFTools/Compare/View/KMCompareView.swift

@@ -290,6 +290,8 @@ extension KMCompareView {
             let cDocument: CPDFDocument = CPDFDocument.init(url: NSURL(fileURLWithPath: filePath) as URL)!
             let file: KMFileAttribute = KMFileAttribute()
             file.pdfDocument = cDocument
+            file.myPDFDocument = document
+            file.filePath = filePath
             
             document.unlock(withPassword: password)
             cDocument.unlock(withPassword: password)

+ 101 - 3
PDF Office/PDF Master/Class/PDFTools/Merge/Model/KMFileAttribute.swift

@@ -86,9 +86,9 @@ import Cocoa
                 }
             }
         } else {
-//            isInvalidString(pagesString)
-            let pages = KMPageRangeTools.findSelectPage(pageRangeString: pagesString, pageCount: Int(pdfDocument.pageCount ))
-            selectPages = pages
+            isInvalidString(pagesString)
+//            let pages = KMPageRangeTools.findSelectPage(pageRangeString: pagesString, pageCount: Int(pdfDocument.pageCount ))
+//            selectPages = pages
             if !bAllPage {
                 self.QuickSort(&selectPages, startIndex: 0, endIndex: selectPages.count-1)
             }
@@ -97,6 +97,104 @@ import Cocoa
         return selectPages
     }
     
+    func isInvalidString(_ text: String) -> Bool {
+        var document: PDFDocument?
+        if (self.myPDFDocument != nil) {
+            document = self.myPDFDocument
+        } else {
+            document = PDFDocument(url: URL(fileURLWithPath: self.filePath ))
+        }
+        
+        if let data = document?.isLocked, data {
+            document?.unlock(withPassword: self.password )
+        }
+        let pageNumber = document?.pageCount ?? 1
+        if (self.bAllPage) {
+            self.selectPages = []
+            for i in 1 ... pageNumber {
+                self.selectPages.append(i)
+            }
+            return false
+        }
+        
+        var pageNumbers: [Int] = []
+        var isInvalid = false
+        var c: unichar = 0
+        for c in text {
+            if (c != "0" && c != "1" && c != "2" && c != "3" && c != "4" && c != "5" && c != "6" && c != "7" && c != "8" && c != "9" && c != "," && c != "-") {
+                isInvalid = true
+                break
+            }else{
+                isInvalid = false
+            }
+        }
+        if (!isInvalid) {
+            let array = text.components(separatedBy: ",")
+            for s in array {
+                if s.isEmpty {
+                    isInvalid = true
+                    break
+                }else{
+                    let pages = s.components(separatedBy: "-")
+                    if (pages.count>2) {
+                        isInvalid = true
+                        break
+                    }else if(pages.count==1){
+                        let p = pages.first!
+                        if p.isEmpty || Int(p)! > pageNumber || Int(p) == 0 {
+                            isInvalid = true
+                            break
+                        }else{
+                            var isEqual = false
+                            for pageNumber in pageNumbers {
+                                if pageNumber == Int(p) {
+                                    isEqual = true
+                                    isInvalid = true
+                                    break
+                                }
+                            }
+                            if (!isEqual) {
+                                pageNumbers.append(Int(p)!)
+                            }
+                        }
+                    }else if(pages.count==2){
+                        let p1 = pages[0]
+                        let p2 = pages[1]
+                        if p1.isEmpty || p2.isEmpty || Int(p1)! >= Int(p2)! || Int(p2)! > pageNumber || Int(p1) == 0 {
+                            isInvalid = true
+                            break
+                        }else{
+                            var isEqual = false
+                            for i in Int(p1)! ... Int(p2)! {
+                                for pageNumber in pageNumbers {
+                                    if pageNumber == i {
+                                        isEqual = true
+                                        isInvalid = true
+                                        break
+                                    }
+                                }
+                            }
+                            if (!isEqual) {
+                                for i in Int(p1)! ... Int(p2)! {
+                                    pageNumbers.append(i)
+                                }
+                            }
+                        }
+                    }
+                }
+            }
+        }
+            if (text.isEmpty) {
+            isInvalid = true
+        }
+        if (isInvalid) {
+            self.selectPages = []
+        }else{
+            self.selectPages = pageNumbers
+        }
+        return isInvalid
+    }
+    
     func QuickSort(_ list: inout [Int],startIndex: Int, endIndex: Int) {
         if(startIndex >= endIndex) {
             return

+ 26 - 22
PDF Office/PDF Master/Class/PDFTools/Print/KMPrintWindowController.swift

@@ -18,6 +18,12 @@ class KMPrintWindowController: NSWindowController, NetServiceBrowserDelegate {
     @IBOutlet weak var bottomView: KMPrintBottomView!
     
     var pdfDocument: CPDFDocument? = nil
+    var password: String = "" {
+        didSet {
+            self.preview.password = password
+            self.presenter.password = password
+        }
+    }
     var isPrintPreView: Bool = true {
         didSet {
             self.preview.isPrintPreView = isPrintPreView
@@ -47,24 +53,25 @@ class KMPrintWindowController: NSWindowController, NetServiceBrowserDelegate {
             self.preview.toPageIndex(UInt(inputPageRange.selectPages.first ?? 0))
         }
     }
-    var inputData: URL? {
-        didSet {
-            let pdfDocument = CPDFDocument.init(url: inputData)
-            self.chooseView.inputData = inputData
-            
-            if pdfDocument != nil {
-                self.preview.pdfDocument = pdfDocument
-                self.pdfDocument = pdfDocument
-                
-                self.presenter.initPresenter(delegate: self, data: self.chooseData, document: pdfDocument!)
-            }
-        }
-    }
+//    var inputData: URL? {
+//        didSet {
+//            let pdfDocument = CPDFDocument.init(url: inputData)
+//            self.chooseView.inputData = inputData
+//            
+//            if pdfDocument != nil {
+//                self.preview.pdfDocument = pdfDocument
+//                self.pdfDocument = pdfDocument
+//                
+//                self.presenter.initPresenter(delegate: self, data: self.chooseData, document: pdfDocument!)
+//            }
+//        }
+//    }
     
     var inputDocument: CPDFDocument? {
         didSet {
             pdfDocument = inputDocument
-            self.chooseView.inputData = URL(string: "")
+            password = inputDocument?.password ?? ""
+//            self.chooseView.inputData = URL(string: "")
             
             if pdfDocument != nil {
                 self.preview.pdfDocument = pdfDocument
@@ -164,7 +171,7 @@ class KMPrintWindowController: NSWindowController, NetServiceBrowserDelegate {
         KMPrintWindowController.showPrintWindowControll(inputData: nil, inputDocument: inputDocument, inputType: inputType,inputPageRange: inputPageRange)
     }
     
-    static func showNewPrintWindowControll(inputData: URL?, inputDocument: CPDFDocument?, inputType: DataNavigationViewButtonActionType = .Print, inputPageRange: KMPrintPageRange, printType: KMPrintModelType = .size) {
+    static func showNewPrintWindowControll(inputDocument: CPDFDocument?, inputType: DataNavigationViewButtonActionType = .Print, inputPageRange: KMPrintPageRange, printType: KMPrintModelType = .size) {
         let printWindowController: KMPrintWindowController = KMPrintWindowController.init(windowNibName: "KMPrintWindowController")
 
         printWindowController.printType = printType
@@ -193,10 +200,7 @@ class KMPrintWindowController: NSWindowController, NetServiceBrowserDelegate {
         if inputDocument != nil {
             printWindowController.inputDocument = inputDocument
         }
-
-        if inputData != nil {
-            printWindowController.inputData = inputData
-        }
+        
         printWindowController.inputType = inputType
         printWindowController.inputPageRange = inputPageRange
     }
@@ -394,21 +398,21 @@ extension KMPrintWindowController: KMPrintBottomViewDelegate {
         self.changeTypeAction?(self, .poster)
         self.cancelAction()
         
-        KMPrintWindowController.showNewPrintWindowControll(inputData: self.pdfDocument?.documentURL, inputDocument: nil, inputPageRange: KMPrintPageRange(), printType: .poster)
+        KMPrintWindowController.showNewPrintWindowControll(inputDocument: self.pdfDocument, inputPageRange: KMPrintPageRange(), printType: .poster)
     }
 
     func multipageAction() {
         self.changeTypeAction?(self, .multipage)
         self.cancelAction()
         
-        KMPrintWindowController.showNewPrintWindowControll(inputData: self.pdfDocument?.documentURL, inputDocument: nil, inputPageRange: KMPrintPageRange(), printType: .multipage)
+        KMPrintWindowController.showNewPrintWindowControll(inputDocument: self.pdfDocument, inputPageRange: KMPrintPageRange(), printType: .multipage)
     }
     
     func bookletAction() {
         self.changeTypeAction?(self, .pamphlet)
         self.cancelAction()
         
-        KMPrintWindowController.showNewPrintWindowControll(inputData: self.pdfDocument?.documentURL, inputDocument: nil, inputPageRange: KMPrintPageRange(), printType: .pamphlet)
+        KMPrintWindowController.showNewPrintWindowControll(inputDocument: self.pdfDocument, inputPageRange: KMPrintPageRange(), printType: .pamphlet)
     }
 }
 

+ 2 - 0
PDF Office/PDF Master/Class/PDFTools/Print/Presenter/KMPrintPresenter.swift

@@ -15,6 +15,7 @@ class KMPrintPresenter: NSObject {
             self.reloadData()
         }
     }
+    var password: String = ""
     var document: CPDFDocument?
     fileprivate weak var delegate: KMPrintPresenterDeleage?
     
@@ -281,6 +282,7 @@ extension KMPrintPresenter: KMPrintPresenterDocument {
      */
     func fetchPages(_ documentURL: URL, _ pageModel: KMPrintPageModel) -> [KMPrintDrawPage] {
         let document = PDFDocument.init(url: documentURL)!
+        document.unlock(withPassword: password)
         var pageIndexs: [Int] = []
         let range = pageModel.range
         let contentType = pageModel.contentType

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

@@ -23,6 +23,7 @@ class KMPrintPreviewView: KMBaseXibView {
     @IBOutlet weak var partitionLabel: NSTextField!
     
     var isPrintPreView: Bool = true
+    var password: String = ""
     var model: KMPrintModel? {
         didSet {
             self.reloadData()
@@ -33,6 +34,7 @@ class KMPrintPreviewView: KMBaseXibView {
     var pdfDocument: CPDFDocument? {
         didSet {
             self.previewView.document = PDFDocument(url: pdfDocument!.documentURL)
+            self.previewView.document?.unlock(withPassword: password)
             self.previewView.autoScales = true
             self.previewView.displayMode = .singlePage
             self.previewView.documentView?.enclosingScrollView?.scrollerStyle = .overlay

+ 3 - 3
PDF Office/PDF Master/Class/PDFWindowController/ViewController/KMMainViewController+MenuAction.swift

@@ -936,15 +936,15 @@ extension KMMainViewController: KMSystemToolMenuProtocol {
     }
     
     func togglePoster(_ sender: Any?) {
-        KMPrintWindowController.showNewPrintWindowControll(inputData: self.listView.document?.documentURL, inputDocument: nil, inputPageRange: KMPrintPageRange(), printType: .poster)
+        KMPrintWindowController.showNewPrintWindowControll(inputDocument: self.listView.document, inputPageRange: KMPrintPageRange(), printType: .poster)
     }
     
     func toggleMultiple(_ sender: Any?) {
-        KMPrintWindowController.showNewPrintWindowControll(inputData: self.listView.document?.documentURL, inputDocument: nil, inputPageRange: KMPrintPageRange(), printType: .multipage)
+        KMPrintWindowController.showNewPrintWindowControll(inputDocument: self.listView.document, inputPageRange: KMPrintPageRange(), printType: .multipage)
     }
     
     func toggleBooklet(_ sender: Any?) {
-        KMPrintWindowController.showNewPrintWindowControll(inputData: self.listView.document?.documentURL, inputDocument: nil, inputPageRange: KMPrintPageRange(), printType: .pamphlet)
+        KMPrintWindowController.showNewPrintWindowControll(inputDocument: self.listView.document, inputPageRange: KMPrintPageRange(), printType: .pamphlet)
     }
     
     func rotateLeft(_ sender: Any?) {