HTML JPG PDF XML DOCX
  Product Family
PDF

在Python SDK中更新PDF表单

使用Python云SDK更新PDF文档中的所有表单字段

Get Started

如何通过Python SDK更新AcroForms

提供的Python代码旨在使用Aspose PDF Cloud API更新PDF文档中的表单字段。它导入必要的模块,读取本地PDF文件,将其上传到云端,然后更新PDF中的特定字段。函数updateFormFields封装了整个过程,处理文件操作和API交互。它还包括错误处理,以管理文件读取和API请求期间可能出现的问题。

为了更新PDF中的AcroForm,我们将使用 Aspose.PDF Cloud Python SDK。该云SDK帮助Python程序员使用Python语言通过Aspose.PDF REST API开发基于云的PDF创建、批注、编辑和转换应用。只需在Aspose for Cloud创建账户并获取应用信息。拥有App SID和密钥后,即可使用Aspose.PDF Cloud Python SDK。

从Github安装


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

Package Manager 控制台命令

     
    pip install asposepdfcloud

通过云Python更新AcroForms的步骤

Aspose.PDF Cloud 开发者可以轻松地在几行代码内加载并更新PDF中的AcroForms。

  1. 从本地路径读取PDF文件。

  2. 将PDF文件上传至Aspose.PDF Cloud存储。

  3. 使用"fieldName"参数创建一个新表单字段,并将其值设置为"aspose-pdf-cloud@example.com"。

  4. 更新Aspose.PDF Cloud存储中PDF文件的表单字段。

  5. 如有需要,下载结果

  6. 如有需要,下载结果

 

此示例代码展示了使用Python更新PDF文档中的AcroForms


import os
import logging
from pathlib import Path
from asposepdfcloud import PdfApi, Field, Fields, FieldType, Rectangle

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

class PdfAcroforms:
    def __init__():
        """Initialize the API client."""
        credentials_file = Path(r"..\credentials.json")
        with credentials_file.open("r", encoding="utf-8") as file:
            credentials = json.load(file)
        api_key, app_id = credentials.get("key"), credentials.get("id")
        self.pdf_api = PdfApi(ApiClient(api_key, app_id))

    def updateField(self, documentName: str, outputDocumentName: str, fieldName: str, localFolder: Path, remoteFolder: str):
        """ Upload a local fileName to the Aspose Cloud server. """
        file_path = localFolder / documentName
        self.pdf_api.upload_file(os.path.join(remoteFolder, fileName), file_path)

        field = Field(
            name=fieldName,
            type=FieldType.TEXT,
            values=["aspose-pdf-cloud@example.com"],
            rect= Rectangle( llx=125, lly=735, urx=200, ury=752),
        )
        
        fields = Fields(list=[field])

        try:
            response = self.pdf_api.put_update_fields(documentName, fields, folder=remoteFolder)
            if response.code == 200:
                logging.info(f"PdfAcroformsUpdate(): Form filed '{fieldName}' successfully updated in the document.")
                """Download the processed PDF document from the Aspose Cloud server."""
                temp_file = self.pdf_api.download_file(str(remoteFolder) + '/' + documentName)
		local_path = localFolder / outputDocumentName
                shutil.move(temp_file, str(local_path))
            else:
                logging.error(f"PdfAcroformsAdd(): Failed to add filed 'Email' to the page #1. Response code: {response.code}")
        except Exception as e:
            logging.error(f"PdfAcroforms(): Error while adding form field: {e}")
 

使用Python SDK操作AcroForms

在PDF中更新AcroForms涉及修改或增强现有的交互式表单字段。可以改善字段对齐、添加提示信息或调整标签顺序,使表单更直观、易于使用。还可添加复选框、下拉框或数字签名区域等附加字段,以满足新的数据收集需求。 更新AcroForms以符合可访问性标准,例如添加标签或提升与辅助技术的兼容性。更新AcroForms以确保与现代设备、浏览器或软件工具的兼容性。 使用Aspose.PDF Cloud Python SDK将AcroForms更新到PDF文档中。

使用我们的Python库,您可以

  • 以文本或图像格式添加PDF文档的页眉和页脚。
  • 向PDF文档添加表格和印章(文本或图像)。
  • 将多个PDF文档追加到已有文件中。
  • 处理PDF附件、批注和表单字段。
  • 对PDF文档进行加密或解密并设置密码。
  • 删除页面或整个PDF文档中的所有印章和表格。
  • 根据ID删除PDF文档中的特定印章或表格。
  • 替换PDF页面或整个文档中单个或多个文本实例。
  • 广泛支持将PDF文档转换为多种其他文件格式。
  • 提取PDF文件的各种元素并优化PDF文档。
  • 您可以试用我们的免费应用在线更新PDF文件中的AcroForms并测试功能。