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():

Copy
1
2
3
4
5
6
7
8
9
10
11
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 we started off.
fragment.setPageIndex(startPage)
Copy
1
2
3
4
5
6
7
8
9
10
11
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 we 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 gives 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:

1
2
3
val config = PdfConfiguration.Builder()
    .restoreLastViewedPage(false)
    .build()
1
2
3
final PdfConfiguration config = new PdfConfiguration.Builder()
    .restoreLastViewedPage(false)
    .build();

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. Furthermore, you can try out the ZoomExample inside our catalog app:

1
2
3
// Zoom to the annotation.
val duration = 250L
fragment.zoomTo(annotation.boundingBox, annotation.pageIndex, duration)
Copy
1
2
3
4
5
6
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!