PDF Forms. Manage via Java

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

How to Manage PDF Forms Using Java Library

In order to add PDF Forms (Acroforms), we’ll use Aspose.PDF for Java API which is a feature-rich, powerful and easy to use conversion API for Java platform. You can download its latest version directly from Maven and install it within your Maven-based project by adding the following configurations to the 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

// 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");