Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Created March 26, 2021 17:24
Show Gist options
  • Save aspose-com-gists/7cb73e8f2df1d8ad4a4cc6f98959bba2 to your computer and use it in GitHub Desktop.
Save aspose-com-gists/7cb73e8f2df1d8ad4a4cc6f98959bba2 to your computer and use it in GitHub Desktop.
Add or Remove PDF Annotation in Java
// Open the source PDF document
Document pdfDocument = new Document("input.pdf");
// Create annotation
TextAnnotation textAnnotation = new TextAnnotation(pdfDocument.getPages().get_Item(1), new com.aspose.pdf.Rectangle(200, 400, 400, 600));
// Set annotation title
textAnnotation.setTitle("Sample Annotation Title");
// Set annotation subject
textAnnotation.setSubject("Sample Subject");
textAnnotation.setState(AnnotationState.Accepted);
// Specify the annotation contents
textAnnotation.setContents("Sample contents for the annotation");
textAnnotation.setOpen(true);
textAnnotation.setIcon(TextIcon.Key);
Border border = new Border(textAnnotation);
border.setWidth(5);
border.setDash(new Dash(1, 1));
textAnnotation.setBorder(border);
textAnnotation.setRect(new com.aspose.pdf.Rectangle(200, 400, 400, 600));
// Add annotation in the annotations collection of the page
pdfDocument.getPages().get_Item(1).getAnnotations().add(textAnnotation);
// Save the output file
pdfDocument.save("output.pdf");
// Open source PDF document
Document pdfDocument = new Document("input.pdf");
// Delete all annotation
pdfDocument.getPages().get_Item(1).getAnnotations().delete();
// Save the update document
pdfDocument.save("output.pdf");
// Open the source PDF document
Document pdfDocument = new Document("input.pdf");
// Delete particular annotation
pdfDocument.getPages().get_Item(1).getAnnotations().delete(1);
// Save the update document
pdfDocument.save("output.pdf");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment