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)