I am trying to implement printing in a new Cocoa application. Everything is working fine, except for scaling, that is, printing at 75%, 125%, etc.
As far as I can tell from Apple's docs, the program should adjust the rectangle returned from the rectForPage method: depending on the scale factor. I found an Apple code sample that seemed to work that way, and an old cocoabuilder post.
https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/Printing/osxp_pagination/osxp_pagination.html
http://www.cocoabuilder.com/archive/cocoa/211683-scaled-printing.html
My rectForPage: the code looked like this:
NSPrintInfo *pi = [[NSPrintOperation currentOperation] printInfo];
NSSize paperSize = [pi paperSize];
CGFloat pageScale = [[[pi dictionary] objectForKey:NSPrintScalingFactor] floatValue];
CGFloat topMargin = [pi topMargin];
CGFloat leftMargin = [pi leftMargin];
CGFloat bottomMargin = [pi bottomMargin];
CGFloat rightMargin = [pi rightMargin];
CGFloat pageHeight = (paperSize.height - topMargin - bottomMargin) / pageScale;
CGFloat pageWidth = (paperSize.width - leftMargin - rightMargin) / pageScale;
NSRect bounds = [self bounds];
NSRect actualPageRect = NSMakeRect(
NSMinX(bounds),
NSMinY(bounds),
pageWidth * pageScale,
pageHeight * pageScale);
return actualPageRect;
. , , 100%. , actualPageRect , , . , Apple , , .
. directForPage: , drawRect: :
- (void)drawRect:(NSRect)dirtyRect
{
[super drawRect:dirtyRect];
NSAffineTransform* xform = [NSAffineTransform transform];
[xform scaleXBy:pageScale yBy:pageScale];
[xform concat];
[self layoutReport];
[xform invert];
[xform concat];
}
knowPageRange: .
- . , , . , . , - , , , , rectForPage:, . , , , .