Supporting link annotations in PDFs using JavaScript
Nutrient Web SDK supports common types of link annotations, including page, web, and email links. Tapping a link invokes the saved action (see PDF actions) and the correct event, such as changing the page or opening a URL.
By default, Nutrient will parse annotations, including links, from a document.
To change the position or action of a link annotation in code, use the update method. In the built-in UI, long-press the link annotation to select it, and then drag it or click Edit.
For more information on links, refer to the API documentation.
Adding link annotations to PDFs
Add a clickable link to an area in one of the following ways:
Adding link annotations programmatically
To add a link annotation programmatically, follow the steps below.
Create an action property that runs when a user clicks a link:
const action = new NutrientViewer.Actions.GoToAction({ pageIndex: 10 });Add the action as part of the link annotation:
// Create a link annotation on the first page.const annotation = new NutrientViewer.Annotations.LinkAnnotation({pageIndex: 0,boundingBox: new NutrientViewer.Geometry.Rect({left: 10,top: 20,width: 30,height: 40}),action: action});
Editing link annotations programmatically
To edit a link annotation programmatically, use the update method:
// Create a new `URIAction`.const newAction = new URIAction({ uri: "pspdfkit://example.com"});
// Update the link annotation with the newly created action.const editedAnnotation = annotation.set("action", newAction);Adding link annotations using the built-in UI
To add a link annotation using the built-in UI, follow the steps below.
Tap the link annotation icon in the contextual menu.

Drag the annotation to its place on the page.
Choose between linking to a page in the current document or linking to a website.

Editing link annotations using the built-in UI
To edit a link annotation using the built-in UI, long-press the link annotation and then click Edit.

Making links visible
A link annotation created without border properties draws nothing on the page, so nothing marks where the link is until the user interacts with it. To mark a link, give it a border or place it over visible text.
Adding a border to a link
To draw a border around the link’s bounding box, set borderColor, borderWidth, and borderStyle when creating the link annotation:
const linkAnnotation = new NutrientViewer.Annotations.LinkAnnotation({ pageIndex: 0, boundingBox: new NutrientViewer.Geometry.Rect({ left: 80, top: 100, width: 200, height: 40 }), action: new NutrientViewer.Actions.URIAction({ uri: "https://www.nutrient.io" }), borderColor: NutrientViewer.Color.RED, borderWidth: 3, borderStyle: NutrientViewer.BorderStyle.solid});
await instance.create(linkAnnotation);In code, set all three properties. Without borderStyle, Nutrient Web SDK draws no border, even with a color and width set.
To add a border using the built-in UI, long-press the link annotation to select it. Then, in the annotation toolbar, pick a color with the Border Color icon and set a width with the Thickness icon.
The exported PDF includes the border. When printing, the default print mode, PrintMode.DOM, prints the border. PrintMode.EXPORT_PDF prints a copy of the document without link annotations, so the border isn’t printed.
Adding link text
To make a piece of text clickable, create a text annotation with the text and a link annotation with the same bounding box. In Nutrient Web SDK, a click on the text opens the link. For a code example, see how to create PDF link annotations with text.
Some browsers, such as the built-in PDF viewer in Microsoft Edge, handle the exported PDF differently. A click on the text selects the text annotation instead of opening the link. To keep the link clickable there, export the PDF with the text annotation flattened. Instance#exportPDFWithOperations() flattens it only in the exported PDF, so the text annotation stays editable in Nutrient Web SDK. This requires the Document Editor component in your license.
Save the annotations first so the exported PDF includes them. Then pass the ID of the created text annotation. The object passed to instance.create() keeps a null ID, so flattening with that ID leaves the text annotation in place.
In this example, createdTextAnnotation is the first item in the array that instance.create() resolves to:
await instance.save();const pdfBuffer = await instance.exportPDFWithOperations([ { type: "flattenAnnotations", annotationIds: [createdTextAnnotation.id] }]);Use pdfBuffer as the PDF users download. The download button in the built-in toolbar exports the document without this flattening.
Don’t use exportPDF({ flatten: true }) for this, because it flattens the link annotations too, and the exported PDF has no links.
Customizing link behavior
Register an event handler for the annotations.press event on the Nutrient instance to run custom application code or prevent the default click behavior for links:
NutrientViewer.load(...).then(instance => { instance.addEventListener("annotations.press", event => { if (event.annotation instanceof NutrientViewer.Annotations.LinkAnnotation) { event.preventDefault(); console.log(event); } });});NutrientViewer.load(...).then(function (instance) { instance.addEventListener("annotations.press", function (event) { if (event.annotation instanceof NutrientViewer.Annotations.LinkAnnotation) { event.preventDefault(); console.log(event); } });});Read more about the AnnotationsPressEvent.
Autodetecting links
Nutrient can optionally analyze the page contents and automatically add links to URLs. Autodetection is slightly expensive because Nutrient must parse and analyze the text. It runs in a background process, so in most cases, it isn’t noticeably slower. This feature is off by default.
Use Configuration#enableAutomaticLinkExtraction to enable autodetection in Nutrient Web SDK. When using Nutrient Web SDK with Document Engine, use the AUTOMATIC_LINK_EXTRACTION environment variable instead.