Docotic.Pdf Library Help > Samples > Compression > Optimize images in PDF document

This sample shows how to optimize existing images in PDF document.

Use one of PdfImage.RecompressWith* methods to optimize images in PDF document. Recompression of images is an effective way to greatly decrease size of the output PDF file.

You can also resize images in a PDF document. To do so please use one of the PdfImage.ResizeTo or PdfImage.Scale methods. Note that resizing of images is not supported in version for .NET Standard.

You can recompress images using Flate, Jpeg and CCITT compression schemes. Please note that recompression is often a lossy process.

C#
using System;
using System.Diagnostics;
using System.IO;
using System.Windows.Forms;

namespace BitMiracle.Docotic.Pdf.Samples
{
    public static class OptimizeImages
    {
        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 = "OptimizeImages.pdf";

            using (PdfDocument pdf = new PdfDocument(originalFile))
            {
                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);
                        }
                    }
                }

                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 OptimizeImages
        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 = "OptimizeImages.pdf"

            Using pdf As New PdfDocument(originalFile)
                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

                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