PNG
JPG
BMP
TIFF
PDF
如何通过云 Python SDK 裁剪 PDF
要裁剪 PDF,我们将使用 Aspose.PDF Cloud Python SDK。该云 SDK 可帮助 Python 程序员使用 Python 编程语言通过 Aspose.PDF REST API 开发基于云的 PDF 创建、注释、编辑和转换应用程序。如果 python 包托管在 Github 上,您可以直接从 Github 安装:
从 Github 安装
pip install git+https://github.com/aspose-pdf-cloud/aspose-pdf-cloud-python.git
包管理器控制台命令
pip install asposepdfcloud
通过 Python SDK 裁剪 PDF 的步骤
Aspose.PDF Cloud 开发人员可以轻松地用几行代码加载和裁剪 PDF。
- 安装 Python SDK
- 将原始文件上传到 Aspose Cloud
- 获取页面元数据(可选,可能用于内部)
- 提取具有所需裁剪尺寸的页面作为图像
- 创建具有给定宽度和高度的新 PDF
- 将裁剪的页面(图像)插入新文档
- 下载并将文档保存到本地
使用 Python 裁剪 PDF
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_")
使用我们的 Python 库,您可以:
使用 Aspose.PDF Cloud Python SDK 裁剪 PDF 文档。
- 合并 PDF 文档。
- 拆分 PDF 文件。
- 将 PDF 转换为其他格式,反之亦然。
- 操作注释。
- 处理 PDF 中的图像等。
- 您可以尝试我们的 免费应用 来在线测试功能。