Java: Reduce the Size of a PDF Document

PDFs are a popular file format for sharing and storing documents, but they can sometimes be quite large. This makes them difficult to upload or send via email, especially if you're dealing with slow internet speeds or limited storage space. Fortunately, there is a solution: compressing your PDF documents. Compressing a PDF can reduce its file size without sacrificing quality, making it easier to share and store your important files. In this article, you will learn how to reduce the size of a PDF document in Java using Spire.PDF for Java.

Install Spire.PDF for Java

First of all, you're required to add the Spire.Pdf.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.

<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</artifactId>
        <version>10.10.7</version>
    </dependency>
</dependencies>
    

Compress Fonts and Images in a PDF Document in Java

Fonts and high-quality images are two main factors that contribute to the size of a PDF document. To reduce the PDF document size, you can compress the font resources and the image quality. The following are the steps to compress a PDF document using Spire.PDF for Java.

  • Load a PDF document while initializing the PdfCompressor object.
  • Get text compression options using PdfCompressor.getOptions().getTextCompressionOptions() method.
  • Compress font resources using TextCompressionOptions.setCompressFonts() method.
  • Get image compression options using PdfCompressor.getOptions().getImageCompressionOptions() method.
  • Set the image compression level using ImageCompressionOptions.setImageQuality() method.
  • Compress images using ImageCompressionOptions.setCompressImage() method.
  • Save the compressed document to file using PdfCompressor.compressToFile() method.
  • Java
import com.spire.pdf.conversion.compression.ImageCompressionOptions;
import com.spire.pdf.conversion.compression.ImageQuality;
import com.spire.pdf.conversion.compression.PdfCompressor;
import com.spire.pdf.conversion.compression.TextCompressionOptions;

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

        //Load a PDF document while initializing the PdfCompressor object
        PdfCompressor compressor = new PdfCompressor("C:\\Users\\Administrator\\Desktop\\ToCompress.pdf");

        //Get text compression options
        TextCompressionOptions textCompression = compressor.getOptions().getTextCompressionOptions();

        //Compress fonts
        textCompression.setCompressFonts(true);

        //Unembed fonts
        //textCompression.setUnembedFonts(true);

        //Get image compression options
        ImageCompressionOptions imageCompression = compressor.getOptions().getImageCompressionOptions();

        //Set the compressed image quality
        imageCompression.setImageQuality(ImageQuality.Low);

        //Resize images
        imageCompression.setResizeImages(true);

        //Compress images
        imageCompression.setCompressImage(true);

        //Save the compressed document to file
        compressor.compressToFile("output/Compressed.pdf");
    }
}

Java: Reduce the Size of a PDF Document

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.