Click or drag to resize
Pdf Library for .NET

Pdf Compression

SelectPdf Library for .NET offers the possibility to reduce the size of newly generated or existing pdf documents using compression.

Compress new PDF documents

Using SelectPdf Library for .NET, PDF compression can be enabled for newly created pdf documents.

Compression Levels

SelectPdf offers 3 compression levels that can be controlled using CompressionLevel property of the PdfDocument object:

  • NoCompression: PDF file is generated without any compression.

  • Normal: PDF compression at normal speed with normal reduction of data size.

  • Best: PDF best compression, but more time consuming.

Sample Code

This sample code shows how to create a new PDF document using SelectPdf, how to add a text element to it and use compression to reduce the size of the generated pdf document.

// create a new pdf document
PdfDocument doc = new PdfDocument();
doc.Margins = new PdfMargins(20, 20, 20, 20);

// set compression level
doc.CompressionLevel = (PdfCompressionLevel)Enum.Parse(
    typeof(PdfCompressionLevel), DdlPdfCompressionLevel.SelectedValue, true);

// create a new pdf font
PdfFont font = doc.AddFont(PdfStandardFont.Helvetica);
font.Size = 16;

// add a new page to the document
PdfPage page = doc.AddPage();

// create a new text element and add it to the page
PdfTextElement text = new PdfTextElement(0, 0, 
    Helper.SomeVeryLongText(), font);
page.Add(text);

// save pdf document
doc.Save(Response, false, "Sample.pdf");

// close pdf document
doc.Close();
Compress existing PDF documents

SelectPdf library can be used to compress existing PDF documents. Using PdfCompressor, PDF documents can be loaded and optimized to decrease the size of the file.

Note: This feature requires the installation of SelectPdf.Extras package. See Installation for more details.

There are several settings that can be used to control how PDF documents are compressed:

  • Compress PDF streams - SelectPdf offers 3 compression levels that can be controlled using StreamsCompressionLevel property of the PdfCompressor object: NoCompression (PDF file is generated without any compression), Normal (PDF compression at normal speed with normal reduction of data size), Best (PDF best compression, but more time consuming).

  • PDF structure compression - The property StructureCompressionEnabled can be used to enable PDF structure compression or not.

  • Remove duplicated fonts - The property RemoveDuplicateFonts can be used to indicate if the duplicated fonts are removed or not from the pdf document.

  • Remove unused font glyphs - The property RemoveUnusedFontGlyphs can be used to indicate if the unused font glyphs are removed or not from the pdf document.

  • Compress images from the PDF document - This can be controlled using the properties ImagesCompressionEnabled and ImagesCompressionLevel (This is a value between 0 - no compression, 100 - best compression).

Sample Code

This sample code shows how to load an existing PDF document using SelectPdf and compress it to reduce the file size.

// load existing file
string file = @"C:\Path\to\document.pdf";
SelectPdf.Compressor.PdfCompressor compressor = new SelectPdf.Compressor.PdfCompressor();
compressor.Load(file);

// set compression options
compressor.StreamsCompressionLevel = SelectPdf.Compressor.PdfCompressionLevel.BestCompression;
compressor.StructureCompressionEnabled = true;
compressor.RemoveDuplicateFonts = true;
compressor.RemoveUnusedFontGlyphs = true;
compressor.ImagesCompressionEnabled = true;
compressor.ImagesCompressionLevel = 20;

// save compressed document
compressor.Save(file + "-compressed.pdf");

// get pdf document details and the result of the compression
Console.WriteLine("Finished compressing PDF document with {0} pages.", compressor.DocumentInformation.PageCount);
Console.WriteLine("Title: " + compressor.DocumentInformation.Title);
Console.WriteLine("Subject: " + compressor.DocumentInformation.Subject);
Console.WriteLine("Author: " + compressor.DocumentInformation.Author);
Console.WriteLine("Producer: " + compressor.DocumentInformation.Producer);
Console.WriteLine("Creation date: " + compressor.DocumentInformation.CreationDate);
Console.WriteLine("Modification date: " + compressor.DocumentInformation.LastModifiedDate);

Console.WriteLine("Initial file size: " + compressor.DocumentInformation.InitialFileSize);
Console.WriteLine("Final file size: " + compressor.DocumentInformation.FinalFileSize);
See Also