2012-02-08 69 views
2

我使用UIMarkupTextPrintFormatter从我的应用打印一些HTML。如果我让打印格式化程序打印到整个页面,它会很好用。如何从UIPrintFormatter获得精确的pageCount

但我想限制在页面的一半格式打印 - 而实现这一点我设置contentInsets属性一旦我知道纸张的尺寸(我设置contentInsets当UIPrintInteractinControllerDelegate方法- (UIPrintPaper *)printInteractionController: (UIPrintInteractionController *)pic choosePaper:(NSArray *)paperList被调用。)

这也适用 - 打印正确地限制到使用contentInsets定义的页面区域。

但是打印页数是错误的。我得到的页数好像打印作业一直使用默认的contentInsets(0,0,0,0)。

我怎样才能打印正确的页数?为什么UIMarkupTextPrintFormatter上的pageCount属性总是返回0?

这里的打印作业设置代码:

UIPrintInteractionController* pic = [UIPrintInteractionController sharedPrintController]; 
pic.delegate = self; 

UIPrintInfo* pi = [UIPrintInfo printInfo]; 
pi.orientation = UIPrintInfoOrientationLandscape; 
pi.outputType = UIPrintInfoOutputGrayscale; 
pi.duplex = UIPrintInfoDuplexNone; 
pic.printInfo = pi; 

UIMarkupTextPrintFormatter* mtpf = [[UIMarkupTextPrintFormatter alloc] initWithMarkupText: myHTML ]; 
pic.printFormatter = mtpf; 

[pic presentAnimated: YES completionHandler:^(UIPrintInteractionController *printInteractionController, BOOL completed, NSError *error) 
{ 
}]; 

,并委托方法:

- (UIPrintPaper *)printInteractionController: (UIPrintInteractionController *)pic choosePaper:(NSArray *)paperList 
{ 
    // we want letter paper 
    UIPrintPaper* printPaper = [UIPrintPaper bestPaperForPageSize: CGSizeMake(612 , 792) withPapersFromArray: paperList]; 

    UIMarkupTextPrintFormatter* mtpf = (UIMarkupTextPrintFormatter*)pic.printFormatter; 

    CGFloat padding = (printPaper.paperSize.height - printPaper.printableRect.size.height)/2.0; 

    mtpf.contentInsets = UIEdgeInsetsMake(0, 0, 0, (printPaper.printableRect.size.height/2.0) + padding); 

    // always 0 ??? 
    NSLog(@"pageCount: %d", mtpf.pageCount); 

    return printPaper; 
} 

回答

0

你需要写一个子类UIPrintPageRenderer的。在printFormatter中为子类numberOfPages方法设置各种指标后,您可以获得正确的页数。 See my long answer with example code located here。这不完全是这个问题,但它回答了很多有关试图解决打印问题的人需要得到的相关问题。

相关问题