Java: Find and Highlight Specific Text in PDF

If you want to emphasize some important words or phrases in a PDF document, you can highlight them with a bright color so that readers won't overlook them. In this article, we will introduce how to find and highlight specific text in PDF in Java using Spire.PDF for Java.

Install Spire.PDF for Java

First of all, you're required 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 in your application 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.3.4</version>
    </dependency>
</dependencies>
    

Find and Highlight Specific Text in PDF in Java

The following are the steps to find and highlight a specific text in a PDF document:

  • Create a PdfDocument instance.
  • Load a PDF document using PdfDocument.loadFromFile() method.
  • Create a PdfTextFindOptions instance.
  • Specify the text finding parameter using PdfTextFindOptions.setTextFindParameter() method.
  • Loop through the pages in the PDF document.
  • Within the loop, create a PdfTextFinder instance.
  • Call PdfTextFinder.find(String targetText, PdfTextFindOptions options) method to find a specific text in the document and save the results into a PdfTextFragment list.
  • Loop through the list and call PdfTextFragment.highLight(Color color) method to highlight all occurrences of the specific text with a color.
  • Save the result document using PdfDocument.saveToFile() method.
  • Java
import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;
import com.spire.pdf.texts.PdfTextFindOptions;
import com.spire.pdf.texts.PdfTextFinder;
import com.spire.pdf.texts.PdfTextFragment;
import com.spire.pdf.texts.TextFindParameter;

import java.awt.*;
import java.util.EnumSet;
import java.util.List;

public class HighlightTextInPdf {
    public static void main(String[] args){
        //Create a PdfDocument instance
        PdfDocument pdf = new PdfDocument();
        //Load a PDF file
        pdf.loadFromFile("Sample.pdf");

        //Create a PdfTextFindOptions instance
        PdfTextFindOptions findOptions = new PdfTextFindOptions();
        //Specify the text finding parameter
        findOptions.setTextFindParameter(EnumSet.of(TextFindParameter.WholeWord));

        //Loop through the pages in the PDF file
        for(PdfPageBase page : (Iterable) pdf.getPages())
        {
            //Create a PdfTextFinder instance
            PdfTextFinder finder = new PdfTextFinder(page);
            //Find a specific text
            List results = finder.find("Video", findOptions);
            //Highlight all occurrences of the specific text
            for (PdfTextFragment text : results)
            {
                text.highLight(Color.green);
            }
        }

        //Save the result file
        pdf.saveToFile("HighlightText.pdf");
    }
}

Java: Find and Highlight Specific Text 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.