How To Get and Set PDF Form Fields in Java
We’re now more than 30 years removed from the introduction of Portable Document Format (PDF) to the budding world of digital documents, and it’s safe to say the concept of a universally interoperable format hasn’t lost its appeal. To call PDF “popular” feels like a seismic understatement; it’s ubiquitous to an absurd degree. It’s not at all ridiculous to imagine that everyone in the world (with access to a computer) will interact with a document in PDF format at some point in their life, and it's likely that every modern business depends on PDF at some point as the export format for their various images, applications, contracts, invoices, and more.
One of the major business advantages PDF offers (and has offered since 1996 — albeit under much different circumstances) is the ability to create and share forms that can be filled out digitally. As is the case now with most digitized processes, the manual counterpart to this process — i.e., printing forms, filling them out manually, and either scanning them or storing them for digital or physical archival — has fully evolved into an excruciating exercise in patience knowing that a purely digital version of that experience exists.
The process of generating a digitally accessible PDF form is extremely simple and convenient. Using any one of the dozens of specialized PDF readers available in modern app-store markets, we can follow a few point-and-click steps to incorporate custom text fields, check boxes, radio buttons, combo boxes, list boxes, and buttons into our digital PDF documents, and all these elements can reliably store client-modified information on our behalf, saving us immense time and resources by completely circumventing a physical printing and re-uploading process.
The convenience doesn’t end there, however. In a modern digital world where distributed, cloud-hosted services are increasingly expanding the horizons of software architecture, we can rapidly find new ways to interact with our PDF forms even more freely than ever before, and we can do so in the logic of our own custom-built web applications with minimal overhead cost (i.e., minimal coding). Once we’ve generated and stored an iteration of a PDF form, we can streamline the process of filling its form fields by retrieving and setting its contents through simple and secure web API requests.
In more practical terms, this means we can quite easily store client-side information in standardized PDF forms without relying directly on the PDF form/field interface. We can, for example, leverage PDF forms without needing to distribute those forms to clients (i.e., via client-side download) and without subsequently asking clients to re-upload their PDF forms once completed. They can simply complete the usual form-filling process in a client-facing web application – very likely a simpler, cleaner, and more enjoyable interface that leaves users with a greater feeling of satisfaction — and both parties can automatically receive a finalized version of the completed form after the fact.
In the remainder of this article, I’ll demonstrate two free-to-use APIs that can be respectively utilized to 1) enumerate existing form fields and values from a PDF and 2) set those fields/values with a subsequent request. Both stateless services process documents in memory and release their data cache on completion so they can be used safely and reliably to handle sensitive document contents.
Demonstration
We can take advantage of both API solutions using ready-to-run Java code examples provided further down the page, and we can authorize our requests for free using a single free-tier API key.
As mentioned above, we can use the first API call to retrieve a list of form field objects available in our PDF document. As a reminder, PDF forms can include text fields, check boxes, radio buttons, combo boxes, list boxes, and buttons. This will be structured like the following example:
{
"Successful": true,
"FormFields": [
{
"FieldName": "string",
"FieldType": "string",
"FieldValue": "string",
"FieldComboBoxSelectedIndex": 0
}
]
}
We can then utilize the second API call to fill the form fields we enumerated in the initial call. We can structure our request using the objects we retrieved.
Our first step is to install the Java SDK. We install with Maven by first adding a reference to the repository in pom.xml:
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
And then adding a reference to the dependency in pom.xml:
<dependencies>
<dependency>
<groupId>com.github.Cloudmersive</groupId>
<artifactId>Cloudmersive.APIClient.Java</artifactId>
<version>v4.25</version>
</dependency>
</dependencies>
We can now use the below code to structure our API call to enumerate the form fields in our PDF document. We need to pass our PDF’s form data in with this request:
// Import classes:
//import com.cloudmersive.client.invoker.ApiClient;
//import com.cloudmersive.client.invoker.ApiException;
//import com.cloudmersive.client.invoker.Configuration;
//import com.cloudmersive.client.invoker.auth.*;
//import com.cloudmersive.client.EditPdfApi;
ApiClient defaultClient = Configuration.getDefaultApiClient();
// Configure API key authorization: Apikey
ApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");
Apikey.setApiKey("YOUR API KEY");
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
//Apikey.setApiKeyPrefix("Token");
EditPdfApi apiInstance = new EditPdfApi();
File inputFile = new File("/path/to/inputfile"); // File | Input file to perform the operation on.
try {
PdfFormFields result = apiInstance.editPdfGetFormFields(inputFile);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling EditPdfApi#editPdfGetFormFields");
e.printStackTrace();
}
Finally, we can use the below code to structure our API call to fill the form fields we retrieved. We need to pass our PDF’s form data AND our form fields with this request:
// Import classes:
//import com.cloudmersive.client.invoker.ApiClient;
//import com.cloudmersive.client.invoker.ApiException;
//import com.cloudmersive.client.invoker.Configuration;
//import com.cloudmersive.client.invoker.auth.*;
//import com.cloudmersive.client.EditPdfApi;
ApiClient defaultClient = Configuration.getDefaultApiClient();
// Configure API key authorization: Apikey
ApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");
Apikey.setApiKey("YOUR API KEY");
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
//Apikey.setApiKeyPrefix("Token");
EditPdfApi apiInstance = new EditPdfApi();
SetPdfFormFieldsRequest fieldValues = new SetPdfFormFieldsRequest(); // SetPdfFormFieldsRequest |
try {
byte[] result = apiInstance.editPdfSetFormFields(fieldValues);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling EditPdfApi#editPdfSetFormFields");
e.printStackTrace();
}
All that’s left is to configure a pleasant user interface to capture our client-side requests, and we’re all set.