Document Interactions

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.
final int startPage = fragment.getPageIndex();

// Change to page 13 (zero-based page numbers).

// 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.

💡 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()
final PdfConfiguration config = new PdfConfiguration.Builder()

Zooming In to the Page

You can programmatically zoom in to the current page using zoomTo, which zooms to a specific scale. If you would like to apply a scale factor to the current scale (e.g. increase page scale by 2), you can use zoomBy. Furthermore, you can use zoomTo(RectF, ...) to zoom in to a RectF of any page (e.g. zoom in to a sentence or annotation).

For more information on zooming, have a look at our Zooming guide. You can also try out the ZoomExample inside our Catalog app:

// Zoom to the annotation.
val duration = 250L
fragment.zoomTo(annotation.boundingBox, annotation.pageIndex, duration)
RectF annotationPosition = annotation.getBoundingBox();
int page = annotation.getPageIndex();
long duration = 250;

// Zoom to the annotation.
fragment.zoomTo(annotationPosition, page, duration);

💡 Tip: zoomTo(RectF, ...) will automatically go to the given page if some other page is currently being viewed. There is no need to switch the page manually!