使用 NSPrintOperation 时将 showsPrintPanel 设置为 false 会导致打印时尺寸不同
Setting showsPrintPanel to false when using NSPrintOperation causes different sizes when printing
当使用 NSPrintOperation 并将 showPrintPanel 设置为 true 时,视图打印得很好,但是当我将 属性 设置为 false 时,视图打印不正确并且比纸张大:
func printBadge(printerName: String){
let printingFrame = NSRect(x: 0, y: 0, width: AppManager.sharedInstance.badge.width, height: AppManager.sharedInstance.badge.height)
let badgeView = BadgeView(frame: printingFrame)
let printOperation = NSPrintOperation(view: badgeView)
let printInfo = printOperation.printInfo
printInfo.printer = NSPrinter(name: printerName)!
printInfo.paperSize = NSSize(width: AppManager.sharedInstance.badge.width, height: AppManager.sharedInstance.badge.height)
printInfo.horizontalPagination = .AutoPagination
printInfo.verticalPagination = .AutoPagination
printInfo.topMargin = 0
printInfo.bottomMargin = 0
printInfo.rightMargin = 0
printInfo.leftMargin = 0
printInfo.horizontallyCentered = true
printInfo.verticallyCentered = true
printOperation.showsPrintPanel = AppManager.sharedInstance.showsPrintPanel!
printOperation.runOperation()
}
最后我解决了这个问题,NSPrintInfo.paperSize取的是POINTS而不是PIXELS,所以我从毫米到像素的计算不正确(打印机设置的单位是毫米而不是像素或点)。
如果有人感兴趣,从毫米到点的数学计算是;
millimetres * 72 / 25.4
当使用 NSPrintOperation 并将 showPrintPanel 设置为 true 时,视图打印得很好,但是当我将 属性 设置为 false 时,视图打印不正确并且比纸张大:
func printBadge(printerName: String){
let printingFrame = NSRect(x: 0, y: 0, width: AppManager.sharedInstance.badge.width, height: AppManager.sharedInstance.badge.height)
let badgeView = BadgeView(frame: printingFrame)
let printOperation = NSPrintOperation(view: badgeView)
let printInfo = printOperation.printInfo
printInfo.printer = NSPrinter(name: printerName)!
printInfo.paperSize = NSSize(width: AppManager.sharedInstance.badge.width, height: AppManager.sharedInstance.badge.height)
printInfo.horizontalPagination = .AutoPagination
printInfo.verticalPagination = .AutoPagination
printInfo.topMargin = 0
printInfo.bottomMargin = 0
printInfo.rightMargin = 0
printInfo.leftMargin = 0
printInfo.horizontallyCentered = true
printInfo.verticallyCentered = true
printOperation.showsPrintPanel = AppManager.sharedInstance.showsPrintPanel!
printOperation.runOperation()
}
最后我解决了这个问题,NSPrintInfo.paperSize取的是POINTS而不是PIXELS,所以我从毫米到像素的计算不正确(打印机设置的单位是毫米而不是像素或点)。
如果有人感兴趣,从毫米到点的数学计算是;
millimetres * 72 / 25.4