2

I'm using a UIMarkupTextPrintFormatter to print some HTML from my app. It works great if I let the print formatter print to the entire page.

But I want to limit the formatter to printing on one half of the page - and to accomplish this I set the contentInsets property once I know the paper size (I set contentInsets when the UIPrintInteractinControllerDelegate method - (UIPrintPaper *)printInteractionController: (UIPrintInteractionController *)pic choosePaper:(NSArray *)paperList is called.)

This works too - the print is correctly limited to the area of the page defined with the contentInsets.

But the number of printed pages is wrong. The number of pages I get is as if the print job had been using the default contentInsets (0,0,0,0).

How can I get it to print the correct number of pages? And why does the pageCount property on the UIMarkupTextPrintFormatter always return 0?

Here's the print job setup code:

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) 
{
}];

and, the delegate method:

- (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;
}
TomSwift
  • 39,369
  • 12
  • 121
  • 149

1 Answers1

0

You need to write a sub-class of UIPrintPageRenderer. After you set various metrics on the printFormatter in sub-classes numberOfPages method you can get the correct page count. See my long answer with example code located here. It's not exactly this problem but it answers a lot of related questions that someone trying to solve printing issues needs to get.

Community
  • 1
  • 1
Cliff Ribaudo
  • 8,932
  • 2
  • 55
  • 78