Fill a PDF Form using PDF.co Web API in Java Asynchronously
This tutorial and the sample source code explain filling a PDF form using the filling functionality from PDF.co Web API with Java, designed for Java developers. The users can utilize the PDF Form Filler API to fill their respective PDF documents with their data interactively.
The process runs asynchronously, which is particularly recommended for handling larger files. Asynchronous processing allows the job to run in the background, avoiding potential timeouts and enabling real-time progress tracking. For more details on managing asynchronous jobs, refer to PDF.co's documentation on async processing.
IN THIS TUTORIAL
Features of PDF Form Filler Web API
Moreover, the PDF.co Web API possesses beneficial tools to fill a form by benefiting from easy field-name mapping, using e-signature support, and using an on-premise API server. The PDF.co Form Filler API works extremely fast to fill out forms rather than manually handling them. The data to be filled can be from different sources such as spreadsheets, CRM’s, and user input.
Form Field Support
The PDF.co Web API can handle a wide variety of form fields, including text fields, checkboxes, radio buttons, and combo boxes, ensuring it meets the needs of complex, multi-field documents. Users can also embed e-signatures or other essential images, such as company logos, directly into the form, enhancing the professionalism and completeness of the output.
A comprehensive demo explaining these features in detail is provided below, helping users understand how the API operates and how each field type can be integrated smoothly into their workflows.
Security
An important feature of PDF.co Web API is its robust security framework. The API transmits documents and data via encrypted connections, ensuring that sensitive data remains secure throughout the form-filling process. This commitment to data security makes PDF.co a reliable choice for industries that prioritize data protection. For a deeper dive into PDF.co's security measures, users can review the API security details here.
Endpoint Parameters
Following are the PDF to CSV endpoint parameters:
- url: It is a required parameter that provides the URL to the source file. The PDF.co platform supports any publicly accessible URL, including those from Dropbox, Google Drive, and the built-in file storage of PDF.co.
- password: The user must provide the file password if he is editing the locked file.
- fields: It is an optional parameter that accepts the value arrays to update the editable fields.
- images: It is an options parameter accepting image objects array to add images in the pdf form.
- encrypt: It is an optional parameter whose value is false by default, and the users can encrypt the output file using this parameter.
- async: Setting
"async": true
enables background processing, allowing large or complex conversions to complete without blocking the initial request. - name: It is an optional parameter that takes the string with the output file name.
- profiles: It is an optional parameter and must be a string. This parameter helps in setting additional configurations and extra options
PDF Filler Source Code in Java
The following source code demonstrates how to fill a sample PDF form asynchronously using the PDF.co Form Filler API. The Java code shows how to populate fields on a sample form like IRS Form1040, which includes text fields, checkboxes, and a signature section. Users can leverage the Get PDF Info tool to retrieve field names from an interactive PDF for easier form-filling. This tool is available under the Helpers Tools menu in PDF.co accounts, with a direct link to the feature provided here.
Users can upload their PDF documents to gather detailed information, such as the form's text fields, edit boxes, checkboxes, page numbers, and each field's location, helping streamline the filling process.
In this example, IRS Form 1040 is used. To use this, users need an API key generated upon logging into PDF.co and the PDF file URL included in the API request. The API then returns the URL of the resulting edited file, which can be downloaded for local storage. The output, result.pdf, will contain the filled document with all necessary information once the asynchronous code execution is complete.
Step-by-Step Guide to Use PDF.co’s Form Filler API in Async Mode
Using PDF.co’s Form Filler API with Java involves several steps. This guide provides a detailed walkthrough on configuring and executing an asynchronous job to fill PDF forms using OkHttp
and Gson
libraries. Async mode is enabled to allow large jobs to process without timing out, which is ideal for extensive or complex form filling tasks.
Setup Required Libraries
You can use the IDE of your choice, whether it's VS Code, IntelliJ, or another environment that you're comfortable with.
- Gson Library:
Gson
is a Java library that helps convert Java objects into JSON and parse JSON responses easily. You can download the relevant JAR file to add to your project or include it through a dependency manager like Maven or Gradle. This library allows efficient handling of JSON data. - OkHttp Library:
OkHttp
is a third-party library that can assist in sending and receiving HTTP-based network requests. It is more efficient in reading and writing data than the standard Java I/O libraries. Users can download the JAR file for OkHttp and include it in their project dependency for efficient load handling and saving bandwidth.
Obtain Your API Key from PDF.co
Login to your PDF.co account to access the API key from the dashboard. This key is required for authenticating requests. Store the API key as a constant in your Java file so that it’s easy to manage and update.
Prepare the JSON Payload
The JSON payload must include the parameters required for the form-filling task, such as:
url
: The URL of the PDF source file.async
: Set to true to enable asynchronous processing.password
: Set this if the PDF document is password-protected.fields
: Define the form fields you want to fill, including the field name, page number, and the value you wish to insert.
Send the API Request
Use OkHttp
to send a POST request to PDF.co’s “PDF Edit” endpoint (https://api.pdf.co/v1/pdf/edit/add
). Include your API key in the header.
Map, Verify, and Populate Your PDF Forms
Upload the relevant PDF document using the Helper Tools menu to obtain the field mapping. After mapping and verifying the required fields, including their page numbers and locations, you can provide the data in the specified format along with the field values. This will populate the form fields accurately, ensuring all necessary details are included.
Moreover, check the fields’ width to put only the critical information according to the limited width to avoid inconvenience.
Provide the relevant information in the payload—for example, the field names, page numbers, and the information to be added. Moreover, the users can add image objects in the payload to include signatures in the PDF form.
Polling the Job Status Asynchronously
Once the conversion request is sent, the API responds with a jobId that can be used to track the conversion status. The code enters a loop to check the status of the job every 3 seconds:
- It sends a status check request to the job status endpoint (
/v1/job/check
). - It checks if the job is complete by comparing the returned status to "
success
". - If not completed, it waits for 3 seconds before re-checking.
Download Filled PDF
The users can download the filled PDF from the obtained URL as well. For this purpose, the users can provide the document link to the download function and provide the output path of the file as well.
For a better experience, the users can write a function to effectively download the edited form from the response URL, as shown in the sample code. The function takes the file URL, output file path, and the web client to download and store the file in the given location. The users can choose to provide the path of the output file in the variable declaration for conveniently changing them according to the requirement.
Filled 1040-T Form Output
Below is the screenshot of the edited form:
Summary of Steps
- Set up
Gson
andOkHttp
libraries. - Obtain and store the API key.
- Upload the PDF document and retrieve field mappings.
- Verify field locations and widths to match the PDF’s layout.
- Prepare the JSON payload, enabling async mode.
- Send the API request and parse the
jobId
. - Poll for job status until completed.
- Download the filled PDF once the job is successful.
Following these steps allows for efficient, automated form filling with PDF.co’s Form Filler API in async mode. This approach ensures stability for larger documents, improving processing times and reliability.