PNG JPG BMP TIFF PDF
Aspose.PDF  for Python

Agregar enlaces a PDF en Python SDK

Agregar un enlace a un documento PDF usando Cloud Python SDK.

Get Started

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

Para agregar enlaces a un PDF, usaremos 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 usar 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 administrador de paquetes

     
    pip install asposepdfcloud

Pasos para agregar enlaces a un PDF a través de Python SDK

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

  1. Instalar Python SDK
  2. Subir un documento PDF al servidor de Aspose Cloud
  3. Descargar el documento PDF procesado desde el servidor de Aspose Cloud
  4. Añadir una nueva anotación de hipervínculo a una página específica en el documento PDF
 

Agregar enlaces a PDF usando Python


    import shutil
    import json
    import logging
    from pathlib import Path
    from asposepdfcloud import ApiClient, PdfApi, LinkAnnotation, LinkActionType, LinkHighlightingMode, Color, Link, Rectangle

    # Configure logging
    logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")


    class Config:
        """Configuration parameters."""
        CREDENTIALS_FILE = Path(r"C:\\Projects\\ASPOSE\\Pdf.Cloud\\Credentials\\credentials.json")
        LOCAL_FOLDER = Path(r"C:\Samples")
        PDF_DOCUMENT_NAME = "sample.pdf"
        LOCAL_RESULT_DOCUMENT_NAME = "output_sample.pdf"
        NEW_LINK_ACTION = "https://reference.aspose.cloud/pdf/"
        PAGE_NUMBER = 2
        LINK_RECT = Rectangle(llx=244.914, lly=488.622, urx=284.776, ury=498.588)


    class PdfLinks:
        """Class for managing PDF links using Aspose PDF Cloud API."""
        def __init__(self, credentials_file: Path = Config.CREDENTIALS_FILE):
            self.pdf_api = None
            self._init_api(credentials_file)

        def _init_api(self, credentials_file: Path):
            """Initialize the API client."""
            try:
                with credentials_file.open("r", encoding="utf-8") as file:
                    credentials = json.load(file)
                    api_key, app_id = credentials.get("key"), credentials.get("id")
                    if not api_key or not app_id:
                        raise ValueError("init_api(): Error: Missing API keys in the credentials file.")
                    self.pdf_api = PdfApi(ApiClient(api_key, app_id))
            except (FileNotFoundError, json.JSONDecodeError, ValueError) as e:
                logging.error(f"init_api(): Failed to load credentials: {e}")

        def upload_document(self):
            """Upload a PDF document to the Aspose Cloud server."""
            if self.pdf_api:
                file_path = Config.LOCAL_FOLDER / Config.PDF_DOCUMENT_NAME
                try:
                    self.pdf_api.upload_file(Config.PDF_DOCUMENT_NAME, str(file_path))
                    logging.info(f"upload_document(): File {Config.PDF_DOCUMENT_NAME} uploaded successfully.")
                except Exception as e:
                    logging.error(f"upload_document(): Failed to upload file: {e}")

        def download_result(self):
            """Download the processed PDF document from the Aspose Cloud server."""
            if self.pdf_api:
                try:
                    temp_file = self.pdf_api.download_file(Config.PDF_DOCUMENT_NAME)
                    local_path = Config.LOCAL_FOLDER / Config.LOCAL_RESULT_DOCUMENT_NAME
                    shutil.move(temp_file, str(local_path))
                    logging.info(f"download_result(): File successfully downloaded: {local_path}")
                except Exception as e:
                    logging.error(f"download_result(): Failed to download file: {e}")

        def append_link(self):
            """Append a new hyperlink annotation to a specific page in the PDF document."""
            if self.pdf_api:
                link_annotation = LinkAnnotation(
                    links=[Link(href=Config.NEW_LINK_ACTION)],
                    action_type= LinkActionType.GOTOURIACTION,
                    action=Config.NEW_LINK_ACTION,
                    highlighting=LinkHighlightingMode.INVERT,
                    color=Color(a=255, r=0, g=255, b=0),
                    rect=Config.LINK_RECT,
                )

                try:
                    response = self.pdf_api.post_page_link_annotations(
                        Config.PDF_DOCUMENT_NAME, Config.PAGE_NUMBER, [link_annotation]
                    )
                    if response.code == 200:
                        logging.info(f"append_link(): Link '{Config.NEW_LINK_ACTION}' added to page #{Config.PAGE_NUMBER}.")
                    else:
                        logging.error(f"append_link(): Failed to add link to the page. Response code: {response.code}")
                except Exception as e:
                    logging.error(f"append_link(): Error while adding link: {e}")


    if __name__ == "__main__":
        pdf_links = PdfLinks()
        pdf_links.upload_document()
        pdf_links.append_link()
        pdf_links.download_result()
 

Trabajar con enlaces en PDF

Agregar enlaces a un PDF mejora la usabilidad, interactividad y accesibilidad. Ya sea para navegación, marketing o referencias cruzadas, los hipervínculos mejoran la efectividad del documento, facilitando a los usuarios encontrar contenido relevante y tomar acción. Agregue los enlaces en documentos PDF con Aspose.PDF Cloud Python SDK.

Con nuestra biblioteca de Python puedes:

  • Combinar documentos PDF.
  • Dividir archivos PDF.
  • Convertir PDF a otros formatos, y viceversa.
  • Manipular anotaciones.
  • Trabajar con imágenes en PDF, etc.
  • Puede probar nuestra aplicación gratuita para probar la funcionalidad en línea.