PNG JPG BMP TIFF PDF
Aspose.PDF  para Python

Adicionar Anotações de Texto Livre a PDF via SDK Python

API para adicionar anotações a documentos PDF usando o Cloud Python SDK.

Get Started

Como adicionar anotações a PDF via Cloud Python SDK

Para adicionar anotações em PDF, usaremos Aspose.PDF Cloud Python SDK. Este Cloud SDK auxilia programadores Python no desenvolvimento de aplicativos criadores, anotadores, editores e conversores de PDF baseados em nuvem usando a linguagem de programação Python via Aspose.PDF REST API. Simplesmente crie uma conta em Aspose for Cloud e obtenha as informações do seu aplicativo. Uma vez que você tenha o App SID & key, você está pronto para usar o Aspose.PDF Cloud Python SDK. Se o pacote Python estiver hospedado no Github, você pode instalar diretamente do Github:

Instalação a partir do Github


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

Comando do Console do Gerenciador de Pacotes

     
    pip install asposepdfcloud

Etapas para adicionar anotações a PDF via Python

Os desenvolvedores do Aspose.PDF Cloud podem facilmente carregar e adicionar anotações a PDF com apenas algumas linhas de código.

  1. Instale o Python SDK.
  2. Carregue o PDF no armazenamento em nuvem.
  3. Crie uma caixa de texto estilizada (com fontes, cores, alinhamento).
  4. Envie a anotação para a página especificada.
  5. Baixe o documento modificado para uso local.
 

Adicionar Anotações de Texto Livre a PDF usando Python


    from annotations_helper import Config, PdfAnnotationsHelper, logging
    from asposepdfcloud import ApiClient, PdfApi, FreeTextAnnotation, Rectangle, TextStyle, Color, FreeTextIntent, Justification, AnnotationFlags, HorizontalAlignment

    class PdfAddFreeTextAnnotations:
        """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_text_annotation(self):
            """Append a new free 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
                }

                text_style = TextStyle(
                    font_size=20,
                    font='Arial', 
                    foreground_color=Color(a=0xFF, r=0, g=0xFF, b=0),
                    background_color=Color(a=0xFF, r=0xFF, g=0, b=0)
                )

                new_annotation = FreeTextAnnotation(
                    rect = Rectangle(llx=100, lly=350, urx=450, ury=400),
                    text_style = text_style,
                    name = 'Free Text Annotation',
                    flags = [AnnotationFlags.DEFAULT],
                    horizontal_alignment = HorizontalAlignment.CENTER,
                    intent = FreeTextIntent.FREETEXTTYPEWRITER,
                    rich_text = Config.NEW_FT_ANNOTATION_TEXT,
                    subject = Config.NEW_FT_ANNOTATION_SUBJECT,
                    contents = Config.NEW_FT_ANNOTATION_CONTENTS,
                    title = Config.NEW_FT_ANNOTATION_DESCRIPTION,
                    z_index = 1,
                    justification = Justification.CENTER,
                )
                new_annotation.attribute_map["icon"] = "Icon"
                new_annotation.swagger_types["icon"] = "TextIcon"
                new_annotation.icon = "Help"

                try:
                    response = self.pdfApi.post_page_free_text_annotations(Config.PDF_DOCUMENT_NAME, Config.PAGE_NUMBER, [new_annotation], **args)
                    if response.code == 200:
                        logging.info(f"append_text_annotation(): annotation '{Config.NEW_FT_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_freetext_")
                    else:
                        logging.error(f"append_text_annotation(): Failed to add annotation to the document. Response code: {response.code}")
                except Exception as e:
                    logging.error(f"append_text_annotation(): Error while adding annotation: {e}")