ios - 如何使用 SWIFT 显示多文档 PDF 页面的单页

标签 ios xcode pdf webview swift3

我想为选定的 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)

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

最佳答案

一步一步的实现,你可以得到教程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
}

关于ios - 如何使用 SWIFT 显示多文档 PDF 页面的单页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42432409/

相关文章:

ios - 提供用于身份验证的默认 Twitter 帐户

iphone - 切换到另一个选项卡时使标题 View 不变

c# - Unity 中的鼠标弹起与触摸弹起

XCode 13 - 为开发语言生成 Localizable.strings 文件(在我的例子中是英语),而不使用 genstrings

python - 将 PDF 文件转换为 Base64 以索引到 Elasticsearch

ios - 在播放一些背景音乐的同​​时播放和录制视频

ios - Xcode View Controller 转换动画运行得非常慢

xcode - swift 中的第 n 个质数

java - 在 iText5 中,如果可能的话,如何将 PdfTemplate 添加到 PdfStamper 中?

pdf 页面属性中的 Javascript 在 Google Chrome 中不起作用