How to Add Underline Annotation into PDF Using Java

The objective of this article is to walk you through the process on how to add underline annotation into PDF using Java, with the aid of a widely used annotation library. Annotations such as underlines, highlights, and comments can enhance the readability and comprehensibility of PDF documents. The primary emphasis of this article is on how to insert underline annotation in PDF using Java, and we will demonstrate this with a code example. Below are the essential steps to follow in accomplishing this task.

Steps to Add Underline Annotation into PDF Using Java

  1. Install GroupDocs.Annotation for Java into your project using the Maven repository
  2. Add the necessary classes for adding underline annotation in PDF
  3. Instantiate an Annotator class object by providing the PDF file path to its constructor
  4. Instantiate objects of the Point structure
  5. Create an object of UnderlineAnnotation class and set some properties e.g. position, page number, point, etc.
  6. Call Annotator.add method and pass UnderlineAnnotation object to it
  7. Call Annotator.save method to save the final output the PDF to disk

The aforementioned guidelines illustrate how to install the annotation library on your computer, allowing you to create underline annotation to PDF in Java. No supplementary software is required to use the annotation library for inserting underline annotations. These instructions are applicable to various popular operating systems like Windows, macOS, and Linux, provided that Java is installed. The following code example demonstrates the process of adding underline annotations to PDF files.

Code to Add Underline Annotation into 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.UnderlineAnnotation;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
public class AddUnderlineAnnotationintoPDFUsingJava {
public static void main(String[] args) {
// Set License to avoid the limitations of Annotation library
License license = new License();
license.setLicense("GroupDocs.Annotation.lic");
// Create an instance of Annotator class
Annotator annotator = new Annotator("input.pdf");
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 UnderlineAnnotation class and set options
UnderlineAnnotation underline = new UnderlineAnnotation();
underline.setCreatedOn(Calendar.getInstance().getTime());
underline.setFontColor(65535);
underline.setMessage("This is underline annotation");
underline.setOpacity(0.1);
underline.setPageNumber(0);
underline.setPoints(points);
// Add underline annotation to Annotator
annotator.add(underline);
// Save the final PDF to disk
annotator.save("result.pdf");
}
}

In the preceding section, we presented a comprehensive explanation of Java underline annotation in PDF, along with a simple code example. After installing the annotation library and making any necessary modifications to the input and output file paths, you can effortlessly integrate the code into your applications. Congratulations on successfully adding an underline annotation to a PDF file using Java!

We have previously covered the topic of adding image annotations to PDF files. If you require additional information, please refer to our guide on how to how to add image annotation in PDF using Java.

 English