在Objective-C中打印多个页面

前端之家收集整理的这篇文章主要介绍了在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.这很好用.

有时我有更多可以放在页面上的信息,因此我有“下一页”按钮,通过使用相关数据重新加载表格来填充表格,其中包含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];

解决方案吗提前致谢.

@R_404_323@

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

但是我不认为应该对你正在做的分页进行调整太难.看一下Providing a Custom Pagination SchemeCustomizing 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方法的作用.

猜你在找的Xcode相关文章