The purpose of this tutorial is to walk you through all steps to highlight text in PDF using Java, utilizing a simple code example. Furthermore, it will provide you with guidance on how to incorporate an annotation library into your project to highlight text using Java in PDF. GroupDocs Annotation is a robust library that enables you to annotate and collaborate on PDF documents in real-time. Follow the steps below to learn how to highlight the text in PDF using it.
Steps to Highlight Text in PDF using Java
- Utilize the Maven repository to download and integrate GroupDocs.Annotation for Java into your project to highlight text within PDF document
- Import the relevant classes for highlighting text in PDF
- Create an object of the Annotator class with input PDF path
- Instantiate an object of the HighlightAnnotation class and define its properties
- Call Annotator.add method and pass HighlightAnnotation object to it
- Call Annotator.save method to save the final PDF to disk
After following these steps, you will be capable of successfully highlighting text in a PDF document. You can employ the above mentioned steps to highlight text of PDF in Java on any machine that supports Java, without requiring any additional software installation. The following code example illustrates how to highlight text within a PDF document.
Code to Highlight Text in PDF using Java
import com.groupdocs.annotation.Annotator; | |
import com.groupdocs.annotation.licenses.License; | |
import com.groupdocs.annotation.models.Point; | |
import com.groupdocs.annotation.models.Reply; | |
import com.groupdocs.annotation.models.annotationmodels.HighlightAnnotation; | |
import java.util.ArrayList; | |
import java.util.Calendar; | |
import java.util.List; | |
public class HighlightTextinPDFusingJava | |
{ | |
public static void main(String[] args) { | |
// Set License to avoid the limitations of Annotation library | |
License license = new License(); | |
license.setLicense("Conholdate.Annotator.lic"); | |
Annotator annotator = new Annotator("input.pdf"); | |
try { | |
// Create an instance of Reply class and add comments | |
Reply reply1; | |
reply1 = new Reply(); | |
reply1.setComment("First comment"); | |
reply1.setRepliedOn(Calendar.getInstance().getTime()); | |
Reply reply2 = new Reply(); | |
reply2.setComment("Second comment"); | |
reply2.setRepliedOn(Calendar.getInstance().getTime()); | |
java.util.List<Reply> replies = new ArrayList<Reply>(); | |
replies.add(reply1); | |
replies.add(reply2); | |
Point point1 = new Point(80, 730); | |
Point point2 = new Point(240, 730); | |
Point point3 = new Point(80, 650); | |
Point point4 = new Point(240, 650); | |
List<Point> points = new ArrayList<Point>(); | |
points.add(point1); | |
points.add(point2); | |
points.add(point3); | |
points.add(point4); | |
// Create an instance of HighlightAnnotation class and set options | |
HighlightAnnotation highlight = new HighlightAnnotation(); | |
highlight.setBackgroundColor(65535); | |
highlight.setCreatedOn(Calendar.getInstance().getTime()); | |
highlight.setFontColor(0); | |
highlight.setMessage("This is highlight annotation"); | |
highlight.setOpacity(0.5); | |
highlight.setPageNumber(0); | |
highlight.setPoints(points); | |
highlight.setReplies(replies); | |
// Add annotation and save to file | |
annotator.add(highlight); | |
// Save the final output PDF to disk | |
annotator.save("result.pdf"); | |
} finally { | |
if (annotator != null) { | |
annotator.dispose(); | |
} | |
} | |
} | |
} |
In the preceding section, we provided a guide for the process of highlighting text in PDF using Java, along with a simple code example. The code is brief and only necessitates a few API calls to execute text highlighting in PDF. After installing the recommended annotation library and configuring the file paths, incorporating this code into your projects is uncomplicated.
We have previously published an article on adding a watermark to a PDF document. For additional information, refer to our guide on how to add a watermark annotation using Java.