How to Validate a PDF Document File using Java

Cloudmersive
2 min readJun 27, 2022

--

You should only ever store valid, trusted data in your database. That is especially true when it comes to PDF files, which are extremely common and prone to containing dozens of errors — or worse. With the help of our PDF Validation API, you can instantly identify invalid PDF files and receive a list of errors and warnings with follow-up details to accompany both. In addition, you’ll be made aware if the file was password-protected. Below, we’ll walk through how to take advantage of this API easily using ready-to-run Java code snippets to structure an API call.

First, let’s install the Java SDK with Maven. Include this reference in the pom.xml repository:

<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>

Next, we can add 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>

With installation complete, let’s now include the import classes and call the function:

// 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.ValidateDocumentApi;
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");
ValidateDocumentApi apiInstance = new ValidateDocumentApi();
File inputFile = new File("/path/to/inputfile"); // File | Input file to perform the operation on.
try {
DocumentValidationResult result = apiInstance.validateDocumentPdfValidation(inputFile);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling ValidateDocumentApi#validateDocumentPdfValidation");
e.printStackTrace();
}

You’ll need to include your API key above — to get one for free, just visit our website and register a free account. That key will provide you with 800 API calls per month.

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.

No responses yet