Browse Source

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

tangchao 10 tháng trước cách đây
mục cha
commit
4d95a325fd

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

@@ -277,78 +277,78 @@ extension KMPrintWindowController {
             document = PDFDocument.init(url: inputData!)!
         }
         
-        //页面选取
-        if ((inputPageRange.type == .currentPage ||
-            inputPageRange.type == .custom) &&
-            inputPageRange.selectPages.count != 0) {
-            
-            let printPresent = KMPrintPresenter()
-            let filePath = KMPrintPresenter.fetchSaveFilePath("")
-            let paperSize = (document?.page(at: 0)?.bounds(for: .cropBox).size) ?? CGSizeZero
-            let context: CGContext = printPresent.createContext(filePath, paperSize)
-            var pages: [PDFPage] = []
-            for index in inputPageRange.selectPages {
-                let page = document?.page(at: index)
-                if page != nil {
-                    pages.append(page!)
-                }
-            }
-            
-            for drawPage in pages {
-                context.beginPDFPage(nil)
-                
-                context.saveGState()
+        if document != nil {
+            //页面选取
+            if ((inputPageRange.type == .currentPage ||
+                inputPageRange.type == .custom) &&
+                inputPageRange.selectPages.count != 0) {
                 
-                var pageSize = drawPage.bounds(for: .cropBox).size
-
-                if (drawPage.rotation == 90 || drawPage.rotation == 270) {
-                    let height = pageSize.height
-                    pageSize.height = pageSize.width
-                    pageSize.width = height
+                let printPresent = KMPrintPresenter()
+                let filePath = KMPrintPresenter.fetchSaveFilePath("")
+                let paperSize = (document?.page(at: 0)?.bounds(for: .cropBox).size) ?? CGSizeZero
+                let context: CGContext = printPresent.createContext(filePath, paperSize)
+                var pages: [PDFPage] = []
+                for index in inputPageRange.selectPages {
+                    let page = document?.page(at: index)
+                    if page != nil {
+                        pages.append(page!)
+                    }
                 }
                 
-                // 检查要自适应的大小是否大于参考矩形的大小
-                let maxWidth = paperSize.width
-                let maxHeight = paperSize.height
-                var scaledSize = pageSize
+                for drawPage in pages {
+                    context.beginPDFPage(nil)
+                    
+                    context.saveGState()
+                    
+                    var pageSize = drawPage.bounds(for: .cropBox).size
 
-                var ratio = 1.0
-                if pageSize.width > maxWidth || pageSize.height > maxHeight {
-                    let widthRatio = maxWidth / pageSize.width
-                    let heightRatio = maxHeight / pageSize.height
-                    ratio = min(widthRatio, heightRatio)
-                    scaledSize = CGSize(width: pageSize.width * ratio, height: pageSize.height * ratio)
-                }
+                    if (drawPage.rotation == 90 || drawPage.rotation == 270) {
+                        let height = pageSize.height
+                        pageSize.height = pageSize.width
+                        pageSize.width = height
+                    }
+                    
+                    // 检查要自适应的大小是否大于参考矩形的大小
+                    let maxWidth = paperSize.width
+                    let maxHeight = paperSize.height
+                    var scaledSize = pageSize
+
+                    var ratio = 1.0
+                    if pageSize.width > maxWidth || pageSize.height > maxHeight {
+                        let widthRatio = maxWidth / pageSize.width
+                        let heightRatio = maxHeight / pageSize.height
+                        ratio = min(widthRatio, heightRatio)
+                        scaledSize = CGSize(width: pageSize.width * ratio, height: pageSize.height * ratio)
+                    }
 
-                // 计算自适应后的矩形大小和位置
-                let scaledWidth = scaledSize.width
-                let scaledHeight = scaledSize.height
-                let scaledRect = CGRect(x: (paperSize.width - scaledWidth) / 2, y: (paperSize.height - scaledHeight) / 2, width: scaledWidth, height: scaledHeight)
+                    // 计算自适应后的矩形大小和位置
+                    let scaledWidth = scaledSize.width
+                    let scaledHeight = scaledSize.height
+                    let scaledRect = CGRect(x: (paperSize.width - scaledWidth) / 2, y: (paperSize.height - scaledHeight) / 2, width: scaledWidth, height: scaledHeight)
 
-                context.translateBy(x: scaledRect.origin.x, y: scaledRect.origin.y)
-                context.scaleBy(x: ratio, y: ratio)
+                    context.translateBy(x: scaledRect.origin.x, y: scaledRect.origin.y)
+                    context.scaleBy(x: ratio, y: ratio)
+                    
+                    drawPage.draw(with: .cropBox, to: context)
+                    context.restoreGState()
+                    context.endPDFPage()
+                }
+                context.closePDF()
                 
-                drawPage.draw(with: .cropBox, to: context)
-                context.restoreGState()
-                context.endPDFPage()
+                document = PDFDocument(url: URL(fileURLWithPath: filePath))!
             }
-            context.closePDF()
-            
-            document = PDFDocument(url: URL(fileURLWithPath: filePath))!
-        }
         
-        if document != nil {
             if inputDocument != nil {
                 document?.unlock(withPassword: password)
             }
             
             if (inputDocument!.allowsPrinting == false) {
-                KMPasswordInputWindow.openWindow(window: NSApp.mainWindow!, type: .owner, url: (inputDocument!.documentURL)!) { result , password in
+                KMPasswordInputWindow.openWindow(window: NSApp.mainWindow!, type: .owner, url: (inputDocument!.documentURL)!) { result , resultPassword in
                     if (result == .cancel) {
                         return
                     }
                     
-                    document!.unlock(withPassword: password!)
+                    document!.unlock(withPassword: resultPassword ?? "")
                     KMPrintWindowController.openPrintView(document: document)
                 }
                 return

+ 2 - 1
PDF Office/PDF Master/Class/PDFWindowController/ViewController/KMMainViewController+Action.swift

@@ -1664,7 +1664,8 @@ extension KMMainViewController {
         page.setBounds(rect, for: .cropBox)
         let image = page.thumbnail(of: rect.size) ?? NSImage()
         let data = image.tiffRepresentation
-        let imageRep : NSBitmapImageRep = NSBitmapImageRep(data: data!) ?? NSBitmapImageRep()
+        guard let data = data else { return }
+        let imageRep : NSBitmapImageRep = NSBitmapImageRep(data: data) ?? NSBitmapImageRep()
         imageRep.size = rect.size
         
         let savePanel = NSSavePanel()