How to Linearize & Optimize a PDF for Streaming Download in Java
When we linearize a PDF, we’re allowing a server to download it one page at a time, speeding up the download process significantly. If that sounds like it might make a difference in your app, you’re in luck: our PDF linearization API can be easily incorporated to perform this service. You can easily take advantage of this API by copying ready-to-run code examples provided below in Java to structure your API call.
First things first, let’s install Maven. Include a reference in the repository in pom.xml:
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
Then add one to the dependency in pom.xml:
<dependencies>
<dependency>
<groupId>com.github.Cloudmersive</groupId>
<artifactId>Cloudmersive.APIClient.Java</artifactId>
<version>v4.25</version>
</dependency>
</dependencies>
That concludes installation; now it’s time to add the import classes and call the API. Use the following code to do so, and ensure you satisfy the following parameters:
- Your input file for the operation
- Your Cloudmersive API key (if you don’t have one, you can get one for free by registering a free account on our website)
// 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();
}