'How to get the current page number of a PDF?

There is a default Core Graphics method that tells the current active PDF page number.
Is there any way to get the current active page number as showing by the page indicator?
And also is there any way to hide the the page indicator?
Answer in Objective-C would be appreciated.

PDF



Solution 1:[1]

It is easy to get page number in PDFKit. Please find the below code snippet.

print(pdfDocument!.index(for: pdfView.currentPage!))

Solution 2:[2]

Swift 5.1

A Simple Solution

    @IBOutlet var pageNumber: NSTextField!

    let curPg = self.thePDFView.currentPage?.pageRef?.pageNumber
    pageNumber.stringValue = "Page \(String(describing: curPg!))"

Solution 3:[3]

This is an old question and the answer should be in Objc but I had to do a quite similar approach in Swift and here's my solution.

First I created a outlet for container, a label for current page and the another label for current page;

@IBOutlet weak var pageInfoContainer: UIView! 
@IBOutlet weak var currentPageLabel: UILabel!
@IBOutlet weak var totalPagesLabel: UILabel!

Also a timer that will explain below;

private var timer: Timer?

To get the current page a soon as it changes you need to add an observer;

NotificationCenter.default.addObserver(self, selector: #selector(handlePageChange), name: Notification.Name.PDFViewPageChanged, object: nil)

Do not forget to remove the observer in viewWillDisappear!

Now that you have everything in place let's apply some logic!

Here's my setup for pdfView;

private func setupPdfView() {
    
    if let pdf: PDFDocument = pdfDocument {
        
        pdfView.autoScales = true
        pdfView.backgroundColor = .clear
        pdfView.document = pdf
        pdfView.usePageViewController(true, withViewOptions: nil)
    }
    
    if let totalPages: Int = pdfView.document?.pageCount {
        
        totalPagesLabel.text = String(totalPages)
    }
}

The method that will handle the pageChange through the observer;

@objc func handlePageChange() {

    if let currentPage: PDFPage = pdfView.currentPage, let pageIndex: Int = pdfView.document?.index(for: currentPage) {
        
        UIView.animate(withDuration: 0.5, animations: {
            
            self.pageInfoContainer.alpha = 1
        }) { (finished) in
            if finished {
                
                self.startTimer()
            }
        }
        
        currentPageLabel.text = String(pageIndex + 1)
    }
}

And finally the timer and method;

private func startTimer() {
    
    timer?.invalidate()
    timer = Timer.scheduledTimer(timeInterval: 2.0, target: self, selector: #selector(whenTimerEnds), userInfo: nil, repeats: false)
}

@objc func whenTimerEnds() {
    
    UIView.animate(withDuration: 1) {
        
        self.pageInfoContainer.alpha = 0
    }
}

Bear in mind that the timer is being used as a "candy" and is completely optional, this will remove the counter after X seconds on the same page

Stay safe!

Solution 4:[4]

Objective C

CGPDFDocumentRef pdf = CGPDFDocumentCreateWithURL((CFURLRef)[NSURL fileURLWithPath:@"pdf path"]);
int pageCount = CGPDFDocumentGetNumberOfPages(pdf);
CGPDFDocumentRelease(pdf);

Set delegate

self.webView.delegate = self;

Implement webViewDidFinishLoad method and you will get page count as follow...

- (void)webViewDidFinishLoad:(UIWebView *)webView {

    NSInteger pageCount = webView.pageCount; 

    //OR

    NSInteger count_horizontal = webView.scrollView.contentSize.width / webView.scrollView.frame.size.width;

    NSInteger count_verticel = webView.scrollView.contentSize.height / webView.scrollView.frame.size.height;
}

Swift version for other

Get number of pages from pdf file.

guard var pdf = CGPDFDocument(URL(fileURLWithPath: "pdf path") as CFURL) else {
        print("Not able to load pdf file.")
        return
    }

    let pageCount = CGPDFDocumentGetNumberOfPages(pdf);

Display pdf in web view and set its delegate

    self.webView.delegate = self;

Implement webViewDidFinishLoad method and you will get page count as follow...

extension YourViewController: UIWebViewDelegate {

   func webViewDidFinishLoad(_ webView: UIWebView) {

    let count = webView.pageCount

    // OR

    // when horizontal paging is required
    let count_horizontal = webView.scrollView.contentSize.width / webView.scrollView.frame.size.width;

    // when vertical paging is required
    let count_vertical = webView.scrollView.contentSize.height / webView.scrollView.frame.size.height;
   }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Leena
Solution 2 Ronald Hofmann
Solution 3
Solution 4 Mahendra