PDF Forms. Manage via Java

Manage Acroforms in PDF document using Aspose.PDF for Java Library

How to Manage PDF Forms Using Aspose.PDF for Java Library

To add interactive PDF forms (AcroForms), we’ll be working with Aspose.PDF for Java — a powerful, feature‑rich, and developer‑friendly library designed to make PDF manipulation effortless. You can grab the latest version directly from Maven and integrate it into your Maven project by adding the configuration below to your pom.xml.

Repository

<repository>
    <id>AsposeJavaAPI</id>
    <name>Aspose Java AP</name>
    <url>https://releases.aspose.com/java/repo/</url>
</repository>

Dependency

<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-pdf</artifactId>
<version>version of aspose-pdf API</version>
</dependency>

How to Create PDF Forms using Java

You need Aspose.PDF for Java to try the code in your environment.

  1. Load PDF in an instance of Document class.
  2. Access the Page via its index.
  3. Call the Form collection’s Add method.
  4. Create the form field you want to add.
  5. Save the PDF file.

Create PDF Forms in PDF - Java

This sample code shows how to Create PDF Forms in PDF using Java

String inputFile = DATA_DIR.resolve("sample.pdf").toString();
String outputFile = DATA_DIR.resolve("java-create-form.pdf").toString();
// Open document
try (Document pdfDocument = new Document(inputFile)) {
    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");

    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(outputFile);
}