Java/ Add Hyperlinks to an Existing PDF

Java/ Add Hyperlinks to an Existing PDF

·

2 min read

A hyperlink refers to an icon, graphic, or text that links to another file or object. It is one of the most commonly used features for manipulating documents. This article will introduce how to programmatically find specific text and add hyperlinks to them in an existing PDF by using Free Spire.PDF for Java.

Install the Free API

Method 1: You can download the free API and unzip it. Then add the Spire.Pdf.jar file to your project as dependency.
Method 2: Or you can directly add the jar dependency to maven project by adding the following configurations to the pom.xml.

<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>http://repo.e-iceblue.com/nexus/content/groups/public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.pdf.free</artifactId>
        <version>4.4.1</version>
    </dependency>
</dependencies>

The detailed steps and complete sample code are as follows.

1. Create a PdfDocument instance and load a sample PDF document using PdfDocument.loadFromFile() method.
2. Get a specific page of the document using PdfDocument.getPages().get() method.
3. Find all matched text in the page using PdfPageBase.findText(String searchPatternText, boolean isSearchWholeWord) method, and return a PdfTextFindCollection object.
4. Create a PdfUriAnnotation instance based on the bounds of a specific find result.
5. Set a URL address for the annotation using PdfUriAnnotation.set(String value) method and set its border and color as well.
6. Add the URL annotation to the PDF annotation collection as a new annotation using PdfPageBase.getAnnotationWidget().add() method.
7. Save the document using PdfDocument.saveToFile() method.

import com.spire.pdf.*;
import com.spire.pdf.annotations.*;
import com.spire.pdf.general.find.*;
import com.spire.pdf.graphics.PdfRGBColor;
import java.awt.*;

public class SearchTextAndAddHyperlink {
    public static void main(String[] args) {
        //Create a PdfDocument instance
        PdfDocument pdf = new PdfDocument();

        //Load a sample PDF document
        pdf.loadFromFile("St. Nicholas.pdf");

        //Get the first page
        PdfPageBase page = pdf.getPages().get(0);

        // Find all matched strings and return a PdfTextFindCollection oject
        PdfTextFindCollection collection = page.findText("St. Nicholas", false);

        //loop through the find collection
        for(PdfTextFind find : collection.getFinds())
        {
            // Create a PdfUriAnnotation instance to add hyperlinks for the searched text
            PdfUriAnnotation uri = new PdfUriAnnotation(find.getBounds());
            uri.setUri("https://www.history.com/news/who-was-st-nicholas");
            uri.setBorder(new PdfAnnotationBorder(1f));
            uri.setColor(new PdfRGBColor(Color.blue));
            page.getAnnotationsWidget().add(uri);
        }

        //Save the document
        pdf.saveToFile("output/searchTextAndAddHyperlink.pdf");
    }
}

PDFlink.jpg