How to Compress PDFs in C#

How to Compress PDFs in C# using IronPDF

IronPDF provides powerful PDF compression features in C# that reduce file sizes by compressing embedded images and optimizing PDF tree structures, making documents more manageable for storage and sharing while maintaining quality.

PDF compression reduces the file size of PDF documents to make them more manageable for storage, sharing, and transmission. Whether you're working with generated PDFs from HTML or existing PDF files, compression is essential for optimizing document handling.

Images typically consume the majority of PDF file sizes. IronPDF offers compression features that reduce embedded image sizes and optimize the tree structure often found in table-heavy PDFs. These techniques work seamlessly with PDFs created through various methods, including URL to PDF conversions and HTML file conversions.

Quickstart: Compress PDF Files with IronPDF

Reduce PDF file sizes using IronPDF's compression tools. Load your PDF with PdfDocument.FromFile, apply compression with the Compress method using default options, and save your optimized PDF. This process ensures significant file size reduction while maintaining quality.

Nuget IconGet started making PDFs with NuGet now:

  1. Install IronPDF with NuGet Package Manager

    PM > Install-Package IronPdf

  2. Copy and run this code snippet.

    PdfDocument.FromFile("input.pdf").CompressImages(40).SaveAs("compressed.pdf");
  3. Deploy to test on your live environment

    Start using IronPDF in your project today with a free trial
    arrow pointer


How Do I Compress Images in PDFs?

JPEG compression quality ranges from 1% (very low quality) to 100% (minimal loss). Understanding these levels helps when working with PDFs containing embedded images or when you need to manage fonts and graphics.

  • 90% and above: high quality
  • 80%-90%: medium quality
  • 70%-80%: low quality

Experiment with various values to balance quality and file size. Quality reduction varies based on input image type—some images show more noticeable clarity loss than others. This is particularly important when working with images from Azure Blob Storage or creating PDFs with custom watermarks.

:path=/static-assets/pdf/content-code-examples/how-to/pdf-compression-image.cs
using IronPdf;

ChromePdfRenderer renderer = new ChromePdfRenderer();

PdfDocument pdf = renderer.RenderUrlAsPdf("https://en.wikipedia.org/wiki/Main_Page");

// Compress images in the PDF
pdf.CompressImages(40);

pdf.SaveAs("compressed.pdf");
$vbLabelText   $csharpLabel

What Results Can I Expect from Image Compression?

Reduced by 39.24%!

File comparison showing compressed.pdf (449 KB) vs nonCompress.pdf (739 KB) demonstrating image compression results

How Do Image Compression Options Work?

Two key image compression options:

ShrinkImage: Scales down image resolution based on visible size in the PDF. This significantly reduces file size by optimizing high-resolution images that don't require full quality for their intended use.

HighQualitySubsampling: Determines the chroma subsampling method. "True" uses 4:4:4 subsampling for full color detail. "False" uses 4:1:1 subsampling, reducing color detail for smaller file sizes.

Chroma subsampling reduces data requirements while preserving visual quality by lowering color information resolution while maintaining brightness resolution. The human eye is more sensitive to brightness than color changes, making this technique effective for photographic images.

In 4:4:4 subsampling, each pixel retains full color information. In 4:1:1 subsampling, color information is reduced, decreasing file size but also color detail. Choose based on your quality versus file size requirements.


How Do I Compress PDF Tree Structure?

This feature reduces PDF size by minimizing the tree structure created by the Chrome Engine. It's most effective with Chrome Engine-generated PDFs containing extensive table data. Some rendering engines output PDFs without this structure, making the feature ineffective in those cases. This technique is valuable when working with complex layouts or merging multiple PDFs with redundant structural elements.

Removing tree structure may affect text highlighting and extraction effectiveness. Consider this trade-off when applying compression, especially if users need to extract text and images from compressed PDFs.

Test the CompressStructTree method using this PDF with table data.

:path=/static-assets/pdf/content-code-examples/how-to/pdf-compression-tree-structure.cs
using IronPdf;

PdfDocument pdf = PdfDocument.FromFile("table.pdf");

// Compress tree structure in PDF
pdf.CompressStructTree();

pdf.SaveAs("compressedTable.pdf");
$vbLabelText   $csharpLabel

What File Size Reduction Can Tree Structure Compression Achieve?

Reduced by 67.90%! This percentage increases with larger table PDFs.

File comparison showing compressedTable.pdf (52 KB) vs table.pdf (162 KB) demonstrating compression results

How Can I Apply Both Compression Methods Together?

IronPDF's Compress method configures both image and tree structure compression simultaneously. This combined approach is particularly effective for complex PDFs containing images and structured data, such as reports with charts or documents with embedded tables and graphics.

:path=/static-assets/pdf/content-code-examples/how-to/pdf-compression-compress.cs
using IronPdf;

PdfDocument pdf = PdfDocument.FromFile("sample.pdf");

CompressionOptions compressionOptions = new CompressionOptions();

// Configure image compression
compressionOptions.CompressImages = true;
compressionOptions.JpegQuality = 80;
compressionOptions.HighQualityImageSubsampling = true;
compressionOptions.ShrinkImages = true;

// Configure tree structure compression
compressionOptions.RemoveStructureTree = true;

pdf.Compress(compressionOptions);

pdf.SaveAs("compressed.pdf");
$vbLabelText   $csharpLabel

What Compression Options Are Available?

  • CompressImages: Compresses existing images using JPG encoding. Defaults to false. Processes all embedded images with specified compression settings.

  • RemoveStructureTree: Significantly reduces document size by removing structure tree. May impact text selection in complex documents. Most effective for HTML-generated PDFs with tables.

  • JpegQuality: Sets JPEG quality (1-100) for image compression. Defaults to 42. Higher values preserve detail but increase file size.

  • HighQualityImageSubsampling: Choose 4:4:4 subsampling for higher quality (true) or 4:1:1 for smaller files (false). Depends on color accuracy requirements.

  • ShrinkImages: Scales down image resolution to drastically reduce size. Effective for high-resolution images exceeding display requirements.

Consider your use case when applying compression. Archival purposes may prioritize quality over file size. Web distribution may require smaller files. Fine-tune settings based on whether you're working with PDF/A compliant documents or standard PDFs.

Ready to see what else you can do? Check out our tutorial page here: Additional Features

Frequently Asked Questions

How much can PDF file size be reduced with compression?

IronPDF can reduce PDF file sizes up to 75% through image compression and structure optimization. In the example shown, a file was reduced by 39.24% using image compression techniques.

What are the main techniques for compressing PDFs in C#?

IronPDF offers two main compression techniques: image compression using the CompressImages method which reduces embedded image sizes, and structure tree compression using the CompressStructTree method which optimizes the PDF's internal tree structure, particularly beneficial for table-heavy documents.

What quality levels should I use for JPEG compression in PDFs?

IronPDF supports JPEG compression quality from 1% to 100%. Recommended levels are: 90% and above for high quality, 80%-90% for medium quality, and 70%-80% for low quality. The optimal setting depends on your balance between file size and visual quality requirements.

Can I compress a PDF in just one line of code?

Yes, IronPDF allows PDF compression in a single line of code: PdfDocument.FromFile("input.pdf").CompressImages(40).SaveAs("compressed.pdf"); This loads a PDF, applies compression with 40% quality, and saves the result.

Does compression work with PDFs created from HTML?

Yes, IronPDF's compression features work seamlessly with PDFs created through various methods including HTML string to PDF conversions, URL to PDF conversions, and HTML file conversions, as well as existing PDF files.

What types of content benefit most from PDF compression?

Images typically consume the majority of PDF file sizes, making image-heavy documents ideal candidates for compression. Additionally, PDFs with complex table structures benefit from IronPDF's structure tree compression feature.

Chaknith Bin
Software Engineer
Chaknith works on IronXL and IronBarcode. He has deep expertise in C# and .NET, helping improve the software and support customers. His insights from user interactions contribute to better products, documentation, and overall experience.
Reviewed by
Jeff Fritz
Jeffrey T. Fritz
Principal Program Manager - .NET Community Team
Jeff is also a Principal Program Manager for the .NET and Visual Studio teams. He is the executive producer of the .NET Conf virtual conference series and hosts 'Fritz and Friends' a live stream for developers that airs twice weekly where he talks tech and writes code together with viewers. Jeff writes workshops, presentations, and plans content for the largest Microsoft developer events including Microsoft Build, Microsoft Ignite, .NET Conf, and the Microsoft MVP Summit
Ready to Get Started?
Nuget Downloads 16,901,161 | Version: 2025.12 just released