Thursday, 11 November 2021

Java: Find Text and Add Hyperlinks for Them in PDF

In PDF, a hyperlink allows viewers to jump to another document or a website. In this article, I’ll show you how to automatically add hyperlinks to all matching text on a specific PDF page by using Java codes.

DEPENDENCY

I used a free third-party library called Free Spire.PDF for Java to finish the operation above. First of all, I’ll add the Spire.PDF.jar file as a dependency in Java program. The JAR file can be downloaded from this link, or directly add the following Maven configuration to 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.free</artifactId>
<version>3.9.0</version>
</dependency>
</dependencies>

USING THE CODE

Free Spire.PDF for Java will use the method provided by PdfTextFindCollection class to find all matched text

in a specific PDF page and set hyperlinks for them. Here are the detailed steps to follow.

l  Create a PdfDocument instance and load a sample PDF document using PdfDocument.loadFromFile() method.

l  Get a specific page of the document using PdfDocument.getPages().get() method.

l  Find all matched text in the page using PdfPageBase.findText(String searchPatternText, boolean isSearchWholeWord)

method, and return a PdfTextFindCollection object.

l  Create a PdfUriAnnotation instance based on the bounds of a specific find result.

l  Set a URL address for the annotation using PdfUriAnnotation.set(String value) method and set its border and color as well.

l  Add the URL annotation to the PDF annotation collection as a new annotation using PdfPageBase.getAnnotationWidget().add()

method.

l  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("C:\\Users\\Test1\\Desktop\\sample.pdf");

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

//Find all matched strings and return a PdfTextFindCollection object
PdfTextFindCollection collection = page.findText("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.oracle.com/index.html");
uri.setBorder(new PdfAnnotationBorder(1f));
uri.setColor(new PdfRGBColor(Color.blue));
page.getAnnotationsWidget().add(uri);
}

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

No comments:

Post a Comment

Change PDF Versions in Java

In daily work, you might need to change the version of a PDF document you have in order to ensure compatibility with another version which a...