PNG JPG BMP TIFF PDF
Aspose.PDF  for Python

Agregar anotaciones de texto resaltado a PDF a través de Python SDK

API para agregar anotaciones a documentos PDF usando Cloud Python SDK.

Get Started

Cómo agregar anotaciones a PDF a través de Cloud Python SDK

Para agregar anotaciones de texto resaltado a un PDF, usaremos Aspose.PDF Cloud Python SDK. Este SDK en la nube ayuda a los programadores de Python a desarrollar aplicaciones de creación, anotación, edición y conversión de PDF basadas en la nube usando el lenguaje de programación Python a través de Aspose.PDF REST API. Simplemente crea una cuenta en Aspose for Cloud y obtén la información de tu aplicación. Una vez que tengas el App SID y la clave, estarás listo para usar Aspose.PDF Cloud Python SDK. Si el paquete de Python está alojado en Github, puedes instalarlo directamente desde Github:

Instalación desde Github


     
    pip install git+https://github.com/aspose-pdf-cloud/aspose-pdf-cloud-python.git

Comando de la consola del administrador de paquetes

     
    pip install asposepdfcloud

Pasos para agregar anotaciones a PDF a través de Python

Los desarrolladores de Aspose.PDF Cloud pueden cargar y agregar anotaciones a un PDF fácilmente en solo unas pocas líneas de código.

  1. Instala Python SDK.
  2. Sube el PDF al almacenamiento en la nube.
  3. Crea una nueva anotación resaltada con posición, color, texto y estilo definidos.
  4. Envía la anotación a la página especificada usando la API de Aspose Cloud.
  5. Verifica la respuesta y registra el resultado.
  6. Descarga el archivo actualizado para su uso local.
 

Agregar anotaciones a PDF usando Python


    from annotations_helper import Config, PdfAnnotationsHelper, logging
    from asposepdfcloud import PdfApi, HighlightAnnotation, Rectangle, Color, Point, AnnotationFlags, HorizontalAlignment, VerticalAlignment

    class PdfAddHighlightAnnotations:
        """Class for managing PDF annotations using Aspose PDF Cloud API."""
        def __init__(self, pdf_api: PdfApi, helper: PdfAnnotationsHelper):
            self.pdfApi = pdf_api
            self.helper = helper

        def append_highlight_annotation(self):
            """Append a new highlight text annotation to the PDF document."""
            if self.pdfApi:
                self.helper.uploadFile(Config.PDF_DOCUMENT_NAME, Config.LOCAL_FOLDER, Config.REMOTE_FOLDER)

                args = {
                    "folder": Config.REMOTE_FOLDER
                }

                new_annotation = HighlightAnnotation(
                    rect = Rectangle(llx=100, lly=350, urx=450, ury=400),
                    name = 'Highlight_Text_Annotation',
                    flags = [AnnotationFlags.DEFAULT],
                    horizontal_alignment = HorizontalAlignment.LEFT,
                    vertical_alignment = VerticalAlignment.TOP,
                    rich_text = Config.NEW_HL_ANNOTATION_TEXT,
                    subject = Config.NEW_HL_ANNOTATION_SUBJECT,
                    contents= Config.NEW_HL_ANNOTATION_CONTENTS,
                    title = Config.NEW_HL_ANNOTATION_DESCRIPTION,
                    z_index = 1,
                    color=Color(a=0xFF, r=0, g=0xFF, b=0),
                    quad_points = [
                        Point(10, 10),
                        Point(20, 10),
                        Point(10, 20),
                        Point(10, 10)
                    ],
                    modified = '03/27/2025 00:00:00.000 AM',
                )
                try:
                    response = self.pdfApi.post_page_highlight_annotations(Config.PDF_DOCUMENT_NAME, Config.PAGE_NUMBER, [new_annotation], **args)
                    if response.code == 200:
                        logging.info(f"append_highlight_annotation(): annotation '{Config.NEW_HL_ANNOTATION_TEXT}' added to the document '{Config.PDF_DOCUMENT_NAME}'.")
                        self.helper.downloadFile(Config.PDF_DOCUMENT_NAME, Config.LOCAL_RESULT_DOCUMENT_NAME, Config.LOCAL_FOLDER, Config.REMOTE_FOLDER, "add_highlight_")
                    else:
                        logging.error(f"append_highlight_annotation(): Failed to add annotation to the document. Response code: {response.code}")
                except Exception as e:
                    logging.error(f"append_highlight_annotation(): Error while adding annotation: {e}")