IronPDF can compress the file size of PDFs by reducing the size of any embedded images. For this purpose, developers can invoke the
compressImages method to reduce the file size PDFs that incorporate large images and other graphical elements. This creates great savings in storage space, time, and costs in transporting PDF efficiently across email and other communication channels.
PdfDocument compressImages(int quality);
compressImages method accepts integer values between 0 and 100, which indicate the targeted quality percentage for embedded images. Moreover, the rules for working with this method are similar to the rules for optimizing file sizes for lossy image types like JPEG. A value of 100 indicates that images must retain their original quality (no loss), and lower values indicate lower original quality thresholds.
The code example featured on this page produces two new PDFs from the same original PDF using different image compression qualities. The first one (line 9) produces a PDF whose images lose much of their original quality. Quality values below 70 will typically produce PDFs whose images are of very low quality but whose file sizes are smaller by a significant margin. Line 14 produces another optimized PDF with a higher quality threshold (90%). This yields a PDF with images of much higher quality but with a much smaller reduction in file size compared to the first.
The ideal quality values will vary based on user requirements. Some experimentation may be needed to find the value that produces the smallest file size while keeping image quality acceptable.
How to compress PDF File in Java
- Install Java library to compress PDF documents
fromFilemethod to import an existing PDF
- Compress the PDF with
compressImagesmethod by specifying the quality
- Export compressed PDF to desired directory
- Achieve huge file's size deduction with little differences in quality