如何在iOS 4.2中打印?

时间:2010-12-07 04:33:37

标签: iphone cocoa-touch ipad printing ios-4.2

我想在我的应用中集成打印功能。

我要打印的文档将采用.doc或.txt格式。 我在iPhone开发方面还不是很有经验,因此很难通过Apple文档来实现它。

如果有人可以通过发布一些示例代码来帮助我,那将是一个很好的帮助。

3 个答案:

答案 0 :(得分:34)

查看 Drawing and Printing Guide for iOS - 我链接到了打印部分。这里有示例代码和更多示例代码的良好链接。

修改:我现在看到您发现您发现难以理解的文档。

Word文档很复杂 - 您需要解析数据,这非常困难。

文字和HTML更容易。我拿了Apple的HTML示例并将其更改为纯文本:

- (IBAction)printContent:(id)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];
    }
}

答案 1 :(得分:2)

首先添加 UIPrintInteractionControllerDelegate 并创建变量

    UIPrintInteractionController *printController;

下面的代码打印所有图像,文档,excel,powerpoint,pdf文件对我有用:

[self printItem:SomeData withFilePath:YourFilePath];

在上面的代码中,您提供了文档/图片的 NSData 网址(filePath)以及 printItem:withFilePath:的更多代码方法

-(void)printItem :(NSData*)data withFilePath:(NSString*)filePath{
printController = [UIPrintInteractionController sharedPrintController];
printController.delegate = self;

UIPrintInfo *printInfo = [UIPrintInfo printInfo];
printInfo.outputType = UIPrintInfoOutputGeneral;
printInfo.jobName = [NSString stringWithFormat:@""];
printInfo.duplex = UIPrintInfoDuplexLongEdge;
printController.printInfo = printInfo;
printController.showsPageRange = YES;


//If NSData contains data of image/PDF
if(printController && [UIPrintInteractionController canPrintData:data]) {
    printController.printingItem = data;

}else{
    UIWebView* webView = [UIWebView new];
    printInfo.jobName = webView.request.URL.absoluteString;
    [webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:filePath]]];

    printController.printFormatter = webView.viewPrintFormatter;

}

    void (^completionHandler)(UIPrintInteractionController *, BOOL, NSError *) = ^(UIPrintInteractionController *printController, BOOL completed, NSError *error) {
        if (!completed && error) {
            //NSLog(@"FAILED! due to error in domain %@ with error code %u", error.domain, error.code);
        }
    };

    // Check wether device is iPad/iPhone , because UIPrintInteractionControllerDelegate has different methods for both devices
    if([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad) {
        [printController presentFromRect:self.view.frame inView:self.view animated:YES completionHandler:completionHandler];
    }
    else {
        [printController presentAnimated:YES completionHandler:completionHandler];
    }
}

我希望它会有所帮助。感谢

//对于Swift,如果你想打印图像 首先为图像创建IBoutlet

@IBOutlet var qrImage : UIImageView?

然后点击打印按钮只需添加以下代码

//In your view controller
@IBAction func printButton(sender: AnyObject) {

    let printInfo = UIPrintInfo(dictionary:nil)
    printInfo.outputType = UIPrintInfoOutputType.general
    printInfo.jobName = "My Print Job"

    // Set up print controller
    let printController = UIPrintInteractionController.shared
    printController.printInfo = printInfo

    // Assign a UIImage version of my UIView as a printing iten
    printController.printingItem = self.qrImage?.image

    // Do it
    printController.present(from: self.view.frame, in: self.view, animated: true, completionHandler: nil)
}

答案 2 :(得分:1)

嗨,如果有任何疑问,可以帮助你试试并发帖。

-(IBAction)printFromIphone:(id)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];
    }
}