Printing multiple pages in Objective-C

I have a print function like this:

- (void)sendToPrinter:(int)code { NSPrintInfo *printInfo; NSPrintInfo *sharedInfo; NSPrintOperation *printOp; NSMutableDictionary *printInfoDict; NSMutableDictionary *sharedDict; sharedInfo = [NSPrintInfo sharedPrintInfo]; sharedDict = [sharedInfo dictionary]; printInfoDict = [NSMutableDictionary dictionaryWithDictionary: sharedDict]; [printInfoDict setObject:NSPrintSpoolJob forKey:NSPrintJobDisposition]; printInfo = [[NSPrintInfo alloc] initWithDictionary: printInfoDict]; [printInfo setHorizontalPagination: NSAutoPagination]; [printInfo setVerticalPagination: NSAutoPagination]; [printInfo setVerticallyCentered:NO]; [printInfo setLeftMargin:10]; [printInfo setRightMargin:10]; [printInfo setTopMargin:10]; [printInfo setBottomMargin:10]; [printInfo setScalingFactor:1.1]; printOp = [NSPrintOperation printOperationWithView:sheet printInfo:printInfo]; [printOp setShowsPrintPanel:YES]; [printOp runOperation]; } 

This prints a page preview view called NSBox . It works great.

Sometimes I have additional information that can fit on a page, and therefore I have buttons for the “next page” that fill the sheet with the presentation of pages Page2, Page3, etc., overloading the sheet with the corresponding data. It works great.

Now, if I want to print information that will fit on 2 or 3 pages, not 1, I want you to be able to deliver additional NSPrintInfo or NSPrintOperation manually before it prints, rather than pagination, What something like:

 printOp = [NSPrintOperation printOperationWithView:sheet printInfo:printInfo]; [self nextPage]; printOp = [NSPrintOperation printOperationWithView:sheet printInfo:printInfo]; [self nextPage]; printOp = [NSPrintOperation printOperationWithView:sheet printInfo:printInfo]; // run this in loop until all the pages are accounted for [printOp setShowsPrintPanel:YES]; [printOp runOperation]; 

Any solutions? Thanks in advance.

+4
source share
1 answer

You cannot avoid pagination with the Cocoa printing system; as your comment mentions, you need to switch to something lower.

However, I don’t think it’s too difficult to adapt what you are doing to pagination. Take a look at Providing a custom pagination scheme and Setting up a drawing for printing . Just a subclass of NSBox , specify the rectangles, size of each page, and adjust your coordinate system in beginPageInRect:atPlacement: so that the box draws in a rectangle. You can get the current page number with [[NSPrintOperation currentOperation] currentPage] so you know what to draw.

Update: It turns out you don’t even have to get confused with your coordinate system if your view already has the right size. Here is an example of a very simple NSBox subclass that simply changes the name for each page:

 @implementation NumberBox - (BOOL)knowsPageRange:(NSRangePointer)aRange; { *aRange = NSMakeRange(1, 10); return YES; } - (void)beginPageInRect:(NSRect)aRect atPlacement:(NSPoint)location; { [self setTitle:[NSString stringWithFormat:@"Page %d", [[NSPrintOperation currentOperation] currentPage]]]; [super beginPageInRect:aRect atPlacement:location]; } - (NSRect)rectForPage:(NSInteger)page; { return [self bounds]; } @end 

One thing that may not have been obvious is the need to invoke the implementation of the superclass beginPageInRect:atPlacement: Also, do not draw rectForPage: it will not work properly - the role of the beginPage… / endPage methods.

+1
source

All Articles