Create PDF Forms via Java

Create acroforms in PDF programmatically using Aspose.PDF for Java Library

How to create PDF forms using Java

In order to create PDF Forms (Acroforms) in PDF file, we’ll use Aspose.PDF for Java API which is a feature-rich, powerful and easy to use document manipulation API for java platform. Open NuGet package manager, search for Aspose.PDF and install. You may also use the following command from the Package Manager Console.

How to Create AcroForm in PDF 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. Create a field.
  3. Create decorations (like Border).
  4. Add field to the document and save modified PDF

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