【问题标题】:Printing multiple pages in Objective-C在 Objective-C 中打印多页
【发布时间】:2011-04-23 11:45:12
【问题描述】:

我有这样的打印功能:

- (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。这工作正常。

有时我有更多的信息可以放在一个页面上,所以我有“下一页”按钮,通过重新加载 sheet 与相关数据。这很好用。

现在,如果我想打印适合 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];

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

【问题讨论】:

  • 我正在阅读有关使用 PDFView 或 Quartz 的建议。这可能是我走的一条路线,但考虑到我正在打印一个 NSBox,这似乎是 OTT,并且通过一种快速方法,可以将整个 NSBox 更改为每个后续页面。我只想让打印操作接受与 page2、page3 等相同的 NSBox,并快速调用将 NSBox 更改为相关页面的方法。
  • 还可以看看这个教程:cocoadevcentral.com/articles/000074.php 它确实帮助我解决了打印问题。

标签: objective-c xcode macos nsprintoperation


【解决方案1】:

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

但是,我认为将您正在做的事情适应分页应该不会太难。看看Providing a Custom Pagination SchemeCustomizing a View's Drawing for Printing。只需子类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方法的作用。

【讨论】:

  • 我希望它会像我在我的问题中总结的那样简洁,但是,如果这绝对不可能,这些是帮助我开始研究的链接。谢谢尼古拉斯。
  • 子类化 NSBox 是否可以直接访问自定义分页(如教程中所述),还是仅与 NSView 相关?
  • 它适用于任何 NSView 子类,包括 NSBox。
  • 啊……出于某种原因,我认为 NSBox 不是从 NSView 派生的。现在有道理了。这在我的代码中带来了一些其他问题,所以我会先解决它们,然后再回来报告。
  • 不要用那种方法画画——它行不通。我用一些示例代码修改了我的答案。事实证明beginPageInRect:atPlacement: 的默认行为实际上非常好;它将您的视图集中在页面顶部,而不是在左下角的页面原点显示它(如果没有它,您会得到什么)。
猜你喜欢
  • 2015-08-07
  • 1970-01-01
  • 2023-03-21
  • 1970-01-01
  • 2011-03-24
  • 2014-12-06
  • 2014-08-25
  • 2014-05-02
  • 1970-01-01
相关资源
最近更新 更多