【问题标题】:Go to specific pages of a pdf file转到 pdf 文件的特定页面
【发布时间】:2018-06-28 17:36:48
【问题描述】:

我不知道该怎么做:当我单击某个 TableView 单元格时,我转到 pdf 文件的某些页面(startPage 是页面的开头,endPage 是页面的结尾)。 打开pdf文件的类:

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)

    }

}

}

点击 TableView 单元格时的转换功能(TableViewController 类):

    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 和 endPage 数组(TableViewController 类):

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

let endPage = [8, 14, 22, 26, 28, 32, 36, 42, 44]

【问题讨论】:

  • 我们需要查看您的 DetailLessonsVC 以了解这里发生了什么,但我认为您可能会在这里找到答案:stackoverflow.com/questions/19231508/…
  • 听着,使用 Swift 4 和 PDFKit 我们可以解决问题

标签: ios arrays swift pdf tableview


【解决方案1】:
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)
    }

}

【讨论】:

  • 听着,使用 Swift 4 和 PDFKit 我们可以完成上述解决方案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-09-26
  • 2011-04-06
  • 2023-03-29
  • 1970-01-01
  • 2012-02-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多