Lessons learned from eaGeier #3

Printing a PDF File

Printing works like before straightforward following the instructions of the documentation at Apple. Start by obtaining a printing controller and check if printing is available at all:

UIPrintInteractionController *pic = [UIPrintInteractionController sharedPrintController];
if  (pic && [UIPrintInteractionController canPrintData:pdfDataWeWantToPrint] ) {
    // we should be able to print

Make yourself the delegate (optional), create the print information and setup the print job:

pic.delegate = self;
UIPrintInfo *printInfo = [UIPrintInfo printInfo];
printInfo.outputType = UIPrintInfoOutputGeneral;                 
printInfo.jobName = @"AMeaningFulName";
printInfo.duplex = UIPrintInfoDuplexLongEdge;
pic.printInfo = printInfo;
pic.showsPageRange = YES;
pic.printingItem = pdfDataWeWantToPrint;

Note that printInfo holds a property called orientation allowing to set the orientation of the printout. However using printingItem to hand over the PDF file, this setting has no effect and a PDF in landscape mode has be rotated manually. Otherwise it will be shrunk and printed in portrait mode.

The actual printing process is started presenting the printing action sheet using presentFromBarButtonItem: or presentAnimated:.

In addition you might want to check if printing is available at all and if not do not even provide that option to the user:

    if (![UIPrintInteractionController isPrintingAvailable])
        // do not even show that you could print ...

Leave a Reply

Your email address will not be published.