在 Objective-C 中打印多页

发布于 2024-11-03 03:57:23 字数 2012 浏览 4 评论 0原文

我有一个像这样的打印功能:

- (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];
}

这会打印一个名为 sheet 的页面预览表示,它是一个 NSBox。这很好用。

有时,我有更多信息可以容纳在一个页面上,因此我有“下一页”按钮,可以通过重新加载工作表来用第2页、第3页等的表示填充工作表与相关数据。这很好用。

现在,如果我想打印出适合 2 或 3 页而不是 1 页的信息,我希望能够在其之前手动提供 NSPrintInfoNSPrintOperation 附加页面进行打印,而不是分页。比如:

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];

有什么解决办法吗?提前致谢。

I have a printing function like so:

- (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 representation of a page preview called sheet, which is an NSBox. This works fine.

Sometimes I have more information that can fit on a page and so I have 'next page' buttons that fill sheet with a representation of Page2, Page3, etc. by reloading sheet with the relevant data. This works fine.

Now, if I want to print out information that will fit on 2 or 3 pages rather than 1, I want to be able to feed NSPrintInfo or NSPrintOperation additional pages manually before it goes to print, rather than pagination. 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

┼── 2024-11-10 03:57:23

Cocoa 打印系统无法避免分页;正如您的评论所提到的,您需要进行较低级别的操作。

不过,我认为将您正在做的事情适应分页应该不会太难。看看 提供自定义分页方案自定义视图的绘图以进行打印。只需子类 NSBox,提供每个页面大小的矩形,并在 beginPageInRect:atPlacement: 中调整坐标系,以便框绘制到矩形中。您可以使用 [[NSPrintOperation currentOperation] currentPage] 获取当前页码,以便知道要绘制什么。

更新:事实证明,如果您的视图尺寸已经正确,您甚至不需要弄乱坐标系。下面是一个非常简单的 NSBox 子类的示例,它只更改每个页面的标题:

@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

可能不太明显的一件事是需要调用超类的 beginPageInRect:atPlacement: 实现。另外,不要在 rectForPage: 中绘制,它将无法正常工作 - 这就是 beginPage.../endPage 方法的作用。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文