Customizing Zoom Options in Our Android PDF Viewer

This guide shows how to programmatically manage the zoom scale of a PDF page.

Manual Zooming

PdfFragment allows you to programmatically zoom to a certain scale or to an area of a PDF document page using a convenient zooming API.

ℹ️ Note: Both methods take PDF page coordinates. To find out more on that topic, see the guide on coordinate space conversion.

You can also get the current zoom level of a page by calling PdfFragment#getZoomScale.

Zooming in to the Page

You can programmatically zoom in to the current page using zoomTo, which zooms to a specific scale. If you’d 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):

// 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’s no need to switch the page manually!

For more information on zooming, you can try out the ZoomExample inside our Catalog app.

Starting the Document at a Given Scale

A document can be started at an already predetermined scale (on the given page). Note that rotating the screen resets the scale. The property that controls this is startZoomScale in PdfConfiguration:

val configuration = PdfConfiguration.Builder()
    .startZoomScale(3.0f)
    .build()
final PdfConfiguration configuration = new PdfConfiguration.Builder()
    .startZoomScale(3.0f)
    .build();

Disabling Zooming

Zooming can be disabled by setting maxZoomScale of PdfConfiguration to 1. It’s important to set this value before creating the view:

val configuration = PdfConfiguration.Builder()
    .maxZoomScale(1.0f)
    .build()
final PdfConfiguration configuration = new PdfConfiguration.Builder()
    .maxZoomScale(1.0f)
    .build();