透過 Python 填寫的 PDF 表單

創建可填寫的 PDF 橫向形式。在 Python for .NET 使用 Aspose.PDF 以編程方式修改 PDF 文件

Aspose.PDF for Python for .NET Logo

如何使用 Python 資源庫填寫 PDF 表單

為了在 PDF 文件中填寫 PDF 表單(Acroforms),我們將使用 [Aspose.PDF for Python via .NET](https://products.aspose.com/pdf/python-net)API,該 API 是用於 Python 應用程序的功能豐富,功能強大且易於使用的文檔操作 API。您可以直接從 [PyPI](https://pypi.org/)軟件包管理器下載其最新版本,搜索 ** aspose-pdf** 並安裝。您也可以從控制台或終端使用下列命令。

如何使用 Python 在 PDF 中填寫 AcroForm


您需要 [Aspose.PDF for .NET](https://releases.aspose.com/pdf/net)才能在您的環境中嘗試代碼。

1.在文件類別的執行個體中載入 PDF。 1.在 PDF 檔案中建立欄位。 1.將欄位新增至文件。 1.儲存修改後的 PDF

在 PDF 中填寫 AcroForm-Python

此示例代碼顯示如何使用 Python 在 PDF 中填寫 PDF 表單

import aspose.pdf as apdf

from os import path

path_infile = path.join(self.data_dir, infile)
path_outfile = path.join(self.data_dir, 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)