IRONSOFTWAREHOME
PDF Compression
import {PdfDocument} from "@ironsoftware/ironpdf";

(async () => {
    // Load the existing PDF document
    const pdf = await PdfDocument.fromFile("my-content.pdf");

    // Compress images with quality parameter (1-100)
    await pdf.compressSize(60);
    // Save the compressed PDF
    await pdf.saveAs("document_compressed.pdf");

    // Compress images and scale down based on visible size in the PDF document
    await pdf.compressSize(90, true);
    // Save the scaled and compressed PDF
    await pdf.saveAs("document_scaled_compressed.pdf");
});
NuGet Download
Install-Package @ironsoftware/ironpdf
PDF Compression

PDF Compression

IronPDF offers the capability to compress PDFs effectively. One of the key methods involves reducing the size of images embedded within the PDF document. This optimization process can be initiated by using the compressSize method on the PDF object.

When it comes to resizing JPEG images, the quality setting plays a crucial role. A quality setting of 100% results in minimal loss, while 1% represents very low-quality output. Typically, image quality levels of 90% and above are considered high-quality, 80%-90% falls into the medium-quality range, and 70%-80% is considered low-quality. Reducing the quality below 70% can lead to lower-quality images, but it can significantly reduce the overall file size of the PDF document.

It's recommended to experiment with different quality settings to find the right balance between image quality and file size that suits your specific requirements. The extent of quality reduction will ultimately depend on the type of input image, and some images may experience more noticeable reductions in clarity than others.

Here is an example of how you might use IronPdf to compress a PDF file by adjusting image quality settings:

In this code:

  • We first import the necessary IronPdf namespace.
  • Then, we define a CompressPdf function that receives the path to the input PDF file, the path for the output PDF, and the image quality percentage.
  • The PdfDocument.FromFile method loads the PDF document from the specified file path.
  • The CompressImages method is used to adjust the image quality within the PDF.
  • Finally, we save the compressed PDF to the specified output path using SaveAs.

By adjusting the imageQuality variable, you can control the quality of the images in the PDF, thus impacting the compression level and final file size.

Explore PDF Compression Examples on GitHub
View on GitHub

Ready to Get Started?

Version:2026.8just released

Key in blue circle

Get your free 30-day Trial Key instantly.

bullet_checkedNo credit card or account creation required
  • Logo Aetna
  • Logo NASA
  • Logo GE
  • Logo Porsche
  • Logo USDA
  • Logo Qatar
Join Millions of Engineers who’ve tried IronPDF
Book your free Live Demo
Booking Badge related to IronPDF Product Demo

Trusted by Millions of Engineers Worldwide

Iron Software's customer logos
Get Your No-Obligation Consult
Complete the form below or email sales@ironsoftware.com
Your details will always be kept confidential.
Trusted by Millions of Engineers Worldwide
Iron Software's customer logos
Get your free 30-day Trial Key instantly.
No credit card or account creation required