Mahmoud R.
Mahmoud R.

Reputation: 1

How to display current PDF page number and save this number as a bookmark in Swift?

I’m new in swift and I’m working on my first app. The app is displaying a pdf file for the holy Qur’an have index which must connect every Surah (chapter) to it’s specific page in the pdf file and every page must have it’s number to make it a bookmark .

NOW I finished displaying THE PDF FILE AND I NEED HELP TO SHOW THE USER THE NUMBER OF THE CURRENT PAGE EVERY SINGLE SWIPE

//here’s WHAT I Have Done

import UIKit
import PDFKit
class ViewController: UIViewController {

    @IBOutlet weak var pdfView2: PDFView!

    override func viewDidLoad() {
        super.viewDidLoad()
        if let path = Bundle.main.path(forResource: "Binder1", ofType: ".pdf") {
            let url = URL(fileURLWithPath: path)
            if let pdfDocument = PDFDocument(url: url) {
                pdfView2.displaysRTL = true
                pdfView2.autoScales = true
                pdfView2.displayMode = .singlePageContinuous
                pdfView2.displayDirection = .horizontal
                pdfView2.document = pdfDocument
                pdfView2.pageBreakMargins = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
                pdfView2.usePageViewController(true)
               navigationController?.hidesBarsOnTap = true
                pdfView2.usePageViewController(true)
                pdfView2.displaysAsBook = true
                pdfView2.backgroundColor = UIColor(white: 0.95, alpha: 1.0)
                print(pdfDocument.index(for: pdfView2.currentPage!))

            }
         } 
      }

  }

enter image description here

IN THIS CIRCLE THE LABEL I NEED TO SHOW THE NUMBER AND THE TITLE OF EVERY PAGE

Upvotes: 0

Views: 2900

Answers (1)

Muhammed Gül
Muhammed Gül

Reputation: 875

To get the current page number

let pageNumber = pdfDocument.index(for: pdfView2.currentPage!)

And you can display it at the top like:

self.navigationItem.title = String(pageNumber)

If you want to store this number as bookmark persistently, you can use UserDefaults. Like:

UserDefaults.standard.set(pageNumber, forKey: "bookmarkedPage") // you can change the key

To get this value later on:

let savedPageNumber = UserDefaults.standard.integer(forKey: "bookmarkedPage")

After getting bookmark, to navigate to saved page in PDFView:

 pdfView2.go(to: pdfDocument?.page(at:savedPageNumber))

Upvotes: 2

Related Questions