Java adds form fields to PDF

PDF form fields, also known as interactive fields, are primarily used to collect user information. Common form fields include radio buttons, checkboxes, list boxes, and combo boxes. This text will show you how to use Free Spire.PDF for Java to create PDF form fields in your Java program.

** Import JAR package ** ** Method 1: ** Download Free Spire.PDF for Java, unzip it, and in the lib folder Import the jar package as a dependency directly into your Java application.

** Method 2: ** Install the jar package from the Maven repository and configure the code in the pom.xml file as follows:

<repositories>
   <repository>
      <id>com.e-iceblue</id>
      <name>e-iceblue</name>
      <url>http://repo.e-iceblue.com/nexus/content/groups/public/</url>
   </repository>
</repositories>
<dependencies>
   <dependency>
      <groupId>e-iceblue</groupId>
      <artifactId>spire.pdf.free</artifactId>
      <version>2.6.3</version>
   </dependency>
</dependencies>

** Java code **

import java.awt.*;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;

import com.spire.pdf.FileFormat;
import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;
import com.spire.pdf.fields.*;
import com.spire.pdf.graphics.*;

public class AddFormFields {
    public static void main(String[] args) throws Exception {

        //Create a PdfDocument object
        PdfDocument doc = new PdfDocument();

        //Add page
        PdfPageBase page = doc.getPages().add();

        //Initialize position variables
        float baseX = 100;
        float baseY = 0;

        //Create a brush object
        PdfSolidBrush brush1 = new PdfSolidBrush(new PdfRGBColor(Color.BLUE));
        PdfSolidBrush brush2 = new PdfSolidBrush(new PdfRGBColor(Color.black));

        //Create TrueType fonts
        PdfTrueTypeFont font= new PdfTrueTypeFont(new Font("MS Mincho",Font.PLAIN,12),true);

        //Add checkbox
        page.getCanvas().drawString("Checkbox:", font, brush1, new Point2D.Float(0, baseY));
        java.awt.geom.Rectangle2D.Float rec1 = new java.awt.geom.Rectangle2D.Float(baseX, baseY, 15, 15);
        PdfCheckBoxField checkBoxField = new PdfCheckBoxField(page, "CheckBox1");
        checkBoxField.setBounds(rec1);
        checkBoxField.setChecked(false);
        page.getCanvas().drawString("Option 2", font, brush2, new Point2D.Float(baseX + 20, baseY));
        java.awt.geom.Rectangle2D.Float rec2 = new java.awt.geom.Rectangle2D.Float(baseX + 70, baseY, 15, 15);
        PdfCheckBoxField checkBoxField1 = new PdfCheckBoxField(page, "CheckBox2");
        checkBoxField1.setBounds(rec2);
        checkBoxField1.setChecked(false);
        page.getCanvas().drawString("Option 2", font,  brush2, new Point2D.Float(baseX+90, baseY));
        doc.getForm().getFields().add(checkBoxField);
        baseY += 25;

        //Add list box
        page.getCanvas().drawString("list box:", font, brush1, new Point2D.Float(0, baseY));
        java.awt.geom.Rectangle2D.Float rec = new java.awt.geom.Rectangle2D.Float(baseX, baseY, 150, 50);
        PdfListBoxField listBoxField = new PdfListBoxField(page, "ListBox");
        listBoxField.getItems().add(new PdfListFieldItem("Item 1", "item1"));
        listBoxField.getItems().add(new PdfListFieldItem("Item 2", "item2"));
        listBoxField.getItems().add(new PdfListFieldItem("Item 3", "item3"));;
        listBoxField.setBounds(rec);
        listBoxField.setFont(font);
        listBoxField.setSelectedIndex(0);
        doc.getForm().getFields().add(listBoxField);
        baseY += 60;

        //Add radio button
        page.getCanvas().drawString("Radio buttons:", font, brush1, new Point2D.Float(0, baseY));
        PdfRadioButtonListField radioButtonListField = new PdfRadioButtonListField(page, "Radio");
        PdfRadioButtonListItem radioItem1 = new PdfRadioButtonListItem("Item1");
        radioItem1.setBounds(new Rectangle2D.Float(baseX, baseY, 15, 15));
        page.getCanvas().drawString("Option 1", font, brush2, new Point2D.Float(baseX + 20, baseY));
        PdfRadioButtonListItem radioItem2 = new PdfRadioButtonListItem("Item2");
        radioItem2.setBounds(new Rectangle2D.Float(baseX + 70, baseY, 15, 15));
        page.getCanvas().drawString("Option 2", font, brush2, new Point2D.Float(baseX + 90, baseY));
        radioButtonListField.getItems().add(radioItem1);radioButtonListField.getItems().add(radioItem2);
        radioButtonListField.setSelectedIndex(0);
        doc.getForm().getFields().add(radioButtonListField);
        baseY += 25;

        //Add combo box
        page.getCanvas().drawString("combo box:", font, brush1, new Point2D.Float(0, baseY));
        Rectangle2D.Float cmbBounds = new Rectangle2D.Float(baseX, baseY, 150, 15);
        PdfComboBoxField comboBoxField = new PdfComboBoxField(page, "ComboBox");
        comboBoxField.setBounds(cmbBounds);
        comboBoxField.getItems().add(new PdfListFieldItem("Item 1", "item1"));
        comboBoxField.getItems().add(new PdfListFieldItem("Item 2", "itme2"));
        comboBoxField.getItems().add(new PdfListFieldItem("Item 3", "item3"));
        comboBoxField.getItems().add(new PdfListFieldItem("Item 4", "item4"));
        comboBoxField.setSelectedIndex(0);
        comboBoxField.setFont(font);
        doc.getForm().getFields().add(comboBoxField);
        baseY += 25;

        //Add signature field
        page.getCanvas().drawString("Signature domain:", font, brush1, new Point2D.Float(0, baseY));
        PdfSignatureField sgnField= new PdfSignatureField(page,"sgnField");
        Rectangle2D.Float sgnBounds = new Rectangle2D.Float(baseX, baseY, 150, 80);
        sgnField.setBounds(sgnBounds);
        doc.getForm().getFields().add(sgnField);
        baseY += 90;

        //Add button
        page.getCanvas().drawString("Submit button:", font, brush1, new Point2D.Float(0, baseY));
        Rectangle2D.Float btnBounds = new Rectangle2D.Float(baseX, baseY, 50, 15);
        PdfButtonField buttonField = new PdfButtonField(page, "Button");
        buttonField.setBounds(btnBounds);buttonField.setText("send");
        buttonField.setFont(font);
        doc.getForm().getFields().add(buttonField);

        //Save the document
        doc.saveToFile("AddFormField.pdf", FileFormat.PDF);
    }
}

p

Recommended Posts

Java adds form fields to PDF
Java adds table to PDF
Java adds page numbers to existing PDF documents
Java to extract PDF text content
Java adds SmartArt graphics to PowerPoint
Java adds hyperlinks to Word documents
Add watermark to Java to PDF document
[Java] Introduction to Java
Introduction to java
How to hide null fields in response in Java
Java adds a text box to PowerPoint slides
How to access Java Private methods and fields
[Java] Convert PDF version
Changes from Java 8 to Java 11
Sum from Java_1 to 100
Java compressed PDF document
[Java] Connect to MySQL
Kotlin's improvements to Java
[Java] PDF viewing settings
Java applications convert Word (DOC / DOCX) documents to PDF
From Java to Ruby !!
Introduction to java command
How to simulate uploading a post-object form to OSS in Java
Challenge to create inquiry form
[Java] How to use Map
How to lower java version
Migration from Cobol to JAVA
[Java] How to use Map
Convert Java Powerpoint to XPS
How to uninstall Java 8 (Mac)
Java to play with Function
Save Java PDF in Excel
Java --How to make JTable
How to use java Optional
New features from Java7 to Java8
How to minimize Java images
How to write java comments
[Java] How to use Optional ②
Connect from Java to PostgreSQL
[Java] How to use removeAll ()
Java turns Excel into PDF
[Java] Introduction to lambda expressions
[Java] How to use string.format
Shell to kill Java process
How to use Java Map
Connect to DB with Java
Connect to MySQL 8 with Java
[Java] Introduction to Stream API
Java8 to start now ~ Optional ~
How to convert Java radix
[Java] Convert ArrayList to array
Java thread to understand loosely
[Java] How to implement multithreading
[Java] How to use Optional ①
From Ineffective Java to Effective Java
How to initialize Java array
Selenium Sample Reservation Form (Java)
[Introduction to rock-paper-scissors games] Java
Input to the Java console
I tried to move the Java compatible FaaS form "Fn Project"