Powered By Blogger

Saturday, May 21, 2011

AirPrint in iPhone SDK

Hi Everyone,
By using below code , you'll be able to print your view using AirPrint in iPhone SDK.

-(void)sendToPrinter
{

    NSString *filename = [appDelegate.pdfFilePath lastPathComponent];   
    filename = [filename stringByReplacingOccurrencesOfString:@".pdf" withString:@""];
    NSString *path = [[NSBundle mainBundle] pathForResource:filename ofType:@"pdf"];

    NSData *myData = [NSData dataWithContentsOfFile:path];
    UIPrintInteractionController *print = [UIPrintInteractionController sharedPrintController];

    print.delegate = self;
    UIPrintInfo *printInfo = [UIPrintInfo printInfo];
    printInfo.outputType = UIPrintInfoOutputGeneral;
    printInfo.jobName = [appDelegate.pdfFilePath lastPathComponent];
    printInfo.duplex = UIPrintInfoDuplexLongEdge;
    print.printInfo = printInfo;
    print.showsPageRange = YES;
    print.printingItem = myData;
    UIViewPrintFormatter *viewFormatter = [self.view viewPrintFormatter];
    viewFormatter.startPage = 0;
    print.printFormatter = viewFormatter;
   

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

if you have query then send it to me. 

Thanks and Regards,
 Nilesh Prajapati

No comments:

Post a Comment