PNG JPG BMP TIFF PDF
Aspose.PDF  para Python

Recortar PDF en Python SDK

Recortar documentos PDF usando Cloud Python SDK.

Get Started

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

Para recortar PDF, utilizaremos Aspose.PDF Cloud Python SDK. Este SDK en la nube ayuda a los programadores de Python a desarrollar aplicaciones basadas en la nube para crear, anotar, editar y convertir PDF usando el lenguaje de programación Python a través de Aspose.PDF REST API. 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 recortar PDF a través de Python SDK

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

  1. Instalar Python SDK
  2. Subir el archivo original a Aspose Cloud
  3. Obtener metadatos de la página (opcional, puede usarse internamente)
  4. Extraer la página como imagen con las dimensiones de recorte deseadas
  5. Crear un nuevo PDF con el ancho y la altura dados
  6. Insertar la página recortada (imagen) en el nuevo documento
  7. Descargar y guardar el documento localmente
 

Recortar PDF usando Python


    import os
    import logging
    from pathlib import Path
    from asposepdfcloud import PdfApi, AsposeResponse, DocumentResponse
    from change_layout_helper import PdfChangeLayoutHelper, Config

    class PdfCropPages:
        def __init__(self, pdf_api: PdfApi, helper: PdfChangeLayoutHelper):
            self.pdfApi = pdf_api
            self.helper = helper

        def cropDocumentPage(self, documentName: str, pageNumber: int, llx: int, lly: int, width: int, height: int, outputDocument: str, localFolder: str, tempFolder: str):
            self.helper.uploadFile(documentName, localFolder, tempFolder)

            self.helper.getPageInfo(documentName, pageNumber, tempFolder)

            imageFile = self.helper.extractPdfPage(documentName, pageNumber, Config.CROP_PAGE_WIDTH, Config.CROP_PAGE_HEIGHT, localFolder, tempFolder)
            newPdf: DocumentResponse = self.helper.createDocument(outputDocument, width, height, tempFolder)
            if newPdf.code != 200:
                logging.error("cropDocumentPage(): Failed to create new PDF document!")
            else:
                response: AsposeResponse = self.helper.insertPageAsImage(outputDocument, imageFile, llx, lly, tempFolder)
                if response.code != 200:
                    logging.error("cropDocumentPage(): Can't crop pdf document page!")
                else:
                    logging.info("cropDocumentPage(): Page successfully cropped.")
                    self.helper.downloadFile(outputDocument, outputDocument, localFolder, tempFolder, "cropped_")
 

Con nuestra biblioteca de Python, usted puede:

Recortar documentos PDF con Aspose.PDF Cloud Python SDK.

  • 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.