2011-04-23 30 views
4

打印多個頁面我有打印功能,如下所示:在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]; 
} 

此打印頁面的預覽的表示稱爲,這是一個NSBox。這工作正常。

有時候,我有一個可以容納一個頁面上,所以我有「下一頁」按鈕,通過重新加載有關數據填補與第二頁,第三頁等的表示更多的信息。這工作正常。

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

任何解決方案?提前致謝。

+0

我正在閱讀關於使用PDFView或Quartz的建議。這可能是我走下去的一條路線,但看起來OTT考慮我正在打印一個NSBox,並且通過一種快速方法,可以將整個NSBox更改爲每個後續頁面。我只想讓打印操作接受與第2頁,第3頁等相同的NSBox,並快速調用該方法將NSBox更改爲相關頁面。 – biscuitstack 2011-04-23 12:49:23

+0

也看看這個教程:http://cocoadevcentral.com/articles/000074.php它真的幫助我的印刷困境。 – siannopollo 2012-11-03 04:59:59

回答

1

您無法避免與可可打印系統分頁;正如你的評論所提到的,你需要去更低層次的東西。

但是,我認爲它不應該太難以適應你正在做的分頁。看看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方法的作用。

+0

我希望它能像我在問題中總結的那樣簡潔,但如果這絕對不可能,這些鏈接對我開始研究很有幫助。謝謝尼古拉斯。 – biscuitstack 2011-04-23 21:02:53

+0

子類化NSBox是否提供直接訪問自定義分頁(如在教程中)還是僅與NSView相關? – biscuitstack 2011-04-24 11:10:15

+1

它適用於包括NSBox在內的任何NSView子類。 – 2011-04-24 15:02:47