【问题标题】:How to display a single page of a multi-document PDF pages using SWIFT如何使用 SWIFT 显示多文档 PDF 页面的单页
【发布时间】:2017-02-24 06:45:37
【问题描述】:

我想为所选的 PDF 文件显示一个页面,即使 PDF 有多个页面。

这样根据选择的页码只显示一个PDF页面。

我正在写这段代码:

    let path = NSURL(fileURLWithPath: Bundle.main.path(forResource: "PDFName", ofType: "pdf")!)
    let request = NSURLRequest(url: path as URL)
    webView.loadRequest(request as URLRequest)

但这会显示文档的所有页面。 我只想按页码显示一页?

【问题讨论】:

  • 将 UIDocumentInteractViewController 用于您的概念
  • 我找了这个我没有找到解决方案。有没有可以帮助我做的示例或解释。我正在开发 UIPageViewController 应用程序,我想添加一个 PDF 文件,以便所有页面都显示在单独的页面中。谢谢

标签: ios xcode pdf webview swift3


【解决方案1】:

一步一步的实现,你可以得到教程here1here2

class ReaderViewController: UIViewController, UIDocumentInteractionControllerDelegate

 func loadDocUsingDocInteractionController() {

if let fileURL = NSBundle.mainBundle().pathForResource("PDFName", ofType: "pdf") { // Use if let to unwrap to fileURL variable if file exists
    let docInteractionController = UIDocumentInteractionController(URL: NSURL(fileURLWithPath: fileURL)!)
    docInteractionController.delegate = self
    docInteractionController.presentPreviewAnimated(true)
}
}

// Return the view controller from which the    UIDocumentInteractionController will present itself.
func documentInteractionControllerViewControllerForPreview(controller: UIDocumentInteractionController) -> UIViewController {
return self
}

swift3

class ViewController:UIViewController,UIDocumentInteractionControllerDelegate

然后像这样调用函数

@IBAction func btnOpenModal(_ sender: UIButton) {

  //  var button: UIButton? = (sender as? UIButton)
    let URLName: URL? = Bundle.main.url(forResource: "sample", withExtension: "pdf")
    if URLName != nil {
        // Initialize Document Interaction Controller
        let  documentInteractionController: UIDocumentInteractionController = UIDocumentInteractionController(url: URLName!)
        // Configure Document Interaction Controller
        documentInteractionController.delegate = self
        // Present Open In Menu
        documentInteractionController.presentOpenInMenu(from: sender .frame, in: self.view, animated: true)
    }
}

func documentInteractionControllerViewControllerForPreview(_ controller: UIDocumentInteractionController) -> UIViewController {

    return self
}

【讨论】:

  • 谢谢。现在你可以显示第4页例如一个独立在ViewControllero中的PDF文件包含一个20页?
  • 谢谢。您的回答帮助我完成了这个项目并且效果很好。 github.com/ILearniOS/PDF-Reader
猜你喜欢
  • 1970-01-01
  • 2010-12-28
  • 1970-01-01
  • 1970-01-01
  • 2015-04-19
  • 1970-01-01
  • 2013-07-22
  • 2022-07-06
  • 1970-01-01
相关资源
最近更新 更多