加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

在Objective-C中打印多个页面

发布时间:2020-12-14 17:20:28 所属栏目:百科 来源:网络整理
导读:我有这样的打印功能: - (void)sendToPrinter:(int)code { NSPrintInfo *printInfo; NSPrintInfo *sharedInfo; NSPrintOperation *printOp; NSMutableDictionary *printInfoDict; NSMutableDictionary *sharedDict; sharedInfo = [NSPrintInfo sharedPrintIn
我有这样的打印功能:

- (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.这很好用.

有时我有更多可以放在页面上的信息,因此我有“下一页”按钮,通过使用相关数据重新加载表格来填充表格,其中包含Page2,Page3等的表示.这很好用.

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

有解决方案吗提前致谢.

解决方法

你无法避免与Cocoa印刷系统的分页;正如你的评论所提到的,你需要去更低级别的东西.

但是我不认为应该对你正在做的分页进行调整太难.看一下Providing a Custom Pagination Scheme和Customizing a View’s Drawing for Printing.只需子类化NSBox,提供每个页面大小的rects并在beginPageInRect:atPlacement中调整坐标系:所以框会绘制到rect中.您可以使用[[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方法的作用.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读