Compress PDF documents using Java programming language
Our PDF Compression API can reduce the document size up to 99% of the original while preserving the same visual quality as the original PDF. You can compress your PDF documents with Java using our ConvertAPI library for Java programming language with no effort at all.
PDF compression made easy for Java
Our PDF Compression API reduces document size by optimizing its structure, removing redundant objects, re-encoding images, subsetting fonts, and squeezing the document's size for sharing or long-term storage. It can linearize the document for fast web-view rendering as well as preserve the PDF/A standard. It can also handle password-protected documents. Although our service works via REST API, we have created a wrapper library for Java, so you can compress your PDF documents without writing complex logic and explicit HTTP calls. In this tutorial, we will run through the steps needed to compress PDFs using Java.
How to compress a PDF using Java?
- Install the ConvertAPI library for Java
- Get your API secret key
- Set up the conversion parameters
- Copy the auto-generated code snippet
Install ConvertAPI Java library
First, you will need to install our CovnertAPI client library for Java. You can do that using Maven. Simply add the following dependency to your pom.xml file:
<dependency> <groupId>com.convertapi.client</groupId> <artifactId>convertapi</artifactId> <version>2.9</version> </dependency>
Get your API secret key
Once you have our library installed and found your secret, set it in the configuration like so:
Set up the compression parameters
ConvertAPI is designed to be super simple to implement. You don't even need to code anything - we will get everything done for you. Visit our PDF Compression API page and set up the compression parameters using our live demo tool. Click on the "Advanced properties" and you will see a complete list of parameters to suit your needs. You can test the compression by clicking the "Post" button.
Grab the auto-generated code snippet
Once you are happy with the compression result, you will find the auto-generated code snippet at the bottom of the page! A PDF compression example in Java might look something like this:
Config.setDefaultSecret("your-api-secret"); ConvertApi.convert("pdf", "compress", new Param("File", Paths.get("/path/to/sample.pdf")), new Param("ColorImageCompression", "jpx"), new Param("ColorImageQuality", "70"), new Param("ColorImageThresholdDpi", "150"), new Param("ColorImageResampleDpi", "100"), new Param("RemoveBookmarks", "true"), new Param("RemoveAnnotations", "true"), new Param("RemoveForms", "true"), new Param("RemovePageLabels", "true"), new Param("RemoveLayers", "true"), new Param("RemoveArticleThreads", "true"), new Param("RemoveNamedDestinations", "true"), new Param("UnembedBaseFonts", "true"), new Param("SubsetEmbeddedFonts", "true"), new Param("Linearize", "true") ).get().saveFilesSync(Paths.get("/path/to/result/dir"));
Advanced PDF Compress techniques in Java
As you may already noticed, using our converters is relatively simple. Nevertheless, it provides some more advanced options. For example, our Java library allows you to convert local files, remote files accessible by a URL, or even file streams. Let's see that in action.
Compress a local file
Compressing a local file is the most straightforward way to go. Specify the path of a file from your machine and set the destination folder for the compressed result like so:
Config.setDefaultSecret("your-api-secret"); ConvertApi.convert("pdf", "compress", new Param("File", Paths.get("/path/to/my_file.pdf")) ).get().saveFilesSync(Paths.get("/path/to/result/dir"));
Compress a remote file
You can compress a remote file accessible via the URL. Please make sure that the file is publicly accessible (does not require authentication) and is linking to an actual PDF file with the appropriate "application/pdf" content-type header.
Config.setDefaultSecret("your-api-secret"); ConvertApi.convert("pdf", "compress", new Param("File", "https://cdn.convertapi.com/cara/testfiles/document-large.pdf") ).get().saveFilesSync(Paths.get("/path/to/result/dir"));
Compress a file stream
To gain the most performance benefits, you might consider working with file streams. To compress a PDF by passing a file stream as a "File" parameter, please have a look at this code example:
Config.setDefaultSecret(getenv("your-api-secret")); // Creating file data stream InputStream stream = Files.newInputStream(new File("src/main/resources/sample.pdf").toPath()); System.out.println("Compressing stream of PDF data"); CompletableFuture<ConversionResult> result = ConvertApi.convert("pdf", "compress", new Param("file", stream, "sample.pdf") ); Path pdfFile = Paths.get(System.getProperty("java.io.tmpdir") + "/compressed.pdf"); result.get().saveFile(pdfFile).get();
You can find more useful examples in our GitHub examples folder.
Compressing files with Java is a piece of cake using ConvertAPI, especially when it comes to PDF documents. Our service provides a fully customizable document compression API that can be implemented using Java programming language in a couple of minutes. You can read more about our Java library in our docs. Happy coding!