通过 Python 创建 PDF 表单

通过 Python 在文档中创建 PDF 表单。使用服务器端 API 构建您自己的 Python for .NET 应用程序。

如何使用Python創建 PDF 表單

为了在 PDF 文件中创建 PDF 表单 (Acroforms),我们将使用 Aspose.PDF for .NET API,这是一款功能丰富、强大且易于使用的适用于 C# 平台的文档操作 API。打开 NuGet 包管理器,搜索aspose.pdf然后安装。您也可以使用包管理器控制台中的以下命令。

如何使用 Python 在 PDF 中创建 AcroForm


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

  1. 在文件類的實例中載入 PDF。
  2. 建立欄位。
  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);