Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Creating Interactive PDF Forms with Java and Free Spire.PDF

Tech May 9 3

PDF form fields enable user interaction within documents, allowing data collection through text entry, selection, and digital signatures. Common field types include text boxes, radio buttons, checkboxes, list boxes, and combo boxes. The Free Spire.PDF for Java library provides comprehensive functionality for programmatically generating these interactive elements.

Dependency Configuration

Add the Free Spire.PDF library to your Maven project by including this configuration in your pom.xml:

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

Implementation Code

import java.awt.*;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import com.spire.pdf.*;
import com.spire.pdf.fields.*;
import com.spire.pdf.graphics.*;

public class PDFFormGenerator {
    public static void main(String[] args) throws Exception {
        PdfDocument pdfDoc = new PdfDocument();
        PdfPageBase currentPage = pdfDoc.getPages().add();
        
        float startX = 100;
        float currentY = 0;
        
        PdfSolidBrush blueBrush = new PdfSolidBrush(new PdfRGBColor(Color.BLUE));
        PdfSolidBrush blackBrush = new PdfSolidBrush(new PdfRGBColor(Color.BLACK));
        PdfTrueTypeFont formFont = new PdfTrueTypeFont(new Font("Arial", Font.PLAIN, 12), true);
        
        // Text Field
        currentPage.getCanvas().drawString("Text Field:", formFont, blueBrush, new Point2D.Float(0, currentY));
        Rectangle2D.Float textFieldArea = new Rectangle2D.Float(startX, currentY, 150, 15);
        PdfTextBoxField inputField = new PdfTextBoxField(currentPage, "textInput");
        inputField.setBounds(textFieldArea);
        inputField.setText("Sample text");
        inputField.setFont(formFont);
        pdfDoc.getForm().getFields().add(inputField);
        currentY += 25;
        
        // Checkboxes
        currentPage.getCanvas().drawString("Checkboxes:", formFont, blueBrush, new Point2D.Float(0, currentY));
        Rectangle2D.Float cbArea1 = new Rectangle2D.Float(startX, currentY, 15, 15);
        PdfCheckBoxField checkbox1 = new PdfCheckBoxField(currentPage, "optionA");
        checkbox1.setBounds(cbArea1);
        checkbox1.setChecked(false);
        currentPage.getCanvas().drawString("Choice A", formFont, blackBrush, new Point2D.Float(startX + 20, currentY));
        
        Rectangle2D.Float cbArea2 = new Rectangle2D.Float(startX + 70, currentY, 15, 15);
        PdfCheckBoxField checkbox2 = new PdfCheckBoxField(currentPage, "optionB");
        checkbox2.setBounds(cbArea2);
        checkbox2.setChecked(false);
        currentPage.getCanvas().drawString("Choice B", formFont, blackBrush, new Point2D.Float(startX + 90, currentY));
        pdfDoc.getForm().getFields().add(checkbox1);
        currentY += 25;
        
        // List Box
        currentPage.getCanvas().drawString("List Box:", formFont, blueBrush, new Point2D.Float(0, currentY));
        Rectangle2D.Float listArea = new Rectangle2D.Float(startX, currentY, 150, 50);
        PdfListBoxField selectionList = new PdfListBoxField(currentPage, "itemList");
        selectionList.getItems().add(new PdfListFieldItem("First Item", "value1"));
        selectionList.getItems().add(new PdfListFieldItem("Second Item", "value2"));
        selectionList.getItems().add(new PdfListFieldItem("Third Item", "value3"));
        selectionList.setBounds(listArea);
        selectionList.setFont(formFont);
        selectionList.setSelectedIndex(0);
        pdfDoc.getForm().getFields().add(selectionList);
        currentY += 60;
        
        // Radio Buttons
        currentPage.getCanvas().drawString("Radio Buttons:", formFont, blueBrush, new Point2D.Float(0, currentY));
        PdfRadioButtonListField radioGroup = new PdfRadioButtonListField(currentPage, "radioGroup");
        
        PdfRadioButtonListItem radioOption1 = new PdfRadioButtonListItem("Option1");
        radioOption1.setBounds(new Rectangle2D.Float(startX, currentY, 15, 15));
        currentPage.getCanvas().drawString("Option 1", formFont, blackBrush, new Point2D.Float(startX + 20, currentY));
        
        PdfRadioButtonListItem radioOption2 = new PdfRadioButtonListItem("Option2");
        radioOption2.setBounds(new Rectangle2D.Float(startX + 70, currentY, 15, 15));
        currentPage.getCanvas().drawString("Option 2", formFont, blackBrush, new Point2D.Float(startX + 90, currentY));
        
        radioGroup.getItems().add(radioOption1);
        radioGroup.getItems().add(radioOption2);
        radioGroup.setSelectedIndex(0);
        pdfDoc.getForm().getFields().add(radioGroup);
        currentY += 25;
        
        // Combo Box
        currentPage.getCanvas().drawString("Combo Box:", formFont, blueBrush, new Point2D.Float(0, currentY));
        Rectangle2D.Float comboArea = new Rectangle2D.Float(startX, currentY, 150, 15);
        PdfComboBoxField dropdown = new PdfComboBoxField(currentPage, "dropdown");
        dropdown.setBounds(comboArea);
        dropdown.getItems().add(new PdfListFieldItem("Selection A", "valA"));
        dropdown.getItems().add(new PdfListFieldItem("Selection B", "valB"));
        dropdown.getItems().add(new PdfListFieldItem("Selection C", "valC"));
        dropdown.setSelectedIndex(0);
        dropdown.setFont(formFont);
        pdfDoc.getForm().getFields().add(dropdown);
        currentY += 25;
        
        // Signature Field
        currentPage.getCanvas().drawString("Signature:", formFont, blueBrush, new Point2D.Float(0, currentY));
        PdfSignatureField signatureArea = new PdfSignatureField(currentPage, "signature");
        Rectangle2D.Float signatureBounds = new Rectangle2D.Float(startX, currentY, 150, 80);
        signatureArea.setBounds(signatureBounds);
        pdfDoc.getForm().getFields().add(signatureArea);
        currentY += 90;
        
        // Submit Button
        currentPage.getCanvas().drawString("Submit Button:", formFont, blueBrush, new Point2D.Float(0, currentY));
        Rectangle2D.Float buttonArea = new Rectangle2D.Float(startX, currentY, 50, 15);
        PdfButtonField submitBtn = new PdfButtonField(currentPage, "submit");
        submitBtn.setBounds(buttonArea);
        submitBtn.setText("Submit");
        submitBtn.setFont(formFont);
        pdfDoc.getForm().getFields().add(submitBtn);
        
        pdfDoc.saveToFile("GeneratedForm.pdf");
    }
}

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.