在 Objective-C 中打印多页
我有一个像这样的打印功能:
- (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 页的信息,我希望能够在其之前手动提供 NSPrintInfo
或 NSPrintOperation
附加页面进行打印,而不是分页。比如:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Cocoa 打印系统无法避免分页;正如您的评论所提到的,您需要进行较低级别的操作。
不过,我认为将您正在做的事情适应分页应该不会太难。看看 提供自定义分页方案和自定义视图的绘图以进行打印。只需子类
NSBox
,提供每个页面大小的矩形,并在beginPageInRect:atPlacement:
中调整坐标系,以便框绘制到矩形中。您可以使用[[NSPrintOperation currentOperation] currentPage]
获取当前页码,以便知道要绘制什么。更新:事实证明,如果您的视图尺寸已经正确,您甚至不需要弄乱坐标系。下面是一个非常简单的 NSBox 子类的示例,它只更改每个页面的标题:
可能不太明显的一件事是需要调用超类的
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 inbeginPageInRect: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:
One thing that may not have been obvious is the need to invoke the superclass's implementation of
beginPageInRect:atPlacement:
. Also, do not draw inrectForPage:
, it won't work properly—that's the role of thebeginPage…
/endPage
methods.