Reputation: 735
I'm trying to implement printing in my OS X app but there's a problem that I can't solve and I hope that maybe one of you has got an idea.
In Safari for instance you can hit print and the print panel will show you how the the contents will be printed in portrait mode and if you click on the orientation button it will show you the contents in landscape mode.
Notice that the contents is scaled with every change of the orientation.
If I try the same in my app the contents does not change; its width stays the same in portrait and in landscape.
Is there any setting in NSPrintOperation or NSPrintInfo that I might have overlooked?
Update - now with code:
- (NSPrintOperation *)printOperationWithSettings:(NSDictionary *)printSettings error:(NSError **)outError {
NSPrintInfo *pInfo = [NSPrintInfo sharedPrintInfo];
[pInfo setLeftMargin:32];
[pInfo setRightMargin:32];
[pInfo setTopMargin:64];
[pInfo setBottomMargin:64];
[pInfo setHorizontalPagination:NSFitPagination];
[pInfo setVerticallyCentered:NO];
[[pInfo dictionary] setValue:[NSNumber numberWithBool:YES] forKey:NSPrintHeaderAndFooter];
[[pInfo dictionary] addEntriesFromDictionary:printSettings];
PrintTextView *printView = [[[PrintTextView alloc] initWithFrame:[pInfo imageablePageBounds]] autorelease];
printView.printJobTitle = @"Printed";
MSTablePrint *tPrint = [[[MSTablePrint alloc] init] autorelease];
NSMutableArray *itemArray = [[[NSMutableArray alloc] init] autorelease];
/*
Objects are added to "itemArray"
*/
NSAttributedString *atString = [tPrint attributedStringFromItems:itemArray];
[[printView textStorage] setAttributedString:atString];
NSPrintOperation *printOp = [NSPrintOperation printOperationWithView:printView printInfo:pInfo];
return printOp;
}
- (IBAction)print:(id)sender {
NSError *printError = nil;
NSPrintOperation *printOperation = [self printOperationWithSettings:nil error:&printError];
[[printOperation printPanel] setOptions:[[printOperation printPanel] options] | NSPrintPanelShowsPageSetupAccessory];
[printOperation runOperation];
}
Thanks very much for your help, best wishes, uem
Upvotes: 2
Views: 858
Reputation: 31
You cannot accomplish the layout change in your NSPrintOperation setting or in the NSPrintInfo settings. You will need to created an inherited view. In that view their are two methods (adjustPageHeightNew, adjustPageWidthNew). These methods are notified when you need to change the views layout. Make sure the you call the super method too.
In the method, change the layout of your view, and all subviews.
- (void)adjustPageHeightNew:(CGFloat *)newBottom top:(CGFloat)oldTop bottom:(CGFloat)oldBottom limit:(CGFloat)bottomLimit
{
[super adjustPageHeightNew:newBottom top:oldTop bottom:oldBottom limit:bottomLimit];
{
NSPrintOperation *printOperation = [NSPrintOperation currentOperation];
if(printOperation)
{
NSPrintInfo *printInfo = [printOperation printInfo];
CGFloat pageMargineX = ([printInfo leftMargin] + [printInfo rightMargin]);
CGFloat pageMargineY = ([printInfo topMargin] + [printInfo bottomMargin]);
CGFloat pageWidth = ([printInfo paperSize].width - pageMargineX);
CGFloat pageHeight = ([printInfo paperSize].height - pageMargineY);
[self setFrame:NSMakeRect(0, 0, pageWidth, ([[self subviews] count] * pageHeight))];
for(NSUInteger nView = 0; nView < [[self subviews] count]; nView ++)
{
NSTextView *PagedView = [[self subviews] objectAtIndex:nView];
[PagedView setFrame:NSMakeRect(0, (nView * pageHeight), pageWidth, pageHeight)];
}
}
}
}
Upvotes: 3