我有一个显示PDFView的应用程序,我希望它能从视图中打印PDF文件。另外,我希望打印面板显示页面设置附件(即纸张大小、方向和比例设置,如预览版,它显示为选项下拉列表的一个面板)。
目前,我错误地打印了PDFView,而不是PDF文档本身。这只给我一页,并包括滚动条打印出来!我看不出如何init引用PDFDocument而不是PDFView的NSPrintOperation。
这是我的代码,它有效,但不是我想要的。我想我必须用定义面板和信息的类似代码覆盖printDocument或printOperation函数NSDocument。
func thePrintInfo() -> NSPrintInfo {
let thePrintInfo = NSPrintInfo()
thePrintInfo.horizontalPagination = .automatic // Tried fit
thePrintInfo.verticalPagination = .automatic // Tried fit
thePrintInfo.isHorizontallyCentered = true // Tried false
thePrintInfo.isVerticallyCentered = true // Tried false
thePrintInfo.leftMargin = 0.0
thePrintInfo.rightMargin = 0.0
thePrintInfo.topMargin = 0.0
thePrintInfo.bottomMargin = 0.0
thePrintInfo.jobDisposition = .spool
return thePrintInfo
}
// Need to show the 'Page Setup' Options as an Accessory
// e.g. Paper size, orientation.
@IBAction func printContent(_ sender: Any) {
let printOperation = NSPrintOperation(view: thePDFView, printInfo: thePrintInfo())
let printPanel = NSPrintPanel()
printPanel.options = [
NSPrintPanel.Options.showsCopies,
NSPrintPanel.Options.showsPrintSelection,
NSPrintPanel.Options.showsPageSetupAccessory,
NSPrintPanel.Options.showsPreview
]
printOperation.printPanel = printPanel
printOperation.run()
}发布于 2019-08-18 10:16:59
基于@Willeke的评论,我想出了以下几点,似乎效果很好。(小问题是打印对话框不是工作表。)如果任何人有任何改进,请张贴一个更好的答案。
@IBAction func printContent(_ sender: Any) {
let printOperation = thePDFView.document?.printOperation(for: thePrintInfo(), scalingMode: .pageScaleNone, autoRotate: true)
printOperation?.printPanel = thePrintPanel()
printOperation?.run()
}https://stackoverflow.com/questions/57537247
复制相似问题