I'm working on trying to get my NSView
subclass to print nicely, but I'm having trouble figuring out custom pagination. My problem is this: only a portion of my NSView
subclass contains data worth printing, so I've written a function - (NSRect)printableBounds
to get the rect within the NSView
that I want to print. I want to print only this rectangle from the view centered and scaled to fit one page.
Ideally, I would like to be able to use NSPrintInfo
's NSFitPagination
, but only have my printable bounds rectangle printed--not the whole view.
I have taken two approaches, neither of which have been completely successful. The first was to try creating my own custom pagination scheme by overriding -knowsPageRange:
and -rectForPage:
in my NSView
subclass, like so:
- (BOOL) knowsPageRange:(NSRangePointer)range {
range->location = 1;
range->length = 1;
return YES;
}
- (NSRect) rectForPage:(NSInteger)page {
return [self printableBounds];
}
When I use this approach, the correct rectangle is printed, but it is not scaled to match the page's size.
My second attempt was to change the bounds of the view itself before printing, and then changing them back afterwards:
- (void) print:(id) sender {
NSPrintInfo *sharedInfo = [NSPrintInfo sharedPrintInfo];
[sharedInfo setHorizontalPagination:NSFitPagination];
NSRect bounds = [self bounds];
[self setBounds:[self printableBounds]];
[super print:sender];
[self setBounds:bounds];
}
This seems to print properly, but it seems awfully hackish and has the undesirable effect of distorting my view in the window it's displaying in for the entire time that the print dialog is open.
I feel like I'm making things ten times harder than they have to be. What is the proper way to accomplish only printing a portion of a view (scaled to fit the page)?