HTML JPG PDF XML DOCX
  Product Family
PDF

在 Python SDK 中移除 PDF 表单

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

Get Started

通过云 Python SDK 从 PDF 文档中移除 AcroForms

为了从 PDF 中删除 AcroForms,我们将使用 Aspose.PDF Cloud Python SDK。此云 SDK 帮助 Python 程序员通过 Aspose.PDF REST API 使用 Python 编程语言开发基于云的 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 文件上传至 Aspose.PDF Cloud 服务。
  2. 删除上传文件中指定的表单字段。
  3. 如有需要,下载结果
 

此示例代码展示了使用 Python 从 PDF 文档中移除 AcroForms


import os
import logging
from pathlib import Path
from asposepdfcloud import PdfApi

# 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 delField(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)
        try:
            response = self.pdf_api.delete_field(documentName, fieldName, folder=remoteFolder)
            if response.code == 200:
                logging.info(f"PdfAcroforms(): Form filed '{fieldName}' successfully deleted from docuemnt.")
                """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 这样的交互元素会增加 PDF 的文件大小。删除它们可以优化文档,以便更快地共享或存储。 使用 Aspose.PDF Cloud Python SDK 从 PDF 文档中移除 AcroForms。

使用我们的 Python 库,您可以

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