DEV Community 👩‍💻👨‍💻

carlwils
carlwils

Posted on

Java/ Compress PDF Document

As one of the commonly used document formats, PDF documents are known for their good compatibility. Generally, a PDF document can contain pictures, tables, texts, etc. However, when the document contains too much content, its upload and transmission may be affected. In this article, you will learn how to use a third-party free Java library to compress a PDF document from the following two aspects:
● Compress the contents in the PDF document.
● Compress the images in the PDF document.

Import JAR Dependency

● You can download the free API (Free Spire.PDF for Java) and unzip it. Then add the Spire.Pdf.jar file to your project as dependency.
● Or you can directly add the jar dependency to maven project by adding the following configurations to the pom.xml.

<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.pdf.free</artifactId>
        <version>4.3.0</version>
    </dependency>
</dependencies>
Enter fullscreen mode Exit fullscreen mode

Compress the contents in the PDF document

import com.spire.pdf.*;

public class CompressPDF {
    public static void main(String[] args) {

        //Load a sample PDF document
        PdfDocument document = new PdfDocument();
        document.loadFromFile("C:\\Users\\Administrator\\Desktop\\sample.pdf");

        //Compress the content in the PDF document
        document.getFileInfo().setIncrementalUpdate(false);
        document.setCompressionLevel(PdfCompressionLevel.Best);

        //Save the document to file
        document.saveToFile("output/CompressPDFcontent.pdf", FileFormat.PDF);
    }
}
Enter fullscreen mode Exit fullscreen mode

CompressContent

Compress the images in the PDF document

import com.spire.pdf.*;
import com.spire.pdf.exporting.PdfImageInfo;
import com.spire.pdf.graphics.PdfBitmap;

public class CompressPDFImage {
    public static void main(String[] args) {

        //Load a sample PDF document
        PdfDocument document = new PdfDocument();
        document.loadFromFile("C:\\Users\\Administrator\\Desktop\\sample.pdf");

        //Compress the image quality in the PDF document
        document.getFileInfo().setIncrementalUpdate(false);

        for (int i = 0; i < document.getPages().getCount(); i++) {

            PdfPageBase page = document.getPages().get(i);
            PdfImageInfo[] images = page.getImagesInfo();
            if (images != null && images.length > 0)
                for (int j = 0; j < images.length; j++) {
                    PdfImageInfo image = images[j];
                    PdfBitmap bp = new PdfBitmap(image.getImage());
                    bp.setQuality(20);
                    page.replaceImage(j, bp);

                }
        }

        //Save the document to file
        document.saveToFile("output/CompressPDFImage.pdf", FileFormat.PDF);
    }
}
Enter fullscreen mode Exit fullscreen mode

CompressImage

Top comments (0)

We are hiring! Do you want to be our Senior Platform Engineer? Forem is hiring a Senior Platform Engineer

If you're interested in ops and site reliability and capable of dipping in to our Linux stack, we'd love your help shoring up our systems!