How to Add Replacement Annotation in PDF using Java

In this tutorial, we will explore the utilization of a widely-used annotation library to add replacement annotation in PDF using Java. We will provide a comprehensive, step-by-step guide that covers the installation of the annotation library and includes a code example. The replacement annotation stands out among various annotation types as it allows users to replace specific content within a PDF with alternative text. Below are the detailed steps to insert replacement annotation to PDF in Java.

Steps to Add Replacement Annotation in PDF using Java

  1. Install GroupDocs.Annotation for Java using Maven repository to insert the replacement annotation into PDF
  2. Add the necessary classes for adding the replacement annotation
  3. Instantiate an object of the Annotator class by passing the PDF file path as an argument to its constructor
  4. Instantiate a ReplacementAnnotation object and set its properties according to your requirements, such as position and page number
  5. Call the Annotator.add method, passing the ReplacementAnnotation object as an argument
  6. Use the Annotator.save method to save the resulting PDF file to disk

By following the aforementioned steps, the task to create replacement annotation in PDF using Java becomes simple. These instructions are compatible with popular operating systems such as Linux, Windows, and macOS, as long as Java is installed. The advantage of annotation library is that there is no need of additional software installation. The code example below illustrates how to insert a replacement annotation into a PDF.

Code to Add Replacement Annotation 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.ReplacementAnnotation;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
public class AddReplacementAnnotationinPDFusingJava {
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");
// Create an instance of Reply class and add comments
Reply 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 ReplacementAnnotation class and set options
ReplacementAnnotation replacement = new ReplacementAnnotation();
replacement.setCreatedOn(Calendar.getInstance().getTime());
replacement.setFontColor(65535);
replacement.setMessage("This is replacement annotation");
replacement.setOpacity(0.7);
replacement.setPageNumber(0);
replacement.setPoints(points);
replacement.setReplies(replies);
replacement.setTextToReplace("replaced text");
// Add text replacement annotation into PDF
annotator.add(replacement);
// Save the final PDF to disk
annotator.save("result.pdf");
}
}

By following the guidelines outlined in this topic, the process of Java add replacement annotation becomes a straightforward task. After installing the document annotation library and customizing the input and output file paths to suit your requirements, integrating the provided code into your applications should be a seamless process. Congratulations! You have successfully implemented a replacement annotation in a PDF document using the Java programming language.

For a more comprehensive understanding of this topic, we suggest referring to our previously published article that provides a detailed explanation of inserting text redaction annotations in PDF files using Java. To delve deeper into this subject, we recommend consulting our comprehensive guide on how to add text redaction annotation in PDF using Java.

 English