2

I can not figure out how to do this: When I click on a certain TableView cell, I go to certain pages of a pdf file (startPage is the beginning of the page, endPage is the end of the page). The class for opening the pdf file:

import UIKit

class DetailLessonsVC: UIViewController {

let pdfTitle = "Strahov_Pages"


override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func openPDFAction(title: Int, sub: Int)
{
    if let url = Bundle.main.url(forResource: pdfTitle, withExtension: "pdf") {
        let webView = UIWebView(frame:self.view.frame)
        let urlRequest = URLRequest(url: url)
        webView.loadRequest(urlRequest as URLRequest)
        self.view.addSubview(webView)

    }

}

}

The transition function when clicking on a TableView cell (TableViewController class):

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let vc = DetailLessonsVC()
    vc.openPDFAction(title: startPage[indexPath.count], sub: endPage[indexPath.count])
    self.navigationController?.pushViewController(vc, animated: true)
    self.tableView.deselectRow(at: indexPath, animated: true)
}

StartPage and endPage Arrays (TableViewController class):

let startPage = [6, 9, 15, 23, 27, 29, 33, 37, 41]

let endPage = [8, 14, 22, 26, 28, 32, 36, 42, 44]
aaallleeexxx918
  • 171
  • 3
  • 11
  • 1
    We'd need to see your DetailLessonsVC to get a sense of what's happening here, but I think you might find your answer here: https://stackoverflow.com/questions/19231508/how-to-control-page-display-to-pdf-loaded-in-uiwebview-for-ios-go-to-page – Nima Yousefi Jun 28 '18 at 17:50
  • Hear,with Swift 4 and PDFKit we can do the Solution – Dhaval Gevariya Aug 18 '18 at 15:29

1 Answers1

9
var pdfFile_View: PDFView!
override func viewDidLoad() {
    super.viewDidLoad()
    pdfFile_View = PDFView()
    // You might want to constraint pdfView here
    // Load pdf from bundle or url
    let url = Bundle.main.url(forResource: "Book", withExtension: "pdf")!
    let document = PDFDocument(url: url)
    pdfView.document = document
    // Grab 16th page, returns optional if out of bounds
    if let page10 = document?.page(at:16) {
        pdfView.go(to: page16)
    }

}
Dhaval Gevariya
  • 870
  • 1
  • 12
  • 16