Max Max - 27 days ago 11
Android Question

How to get the current page number of a PDF in Android

I have to implement a custom PDF Viewer in my Android application, I have implemented this:

https://github.com/barteksc/AndroidPdfViewer

To open my pdf I used this function:

/* Load PDF in the activity */
private void openPDF(){
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() +"/Download/verbale.pdf");
PDFView pdfView = (PDFView) findViewById(R.id.pdfView);

pdfView.fromFile(file)
.defaultPage(0)
.enableAnnotationRendering(true)
.onLoad(this)
.onPageChange(this)
.scrollHandle(new DefaultScrollHandle(this))
.load();
}


The PDF is correctly rendered but now I need to get the page number of the page during the scroll.

How I can do this ?

I have tried to use this:

pdfView.getCurrentPage()
pdfView.getPageCount()


But not it doesn't work...

Answer

You can take a look at the official example of the AndroidPdfViewer: Link


Take a look at this two snippets from that example:

1) Create your PDFView:

pdfView.fromAsset(SAMPLE_FILE)
                .defaultPage(pageNumber)
                .onPageChange(this)
                .enableAnnotationRendering(true)
                .onLoad(this)
                .scrollHandle(new DefaultScrollHandle(this))
                .load();

2) and implement onPageChanged(..):

@Override
public void onPageChanged(int page, int pageCount) {
    pageNumber = page;
    //do what you want with the pageNumber
}

Also don't forget to implement OnPageChangeListener

Comments