Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Annotate PDF using Java - Add or Remove Annotations of PDF files
// Add multiple annotations to PDF using Java
// Adding Arrow, Area, Oval (Ellipse), Distance annotations to PDF with messages and replies using Java
final Annotator annotator = new Annotator(Constants.INPUT);
// Setting Replies
Reply reply1 = new Reply();
reply1.setComment("Please look in to these issues.");
reply1.setRepliedOn(Calendar.getInstance().getTime());
Reply reply2 = new Reply();
reply2.setComment("Change Description");
reply2.setRepliedOn(Calendar.getInstance().getTime());
Reply reply3 = new Reply();
reply2.setComment("On-Premises APIs");
reply2.setRepliedOn(Calendar.getInstance().getTime());
Reply reply4 = new Reply();
reply2.setComment("Add images as well.");
reply2.setRepliedOn(Calendar.getInstance().getTime());
java.util.List<Reply> replies = new ArrayList<Reply>();
replies.add(reply1);
replies.add(reply2);
replies.add(reply3);
replies.add(reply4);
// Arrow Annotation =================================
ArrowAnnotation arrow = new ArrowAnnotation();
arrow.setBox(new Rectangle(560, 250, 60, -60));
arrow.setCreatedOn(Calendar.getInstance().getTime());
arrow.setMessage("This image is little upwards.");
arrow.setOpacity(0.7);
arrow.setPenColor(-3407872);
arrow.setPenWidth((byte) 2);
arrow.setReplies(replies.subList(0, 1));
// Area Annotation ===================================
AreaAnnotation area = new AreaAnnotation();
area.setBox(new Rectangle(50, 100, 500, 100));
area.setCreatedOn(Calendar.getInstance().getTime());
area.setMessage("Annotate documents and images.");
area.setOpacity(0.7);
area.setPenColor(-13076963);
area.setPenStyle(PenStyle.Dash);
area.setPenWidth((byte) 3);
area.setReplies(replies.subList(1, 2));
// Oval or Ellipse Annotation ========================
EllipseAnnotation ellipse = new EllipseAnnotation();
ellipse.setBox(new Rectangle(275, 505, 300, 80));
ellipse.setCreatedOn(Calendar.getInstance().getTime());
ellipse.setMessage("Shows all the available Annotation APIs.");
ellipse.setOpacity(0.7);
ellipse.setPenColor(-16034924);
ellipse.setPenStyle(PenStyle.Dot);
ellipse.setPenWidth((byte) 3);
ellipse.setReplies(replies.subList(2, 3));
// Distance Annotation ===============================
DistanceAnnotation distance = new DistanceAnnotation();
distance.setBox(new Rectangle(775, 235, 0, 150));
distance.setCreatedOn(Calendar.getInstance().getTime());
distance.setMessage("This is the heading area");
distance.setOpacity(0.7);
distance.setPenColor(-21197);
distance.setPenStyle(PenStyle.Solid);
distance.setPenWidth((byte) 1);
distance.setReplies(replies.subList(3, 4));
// Adding Annotations =================================
annotator.add(arrow);
annotator.add(area);
annotator.add(ellipse);
annotator.add(distance);
// Saving annotated PDF ===============================
annotator.save(outputPath);
annotator.dispose();
// Add Arrow annotation to PDF using Java
final Annotator annotator = new Annotator("document.pdf");
ArrowAnnotation arrow = new ArrowAnnotation();
arrow.setBox(new Rectangle(100, 100, 100, 100)); // (x, y, width, height)
annotator.add(arrow);
annotator.save("path/annotated-with-arrow.pdf");
// Distance Annotation using Java
final Annotator annotator = new Annotator("document.pdf");
// Distance Annotation
DistanceAnnotation distance = new DistanceAnnotation();
distance.setBox(new Rectangle(775, 235, 0, 150));
// add to document
annotator.add(area);
annotator.save("path/annotated-with-distance.pdf");
// Add Oval or Ellipse Annotation in PDF using Java
final Annotator annotator = new Annotator("document.pdf");
// Oval or Ellipse Annotation
EllipseAnnotation ellipse = new EllipseAnnotation();
ellipse.setBox(new Rectangle(275, 505, 300, 80));
// add to document
annotator.add(area);
annotator.save("path/annotated-with-ellipse.pdf");
// Add Area Annotation or Rectangle Annotation to PDF using Java
final Annotator annotator = new Annotator("document.pdf");
AreaAnnotation area = new AreaAnnotation();
area.setBox(new Rectangle(50, 100, 500, 100));
area.setCreatedOn(Calendar.getInstance().getTime());
area.setMessage("Annotate documents and images.");
area.setOpacity(0.7);
area.setPenColor(-13076963);
area.setPenStyle(PenStyle.Dash);
area.setPenWidth((byte) 3);
// add to document
annotator.add(area);
annotator.save("path/annotated-with-rectangle.pdf");
// Remove all the annotations from the PDF document using Java
final Annotator annotator = new Annotator("document.pdf");
SaveOptions saveOptions = new SaveOptions();
saveOptions.setAnnotationTypes(AnnotationType.None);
// Save the PDF with no more annotations in it.
annotator.save("path/annotations-removed.pdf", saveOptions);
annotator.dispose();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment