Persist PDF zoom with Nutrient
Table of contents
- Persist zoom level across PDF pages during scrolling
- Use
DocumentListenerto track page changes and zoom factor - Apply zoom with the
zoomTomethod after each page change
Persisting zoom level across a PDF document improves the experience for users who want consistent viewing or have visual impairments. This tutorial shows how to implement persistent zoom in your Android app using Nutrient Android SDK.
Requirements
To get started, you’ll need:
- Android Studio(opens in a new tab) — The official integrated development environment (IDE) for Android.
- Nutrient Android Catalog(opens in a new tab) — Clone and run this repository before proceeding.
Persisting zoom
Once you have Catalog up and running, navigate to app/src/main/java/com/pspdfkit/catalog/examples/kotlin/ZoomExample.kt, which is the file you’ll be modifying. More specifically, you’ll be modifying the ZoomExampleActivity class in the file. You can see how this example works by installing the app and opening Zoom Example in your device or emulator.
To persist the zoom, you need to do the following things:
- Listen to page changes in the document as a user scrolls between the pages. For this, you’ll use
DocumentListener. - Keep track of the scale factor once the zoom has been modified.
- Apply the zoom to the document after the page has changed. For this, you’ll use the
zoomTomethod. - Finally, clean up.
In the ZoomExampleActivity class, paste the following as a class member variable:
private var documentListener:DocumentListener? = nullIn the companion object, add the following constant:
private const val ZOOM_DURATION = 100LThe listener above will be used to track the changes, while the ZOOM_DURATION is a constant to determine how long the zoom animation should take. Next, add the following method in the ZoomExampleActivity class:
private fun persistZoom() { val fragment = requirePdfFragment() var zoom = 1.0f
documentListener = object : DocumentListener { override fun onPageChanged(document: PdfDocument, pageIndex: Int) { val size = fragment.document?.getPageSize(pageIndex) fragment.zoomTo(0, size?.height?.toInt() ?: 0, pageIndex, zoom, ZOOM_DURATION) }
override fun onDocumentZoomed(document: PdfDocument, pageIndex: Int, scaleFactor: Float) { zoom = scaleFactor } }
documentListener?.let { listener -> fragment.addDocumentListener(listener) } }In the method above, you get PdfFragment and declare a zoom variable to track the current zoom factor. The zoom factor is always updated once the document zoom has changed in the onDocumentZoomed method. You then set the zoom on the document using the onPageChanged method. By setting the zoom as shown above, the document will always zoom to the top-left corner. Then, you call this method at the end of onDocumentLoaded. To have the document zoom to the center, for example, you can use the following code instead:
fragment.zoomTo((size?.width?.toInt() ?: 0) / 2, (size?.height?.toInt() ?: 0) / 2, pageIndex, zoom, ZOOM_DURATION)Feel free to play around with the values above to get the desired behavior.
Finally, inside the onDestroy method, clean up as shown below to remove the listener you added:
override fun onDestroy() { super.onDestroy() ...
documentListener?.let { listener -> requirePdfFragment().removeDocumentListener(listener) } }Installing Catalog again in a device or emulator will now show you a consistent zoom across the document as you swipe from one page to another.
Conclusion
Persistent zoom improves the reading experience by maintaining a consistent zoom level as users scroll between pages. The DocumentListener tracks page changes and zoom factors, while zoomTo applies the saved zoom to each new page.
If you run into issues, contact our Support team. Start a free trial or check out the demo.
FAQ
Use a DocumentListener to track zoom changes via onDocumentZoomed, store the scale factor, and apply it to each new page using zoomTo in the onPageChanged callback.
DocumentListener interface used for?DocumentListener provides callbacks for document events like page changes, zoom changes, and document loading. It lets you respond to user interactions with the PDF viewer.
zoomTo method work?The zoomTo method animates the view to a specific zoom level. It takes focus coordinates (x, y), page index, target scale factor, and animation duration in milliseconds.
Yes. Adjust the focusX and focusY parameters in zoomTo. Use (0, height) for top-left, or (width/2, height/2) for center focus.
DocumentListener in onDestroy?Removing the listener prevents memory leaks and ensures your app doesn’t hold references to destroyed activities or fragments.