Tech Specs
How-to C#
How-to VB.NET
C# PDF: How to PDF Create PDF Export File and Page Process PDF Read PDF Write Form Process Document Protect Annotation & Drawing PDF Print WPF Viewer & Editor Work with Other SDKs Barcode Read Barcode Create OCR Twain
File: Compress PDF
Home ›› XDoc.PDF ›› C# PDF: Compress PDF

How to compress, optimize, condense Adobe PDF documents using C# in ASP.NET, Winforms, Azure, Sharepoint

C# full source code to Compress & Decompress PDF Document in C# Using .NET PDF Control. Free Online Download.

Look for HTML5 PDF Editor?

EdgePDF: ASP.NET PDF Editor is the best HTML5 PDF Editor and ASP.NET PDF Viewer based on XDoc.PDF, JQuery, HTML5. It supports ASP.NET MVC and WebForms projects.

Best C# PDF file reducer, optimizer sdk libary 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.

PDF document has been widely used by large enterprises and organizations to distribute and view documents. But in practical applications, the large size issue of PDF document (especially when the PDF document contains large number of images) causes a heavy burden on the bandwidth costs, network transmission time and digital storage. Thus, how to compress large-size existing PDF documents is quite useful for increasing the efficiency and productiveness of existing document management system.

The PDF document compressor control from RasterEdge DocImage SDK for .NET owns strong and mature PDF document compression and decompression functionalities that can help developers reduce the size of existing PDF documents while keeping the original PDF quality. As it's written in managed C# code, this PDF document compressing control is designed to offer C# developers to compress existing PDF documents in .NET class applications, such as ASP.NET web site and Windows Forms application. Using this C#.NET PDF document compressing control, you can easily and quickly complete a high-quality PDF document compression programmatically.

Apart from offering standard solution options for the compression and decompression of PDF document, RasterEdge DocImage SDK for .NET also supports compressing and decompressing other document image files using C#.NET, such as BIG2 compression and decompression method, JPEG2000 compressing & decompression method, TIFF files compression and decompression method and Image files compression and decompression method.

C#.NET PDF Document Optimization

PDF document optimization is mainly to reduce or minimize original PDF document size, mainly methods are given below:

Reduce image resources: 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.

Reduce font resources: Font resources will also take up too much space, glyph file unreferenced can be deleted. But it's too complicated to implement this work.

Delete unimportant contents:

  Flatten form fields

  JavaScript actions

  Alternate images

  Embedded page thumbnails

  Document tags

  Embedded print settings

  Embedded search index


  Comments, forms and multimedia

  Document and metadata

  All object data

  File attachment

  External cross references

  Private data of other applications

  Hidden layer content

  Convert smooth lines to curves

  Detect and merge image fragments

  Flatten visible layers

C#: decrease, downsize a PDF file by image compression

Note: When you get the error "Could not load file or assembly 'RasterEdge.Imaging.Basic' or any other assembly or one of its dependencies. An attempt to load a program with an incorrect format", please check your configure as follows:


       If you are using x64 libraries/dlls, Right click the project -> Properties -> Build -> Platform target: x64.


       If using x86, the platform target should be x86.

Sample Code list below is mainly to optimize PDF file with multiple ways with C# Programming Language in .NET Class.

        #region compress pdf image content
        internal static void compressPdfImage()
            String inputFilePath = @"C:\3.pdf";
            String outputFilePath = @"C:\3_optimized.pdf";

            //  create optimizing options
            PDFOptimizeOptions ops = new PDFOptimizeOptions();

            //  -- 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

PDFOptimizeOptions ops = new PDFOptimizeOptions();
ops.DiscardOptions.DiscardAllLinks = true;
ops.DiscardOptions.DiscardAnnotations = true;
ops.DiscardOptions.DiscardBookmarks = true;
ops.DiscardOptions.DiscardDocumentInfo = true;
ops.DiscardOptions.DiscardFormFields = true;
ops.DiscardOptions.DiscardPageThumbnails = true;

String inputFilePath = @"input.pdf";
String outputFilePath = @"output.pdf";

    PDFOptimizer.Optimize(inputFilePath, outputFilePath, ops);
catch (PDFException pdfEx)
    Console.WriteLine("[Warning]: " + pdfEx.Message);
catch (Exception ex)
    Console.WriteLine("[Error]: unexcepted exception - " + ex.Message);

.NET Class Namespace Required

In order to run the sample code, the following steps would be necessary.

Add necessary references:









Use corresponding namespaces;

  using RasterEdge.Imaging.Basic;

  using RasterEdge.XDoc.PDF;