Gérez les annotations au format PDF via Python

Gestion des annotations dans un document PDF. Utilisez Aspose.PDF pour Python for .NET pour modifier les fichiers PDF par programmation

Comment gérer les annotations à l'aide de la bibliothèque Python for .NET

Afin d’ajouter des annotations textuelles dans un fichier PDF, nous utiliserons l’API Aspose.PDF pour .NET qui est une API de manipulation de documents riche en fonctionnalités, puissante et facile à utiliser pour la plate-forme python-net. Ouvrez le gestionnaire de packages NuGet, recherchez Aspose.pdf et installez. Vous pouvez également utiliser la commande suivante depuis la console du gestionnaire de packages.

Console

pip install aspose-pdf

Créer des annotations dans un document PDF via Python


Vous avez besoin de Aspose.PDF pour Python via .NET pour essayer le code dans votre environnement.

  1. Chargez le PDF dans une instance de la classe Document.
  2. Créez une annotation que vous souhaitez ajouter au PDF.
  3. Ajoutez l’annotation à la collection Annotations de l’objet Page.
  4. Enregistrez le fichier PDF.

Annotation de texte PDF - 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)