PDF 表单。通过 Python 进行管理

PDF 表单。通过现代 Python via .NET 库使用自己的 API 管理 PDF 中的 Acroforms

最常用的動作

如何使用 Python via .NET 库管理 PDF 表单

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

Python Package Manager Console

pip install aspose-pdf

如何使用 Python 创建 PDF 表单


您需要 [Aspose.PDF Python](https://releases.aspose.com/pdf/net)來嘗試環境中的代碼。

  1. 在文件類的實例中載入 PDF。
  2. 通過主頁索引訪問主頁。 調用表單集合的 Add 方法。
  3. 建立要添加的表單欄位。
  4. 保存 PDF 檔。

在 PDF 中创建 PDF 表单-Python

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

    def add_text_box_field(self, infile, outfile):

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

            # Open document
            pdfDocument = Document(path_infile);

            # Create a field
            textBoxField = TextBoxField(pdfDocument.Pages[1], Rectangle(100, 200, 300, 300));
            textBoxField.PartialName = "textbox1";
            textBoxField.Value = "Text Box";

            border = Border(textBoxField);
            border.Width = 5;
            border.Dash = Dash(1, 1);
            textBoxField.Border = border;

            textBoxField.Color = Color.FromRgb(Color.Green);

            # Add field to the document
            pdfDocument.Form.Add(textBoxField, 1);

            # Save modified PDF
            pdfDocument.Save(path_outfile);