You will discover how to use Annotation library to add reply to annotation in PDF using Java in this how-to tutorial. The tutorial also encompasses the configuration instructions for the annotation package in Java to support PDF-based annotation replies. You’ll swiftly learn how to reply annotation in PDF using Java. Following are the step-by-step instructions and an illustrative code snippet for creating PDF annotation reply.
Steps to Add Reply to Annotation in PDF using Java
- Install GroupDocs.Annotation for Java from the Maven repository
- Import the necessary classes to add reply to annotation
- Create object of Annotator class with input PDF path
- Instantiate User, AreaAnnotation, and Reply objects
- Assign User object to Reply object
- Assign Reply objects to Replies collection of AreaAnnotation
- Call Annotator.add method and pass annotation object to it
- Call Annotator.save method with resultant PDF path
The above comprehensive step can be followed at any operating system where Java is installed to annotate PDF with reply in Java. The process involves loading the input PDF using the Annotator class, setting the annotation attributes, and then saving the output PDF to disk. The following code snippet exemplifies how to insert replies into an annotation.
Code to Add Reply to Annotation in PDF using Java
import com.groupdocs.annotation.Annotator; | |
import com.groupdocs.annotation.licenses.License; | |
import com.groupdocs.annotation.models.*; | |
import com.groupdocs.annotation.models.annotationmodels.AreaAnnotation; | |
import java.util.ArrayList; | |
import java.util.Calendar; | |
public class AddReplytoAnnotationInPDFusingJava | |
{ | |
public static void main(String[] args) { | |
// Set License to avoid the limitations of Annotation library | |
License license = new License(); | |
license.setLicense("Conholdate.Annotator.lic"); | |
// Create an instance of Annotator class | |
Annotator annotator = new Annotator("input.pdf"); | |
try { | |
// Create an instance of User class and add data | |
User user1 = new User(); | |
user1.setId(1); | |
user1.setName("Tom"); | |
user1.setEmail("somemail@mail.com"); | |
User user2 = new User(); | |
user2.setId(2); | |
user2.setName("Jack"); | |
user2.setEmail("somebody@mail.com"); | |
User user3 = new User(); | |
user3.setId(3); | |
user3.setName("Mike"); | |
user3.setEmail("somemike@mail.com"); | |
// Create an instance of AreaAnnotation class and set options | |
AreaAnnotation area = new AreaAnnotation(); | |
area.setBackgroundColor(65535); | |
area.setBox(new Rectangle(100, 100, 100, 100)); | |
area.setCreatedOn(Calendar.getInstance().getTime()); | |
area.setMessage("This is area annotation"); | |
area.setOpacity(0.7); | |
area.setPageNumber(0); | |
area.setPenColor(65535); | |
area.setPenStyle(PenStyle.DOT); | |
area.setPenWidth((byte) 3); | |
// Create an instance of Reply class and add comments | |
Reply reply1 = new Reply(); | |
reply1.setId(1); | |
reply1.setComment("First comment"); | |
reply1.setRepliedOn(Calendar.getInstance().getTime()); | |
reply1.setUser(user1); | |
Reply reply2 = new Reply(); | |
reply2.setId(2); | |
reply2.setComment("Second comment"); | |
reply2.setRepliedOn(Calendar.getInstance().getTime()); | |
reply2.setUser(user2); | |
Reply reply3 = new Reply(); | |
reply3.setId(3); | |
reply3.setComment("Third comment"); | |
reply3.setRepliedOn(Calendar.getInstance().getTime()); | |
reply3.setUser(user1); | |
Reply reply4 = new Reply(); | |
reply4.setId(4); | |
reply4.setComment("Fourth comment"); | |
reply4.setRepliedOn(Calendar.getInstance().getTime()); | |
reply4.setUser(user2); | |
Reply reply5 = new Reply(); | |
reply5.setId(5); | |
reply5.setComment("Five comment"); | |
reply5.setRepliedOn(Calendar.getInstance().getTime()); | |
reply5.setUser(user3); | |
java.util.List<Reply> replies = new ArrayList<Reply>(); | |
replies.add(reply1); | |
replies.add(reply2); | |
replies.add(reply3); | |
replies.add(reply4); | |
replies.add(reply5); | |
area.setReplies(replies); | |
// Add annotation and save to file | |
annotator.add(area); | |
annotator.save("result.pdf"); | |
} finally { | |
if (annotator != null) { | |
annotator.dispose(); | |
} | |
} | |
} | |
} |
Annotations can be added to PDFs to provide feedback or comments on specific sections of the document. In some cases, it’s necessary to respond to an annotation to clarify a point or provide additional information. This is where reply annotations come in handy. The aforementioned code example shows how to insert reply to PDF annotation in Java by using some annotation library’s APIs.
Our most recent article on highlighting text in PDFs is available. For more details, see our guide on how to highlight text in PDF using Java.