How to Convert a PDF file to PDF/A using Java

Cloudmersive
2 min readAug 26, 2022

--

Converting PDFs to PDF/A is essentially a way of preserving a particular document for the long term; it ensures your document will be readable in its exact form/formatting regardless of the inevitable technological advancements that will, one day, make the original file format you used obsolete.

With our PDF to PDF/A API, it’s easy to make this transition at scale — and you can call this API with the ready-to-run code examples provided below in Java. It’s important to be aware up front that this API contains an optional parameter which allows you to configure the specific conformance level for your converted PDF; you may specify ‘1b’ for PDF/A-1b, or specify ‘2b’ for PDF/A-2b. If you choose to leave this blank, the default option is PDF/A-1b.

We can begin by installing the SDK with Maven. To do so, first add a reference to the pom.xml repository:

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

Then add one to the pom.xml dependency:

<dependencies>
<dependency>
<groupId>com.github.Cloudmersive</groupId>
<artifactId>Cloudmersive.APIClient.Java</artifactId>
<version>v4.25</version>
</dependency>
</dependencies>

Now you can complete the API call, starting by including the imports at the top of your file, and including your API key directly after. If you don’t have a Cloudmersive API key, you can easily get one for free by visiting our website & registering a free account:

// 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.
String conformanceLevel = "conformanceLevel_example"; // String | Optional: Select the conformance level for PDF/A - specify '1b' for PDF/A-1b or specify '2b' for PDF/A-2b; default is PDF/A-1b
try {
byte[] result = apiInstance.editPdfConvertToPdfA(inputFile, conformanceLevel);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling EditPdfApi#editPdfConvertToPdfA");
e.printStackTrace();
}

After that, you’re all done — no more contributions required. Happy archiving!

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

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

No responses yet