PNG JPG BMP TIFF PDF
Aspose.PDF  para Python

Reemplazar anotaciones de texto en PDF a través de Python SDK

API para reemplazar anotaciones en documentos PDF usando Cloud Python SDK.

Get Started

Cómo reemplazar anotaciones de texto en PDF a través de Cloud Python SDK

Para reemplazar anotaciones de texto en PDF, utilizaremos Aspose.PDF Cloud Python SDK. Este Cloud SDK ayuda a los programadores de Python a desarrollar aplicaciones basadas en la nube para crear, anotar, editar y convertir PDFs usando el lenguaje de programación Python a través de Aspose.PDF REST API. Simplemente cree una cuenta en Aspose for Cloud y obtenga la información de su aplicación. Una vez que tenga el App SID y la clave, estará listo para utilizar Aspose.PDF Cloud Python SDK. Si el paquete de Python está alojado en Github, puede 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 gestor de paquetes

     
    pip install asposepdfcloud

Pasos para reemplazar anotaciones de texto en PDF a través de Python

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

  1. Instalar Python SDK.
  2. Obtener anotación usando get_text_annotation().
  3. Modificar el contenido de la anotación y el icono.
  4. Enviar la actualización.
  5. Validar la respuesta.
  6. Descargar el documento actualizado.
 

Reemplazar anotaciones de texto en PDF usando Python


    from annotations_helper import Config, PdfAnnotationsHelper, logging
    from asposepdfcloud import PdfApi, TextAnnotationResponse

    class PdfReplaceAnnotations:
        """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 _get_annotation(self, annotation_id):
            """Get annotation from the page in the PDF document."""
            if self.pdfApi:
                args = {
                    "folder": Config.REMOTE_FOLDER
                }
                response: TextAnnotationResponse = self.pdfApi.get_text_annotation(Config.PDF_DOCUMENT_NAME, annotation_id, **args)
                if response.code == 200:
                    logging.info(f"get_annotationn(): annotation '{annotation_id}' successfully found '{response.annotation.contents}' in the document '{Config.PDF_DOCUMENT_NAME}'.")
                    return response.annotation
                else:
                    logging.error(f"get_annotation(): Failed to get annotation in the document. Response code: {response.code}")
                    return None

        def modify_annotation(self):
            if self.pdfApi:
                if Config.ANNOTATION_ID:
                    self.helper.uploadFile(Config.PDF_DOCUMENT_NAME, Config.LOCAL_FOLDER, Config.REMOTE_FOLDER)
                    args = {
                        "folder": Config.REMOTE_FOLDER
                    }
                    annotation = self._get_annotation(Config.ANNOTATION_ID)
                    annotation.contents = Config.REPLACED_CONTENT
                    annotation.icon = "Star"
                    response = self.pdfApi.put_text_annotation(Config.PDF_DOCUMENT_NAME, Config.ANNOTATION_ID, annotation, **args)
                    if response.code == 200:
                        logging.info(f"modify_annotation(): annotation '{annotation.id}' successfully modified in the document '{Config.PDF_DOCUMENT_NAME}'.")
                        self.helper.downloadFile(Config.PDF_DOCUMENT_NAME, Config.LOCAL_RESULT_DOCUMENT_NAME, Config.LOCAL_FOLDER, Config.REMOTE_FOLDER, "replaced_annotatiom_")
                    else:
                        logging.error(f"modify_annotation(): Failed to modify annotation in the document. Response code: {response.code}")