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

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
aimingoo的专栏
aimingoo的专栏
P
Proofpoint News Feed
宝玉的分享
宝玉的分享
MyScale Blog
MyScale Blog
The GitHub Blog
The GitHub Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
月光博客
月光博客
量子位
博客园 - 司徒正美
V
V2EX
I
InfoQ
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Vercel News
Vercel News
H
Hackread – Cybersecurity News, Data Breaches, AI and More
美团技术团队
N
Netflix TechBlog - Medium
L
LangChain Blog
IT之家
IT之家
Blog — PlanetScale
Blog — PlanetScale
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Stack Overflow Blog
Stack Overflow Blog
A
About on SuperTechFans
Microsoft Azure Blog
Microsoft Azure Blog

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
How to add PDF page navigation, zoom, and rotation contro...
Austin Nguyen · 2026-06-16 · via Inside Nutrient

Table of contents

    Build a complete PDF viewer toolbar with page navigation, zoom controls, and rotation using the PDF.js PDFViewer API and EventBus. This tutorial includes a ready-to-use React toolbar component.

    How to add PDF page navigation, zoom, and rotation controls with PDF.js

    TL;DR

    Build a PDF.js toolbar with navigation, zoom, and rotation by operating on the PDFViewer instance and subscribing to EventBus events:

    • Page navigationviewer.currentPageNumber, previousPage(), nextPage(), listen for pagechanging
    • Zoom — Assign string values ("auto", "page-width", "1.5") to viewer.currentScaleValue, listen for scalechanging
    • Rotation — Increment viewer.pagesRotation in 90-degree increments, listen for rotationchanging
    • Layout refresh — Call viewer.update() on panel resizes
    • Precise scroll-to-position — Use PDFLinkService.goToDestination with the XYZ destination type

    If you’d rather skip the wiring, Nutrient Web SDK ships a built-in toolbar and a typed ViewState API.

    Building a toolbar for your PDF viewer means wiring up controls for page navigation, zoom, and rotation. All three operate through the PDFViewer instance and the EventBus.

    The PDFViewer instance tracks the current page and exposes methods to move between pages, while the EventBus reports page changes back to your UI.

    Reading current page

    const currentPage = viewer.currentPageNumber; // 1-indexed.

    const totalPages = pdfDocument.numPages;

    Changing pages

    // Go to a specific page.

    viewer.currentPageNumber = 5;

    // Previous/Next.

    viewer.previousPage();

    viewer.nextPage();

    Listening for page changes

    eventBus.on("pagechanging", (evt) => {

    setCurrentPage(evt.pageNumber);

    });

    Use PDFLinkService.goToDestination to scroll to an exact position on a page:

    linkService.goToDestination([

    pageIndex, // 0-indexed page number.

    { name: "XYZ" }, // destination type.

    scrollLeft, // x position (PDF coordinates), or null for current.

    scrollTop, // y position (PDF coordinates), or null for current.

    null, // zoom level, or null for current.

    ]);

    This is the same mechanism PDF internal links use to scroll to a target location.

    React page navigation component

    The component below assumes a PDFContext that exposes viewer, eventBus, and pdfDocument as references (typically populated when the PDF.js PDFViewer is initialized in a parent component).

    import { useContext, useEffect, useState } from "react";

    import { PDFContext } from "./PDFContext";

    function PageToolbar() {

    const { viewer, eventBus, pdfDocument } = useContext(PDFContext);

    const [currentPage, setCurrentPage] = useState(1);

    const [totalPages, setTotalPages] = useState(0);

    useEffect(() => {

    if (pdfDocument.current) {

    setTotalPages(pdfDocument.current.numPages);

    }

    }, [pdfDocument]);

    useEffect(() => {

    const handler = (evt) => setCurrentPage(evt.pageNumber);

    eventBus.current?.on("pagechanging", handler);

    return () => eventBus.current?.off("pagechanging", handler);

    }, [eventBus]);

    return (

    <div>

    <button

    onClick={() => viewer.current?.previousPage()}

    disabled={currentPage <= 1}

    >

    Previous

    </button>

    <input

    type="number"

    value={currentPage}

    min={1}

    max={totalPages}

    onChange={(e) => {

    const page = parseInt(e.target.value, 10);

    if (page >= 1 && page <= totalPages) {

    viewer.current.currentPageNumber = page;

    }

    }}

    />

    <span>of {totalPages}</span>

    <button

    onClick={() => viewer.current?.nextPage()}

    disabled={currentPage >= totalPages}

    >

    Next

    </button>

    </div>

    );

    }

    Zoom/scale

    Zoom is controlled through viewer.currentScaleValue, which accepts both preset keywords and numeric values, and the EventBus emits a scalechanging event whenever the scale updates.

    Setting scale

    // Preset values (strings).

    viewer.currentScaleValue = "auto"; // Fit to container.

    viewer.currentScaleValue = "page-width"; // Fit width.

    viewer.currentScaleValue = "page-fit"; // Fit entire page.

    // Numeric values (as string).

    viewer.currentScaleValue = "1.5"; // 150 percent zoom.

    viewer.currentScaleValue = "0.75"; // 75 percent zoom.

    Zoom in/out

    function zoomIn(viewer) {

    const current = parseFloat(viewer.currentScaleValue) || 1;

    viewer.currentScaleValue = String(Math.min(current + 0.25, 5));

    }

    function zoomOut(viewer) {

    const current = parseFloat(viewer.currentScaleValue) || 1;

    viewer.currentScaleValue = String(Math.max(current - 0.25, 0.25));

    }

    Listening for scale changes

    eventBus.on("scalechanging", (evt) => {

    setScale(evt.scale); // numeric value.

    setPreset(evt.presetValue); // "auto", "page-width", etc.

    });

    Rotation

    The viewer.pagesRotation property holds the current rotation, which you change in 90-degree increments. Each change adds to the previous value, and the EventBus fires a rotationchanging event whenever the rotation updates.

    Rotating pages

    // Rotate 90 degrees clockwise.

    viewer.pagesRotation += 90;

    // Rotate 90 degrees counterclockwise.

    viewer.pagesRotation -= 90;

    pagesRotation is cumulative. Values are in degrees (0, 90, 180, 270).

    Listening for rotation changes

    eventBus.on("rotationchanging", (evt) => {

    setRotation(evt.pagesRotation);

    });

    Force refresh on layout changes

    If your PDF viewer is in a resizable panel (sidebar toggle, split view), the pages may not reflow automatically. Force a refresh by calling viewer.update():

    function refreshViewer() {

    viewer.update();

    }

    // Call on panel resize, sidebar toggle, etc.

    window.addEventListener("resize", refreshViewer);

    // Or use a custom event.

    window.addEventListener("refresh-pdf-viewer", refreshViewer);

    import { useContext, useEffect, useState } from "react";

    import { PDFContext } from "./PDFContext";

    function PDFToolbar() {

    const { viewer, eventBus, pdfDocument } = useContext(PDFContext);

    const [page, setPage] = useState(1);

    const [total, setTotal] = useState(0);

    useEffect(() => {

    setTotal(pdfDocument.current?.numPages || 0);

    const h = (e) => setPage(e.pageNumber);

    eventBus.current?.on("pagechanging", h);

    return () => eventBus.current?.off("pagechanging", h);

    }, [eventBus, pdfDocument]);

    return (

    <div className="pdf-toolbar">

    {/* Page Navigation */}

    <button onClick={() => viewer.current?.previousPage()}>Prev</button>

    <span>{page} / {total}</span>

    <button onClick={() => viewer.current?.nextPage()}>Next</button>

    {/* Zoom */}

    <button onClick={() => viewer.current && zoomOut(viewer.current)}>-</button>

    <button onClick={() => {

    if (viewer.current) viewer.current.currentScaleValue = "page-width";

    }}>Fit Width</button>

    <button onClick={() => viewer.current && zoomIn(viewer.current)}>+</button>

    {/* Rotation */}

    <button onClick={() => {

    if (viewer.current) viewer.current.pagesRotation -= 90;

    }}>Rotate Left</button>

    <button onClick={() => {

    if (viewer.current) viewer.current.pagesRotation += 90;

    }}>Rotate Right</button>

    </div>

    );

    }

    Key points

    • Page numbers are 1-indexed in viewer.currentPageNumber but 0-indexed in goToDestination
    • Scale values are strings — even numeric values like "1.5"
    • Rotation accumulates in degrees on viewer.pagesRotation
    • Always listen to EventBus events to keep your UI in sync with the viewer state
    • Call viewer.update() to force a layout refresh after panel resizes
    • goToDestination with "XYZ" type gives you precise scroll-to-position control

    FAQ

    currentPageNumber is the user-facing page number (matches “Page 1 of 10” in the UI). goToDestination takes the array form from the PDF specification, where the first element is a zero-based page index. The two indexing conventions are intentional but easy to mix up.

    viewer.currentScaleValue accepts both preset keywords ("auto", "page-width", "page-fit") and numeric zoom levels ("1.5" for 150 percent) as strings. Using a single string type lets PDF.js distinguish “fit-to-width” from a hardcoded zoom. Always pass strings — viewer.currentScaleValue = 1.5 silently fails.

    Call viewer.update(). This remeasures the container and reflows the pages. Common triggers: sidebar toggles, split-pane resizes, fullscreen transitions, and dynamic CSS changes that affect the container’s dimensions.

    You need pdfjs-dist/web/pdf_viewer.css for the text layer, annotation layer, and page layout. The toolbar is your own React component, so it uses your own styles. Without the PDF.js CSS, pages render, but text selection and annotations break.

    Nutrient ships a built-in toolbar with navigation, zoom, rotation, and search controls, plus a typed ViewState API for programmatic control. There’s no EventBus, no string-typed scale values, and no manual layout refresh. See the migration guide for switching from PDF.js.

    How Nutrient Web SDK handles this

    Instead of wiring up EventBus listeners, parsing string-typed scale values, and manually refreshing layouts, Nutrient’s ViewState API handles navigation, zoom, and rotation declaratively:

    // Page navigation.

    instance.setViewState((v) => v.set("currentPageIndex", 4));

    // Zoom.

    instance.setViewState((v) => v.set("zoom", "FIT_WIDTH"));

    // Rotation.

    instance.setViewState((v) =>

    v.set("pagesRotation", (v.pagesRotation + 90) % 360),

    );

    The built-in toolbar ships with navigation controls, and ViewState keeps the toolbar, thumbnails, and scroll position in sync automatically.


    See Nutrient Web SDK for an alternative to PDF.js, or follow the migration guide to switch. Talk to Sales about your requirements.

    Explore related topics

    Try for free Ready to get started?

    Related SDK articles

    Explore more