Printing multiple pages in Objective-C

十年热恋 提交于 2019-12-05 12:02:47

You can't avoid pagination with the Cocoa printing system; as your comment mentions, you'll need to go to something lower-level.

However I don't think it should be too hard to adapt what you're doing to pagination. Take a look at Providing a Custom Pagination Scheme and Customizing a View's Drawing for Printing. Just subclass NSBox, provide rects that are the size of each page and adjust your coordinate system in beginPageInRect:atPlacement: so the box draws into the rect. You can get the current page number with [[NSPrintOperation currentOperation] currentPage] so you know what to draw.

Update: Turns out you don't even need to mess with your coordinate system if your view is already the right size. Here's an example of a very simple NSBox subclass that just changes its title for every 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 superclass's implementation of beginPageInRect:atPlacement:. Also, do not draw in rectForPage:, it won't work properly—that's the role of the beginPage…/endPage methods.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!