使用此代码,我可以打印pdf文件:
let dictDocuments = NSURL(fileURLWithPath: NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0])
let completePath = dictDocuments.appendingPathComponent("test.pdf")
let pdfDocument = PDFDocument(url: completePath!)
let printInfoDict = NSMutableDictionary(dictionary: NSPrintInfo.shared().dictionary())
printInfoDict.setObject(NSPrintSpoolJob, forKey: NSPrintJobDisposition as NSCopying)
let printInfo = NSPrintInfo(dictionary: printInfoDict as! [String : Any])
printInfo.isHorizontallyCentered = true
printInfo.isVerticallyCentered = true
printInfo.scalingFactor = 1.5
let printOp = pdfDocument!.printOperation(for: printInfo, scalingMode: .pageScaleNone, autoRotate: true)!
printOp.runModal(for: self.view.window!, delegate: self, didRun: nil, contextInfo: nil)
我可以检查用户在打印面板中选择了哪个按钮? (取消或打印)
最佳答案
从documentation
didRunSelector
参数指定的方法必须具有以下签名:
func printOperationDidRun(_ printOperation NSPrintOperation, success: Bool, contextInfo: UnsafeMutableRawPointer?)
如果打印操作顺利完成而没有取消或错误,则成功值为
true
,否则为false
。如果将
canSpawnSeparateThread
发送到参数为NSPrintOperation
的true
对象,则随后在runOperationModalForWindow:delegate:didRunSelector:contextInfo:
调用中指定的委托可能会在该生成的非主线程中发送消息。关于swift - nsprintoperation-检查选择了什么用户,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45285062/