How to Linearize and Optimize a PDF for Streaming Download with Java
When you want your PDF to be quickly/easily opened & viewed on the internet, linearized PDFs are the way to go. You can tell a PDF is linearized by simply reviewing the properties (specifically, the “Fast Web View” property) of the document. So, how do you get PDFs into this internet-optimized format? You can accomplish this at scale using a PDF linearization and optimization API. With your PDFs fully optimized, you’ll enjoy much faster streaming and download speeds, ensuring fewer hiccups and delays in your workflow. Below, I’ll demonstrate how you can use this API by structuring your call with ready-to-run Java code examples.
Let’s kick off by installing the SDK with Maven. Start by adding a reference to the repository in pom.xml:
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
Next, 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 include our imports and call the API. Below your imports, you’ll notice a prompt to include an API key — you can get one easily by registering a free account on our website (this account comes with zero commitments & a hard limit of 800 API calls per month):
// 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 {
byte[] result = apiInstance.editPdfLinearize(inputFile);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling EditPdfApi#editPdfLinearize");
e.printStackTrace();
}
Beyond your API key, the parameters are straightforward; simply include your PDF document where indicated. After that, you’re all set.