我想为选定的 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)
但这会显示文档的所有页面。 我想按页码只显示一页?
最佳答案
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/