惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

MyScale Blog
MyScale Blog
博客园 - 司徒正美
A
About on SuperTechFans
Vercel News
Vercel News
H
Hackread – Cybersecurity News, Data Breaches, AI and More
爱范儿
爱范儿
I
InfoQ
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
博客园_首页
Google DeepMind News
Google DeepMind News
T
Tailwind CSS Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
F
Fortinet All Blogs
S
SegmentFault 最新的问题
阮一峰的网络日志
阮一峰的网络日志
D
Docker
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
G
Google Developers Blog
Stack Overflow Blog
Stack Overflow Blog
M
MIT News - Artificial intelligence
Jina AI
Jina AI
H
Help Net Security
量子位
IT之家
IT之家

Inside Nutrient

A guide to the invisible work behind documents Introducing Nutrient Documents for Salesforce: Native document generation and signing Document AI vs. traditional OCR: Choosing between OCR, AI, and hybrid pipelines PDF SDK compliance and security evaluation checklist for enterprise teams (2026) Invariant Corp replaces paper processes with Nutrient Workflow and scales without limits What is process mapping? A complete guide Nutrient vs. Conga Composer for Salesforce document generation (2026) Document routing: How to automate document distribution The CTO’s AI playbook: Why accountability architecture beats orchestration Compliance workflow automation: Why built-in compliance is table stakes Workflow diagrams: Examples, symbols, and how to build one that actually runs Digital forms: Replace paper forms with automated workflows Approval workflow software: How to automate approvals Why document-centric automation is different The CEO’s AI playbook: Why decision architecture beats model selection Nutrient SDK product updates for Q1 2026 PDF redaction verification: How to prove sensitive data is permanently removed What is a VPAT? The complete guide to accessibility conformance reports What is PDF/UA? The accessible PDF standard explained Salesforce eSignatures: Generate, sign, and track documents in one flow Online document viewer: Options, tradeoffs, and how to embed one Document viewer for web apps: React, Vue, Angular (2026) Best document viewers in 2026: A buyer’s guide How to edit a PDF in Python: Add text, images, and annotations Nutrient advances Workflow platform with agentic AI for enterprise-grade speed and consistency in document-heavy operations How to create a Salesforce quote template from opportunity data The business case for accessibility: Five ways it drives enterprise value Python PDF library comparison (2026): 7 libraries for developers Why your AI agent hallucinates PDF table data PDF.js limitations: When to upgrade to a commercial PDF SDK
Persist PDF zoom with PSPDFKit
Ferdinand Bada · 2023-12-01 · via Inside Nutrient

Table of contents

    Persist PDF zoom with PSPDFKit

    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

    Try for free Ready to get started?

    Related SDK articles

    Explore more