Docotic.Pdf Library Help > Samples > Images > Add recompressed images

This sample shows how to change images compression while 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 method. Then setup compression options for all or selected frames. If needed, each frame will be recompressed before adding it to document.

You can compress images using Flate, Jpeg and CCITT compression schemes. You may even completely decompress an image if you wish or have to do so.

You can also recompress existing images in a PDF document. Please look at "Compression / Optimize images in PDF document" sample for more detail.

using System.Diagnostics;

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

            string pathToFile = "AddRecompressedImages.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);


Imports System.Diagnostics

Imports BitMiracle.Docotic.Pdf

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

            Dim pathToFile As String = "AddRecompressedImages.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