Java: Find Text and Add Hyperlinks for Them in PDF

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. Spire.PDF for Java supports creating a new PDF document and adding various hyperlinks to it, including ordinary links, hypertext links, email links and document links. This article will show you how to add hyperlinks to specific text in an existing PDF.

Install Spire.PDF for Java

First of all, you need to add the Spire.Pdf.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file by adding the following code to your project's pom.xml file.

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

Find Text and Add Hyperlinks for Them in PDF

With Spire PDF for Java, you can find all matched text in a specific PDF page and add hyperlinks to them. Here are the detailed steps to follow.

  • Create a PdfDocument instance and load a sample PDF document using PdfDocument.loadFromFile()method.
  • Get a specific page of the document using PdfDocument.getPages().get() method.
  • Find all matched text in the page using PdfPageBase.findText(String searchPatternText, boolean isSearchWholeWord) method, and return a PdfTextFindCollection object.
  • Create a PdfUriAnnotation instance based on the bounds of a specific find result.
  • Set a URL address for the annotation using PdfUriAnnotation.set(String value) method and set its border and color as well.
  • Add the URL annotation to the PDF annotation collection as a new annotation using PdfPageBase.getAnnotationWidget().add() method.
  • Save the document using PdfDocument.saveToFile() method.
  • Java
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("C:\\Users\\Test1\\Desktop\\sample.pdf");

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

        // Find all matched strings and return a PdfTextFindCollection oject
        PdfTextFindCollection collection = page.findText("Spire.PDF for Java", 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.e-iceblue.com/Introduce/pdf-for-java.html");
            uri.setBorder(new PdfAnnotationBorder(1f));
            uri.setColor(new PdfRGBColor(Color.blue));
            page.getAnnotationsWidget().add(uri);
        }

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

Java: Find Text and Add Hyperlinks for Them in PDF

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.