可通过 Python 填写的 PDF 表单

创建可填写的 PDF 表单。使用 Aspose.PDF for Python for .NET 以编程方式修改 PDF 文件

如何使用 Python for .NET 库填写 PDF 表单

为了在 PDF 文件中填写 PDF 表单 (Acroforms),我们将使用 Aspose.PDF for .NET API,这是适用于 python-net 平台的功能丰富、强大且易于使用的文档操作 API。你可以直接从 NuGet 包管理器下载其最新版本,搜索aspose.pdf然后安装。您也可以使用包管理器控制台中的以下命令。

如何使用 Python 在 PDF 中填写 AcroForm


你需要 Aspose.PDF for .NET 才能在你的环境中试用代码。

  1. 在 “文档” 类的实例中加载 PDF。
  2. 在 PDF 文件中创建字段。
  3. 向文档中添加字段。
  4. 保存修改过的 PDF

在 PDF 中填写 AcroForm-Python

此示例代码显示如何使用 Python 在 PDF 中填写 PDF 表单

import aspose.pdf as apdf

path_infile = self.dataDir + infile
path_outfile = self.dataDir + outfile

# Define the new field values
new_field_values = {
    "First Name": "Alexander_New",
    "Last Name": "Greenfield_New",
    "City": "Yellowtown_New",
    "Country": "Redland_New"
}

# Create a Form object from the input PDF file
form = apdf.facades.Form(path_infile)

# Fill out the form fields with the new values
for formField in form.field_names:
    if formField in new_field_values:
        form.fill_field(formField, new_field_values[formField])

# Save the modified form to the output PDF file
form.save(path_outfile)