Blog Post

How to Persist Zoom While Scrolling through a Document Using the PSPDFKit Android Library

Ferdinand Bada
Illustration: How to Persist Zoom While Scrolling through a Document Using the PSPDFKit Android Library

In this brief tutorial, you’ll learn how to persist a zoom level across an entire PDF document during scrolling. Say you have a user who wants to have a consistent feel to their PDFS. Or maybe your user is visually impaired. While it’s nice to have the ability to zoom in and out as desired, in both scenarios, it’d be even better for your app to support a persistent zoom level to ensure the best user experience. To learn how to implement this functionality, read on.

Requirements

To get started, you’ll need:

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 zoomTo method.

  • Finally, clean up.

In the ZoomExampleActivity class, paste the following as a class member variable:

private var documentListener:DocumentListener? = null

In the companion object, add the following constant:

private const val ZOOM_DURATION = 100L

The 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 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 follows 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

In this post, you learned how to persist the zoom across a document as a user is scrolling between pages. If you hit any snags while trying to implement any of the steps, don’t hesitate to reach out to our Support team for help.

At PSPDFKit, we offer a commercial, feature-rich, and completely customizable Android PDF library that’s easy to integrate and comes with well-documented APIs to handle advanced use cases. Try it for free, or visit our demo to see it in action.

Explore related topics

Share post
Free trial Ready to get started?
Free trial