Despite the accepted answer, in fact it does not answer the main question, it just gives a workaround.
I had a similar problem (I have an array of objects, each of which is displayed by the same NSView subclass, one per page). This is how I solved it, without the baroque nonsense of creating one gigantic view containing all the pages ...
1) We have a subclass of NSView that has a model associated with it ( NSImage in your case, ModelData in my case)
class BaseView: NSView { var modelData: ModelData
2) Your document controller has an array of var models: [ModelData] .
3) Create a new subclass that will be displayed on the printer.
class PrinterView: BaseView { var pageIndex: Int = 1 var modelArray: [ModelData] init(frame: NSRect, models: [ModelData]) { self.modelArray = models super.init(frame: frame) } required init?(coder: NSCoder) { fatalError("invalid initializer") } override func knowsPageRange(_ range: NSRangePointer) -> Bool { range.pointee.location = 1 range.pointee.length = self.modelArray.count return true } override func rectForPage(_ page: Int) -> NSRect { self.pageIndex = page return self.bounds } override func draw(_ dirtyRect: NSRect) { self.model = self.modelArray[self.page - 1]
4) Do this in your document controller:
override func printOperation(withSettings printSettings: [String : Any]) throws -> NSPrintOperation { self.printInfo.horizontalPagination = .fitPagination self.printInfo.verticalPagination = .clipPagination let printView = PrinterView(size: self.printInfo.paperSize, models: self.models) return NSPrintOperation(view: printView, printInfo: self.printInfo) }
source share