Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Created July 8, 2026 06:49
Show Gist options
  • Select an option

  • Save aspose-com-gists/cc27aac82b9208d1f960d04f04767c68 to your computer and use it in GitHub Desktop.

Select an option

Save aspose-com-gists/cc27aac82b9208d1f960d04f04767c68 to your computer and use it in GitHub Desktop.
Add or Remove Annotations in Python

Add or Remove Annotations in Python

Discover how Python developers can add and remove PDF annotations with Aspose.PDF for Python via .NET. The guide covers SDK setup, handling various annotation types, performance for large PDFs, and best‑practice tips for annotation management.

Read the full guide here: https://blog.aspose.com/pdf/add-or-remove-annotations-in-python/

import aspose.pdf as ap
# Load an existing PDF
pdf_path = "sample.pdf"
doc = ap.Document(pdf_path)
# -------------------------------------------------
# Add a Text Annotation (Sticky Note)
# -------------------------------------------------
text_annot = ap.annotations.TextAnnotation(doc.pages[1])
text_annot.rect = ap.Rectangle(100, 600, 200, 650) # Position on the page
text_annot.contents = "Review this section"
text_annot.title = "Reviewer"
doc.pages[1].annotations.add(text_annot)
# Save the PDF with the new annotation
doc.save("with_annotation.pdf")
# -------------------------------------------------
# Remove the previously added annotation
# -------------------------------------------------
# Reload the document to demonstrate removal
doc2 = ap.Document("with_annotation.pdf")
# Assume we know the annotation's title or we iterate to find it
for annot in list(doc2.pages[1].annotations):
if isinstance(annot, ap.annotations.TextAnnotation) and annot.title == "Reviewer":
doc2.pages[1].annotations.delete(annot)
# Save the cleaned PDF
doc2.save("without_annotation.pdf")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment