Contents Menu Expand Light mode Dark mode Auto light/dark mode
MuPDF.js documentation
Light Logo Dark Logo
MuPDF.js documentation

Documentation

  • Getting Started
    • Setup with NPM
    • Using TypeScript
  • How To Guide
    • Working with Files
    • Working with Documents
    • Working with Pages
    • Working with Annotations
      • Getting Started
      • Text
      • Redactions
      • Managing Links
      • Drawing & Shapes
      • Stamps
      • File Attachment Annotations
    • Destroying Objects
    • Glossary

Examples

  • Building Web Apps
    • Client Side Rendering
      • Vanilla
      • Angular.js
      • React
      • Vue
    • Server Side Rendering
      • Next.js
    • Desktop Apps

Reference

  • API Reference
  • Deprecated Modules

FAQ

  • Frequently Asked Questions
Back to top

Managing Links#

Links are a special kind of annotation which are not related to the PDFAnnotation class. Instead they use the Link class.

Getting Page Links#

Links can be retrieved with the getLinks method on a page instance, assuming the page contains a link then it can be investigated as follows:

EXAMPLE

let links = page.getLinks()

if (links.length) {
    let link = links[0]
    let linkBounds = link.getBounds()
    let linkURI = link.getURI()
    let linkIsExternal = link.isExternal()
}

Note

The resulting array contains an array of Link objects which have their own bounds and uri for the link.

If there are no links then an empty array is returned.

Creating Links#

To create a link use insertLink from the PDFPage class.

There are two fundamental kinds of links:

  • External URI links to websites

  • Internal document links

Adding an external link is relatively simple, for example, this would add a link area with a bounding box of 100x30 at the top left of the document which hyperlinked to https://mupdfjs.readthedocs.io.

EXAMPLE

page.createLink([ 10, 10, 100, 40 ], "https://mupdfjs.readthedocs.io")

For an internal document link we need to understand a little about the LinkDestination and create a suitable object to represent our needs for the link.

We then need to format the link destination object with formatLinkURI and create the link.

For example, this would add a link with a bounding box of 100x100 at the top left of the page which would link to page 2 of the document (because page numbers are zero-indexed).

EXAMPLE

page.createLink([x, y, x + width, y + height], document.formatLinkURI({ type: "Fit", page: 1 }))

Resolving Internal Links#

Sometimes when we retrieve a link object it may be an internal link. Use resolveLinkDestination to a LinkDestination do the following:

EXAMPLE

let linkDestinationObject = document.resolveLinkDestination(my_link)

Deleting Links#

Use the deleteLink method on a PDFPage instance as follows:

EXAMPLE

page.deleteLink(link)
Next
Drawing & Shapes
Previous
Redactions
Copyright © 2023-2026, Artifex
Made with Furo
Find #mupdf on Discord
On this page
  • Managing Links
    • Getting Page Links
    • Creating Links
    • Resolving Internal Links
    • Deleting Links