Recompress images

Docotic.Pdf Library Help > Samples > Images > Recompress images
Docotic.Pdf documentation

This sample shows how to recompress images before adding them to a PDF document.

To recompress an image or a frame of an image before adding it to a PDF document, open the image using PdfDocument.OpenImage and setup compression options for all or selected frames. Each frame will be recompressed if needed before adding it to document.

You can also recompress existing images in a PDF document. To do so please use one of the PdfImage.RecompressWith* methods.

Flate, Jpeg and CCITT compression methods are supported. You may even completely decompress an image if you wish or have to do so.

using System.Diagnostics;

namespace BitMiracle.Docotic.Pdf.Samples
    public static class RecompressImage
        public static void Main()
            // NOTE: 
            // When used in trial mode, the library imposes some restrictions.
            // Please visit
            // for more information.

            string pathToFile = "RecompressImage.pdf";

            using (PdfDocument pdf = new PdfDocument())
                PdfCanvas canvas = pdf.Pages[0].Canvas;

                PdfImageFrames imageFrames = pdf.OpenImage(@"Sample Data\pink.png");
                PdfImage originalImage = pdf.AddImage(imageFrames[0]);
                canvas.DrawImage(originalImage, 10, 10, 0);

                PdfImageFrames imageFramesToRecompress = pdf.OpenImage(@"Sample Data\pink.png");
                PdfImageFrame frame = imageFramesToRecompress[0];
                frame.OutputCompression = PdfImageCompression.Jpeg;
                frame.JpegQuality = 50;
                frame.RecompressAlways = true;

                PdfImage compressedImage = pdf.AddImage(frame);
                canvas.DrawImage(compressedImage, 210, 10, 0);


Visual Basic 
Imports System.Diagnostics

Imports BitMiracle.Docotic.Pdf

Namespace BitMiracle.Docotic.Pdf.Samples
    Public NotInheritable Class RecompressImage
        Public Shared Sub Main()
            ' NOTE: 
            ' When used in trial mode, the library imposes some restrictions.
            ' Please visit
            ' for more information.

            Dim pathToFile As String = "RecompressImage.pdf"

            Using pdf As New PdfDocument()
                Dim canvas As PdfCanvas = pdf.Pages(0).Canvas

                Dim imageFrames As PdfImageFrames = pdf.OpenImage("Sample Data\pink.png")
                Dim originalImage As PdfImage = pdf.AddImage(imageFrames(0))
                canvas.DrawImage(originalImage, 10, 10, 0)

                Dim imageFrames2 As PdfImageFrames = pdf.OpenImage("Sample Data\pink.png")
                Dim frame As PdfImageFrame = imageFrames2(0)
                frame.OutputCompression = PdfImageCompression.Jpeg
                frame.JpegQuality = 50
                frame.RecompressAlways = True

                Dim compressedImage As PdfImage = pdf.AddImage(frame)
                canvas.DrawImage(compressedImage, 210, 10, 0)

            End Using

        End Sub
    End Class
End Namespace