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 and call CompressAndSaveAs to compress and save your optimized PDF in one step. This process ensures significant file size reduction while maintaining quality.

  1. Install IronPDF with NuGet Package Manager

    PM > Install-Package IronPdf
  2. Copy and run this code snippet.

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

    Start using IronPDF in your project today with a free trial

    arrow pointer


Please notePrevious methods for compression such as CompressImages, CompressStructTree, and Compress(CompressionOptions) are deprecated.

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.CompressAndSaveAs("compressed.pdf", 40);
$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 Does the Quality Parameter Work?

The quality parameter in CompressAndSaveAs controls the JPEG encoding quality applied to every embedded image. Pass an integer from 1 (maximum compression, lowest quality) to 100 (minimal compression, highest quality), or null to skip image compression entirely.

IronPDF automatically re-encodes and scales images during compression, so no additional options are required. Lower values produce smaller files but may introduce visible artifacts — experiment to find the right balance for your content.


How Do I Compress PDF Tree Structure?

This feature reduces PDF size by stripping the tree structure created by the Chrome Engine. Pass removeStructureTree: true as the third parameter of CompressAndSaveAs to enable it. 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 CompressAndSaveAs with tree structure removal 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.CompressAndSaveAs("compressedTable.pdf", null, true);
$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?

CompressAndSaveAs handles both image and tree structure compression in a single call — pass a quality value and set removeStructureTree to true. 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");

// Configure compression with automatic optimization
pdf.CompressAndSaveAs("compressed.pdf", 80, true);
$vbLabelText   $csharpLabel

What Parameters Does CompressAndSaveAs Accept?

  • outputPath (string): The file path where the compressed PDF will be saved.

  • quality (int?, 1–100): JPEG quality applied to every embedded image. Lower values produce smaller files. Pass null to skip image compression entirely.

  • removeStructureTree (bool, default false): When true, strips the PDF structure tree to further reduce file size. Most effective for HTML-generated PDFs with tables.

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.

How does IronPDF compress PDFs in C#?

IronPDF handles compression through a single method — CompressAndSaveAs. It reduces embedded image sizes using a configurable JPEG quality (1–100) and can optionally strip the PDF's internal structure tree by setting removeStructureTree: true, which is especially effective 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").CompressAndSaveAs("compressed.pdf", 40); This loads a PDF, compresses images at 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.

Curtis Chau
Technical Writer

Curtis Chau holds a Bachelor’s degree in Computer Science (Carleton University) and specializes in front-end development with expertise in Node.js, TypeScript, JavaScript, and React. Passionate about crafting intuitive and aesthetically pleasing user interfaces, Curtis enjoys working with modern frameworks and creating well-structured, visually appealing manuals.

...

Read More
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 17,803,474 | Version: 2026.3 just released
Still Scrolling Icon

Still Scrolling?

Want proof fast? PM > Install-Package IronPdf
run a sample watch your HTML become a PDF.