Manage Annotations in PDF using Python

Managing annotations in PDF document. Use Aspose.PDF for Python for .NET to modify PDF files programmatically

How to Manage Annotations Using Python for .NET Library

In order to add Text Annotation in PDF file, we’ll use Aspose.PDF for .NET API which is a feature-rich, powerful and easy to use document manipulation API for python-net platform. Open NuGet package manager, search for Aspose.PDF and install. You may also use the following command from the Package Manager Console.

Console

pip install aspose-pdf

Create Annotations in PDF document via Python


You need Aspose.PDF for Python via .NET to try the code in your environment.

  1. Load PDF in an instance of Document class.
  2. Create an annotation that you want to add to the PDF.
  3. Add the annotation to the Page object’s Annotations collection.
  4. Save the PDF file.

PDF Text Annotation - Python

Example: Python

import aspose.pdf as apdf

from os import path

path_infile = path.join(self.data_dir, infile)
path_outfile = path.join(self.data_dir, outfile)

document = apdf.Document(path_infile)

appearance = apdf.annotations.DefaultAppearance()
appearance.font_size = 12
appearance.font_name = "Arial"

freeTextAnnotation = apdf.annotations.FreeTextAnnotation(
    document.pages[1],
    apdf.Rectangle(299.988, 703.664, 508.708, 720.769, True),
    appearance
)
freeTextAnnotation.contents = "This is a free text annotation."
freeTextAnnotation.name = "FreeText1"
freeTextAnnotation.subject = "Revision 01"
freeTextAnnotation.title = "Free Text Annotation"
freeTextAnnotation.popup = apdf.annotations.PopupAnnotation(
    document.pages[1], apdf.Rectangle(299.988, 713.664, 308.708, 720.769, True)
)
freeTextAnnotation.popup.open = True
document.pages[1].annotations.add(freeTextAnnotation, consider_rotation=False)
document.save(path_outfile)