Browse Source

【综合】调整打印方法为 printDocument: 规避打印事件响应异常崩溃

wanjun 10 months ago
parent
commit
7ef41a3935

+ 1 - 1
PDF Office/PDF Master DMG/Base.lproj/Main.storyboard

@@ -112,7 +112,7 @@
                                         </menuItem>
                                         <menuItem title="Print…" keyEquivalent="p" id="aTl-1u-JFS">
                                             <connections>
-                                                <action selector="print:" target="Ady-hI-5gd" id="qaZ-4w-aoO"/>
+                                                <action selector="printDocument:" target="Ady-hI-5gd" id="rNu-jn-HyX"/>
                                             </connections>
                                         </menuItem>
                                     </items>

+ 1 - 1
PDF Office/PDF Master Pro/Base.lproj/Main.storyboard

@@ -112,7 +112,7 @@
                                         </menuItem>
                                         <menuItem title="Print…" keyEquivalent="p" id="aTl-1u-JFS">
                                             <connections>
-                                                <action selector="print:" target="Ady-hI-5gd" id="qaZ-4w-aoO"/>
+                                                <action selector="printDocument:" target="Ady-hI-5gd" id="EVI-gk-cBE"/>
                                             </connections>
                                         </menuItem>
                                     </items>

+ 51 - 0
PDF Office/PDF Master/Class/Document/KMMainDocument.swift

@@ -1174,6 +1174,57 @@ typealias KMMainDocumentCloudUploadHanddler = (@escaping(Bool, String)->()) -> (
             formatPopup?.removeItem(at: formatPopup!.index(of: item))
         }
     }
+    
+    // MARK: - Printing
+    
+    override func printOperation(withSettings printSettings: [NSPrintInfo.AttributeKey : Any]) throws -> NSPrintOperation {
+        let printInfo = self.printInfo.copy() as! NSPrintInfo
+        printInfo.dictionary().addEntries(from: printSettings)
+        
+        var printOperation: NSPrintOperation?
+        if self.isHome {
+            return NSPrintOperation()
+        }
+        let documentURL = self.mainViewController?.document?.documentURL
+        if documentURL == nil {
+            return NSPrintOperation()
+        }
+
+        guard let pdfDoc = PDFDocument(url: documentURL!) else {
+            return NSPrintOperation()
+        }
+
+        if pdfDoc.responds(to: #selector(PDFDocument.printOperation(for:scalingMode:autoRotate:))) {
+            printOperation = pdfDoc.printOperation(for: printInfo, scalingMode: .pageScaleNone, autoRotate: true)
+        } else if pdfDoc.responds(to: #selector(PDFDocument.getPrintOperation(for:autoRotate:))) {
+            printOperation = pdfDoc.getPrintOperation(for: printInfo, autoRotate: true)
+        }
+        
+        // NSPrintProtected is a private key that disables the items in the PDF popup of the Print panel, and is set for encrypted documents
+        if pdfDoc.isEncrypted {
+            printOperation?.printInfo.dictionary().setValue(false, forKey: "NSPrintProtected")
+        }
+        
+        let printPanel = printOperation?.printPanel
+        printPanel?.options = [.showsCopies, .showsPageRange, .showsPaperSize, .showsOrientation, .showsScaling, .showsPreview]
+        
+        if printOperation == nil {
+            throw NSError.printDocumentError(withLocalizedDescription: "")
+        }
+        
+        return printOperation!
+    }
+    
+    override func runModalPrintOperation(_ printOperation: NSPrintOperation, delegate: Any?, didRun didRunSelector: Selector?, contextInfo: UnsafeMutableRawPointer?) {
+        printOperation.run()
+    }
+}
+
+extension PDFDocument {
+    @objc func getPrintOperation(for printInfo: NSPrintInfo, autoRotate: Bool) -> NSPrintOperation {
+        // 在此处实现方法的具体逻辑
+        return NSPrintOperation()
+    }
 }
 
 extension KMMainDocument {