2017-02-24 69 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 –