通过 Java 创建 PDF 表单

使用适用于 Java 库的 Aspose.PDF 以编程方式在 PDF 中创建 acroforms

如何使用 Java 创建 PDF 表单

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

如何使用 Java 在 PDF 中创建 AcroForm


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

  1. 在 “文档” 类的实例中加载 PDF。
  2. 创建字段。
  3. 创建装饰品(如边框)。
  4. 在文档中添加字段并保存修改过的 PDF

在 PDF 中创建 PDF 表单-Java

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

// Open document
Document pdfDocument = new Document(_dataDir + "TextField.pdf");
Page page = pdfDocument.getPages().get_Item(1);
// Create a field
TextBoxField textBoxField = new TextBoxField(
    page, 
    new Rectangle(100, 200, 300, 300));
textBoxField.setPartialName("textbox1");
textBoxField.setValue("Text Box");

// TextBoxField.Border = new Border(
Border border = new Border(textBoxField);
border.setWidth(5);
border.setDash(new Dash(1, 1));
textBoxField.setBorder(border);

textBoxField.setColor(Color.getGreen());

// Add field to the document
pdfDocument.getForm().add(textBoxField, 1);

// Save modified PDF
pdfDocument.save(_dataDir + "TextBox_out.pdf");