There is no doubt that highlighting a text using a color in a PDF file is a great strategy for reading and retaining key information, which can help in bringing important information immediately to the reader's attention. In this article, I’ll introduce how to find specified text and then highlight it using Java codes with the help of a free third-party library called FreeSpire.PDF for Java.
DEPENDENCY
In order to finish the operation mentioned
above using Java codes, we need to create a development environment –
downloading and installing JDK 1.8.0 and Intellij IDEA 2019. Furthermore, get
the package of Free Spire.PDF for Java from the official website, and find
Spire.Pdf.jar in the “lib” folder. Finally, add the Jar file to IDEA.
Of course, you can directly add the Jar file by using the following Maven configurations.
<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
Actually, Free Spire.PDF for Java supports finding the specified text through two methods. One is
directly pointing out the content of text string, the other is defining a pattern and finding results that
match the pattern.
Find and Highlight Text by Text String
import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;
import com.spire.pdf.general.find.PdfTextFind;
import java.awt.*;
public class FindAndHighlightTextString {
public static void main(String[] args) {
//Load the PDF file
PdfDocument pdf = new PdfDocument();
pdf.loadFromFile("C:\\Users\\Test1\\Desktop\\Test.pdf");
PdfTextFind[] result;
//Find and highlight the string “New Zealand” in every PDF page
for (PdfPageBase page : (Iterable<PdfPageBase>) pdf.getPages()) {
result = page.findText("cashless society",false,false).getFinds();
for (PdfTextFind find : result) {
find.applyHighLight(Color.yellow);
}
}
//Save the file
pdf.saveToFile("output/FindAndHighlightText.pdf");
}
}Output
Find and Highlight Text by Regular Expressionimport com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;
import com.spire.pdf.general.find.PdfTextFind;
import java.awt.*;
public class FindByRegularExpression {
public static void main(String[] args) {
//Load a PDF document
PdfDocument pdf = new PdfDocument();
pdf.loadFromFile("C:\\Users\\Test1\\Desktop\\Test.pdf");
//Create an object of PdfTextFind collection
PdfTextFind[] results;
//Loop through the pages
for (Object page : (Iterable) pdf.getPages()) {
PdfPageBase pageBase = (PdfPageBase) page;
//Define a regular expression
String pattern = "\\#\\w+\\b";
//Find all results that match the pattern
results = pageBase.findText(pattern).getFinds();
//Highlight the search results with yellow
for (PdfTextFind find : results) {
find.applyHighLight(Color.yellow);
}
}
//Save to file
pdf.saveToFile("output/FindTextByRegularExpression.pdf");
}
}Output
No comments:
Post a Comment