Printing a multi-page document, without breaking items on page boundaries

Skip to first unread message

David Phillip Oster

Aug 20, 2021, 6:56:55 PM8/20/21
to CocoaHeads - Silicon Valley
I've written up a simple example of printing a multi-page document, an NSTableView, on macOS without breaking items on page boundaries.

The example pretends to be a mailing label or business card printing program, displaying 1000 items in a scrollview in a resizable window, and allowing you to print, with page setup scaling not breaking labels across page boundaries.

The key insight is proper use of NSView's 

- (BOOL)knowsPageRange:(NSRangePointer)range - return number of printed pages in your content view

- (NSRect)rectForPage:(NSInteger)page - for a given page by number, from 0 to N, return rectangle of the printable items on that page.

or in Swift:

func knowsPageRange(_ range: NSRangePointer) -> Bool 

func rectForPage(_ page: Int) -> NSRect 

The version of the example in ObjectiveC is at:

and since most people like to work in Swift, the Swift version is at:

Reply all
Reply to author
0 new messages