Hi,
I'm using the following code to print the contents of a view.
I have overridden 'drawRect' of the PrintView class, where I draw an image. The issue is, the 'rect' passed as argument to 'drawRect' is not the same as 'printViewFrame', so the contents of my view are clipped in the print panel's preview. Any clue why is this happening and how to rectify it?
Also, my print preview hangs on 'Processing File' when I try to preview as PDF in Mac's 'Preview'. The same image file, when opened in 'Preview', works fine. Is there something more to be done to get basic printing right?
Thanks for your time.
I'm using the following code to print the contents of a view.
Code:
NSPrintInfo* printInfo = [[NSPrintInfo sharedPrintInfo] copyWithZone:nil]; // should I release this?
NSSize paperSize = [printInfo paperSize];
NSRect printableRect = [printInfo imageablePageBounds];
// calculate page margins
float marginL = printableRect.origin.x;
float marginR = paperSize.width - (printableRect.origin.x + printableRect.size.width);
float marginB = printableRect.origin.y;
float marginT = paperSize.height - (printableRect.origin.y + printableRect.size.height);
// Make sure margins are symetric and positive
float marginLR = MAX(0,MAX(marginL,marginR));
float marginTB = MAX(0,MAX(marginT,marginB));
// Tell printInfo what the nice new margins are
[printInfo setLeftMargin: marginLR];
[printInfo setRightMargin: marginLR];
[printInfo setTopMargin: marginTB];
[printInfo setBottomMargin: marginTB];
NSRect printViewFrame = {};
printViewFrame.size.width = paperSize.width - marginLR*2;
printViewFrame.size.height = paperSize.height - marginTB*2;
NSString *nsImageFileName = [[NSString alloc] initWithCString:inImageFileName];
NSURL *absURL = [[NSURL alloc] initFileURLWithPath:nsImageFileName];
PrintView *view = [[PrintView alloc] initWithFrame:printViewFrame];
[view readFromURL:absURL error:nil];
[view setNeedsDisplay:YES];
[view print:nil];
[view release];
[printInfo release];
[nsImageFileName release];
[absURL release];
I have overridden 'drawRect' of the PrintView class, where I draw an image. The issue is, the 'rect' passed as argument to 'drawRect' is not the same as 'printViewFrame', so the contents of my view are clipped in the print panel's preview. Any clue why is this happening and how to rectify it?
Also, my print preview hangs on 'Processing File' when I try to preview as PDF in Mac's 'Preview'. The same image file, when opened in 'Preview', works fine. Is there something more to be done to get basic printing right?
Thanks for your time.