Pdf Compression | |
SelectPdf Library for .NET offers the possibility to reduce the size of newly generated or existing pdf documents using compression.
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.
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), compressionLevelValue, true); // create a new pdf font PdfFont font = doc.Fonts.Add(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("Sample.pdf"); // close pdf document doc.Close();
SelectPdf can also compress existing PDF documents. Load the document with the loaded-document constructor (PdfDocument), set the compression options and save - the optimizer runs once during the save. This is built into the main library; no additional package is required.
Two independent compression axes control how the document is optimized:
Compress PDF streams and fonts - the 3 compression levels of CompressionLevel (PdfCompressionLevel.NoCompression, PdfCompressionLevel.Normal, PdfCompressionLevel.Best) control Flate/DEFLATE compression on content streams and fonts. Best additionally strips unused embedded font subsets.
Compress images from the PDF document - controlled by JpegCompressionEnabled and JpegCompressionLevel (a value between 0 - best quality / least compression, and 100 - most compression). When enabled, image XObjects are re-encoded as JPEG.
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"; PdfDocument doc = new PdfDocument(file); // set compression options doc.CompressionLevel = PdfCompressionLevel.Best; // streams + fonts doc.JpegCompressionEnabled = true; // re-encode images as JPEG // 0 = best quality, 100 = most compression doc.JpegCompressionLevel = 20; // save compressed document (optimizer runs once during save) doc.Save(file + "-compressed.pdf"); // get pdf document details Console.WriteLine("Finished compressing PDF document with {0} pages.", doc.Pages.Count); Console.WriteLine("Title: " + doc.DocumentInformation.Title); Console.WriteLine("Author: " + doc.DocumentInformation.Author); Console.WriteLine("Producer: " + doc.DocumentInformation.Producer); doc.Close();