首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在目标C中打印多页

在目标C中打印多页
EN

Stack Overflow用户
提问于 2011-04-23 11:45:12
回答 1查看 5.1K关注 0票数 4

我有这样的打印功能:

代码语言:javascript
复制
- (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];
}

这将打印称为工作表( NSBox )的页面预览的表示形式。这个很好用。

有时,我有更多的信息可以适合一个页面,所以我有“下一页”按钮,这些按钮通过用相关数据重新加载工作表来填充表中的Page2、Page3等。这个很好用。

现在,如果我想打印出适合于2或3页而不是1的信息,我希望能够在NSPrintInfoNSPrintOperation附加页面开始打印之前手动输入它,而不是分页。类似于:

代码语言:javascript
复制
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];

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-04-23 20:27:34

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

然而,我不认为这是太难适应您正在做的分页。看看提供自定义分页方案自定义视图的打印绘图。只需子类NSBox,提供每一页大小的rect,并在beginPageInRect:atPlacement:中调整您的坐标系统,这样方框就会进入rect。您可以使用[[NSPrintOperation currentOperation] currentPage]获得当前的页码,这样您就知道要绘制什么了。

更新:如果视图的大小已经合适,那么甚至不需要处理您的坐标系统。下面是一个非常简单的NSBox子类的示例,它只更改每个页面的标题:

代码语言:javascript
复制
@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方法的作用。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5763995

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档