C# PDF Compression Library
How to compress, reduce PDF file size using C#.net
C# full source code to Compress & Decompress PDF Document in C# Using .NET PDF Control. Free Online Download.
In this C# tutorial, you learn how to compress PDF document and reduce PDF file size in the C# application through the following settings:
- Image compression
- Unused objects remove
- User data remove
- Document clean up
How to compress PDF file using C#
- Best C# PDF file reducer, optimizer sdk library for shrinking, decreasing large PDF files in Visual Studio .net applications
- A high PDF compressing ratio control for Visual C# and compatible with Windows 32-bit or 64-bit operating system
- Efficient PDF compressor for .NET WinForms application and ASP.NET WebForms application
- Support .NET WinForms, ASP.NET MVC in IIS, ASP.NET Ajax, Azure cloud service, DNN (DotNetNuke), SharePoint
- Support batch compression pdfs with large-size of 1000+ pages to smaller one in a short time while without losing high image quality
- Easy to decrease & compress PDF document file in .NET framework
- Remove bookmarks, annotations, watermark, page labels and article threads from PDF while compressing
- C# class demo code for PDF document optimization in both .NET WinForms and ASPX webpage
- Free full features online compression demo. No email required, no watermark embed.
About PDF document optimization
In XDoc.PDF for .NET SDK, class PDFOptimizer provides many settings for reducing the size of PDF files.
Whether you apply all of these settings or only a few depends on how you intend to use the PDF files and on the essential properties a file must have.
In most situations, the default settings are appropriate for maximum efficiency, saving space by compressing images, and removing objects, user data from the pdf file that are no longer used.
We can use several methods to optimize PDF:
- Image Compression: Allow you set options for color, grayscale, and monochrome image compression, and image downsampling. Since images are usually or large size, images size reducing can help to reduce PDF file size effectively. The disadvantage is that image resources pixel point might be lost.
- Discard Objects: Allow you specify objects to remove from the PDF file. You can discard objects created in PDF reader, editor applications, such as
EdgePDF, and Acrobat. Selecting an object removes all occurrences of that object within the PDF.
- Discard User Data: Allow you to remove, delete any personal information that you don't want to distribute or share with others.
- Document Clean Up: Allow you to remove useless items from the entire PDF document using C#. These items include elements that are obsolete or unnecessary for your intended use of the document.
Our EdgePDF (asp.net pdf viewer and editor web control) has implemented an
ASP.NET PDF document compression solution based on the above functions
Standard methods to compress a PDF file
Just like Acrobat, XDoc.PDF SDK has included several standard methods to optimize a PDF file. These methods can be easily applied to the pdf file through property settings in class "PDFOptimizer".
C#: Reduce a PDF file size by image compression
The image compression settings allow you to set options for color, grayscale, and monochrome image compression, and image downsampling.
- Downsample: Use options "DownsampleMode", "MaxResolutionLimit" and "TargetResolution" to reduces file size by lowering the resolution of images, which involves merging the colors of original pixels into larger pixels.
- Compression: Reduces file size by eliminating unnecessary pixel data. In general, JPEG and JPEG 2000 compressions give better results on images like photographs with gradual transitions from color to color. ZIP is the better choice for illustrations with large areas of solid, flat color, or patterns made up of flat colors. For monochrome images, JBIG2 compression, which is available in PDF Optimizer but not in Distiller, and it is superior to CCITT.
- Quality: Use option "JPEGImageQualityLevel" to apply JPEG and JPEG 2000 formats image quality level.
JPEG and JPEG 2000 compression methods are typically lossy, a process that permanently removes some pixel data.
You can apply lossy JPEG or JPEG 2000 compression to color images at various levels (Minimum, Low, Medium, High, Maximum).
For JPEG 2000 compression, you can also specify lossless so that no pixel data is removed. Compression for monochrome images is lossless, except for JBIG2 compression, which provides both Lossy and Lossless modes of compression.
Our EdgePDF (asp.net pdf viewer and editor web control) has implemented a web PDF document compression solution based on this feature.
Sample Code list below is mainly to optimize PDF file with multiple ways with C# Programming Language in .NET Class.
String inputFilePath = Program.RootPath + "\\" + "3.pdf";
String outputFilePath = Program.RootPath + "\\" + "3_optimized.pdf";
// create optimizing options
PDFOptimizeOptions ops = new PDFOptimizeOptions();
// Enable image optimization
ops.EnableImagesOptimization = true;
// -- Options for Monochrome Image --
// to enable downsampling for those images with resolution higher than 300 dpi to 150 dpi
ops.MonochromeImageOptions.DownsampleMode = ImageDownsampleMode.Bicubic;
ops.MonochromeImageOptions.MaxResolutionLimit = 300F;
ops.MonochromeImageOptions.TargetResolution = 150F;
// to change image compression mode to JBIG2
ops.MonochromeImageOptions.KeepCompressionMode = false;
ops.MonochromeImageOptions.Compression = PDFCompression.JBIG2Decode;
// -- Options for Grayscale Image --
// to enable downsampling for those images with resolution higher than 120 dpi to 96 dpi
ops.GrayscaleImageOptions.DownsampleMode = ImageDownsampleMode.Bilinear;
ops.GrayscaleImageOptions.MaxResolutionLimit = 120F;
ops.GrayscaleImageOptions.TargetResolution = 96F;
// to change image compression mode to DCT
ops.GrayscaleImageOptions.KeepCompressionMode = false;
ops.GrayscaleImageOptions.Compression = PDFCompression.DCTDecode;
// set quality level, only available for compression mode DCT
ops.GrayscaleImageOptions.JPEGImageQualityLevel = JPEGImageQualityLevel.High;
// -- Options for Color Image --
// to enable downsampling for those images with resolution higher than 120 dpi to 96 dpi
ops.ColorImageOptions.DownsampleMode = ImageDownsampleMode.Bicubic;
ops.ColorImageOptions.MaxResolutionLimit = 120F;
ops.ColorImageOptions.TargetResolution = 96F;
// to change image compression mode to DCT
ops.ColorImageOptions.KeepCompressionMode = false;
ops.ColorImageOptions.Compression = PDFCompression.DCTDecode;
// set quality level, only available for compression mode DCT
ops.ColorImageOptions.JPEGImageQualityLevel = JPEGImageQualityLevel.Highest;
// apply optimizing
PDFOptimizer.Optimize(inputFilePath, outputFilePath, ops);
C#: decrease, downsize a PDF file by discarding unnecessary objects
The Discard Objects settings allow you to discard, remove unused objects created in other PDF reader, editor applications, such as
EdgePDF ASP.NET PDF Viewer, Acrobat and in other PDF applications.
- Discard All Form Submission, Import & Reset Actions: Option "DiscardFormActions"
disables all actions related to submit or import form data, and resets form fields.
- Flatten Form Fields: Option "DiscardFormFields" supports make form fields unusable with no change to their appearance. Form data is merged with the page to become page content.
- Discard All JavaScript Actions: Option "DiscardJavaScriptActions" supports remove any actions in the PDF that use JavaScript.
- Discard All Alternate Images: Option "DiscardAlternateImages" removes all versions of an image except the one destined for on-screen viewing. Some PDFs include multiple versions of the same image for different purposes, such as low-resolution on-screen viewing and high-resolution printing.
- Discard Embedded Page Thumbnails: Option "DiscardPageThumbnails" removes embedded page thumbnails.
- Discard Embedded Print Settings: Option "DiscardEmbeddedPrintSettings" removes embedded print settings, such as page scaling and duplex mode, from the document.
- Discard Embedded Search Index: Option "DiscardEmbeddedSearchIndex" removes embedded search indexes, which reduces the file size.
- Discard Bookmarks: Option "DiscardBookmarks" removes all bookmarks from the document.
- Discard Unused Resources: Option "DiscardUnusedResourcesInPage" discards all embedded resources not used in the page.
Our EdgePDF (asp.net pdf viewer and editor web control) has implemented a web PDF document compression solution based on this feature.
String inputFilePath = Program.RootPath + "\\" + "1.pdf";
String outputFilePath = Program.RootPath + "\\" + "optimized.pdf";
// Initial an optimize options object with default settings.
PDFOptimizeOptions ops = new PDFOptimizeOptions();
// Discard all form submission, import and reset actions.
ops.DiscardOptions.DiscardFormActions = true;
// Flatten form fields.
ops.DiscardOptions.FlattenFormFields = true;
// Discard all JavaScript actions.
ops.DiscardOptions.DiscardJavaScriptActions = true;
// Discard all alternate images.
ops.DiscardOptions.DiscardAlternateImages = true;
// Discard embedded page thumbnails.
ops.DiscardOptions.DiscardPageThumbnails = true;
// Discard embedded print settings.
ops.DiscardOptions.DiscardEmbeddedPrintSettings = true;
// Discard embedded search index.
ops.DiscardOptions.DiscardEmbeddedSearchIndex = true;
// Discard bookmarks.
ops.DiscardOptions.DiscardBookmarks = true;
// Discard all unused embedded resources in page.
ops.DiscardOptions.DiscardUnusedResourcesInPage = true;
try
{
PDFOptimizer.Optimize(inputFilePath, outputFilePath, ops);
}
catch (PDFException pdfEx)
{
Console.WriteLine("[Warning]: " + pdfEx.Message);
}
catch (Exception ex)
{
Console.WriteLine("[Error]: unexcepted exception - " + ex.Message);
}
Discard, remove user data from a PDF file using C#
The Discard User Data settings allow you to discard, remove any personal information data which you do not want to distribute with others.
- Discard All Comments: Option "DiscardComments" removes all comments from the PDF.
- Discard AcroForm Fields: Option "DiscardFormFields" removes all form fields from the PDF document.
- Discard All Multimedia: Option "DiscardMultimedia" removes all multimedia from the PDF.
- Discard Document Information: Option "DiscardDocumentInfo" removes information in the document information dictionary
- Discard Metadata: Option "DiscardMetadata" removes all metadata streams
- Discard File Attachments: Option "DiscardFileAttachments" removes all file attachments, including attachments added to the PDF as comments.
- Discard External Cross References: Option "DiscardExternalCrossRefs" removes links to other documents.
- Discard Private Data: Option "DiscardPrivateData" removes information data inside a PDF document
which are created by PDF reader, editor applications. This does not affect the functionality of the PDF, but it will decrease the pdf file size.
- Discard Hidden Layer Content & Flatten Visible Layers: Option "FlattenPageLayers"
removes all layer information from a PDF file. The optimized PDF document looks like the original PDF.
- Discard All Links: Option "DiscardAllLinks" removes links to jump to other locations within the PDF.
Our EdgePDF (asp.net pdf viewer and editor web control) has implemented a web PDF document compression solution based on this feature.
String inputFilePath = Program.RootPath + "\\" + "1.pdf";
String outputFilePath = Program.RootPath + "\\" + "optimized.pdf";
// Initial an optimize options object with default settings.
PDFOptimizeOptions ops = new PDFOptimizeOptions();
// Discard all comments.
ops.DiscardOptions.DiscardComments = true;
// Discard all form fields.
ops.DiscardOptions.DiscardFormFields = true;
// Discard all multimedia.
ops.DiscardOptions.DiscardMultimedia = true;
// Discard document information.
ops.DiscardOptions.DiscardDocumentInfo = true;
// Discard all metadata streams.
ops.DiscardOptions.DiscardMetadata = true;
// Discard file attachments.
ops.DiscardOptions.DiscardFileAttachments = true;
// Discard external cross references.
ops.DiscardOptions.DiscardExternalCrossRefs = true;
// Discard private data of other applications.
ops.DiscardOptions.DiscardPrivateData = true;
// Discard hidden layer content and flatten visible layers.
ops.DiscardOptions.FlattenPageLayers = true;
// Discard all link annotations.
ops.DiscardOptions.DiscardAllLinks = true;
try
{
PDFOptimizer.Optimize(inputFilePath, outputFilePath, ops);
}
catch (PDFException pdfEx)
{
Console.WriteLine("[Warning]: " + pdfEx.Message);
}
catch (Exception ex)
{
Console.WriteLine("[Error]: unexcepted exception - " + ex.Message);
}
Document clean up: remove useless items from the document using C#
The Document clean up settings allow you to remove useless items from the entire PDF document using C#.
These items include elements that are obsolete or unnecessary for your intended use of the document.
- Remove Flate compression in the file: Remove all Flate compressions in the entire PDF file.
When this option is enabled, the next two options will be ignored.
- Use Flate to encode streams that are not encoded:
Apply Flate compression to all streams which are not encoded yet.
- In streams that us LZW encoding, use Flate instead:
Apply Flate compression to all content streams and images which are using LZW encoding.
- Discard invalid bookmarks:
Remove bookmarks that point to invalid pages in the document
- Discard invalid links:
Remove links that jump to invalid destinations.
- Optimize page content:
Convert all end-of-line characters to space characters, which improves Flate compression.
- Discard All Links: Option "DiscardAllLinks" removes links to jump to other locations within the PDF.
Our EdgePDF (asp.net pdf viewer and editor web control) has implemented a web PDF document compression solution based on this feature.
String inputFilePath = Program.RootPath + "\\" + "1.pdf";
String outputFilePath = Program.RootPath + "\\" + "optimized.pdf";
// Initial an optimize options object with default settings.
PDFOptimizeOptions ops = new PDFOptimizeOptions();
// Disable flag RemoveFlateCompression.
ops.DiscardOptions.RemoveFlateCompression = false;
// Use Flate To Encode Streams That Are Not Encoded
// This property is ignored if RemoveFlateCompression is set.
ops.DiscardOptions.UseFlateToEncodePlainStreams = true;
// In Streams That Use LZW Encoding, Use Flate Instead
// This property is ignored if RemoveFlateCompression is set.
ops.DiscardOptions.UseFlateToReplaceLZW = true;
// Discard Invalid Bookmarks
ops.DiscardOptions.DiscardInvalidBookmarks = true;
// Discard Invalid Links
ops.DiscardOptions.DiscardInvalidLinks = true;
// Discard Unreferenced Named Destinations
ops.DiscardOptions.DiscardUnReferencedNamedDest = true;
// Optimize Page Content
ops.DiscardOptions.OptimizePageContent = true;
try
{
PDFOptimizer.Optimize(inputFilePath, outputFilePath, ops);
}
catch (PDFException pdfEx)
{
Console.WriteLine("[Warning]: " + pdfEx.Message);
}
catch (Exception ex)
{
Console.WriteLine("[Error]: unexcepted exception - " + ex.Message);
}
Reduce pdf file size by flatten form fields using c#
String inputFilePath = Program.RootPath + "\\" + "1_AF.pdf";
String outputFilePath = Program.RootPath + "\\" + "output.pdf";
// Flatten all AcroForm fields in the input file.
PDFFormHandler.FlattenFormFields(inputFilePath, outputFilePath);