Fill Values to Editable PDF forms in Java
In this example, we will show you how to use Apache PDFBox Java library to fill up editable PDF forms in Java.
Apache PDFBox is an open source Java library that helps to work with PDF documents. PDFBox library allows to create new PDF documents, updating of existing PDF documents, and is also capable of extracting content from documents.
Follow the steps below to fill values to editable PDF form and generate a new PDF document in Java:
- The first thing you'll need to do is, add the Apache PDFBox library dependency to your application.
- To fill an editable PDF form and create a new pdf document, use the code as shown in the example below:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import org.apache.pdfbox.Loader;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDDocumentCatalog;
import org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm;
import org.apache.pdfbox.pdmodel.interactive.form.PDField;
import org.joda.time.LocalDate;
public class PdfBoxExample {
public void fillPdfForm() {
InputStream input = null;
try {
input = new FileInputStream(new File("C:\\Users\\documents\\editable-form.pdf"));
} catch (FileNotFoundException ex) {
ex.printStackTrace();
}
//Load editable pdf file
try (PDDocument pdfDoc = Loader.loadPDF(input);) {
PDDocumentCatalog docCatalog = pdfDoc.getDocumentCatalog();
PDAcroForm acroForm = docCatalog.getAcroForm();
PDField firstnameField = acroForm.getField("first_name");
firstnameField.setValue("Danny");
PDField lastnameField = acroForm.getField("last_name");
lastnameField.setValue("Clark");
PDField registrationDateField = acroForm.getField("registration_date");
registrationDateField.setValue(String.valueOf(LocalDate.now()));
PDField registrationFeeField = acroForm.getField("registration_fee");
registrationFeeField.setValue(String.valueOf(100.50));
/*make the final document uneditable*/
acroForm.flatten();
/*generate a new pdf file and save it to the given location*/
pdfDoc.save(new File("C:\\Users\\documents\\final-document.pdf"));
} catch (IOException e) {
e.printStackTrace();
}
}
}
Sometimes, you may need to fill data to editable PDF forms and send the final generated document to a remote computer. In such cases, you will need the final document in the form of byte streams as shown in the example below:
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import org.apache.pdfbox.Loader;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDDocumentCatalog;
import org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm;
import org.apache.pdfbox.pdmodel.interactive.form.PDField;
import org.joda.time.LocalDate;
public class PdfBoxExample2 {
public ByteArrayInputStream fillPdfForm() {
InputStream input = null;
try {
input = new FileInputStream(new File("C:\\Users\\documents\\editable-form.pdf"));
} catch (FileNotFoundException ex) {
ex.printStackTrace();
}
//Load editable pdf file
try (PDDocument pdfDoc = Loader.loadPDF(input);) {
PDDocumentCatalog docCatalog = pdfDoc.getDocumentCatalog();
PDAcroForm acroForm = docCatalog.getAcroForm();
ByteArrayOutputStream out = new ByteArrayOutputStream();
PDField firstnameField = acroForm.getField("first_name");
firstnameField.setValue("Danny");
PDField lastnameField = acroForm.getField("last_name");
lastnameField.setValue("Clark");
PDField registrationDateField = acroForm.getField("registration_date");
registrationDateField.setValue(String.valueOf(LocalDate.now()));
PDField registrationFeeField = acroForm.getField("registration_fee");
registrationFeeField.setValue(String.valueOf(100.50));
/*make the final document uneditable*/
acroForm.flatten();
pdfDoc.save(out);
return new ByteArrayInputStream(out.toByteArray());
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}