使用Swift打印本地PDF

时间:2014-09-26 21:38:18

标签: swift pdf ios8

我在Objective-C中有一个打印例程,用于打印本地PDF文件。

我想在Swift中使用相同的例程。有人可以帮忙吗?

- (void)printFile:(NSURL *)url {

    if ([UIPrintInteractionController .canPrintURL(url]) {

        UIPrintInteractionController *
            controller = [UIPrintInteractionController
            sharedPrintController()];

        controller.printingItem = url;

        UIPrintInfo *printInfo = [UIPrintInfo printInfo];
        PrintInfo.outputType = UIPrintInfoOutputGeneral;
        PrintInfo.jobName = [url lastPathComponent];
        controller.printInfo = printInfo;

        controller.showPageRange = YES;

        [controller presentAnimated:YES completionHandler:Null];

    }

}

2 个答案:

答案 0 :(得分:7)

这应该指向正确的方向,按照链接来了解它的作用。

@IBAction func print(sender: UIBarButtonItem) {
   if UIPrintInteractionController.canPrintURL(imageURL) {
    let printInfo = UIPrintInfo(dictionary: nil)
    printInfo.jobName = imageURL.lastPathComponent
    printInfo.outputType = .Photo

    let printController = UIPrintInteractionController.sharedPrintController()!
    printController.printInfo = printInfo
    printController.showsNumberOfCopies = false

    printController.printingItem = imageURL

    printController.presentAnimated(true, completionHandler: nil)
  }
}

取自http://nshipster.com/uiprintinteractioncontroller/

答案 1 :(得分:0)

更新为Swift 4.2

@IBAction func printAction(_ sender: UIButton) {
        if let guide_url = Bundle.main.url(forResource: "RandomPDF", withExtension: "pdf"){
            if UIPrintInteractionController.canPrint(guide_url) {
                let printInfo = UIPrintInfo(dictionary: nil)
                printInfo.jobName = guide_url.lastPathComponent
                printInfo.outputType = .photo

                let printController = UIPrintInteractionController.shared
                printController.printInfo = printInfo
                printController.showsNumberOfCopies = false

                printController.printingItem = guide_url

                printController.present(animated: true, completionHandler: nil)
            }
        }
    }