2017-02-24 90 views
0

即使PDF有多个页面,我也想为所选PDF文件显示单页面。如何使用SWIFT显示多文档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) 

但是,显示文档的所有页面。 我想只显示一页页码?

+1

使用UIDocumentInteractViewController为您r的概念 –

+0

我找了这个,我没有找到解决方案。有没有可以帮助我做的例子或解释。我正在研究UIPageViewController应用程序,我想添加一个PDF文件,以便所有页面都显示在单独的页面中。谢谢 –

回答

0

为分步实施,就可以得到教程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 
} 
+0

谢谢。现在你可以显示页码4,例如一个独立的ViewControllero中的PDF文件包含20页? –

+0

谢谢。你的回答帮助我实现了这个项目,并且这很好。 https://github.com/ILearniOS/PDF-Reader –

相关问题