Add recompressed images

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

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.

C# 
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 http://bitmiracle.com/pdf-library/trial-restrictions.aspx
            // 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);

                pdf.Save(pathToFile);
            }

            Process.Start(pathToFile);
        }
    }
}
Visual Basic 
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 http://bitmiracle.com/pdf-library/trial-restrictions.aspx
            ' 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)

                pdf.Save(pathToFile)
            End Using

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