Customizing page navigation in our Android PDF viewer
After loading a PDF document, you can programmatically interact with it, which allows you to scroll to different pages or zoom in and out. All of the interaction APIs are available on the PdfFragment
, which is the core PDF viewer. Furthermore, most methods are also available on the PdfActivity
.
Changing the current page
While the user can manually change the current page by doing a swipe gesture on the device, you can also programmatically change pages using setPageIndex(int)
and setPageIndex(int, boolean)
. You can fetch the current page using getPageIndex()
:
val startPage = fragment.getPageIndex() // Change to page 13 (zero-based page numbers). fragment.pageIndex = 12 // Go to the last page using a transitioning animation. val lastPage = document.pageCount - 1 fragment.setPageIndex(lastPage, true) // Go back to the page where you started off. fragment.setPageIndex(startPage)
final int startPage = fragment.getPageIndex(); // Change to page 13 (zero-based page numbers). fragment.setPageIndex(12); // Go to the last page using a transitioning animation. int lastPage = document.getPageCount() - 1; fragment.setPageIndex(lastPage, true); // Go back to the page where you started off. fragment.setPageIndex(startPage);
💡 Tip: While setPageIndex(int, boolean)
gives you complete control of choosing whether or not a page transitioning animation should be performed, setPageIndex(int)
will intelligently decide whether or not an animation is suitable for the current situation.
Restore the last viewed page
By default, PdfFragment
will remember the last viewed page of a document and will restore it when loading the document at a later point. This usually provides a better experience when using a document across several sessions. If you would like to disable this mechanism, you can set PdfConfiguration#restoreLastViewedPage
to false
:
val config = PdfConfiguration.Builder() .restoreLastViewedPage(false) .build()
final PdfConfiguration config = new PdfConfiguration.Builder() .restoreLastViewedPage(false) .build();