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

推荐订阅源

A
Arctic Wolf
T
The Blog of Author Tim Ferriss
月光博客
月光博客
Recent Announcements
Recent Announcements
V
V2EX
Microsoft Azure Blog
Microsoft Azure Blog
博客园 - 三生石上(FineUI控件)
P
Proofpoint News Feed
The Register - Security
The Register - Security
博客园 - 叶小钗
博客园 - Franky
The Cloudflare Blog
雷峰网
雷峰网
罗磊的独立博客
M
MIT News - Artificial intelligence
I
InfoQ
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 【当耐特】
Engineering at Meta
Engineering at Meta
N
Netflix TechBlog - Medium
爱范儿
爱范儿
博客园 - 司徒正美
Recorded Future
Recorded Future
酷 壳 – CoolShell
酷 壳 – CoolShell
Google DeepMind News
Google DeepMind News
Martin Fowler
Martin Fowler
Microsoft Security Blog
Microsoft Security Blog
F
Full Disclosure
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
B
Blog
大猫的无限游戏
大猫的无限游戏
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
腾讯CDC
WordPress大学
WordPress大学
小众软件
小众软件
K
Kaspersky official blog
Attack and Defense Labs
Attack and Defense Labs
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Forbes - Security
Forbes - Security
aimingoo的专栏
aimingoo的专栏
IT之家
IT之家
The Last Watchdog
The Last Watchdog
N
News and Events Feed by Topic
B
Blog RSS Feed
S
Security @ Cisco Blogs
美团技术团队
量子位
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Cloudbric
Cloudbric
Hacker News - Newest:
Hacker News - Newest: "LLM"

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 Subject scaled 5× with Nutrient’s PDF SDK without rebuilding its document layer I replaced our sales training with an AI coach that runs in Slack — here’s what broke Redirecting to: https://securitybuzz.com/cybersecurity-news/why-enterprise-permissions-are-ais-most-dangerous-inheritance/ Nutrient .NET SDK vs. iText Core: Complete comparison for .NET developers DocuVieware: Support’s most frequently asked setup questions Introducing Nutrient Workflow How to convert PDF to Word in C# (.NET) When email and spreadsheets stop working: Work order approval workflows for field teams on the move Compliance with confidence: Why document-centric automation is the foundation of your mission Nutrient expands AI Assistant, automating multistep document workflows inside any application What is document generation? A developer’s guide to PDF generation Document Converter data flow and how real-time watermarks skip the queue PDF/UA compliance guide: Requirements, standards, and best practices Computers still can’t understand you How Athena Intelligence built AI agents for regulated enterprises with Nutrient’s document infrastructure How to convert HTML to PDF (2026): 4 methods from browser print to SDK How to build a document extraction pipeline with Nutrient Vision API OCR vs. intelligent document processing: Choosing the right document extraction engine Beyond OCR: How document intelligence eliminates manual processing in regulated industries Nutrient vs. IronPDF: Complete comparison for .NET developers Nutrient vs. Aspose.PDF: Complete comparison for .NET developers Redirecting to: https://fortune.com/2026/02/19/openclaw-who-is-peter-steinberger-openai-sam-altman-anthropic-moltbook/ Lufthansa Systems uses Nutrient to deliver reliable, scalable PDF rendering for pilots worldwide Nutrient vs. Syncfusion: Complete comparison for .NET developers React’s useTransition: The hook you’re probably using wrong First City Monument Bank streamlines banking processes with Nutrient Workflow Redirecting to: https://www.sdcexec.com/warehousing/automation/article/22957364/nutrient-workflow-automation-the-missing-link-in-supply-chain-efficiency The complete guide to digital signatures: PAdES, CAdES, and XAdES explained Nutrient Python SDK: Production-grade document processing for Python Introducing agentic document editing for web applications with AI Assistant Nutrient vs. QuestPDF: Complete comparison for .NET developers How we fixed the GdPicture license expiration (and what to do if you’re affected) Red team security testing with agentic AI The future of healthcare document automation Best healthcare workflow software compared Nutrient SDK product updates for Q4 2025 How Harvey scaled legal document workflows 50 percent MoM without rebuilding infrastructure HIPAA-compliant document management in hospitals How we optimized rendering performance while handling thousands of annotations in React — Part 2 Automated PII removal with Nutrient API Redirecting to: https://www.devopsdigest.com/2026-low-code-no-code-predictions Redirecting to: https://www.kmworld.com/Articles/Editorial/ViewPoints/Leaders-predict-AI-to-continue-permeating-all-aspects-of-KM-in-2026-172594.aspx What are deep agents and how do they solve complex problems? Whipping up document magic: Your easy-bake recipe for Vue and Nutrient Web SDK 🧁 What I’ve learned about product iteration planning while building SDKs Passwordless document signing: Three-layer security guide New zip folder functionality streamlines file management in Document Automation Server The keyboard shortcuts playbook: Taking control of keyboard events in Nutrient Web SDK From experienced engineer to AI beginner: My unexpected journey AI-assisted manual testing: Handling Safari’s PDF rendering and UI quirks How to keep a 20-year-old SDK up to date How we optimized rendering performance while handling thousands of annotations in React — Part 1 Nutrient announces new executive hires to accelerate next phase of growth High performance UI using web workers Automate document conversion at scale with Python and Nutrient DCS From curiosity to PLG (and AI): My journey to understanding product-led growth Prost to progress: One year as Nutrient Pigeon usage at Nutrient: Bridging native SDKs to Flutter Modernizing CI build servers: How to migrate from Chef to Ansible Unix man pages: AI-friendly documentation since 1971 Consistent hashing for even load distribution Best AI redaction APIs: Complete comparison guide for 2025 Why AI document redaction matters for modern security From coding to coordinating: How AI transformed my workflow What is intelligent document processing (IDP)? A complete guide Enterprise PDF SDKs: Best PSPDFKit (now Nutrient) alternatives Nutrient SDK product updates for Q3 2025 GdPicture support best practices Redacting sensitive data with Nutrient AI redaction API How AI is transforming the customer experience at Nutrient: From instant answers to intelligent support
Rendering custom overlays on PDF.js pages with React portals
Austin Nguyen · 2026-06-19 · via Inside Nutrient

Table of contents

    PDF.js renders each page inside its own DOM element. This guide shows how to use React portals to inject custom overlay layers — like highlights and annotations — into PDF.js page elements so they scroll, zoom, and rotate with each page.

    Rendering custom overlays on PDF.js pages with React portals

    TL;DR

    PDF.js owns the DOM tree for each rendered page. To overlay highlights or annotations that scroll, zoom, and rotate with the page, inject a container div into each .page element and render React into it via createPortal. The pattern has four moving parts:

    • Find or create an overlay container inside each .page div.
    • Wait for the textlayerrendered event before creating containers.
    • Recheck container.isConnected after every page rerender (zoom invalidates the DOM).
    • Recompute positions on scalechanging and rotationchanging using pageView.viewport.

    If you’d rather not maintain this glue, Nutrient Web SDK exposes a customRenderers API that handles the lifecycle for you.

    PDF.js renders each page inside its own DOM element. To add highlights, annotations, or other visual overlays, you need to inject custom layers into these page elements. React portals are the perfect tool for this.

    The problem

    PDF.js controls its own DOM tree:

    #pdf-container

    .pdfViewer

    .page[data-page-number="1"]

    .canvasWrapper

    canvas

    .textLayer

    .page[data-page-number="2"]

    ...

    You can’t just render React components next to the canvas — they need to live inside each .page element to scroll, zoom, and rotate with the page.

    Step 1: Find or create a layer container

    For each page, create a <div> inside the .page element to host your overlay:

    function findOrCreateContainerLayer(container, className) {

    let layer = container.querySelector(`.${className}`);

    if (!layer) {

    layer = document.createElement("div");

    layer.className = className;

    container.append(layer);

    }

    return layer;

    }

    function findOrCreateHighlightLayer(viewer, pageNumber) {

    const pageView = viewer.getPageView(pageNumber - 1);

    if (!pageView?.div) return null;

    return findOrCreateContainerLayer(

    pageView.div,

    "custom-highlight-layer",

    );

    }

    This gives you:

    .page[data-page-number="1"]

    .canvasWrapper

    .textLayer

    .custom-highlight-layer <-- your overlay container

    Step 2: Wait for text layer rendering

    You can only create overlay containers after the page’s text layer has rendered. Listen for textlayerrendered:

    import React from "react";

    // Group an array of items by their `.page` property.

    function groupByPage(items) {

    return items.reduce((acc, item) => {

    (acc[item.page] ||= []).push(item);

    return acc;

    }, {});

    }

    function useOverlayPortals(viewer, eventBus, pdfDocument, data) {

    const containers = React.useRef({});

    const [portals, setPortals] = React.useState([]);

    const dataByPage = React.useMemo(() => groupByPage(data), [data]);

    React.useEffect(() => {

    const renderOverlays = () => {

    const portalList = [];

    for (let page = 1; page <= pdfDocument.numPages; page++) {

    // Reuse existing container if still connected to DOM.

    let container = containers.current[page];

    if (!container?.isConnected) {

    container = findOrCreateHighlightLayer(viewer, page);

    if (container) containers.current[page] = container;

    }

    if (container) {

    portalList.push({

    element: container,

    data: dataByPage[page] || [],

    page,

    });

    }

    }

    setPortals(portalList);

    };

    renderOverlays();

    eventBus.on("textlayerrendered", renderOverlays);

    return () => {

    eventBus.off("textlayerrendered", renderOverlays);

    };

    }, [viewer, eventBus, pdfDocument, dataByPage]);

    return portals;

    }

    Step 3: Render via React portals

    Wire useOverlayPortals into a component that renders each portal. The component calls createPortal with the container from step 1 and the page data grouped in step 2:

    import { useContext } from "react";

    import { createPortal } from "react-dom";

    import { PDFContext } from "./PDFContext";

    function HighlightOverlay({ locations, onClear }) {

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

    const portals = useOverlayPortals(

    viewer.current,

    eventBus.current,

    pdfDocument.current,

    locations,

    );

    return (

    <>

    {portals.map(({ element, data, page }, index) =>

    createPortal(

    <HighlightLayer

    page={page}

    locations={data}

    onClear={onClear}

    />,

    element,

    `highlight-${index}`,

    ),

    )}

    </>

    );

    }

    Step 4: Position elements using viewport

    Inside each portal, convert PDF coordinates to CSS positions. pdfToScreen and nodeLocationToScaled below are your own coordinate-conversion helpers — see PDF.js coordinate systems for the math.

    function HighlightLayer({ page, locations, onClear }) {

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

    const [viewport, setViewport] = React.useState(undefined);

    // Update viewport on scale/rotation changes.

    React.useEffect(() => {

    const update = () => {

    const pageView = viewer.current?.getPageView(page - 1);

    if (pageView?.viewport) setViewport(pageView.viewport);

    };

    update();

    eventBus.current?.on("scalechanging", update);

    eventBus.current?.on("rotationchanging", update);

    return () => {

    eventBus.current?.off("scalechanging", update);

    eventBus.current?.off("rotationchanging", update);

    };

    }, [page, viewer, eventBus]);

    if (!viewport) return null;

    return locations.map((location, i) => {

    const position = pdfToScreen(

    nodeLocationToScaled(location),

    viewport,

    );

    return (

    <div

    key={i}

    onDoubleClick={onClear}

    style={{

    position: "absolute",

    background: "rgba(96, 4, 255, 0.2)",

    left: position.left,

    top: position.top,

    width: position.width,

    height: position.height,

    }}

    />

    );

    });

    }

    CSS layer ordering

    The overlay layer needs a proper z-index to sit above the canvas but allow text selection through it:

    /* Ensure highlight layer appears above canvas but below text. */

    .custom-highlight-layer {

    position: absolute;

    top: 0;

    left: 0;

    width: 100%;

    height: 100%;

    pointer-events: none; /* Allow text selection through. */

    z-index: 1;

    }

    .custom-highlight-layer > div {

    pointer-events: auto; /* But highlights themselves are clickable. */

    }

    /* Override PDF.js text layer z-index if needed. */

    .textLayer {

    z-index: 2 !important;

    }

    Checking container connectivity

    PDF.js may rerender pages (e.g. on zoom), destroying your injected containers. Always check isConnected before reusing:

    const container = containers.current[page];

    if (container?.isConnected) {

    // Reuse — still in the DOM.

    } else {

    // Recreate — page was rerendered.

    containers.current[page] = findOrCreateHighlightLayer(viewer, page);

    }

    Toggling visibility and interactivity

    Use CSS classes to show/hide overlays or disable interaction when annotation tools are active:

    React.useEffect(() => {

    for (const portal of portals) {

    // Hide when annotations are toggled off.

    portal.element.classList.toggle("hidden", !showAnnotations);

    // Disable pointer events when drawing new annotations.

    portal.element.classList.toggle("noInteraction", isDrawing);

    }

    }, [portals, showAnnotations, isDrawing]);

    .hidden { display: none; }

    .noInteraction { pointer-events: none !important; }

    Key takeaways

    • Use createPortal() to render React components inside PDF.js page divs.
    • Wait for textlayerrendered before creating overlay containers.
    • Check isConnected to handle page rerenders.
    • Recompute coordinates on scalechanging and rotationchanging.
    • Use pointer-events: none on the container, auto on individual items.
    • Store container refs in a useRef map keyed by page number.

    FAQ

    PDF.js controls the DOM tree for each page. Rendering React siblings next to the canvas means your overlay won’t scroll, zoom, or rotate with the page. createPortal lets you render React components into a DOM node that lives inside each .page element, so the page’s own transforms apply automatically.

    PDF.js renders pages asynchronously: The canvas paints first, followed by the text layer and then the annotation layer. The .page element exists earlier, but appending a sibling to it before the text layer is ready can race with PDF.js’s own DOM mutations and result in your container being wiped. textlayerrendered fires when the page is fully laid out and safe to extend.

    PDF.js may rerender a page when zoom or rotation changes — and rerendering replaces the old .page DOM subtree entirely, orphaning any nodes you injected. Node.isConnected returns false for nodes that have been detached. Checking it before reusing a container catches the rerender and forces you to create a fresh layer.

    Without pointer-events: none on the overlay container, your divs intercept mouse events that would otherwise reach the text layer. Set pointer-events: none on the container and pointer-events: auto only on the individual overlay items that need to be clickable. Combined with a z-index lower than the text layer (z-index: 1 on overlay, z-index: 2 on .textLayer), selection and interaction both work.

    Use the viewport object that PDF.js attaches to each page view: viewer.getPageView(pageIndex).viewport. The viewport provides convertToViewportPoint(x, y) for points and convertToViewportRectangle([x1, y1, x2, y2]) for rectangles, plus a transform matrix you can compose with your own coordinates. Recompute on scalechanging and rotationchanging events.

    Nutrient’s customRenderers API lets you provide your own DOM or React element for any annotation type. The SDK handles positioning, z-index, scaling, rotation, and lifecycle — no portals, no textlayerrendered listeners, no isConnected checks. See the migration guide for information on moving from PDF.js.

    How Nutrient Web SDK handles this

    With Nutrient, there are no React portals, no DOM injection, no textlayerrendered event listeners, and no isConnected checks to manage. Nutrient’s custom renderer API lets you provide your own UI for any annotation type while the SDK handles positioning, scaling, and lifecycle:

    const instance = await NutrientViewer.load({

    container: "#pdf-container",

    document: "document.pdf",

    customRenderers: {

    Annotation: ({ annotation }) => {

    // Return a custom React/DOM element for any annotation.

    const node = document.createElement("div");

    node.className = "custom-overlay";

    node.textContent = annotation.text?.value || "";

    return { node, append: true };

    },

    },

    });

    The SDK also includes 17+ built-in annotation types with automatic z-index management and zoom-aware rendering.


    For a managed alternative, see Nutrient Web SDK 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