1. kevin smith 619's Avatar
    I have been reading through apples printing documentation today to implement it into my app, which is basically a form contained within a scroll view, everything is going just fine however the margins aren't working. Here's my code:

    Code:
    view plaincopy to clipboardprint?
    UIGraphicsBeginImageContext(Frame.frame.size);  
    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];  
    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();  
    UIGraphicsEndImageContext();  
      
    UIMarkupTextPrintFormatter *formatter = [[UIMarkupTextPrintFormatter alloc]  
                                                 initWithMarkupText:[NSString stringWithFormat:@"%@'s Patient Report", Name.text]];  
    formatter.startPage = 0;  
    formatter.contentInsets = UIEdgeInsetsMake(142.0, 36.0, 36.0, 36.0);  
      
    UIPrintInteractionController *controller = [UIPrintInteractionController sharedPrintController];  
    controller.delegate = self;  
      
    UIPrintInfo *printInfo = [UIPrintInfo printInfo];  
    printInfo.outputType = UIPrintInfoOutputGeneral;  
    printInfo.jobName = [NSString stringWithFormat:@"%@'s Patient Report", Name.text];  
    controller.printInfo = printInfo;  
    controller.showsPageRange = YES;  
    printInfo.duplex = UIPrintInfoDuplexLongEdge;  
    controller.printingItem = viewImage;controller.printFormatter = formatter;  
    [formatter release];  
      
    void (^completionHandler)(UIPrintInteractionController *, BOOL, NSError *) =  
    ^(UIPrintInteractionController *pic, BOOL completed, NSError *error) {  
        if (!completed && error)  
            NSLog(@"Print failed due to error in domain %@ with error code %u",  
                  error.domain, error.code);  
    };  
      
    [controller presentFromBarButtonItem:printButton animated:YES completionHandler:completionHandler];

    All I need really is the picture to be printed below about 2 inches below the top of the paper however when I print it the screen shot fits the page.

    Any help is welcomed!
    02-03-2012 12:53 AM
  2. KevinMartin's Avatar
    I got some more code about printing in iOS 4.2 but I am still confused. If anyone solve out my problem.
    - (IBAction)printContentid)sender {
    UIPrintInteractionController *pic = [UIPrintInteractionController sharedPrintController];
    pic.delegate = self;

    UIPrintInfo *printInfo = [UIPrintInfo printInfo];
    printInfo.outputType = UIPrintInfoOutputGeneral;
    printInfo.jobName = self.documentName;
    pic.printInfo = printInfo;

    UISimpleTextPrintFormatter *textFormatter = [[UISimpleTextPrintFormatter alloc]
    initWithText:yourNSStringWithContextOfTextFileHere];
    textFormatter.startPage = 0;
    textFormatter.contentInsets = UIEdgeInsetsMake(72.0, 72.0, 72.0, 72.0); // 1 inch margins
    textFormatter.maximumContentWidth = 6 * 72.0;
    pic.printFormatter = textFormatter;
    [textFormatter release];
    pic.showsPageRange = YES;

    void (^completionHandler)(UIPrintInteractionController *, BOOL, NSError *) =
    ^(UIPrintInteractionController *printController, BOOL completed, NSError *error) {
    if (!completed && error) {
    NSLog(@"Printing could not complete because of error: %@", error);
    }
    };
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
    [pic presentFromBarButtonItem:sender animated:YES completionHandler:completionHandler];
    } else {
    [pic presentAnimated:YES completionHandler:completionHandler];
    }
    }
    09-12-2012 10:18 AM
LINK TO POST COPIED TO CLIPBOARD