Docotic.Pdf Library Help > Samples > Compression > Compress PDF document in .NET

This sample shows how to optimize size of an existing PDF document using Docotic.Pdf.

NOTE: To get proper results please use this sample code with a license key. You can get a free license key here:

http://bitmiracle.com/pdf-library/download-pdf-library.aspx

Docotic.Pdf supports different compression methods. You can use all of them to get the best compression ratio:

1. recompress images (if any)

2. setup save options

3. remove structure information

4. flatten form fields

5. remove metadata

6. unembed fonts

C#
using System;
using System.Diagnostics;
using System.IO;
using System.Windows.Forms;
using BitMiracle.Docotic.Pdf;

namespace BitMiracle.Docotic.Pdf.Samples
{
    public static class CompressAllTechniques
    {
        public static void Main()
        {
            // NOTE: 
            // When used in trial mode, the library imposes some restrictions.
            // Please visit http://bitmiracle.com/pdf-library/trial-restrictions.aspx
            // for more information.

            const string originalFile = @"Sample Data\jpeg.pdf";
            const string compressedFile = "CompressAllTechniques.pdf";

            using (PdfDocument pdf = new PdfDocument(originalFile))
            {
                // 1. Recompress images
                foreach (PdfPage page in pdf.Pages)
                {
                    foreach (PdfPaintedImage painted in page.GetPaintedImages())
                    {
                        PdfImage image = painted.Image;

                        // image that is used as mask or image with attached mask are
                        // not good candidates for recompression
                        if (image.IsMask || image.Mask != null || image.Width < 8 || image.Height < 8)
                            continue;

                        // get size of the painted image
                        int width = Math.Max(1, (int)painted.Bounds.Width);
                        int height = Math.Max(1, (int)painted.Bounds.Height);

                        if (image.ComponentCount == 1 && image.BitsPerComponent == 1 &&
                            image.Compression != PdfImageCompression.Group4Fax)
                        {
                            image.RecompressWithGroup4Fax();
                        }
                        else if (width < image.Width || height < image.Height)
                        {
                            // NOTE: PdfImage.ResizeTo() method is not supported in version for .NET Standard
                            if (image.ComponentCount >= 3)
                                image.ResizeTo(width, height, PdfImageCompression.Jpeg, 90);
                            else
                                image.ResizeTo(width, height, PdfImageCompression.Flate, 9);
                        }
                    }
                }

                // 2 Setup save options
                pdf.SaveOptions.Compression = PdfCompression.Flate;
                pdf.SaveOptions.UseObjectStreams = true;
                pdf.SaveOptions.RemoveUnusedObjects = true;
                pdf.SaveOptions.OptimizeIndirectObjects = true;
                pdf.SaveOptions.WriteWithoutFormatting = true;

                // 3. Remove structure information
                pdf.RemoveStructureInformation();

                // 4. Flatten form fields
                // Controls become uneditable after that
                pdf.FlattenControls();

                // 5. Clear metadata
                pdf.Metadata.Basic.Clear();
                pdf.Metadata.DublinCore.Clear();
                pdf.Metadata.MediaManagement.Clear();
                pdf.Metadata.Pdf.Clear();
                pdf.Metadata.RightsManagement.Clear();

                pdf.Metadata.Custom.Properties.Clear();

                foreach (XmpSchema schema in pdf.Metadata.Schemas)
                    schema.Properties.Clear();

                pdf.Info.Clear(false);

                // 6. Unembed fonts
                foreach (PdfFont font in pdf.Fonts)
                {
                    // Only unembed popular fonts installed in the typical OS. You can extend
                    // the list of such fonts in the "if" statement below.
                    if (font.Name == "Arial" || font.Name == "Verdana")
                        font.Unembed();
                }

                pdf.Save(compressedFile);
            }

            string message = string.Format(
                "Original file size: {0} bytes;\r\nCompressed file size: {1} bytes",
                new FileInfo(originalFile).Length,
                new FileInfo(compressedFile).Length
            );
            MessageBox.Show(message);

            Process.Start(compressedFile);
        }
    }
}
VB
Imports System
Imports System.Diagnostics
Imports System.IO
Imports System.Windows.Forms

Imports Microsoft.VisualBasic

Imports BitMiracle.Docotic.Pdf

Namespace BitMiracle.Docotic.Pdf.Samples
    Public NotInheritable Class CompressAllTechniques
        Public Shared Sub Main()
            ' NOTE: 
            ' When used in trial mode, the library imposes some restrictions.
            ' Please visit http://bitmiracle.com/pdf-library/trial-restrictions.aspx
            ' for more information.

            Const originalFile As String = "Sample Data\jpeg.pdf"
            Const compressedFile As String = "CompressAllTechniques.pdf"

            Using pdf As New PdfDocument(originalFile)
                ' 1. Recompress images
                For Each page As PdfPage In pdf.Pages
                    For Each painted As PdfPaintedImage In page.GetPaintedImages()
                        Dim image As PdfImage = painted.Image

                        ' image that is used as mask or image with attached mask are
                        ' not good candidates for recompression
                        If image.IsMask OrElse image.Mask IsNot Nothing OrElse image.Width < 8 OrElse image.Height < 8 Then
                            Continue For
                        End If

                        ' get size of the painted image
                        Dim width As Integer = Math.Max(1, CInt(painted.Bounds.Width))
                        Dim height As Integer = Math.Max(1, CInt(painted.Bounds.Height))

                        If image.ComponentCount = 1 AndAlso image.BitsPerComponent = 1 AndAlso image.Compression <> PdfImageCompression.Group4Fax Then
                            image.RecompressWithGroup4Fax()
                        ElseIf width < image.Width OrElse height < image.Height Then
                            ' NOTE: PdfImage.ResizeTo() method is not supported in version for .NET Standard
                            If image.ComponentCount = 3 Then
                                image.ResizeTo(width, height, PdfImageCompression.Jpeg, 90)
                            Else
                                image.ResizeTo(width, height, PdfImageCompression.Flate, 9)
                            End If
                        End If
                    Next
                Next

                ' 2 Setup save options
                pdf.SaveOptions.Compression = PdfCompression.Flate
                pdf.SaveOptions.UseObjectStreams = True
                pdf.SaveOptions.RemoveUnusedObjects = True
                pdf.SaveOptions.OptimizeIndirectObjects = True
                pdf.SaveOptions.WriteWithoutFormatting = True

                ' 3. Remove structure information
                pdf.RemoveStructureInformation()

                ' 4. Flatten form fields
                ' Controls become uneditable after that
                pdf.FlattenControls()

                ' 5. Clear metadata
                pdf.Metadata.Basic.Clear()
                pdf.Metadata.DublinCore.Clear()
                pdf.Metadata.MediaManagement.Clear()
                pdf.Metadata.Pdf.Clear()
                pdf.Metadata.RightsManagement.Clear()

                pdf.Metadata.[Custom].Properties.Clear()

                For Each schema As XmpSchema In pdf.Metadata.Schemas
                    schema.Properties.Clear()
                Next

                pdf.Info.Clear(False)

                ' 6. Unembed fonts
                For Each font As PdfFont In pdf.Fonts
                    ' Only unembed popular fonts installed in the typical OS. You can extend
                    ' the list of such fonts in the "if" statement below.
                    If font.Name = "Arial" OrElse font.Name = "Verdana" Then
                        font.Unembed()
                    End If
                Next

                pdf.Save(compressedFile)
            End Using

            Dim message As String = String.Format(
                "Original file size: {0} bytes;" & vbCr & vbLf & "Compressed file size: {1} bytes",
                New FileInfo(originalFile).Length,
                New FileInfo(compressedFile).Length
            )
            MessageBox.Show(message)

            Process.Start(compressedFile)
        End Sub
    End Class
End Namespace