Reference Guide
Programming / PDF Optimization & MRC / Optimizing PDF documents by re-compressing images
In This Topic
    Optimizing PDF documents by re-compressing images
    In This Topic

    Images are the next big part of the optimization process. You can take control of the image compression by recompressing existing images in the PDF document. Decreasing unnecessary high resolutions can dramatically reduce the file size without affecting the viewing experience. Another benefit is that the engine optimizes images in case of size reduction only. The evidence of achieved compressions is provided in this handy article.

    Let's benefit from GdPicturePDFReducer class when reducing images. With the help of PDFReducerConfiguration class, you can select options you find appropriate for your documents to save storage space. Here is an example.

    This class is only available in Ultimate Edition of the toolkit.
    Copy Code
    Dim gdpicturePDFReducer As GdPicturePDFReducer = New GdPicturePDFReducer()
    
    'PDFReducerConfiguration class provides different properties and options for the compression.
    gdpicturePDFReducer.PDFReducerConfiguration.Author = "GdPicture.NET PDF Reducer SDK"
    gdpicturePDFReducer.PDFReducerConfiguration.Producer = "GdPicture.NET 14"
    gdpicturePDFReducer.PDFReducerConfiguration.ProducerName = "Orpalis"
    gdpicturePDFReducer.PDFReducerConfiguration.Title = "Re-compress images"
    
    'When compressing your PDF files, you have the possibility to decide which version of PDF to use.
    gdpicturePDFReducer.PDFReducerConfiguration.OutputFormat = PDFReducerPDFVersion.PdfVersionRetainExisting
    
    'By selecting required options through the PDFReducerConfiguration class you enable or disable the features you want to accent.
    
    'Recompressing images to obtain better compression ratio.
    gdpicturePDFReducer.PDFReducerConfiguration.RecompressImages = True
    gdpicturePDFReducer.PDFReducerConfiguration.ImageQuality = PDFReducerImageQuality.ImageQualityHigh
    
    'Reducing the size by decreasing the image resolution.
    gdpicturePDFReducer.PDFReducerConfiguration.DownscaleImages = True
    gdpicturePDFReducer.PDFReducerConfiguration.DownscaleResolution = 200
    
    Dim inputSize As Long = New System.IO.FileInfo("input.pdf").Length
    
    'Processing the specified document.
    Dim status As GdPictureStatus = gdpicturePDFReducer.ProcessDocument("input.pdf", "output.pdf")
    If status = GdPictureStatus.OK Then
        Dim outputSize As Long = New System.IO.FileInfo("output.pdf").Length
        Dim ratio As Integer = 100 - CInt((outputSize * 100 / inputSize))
        MessageBox.Show("The compression ratio is " + ratio + "%." + vbCrLf + inputSize / 100 + "KB -> " + outputSize / 100 + "KB", "Optimizing PDF documents", MessageBoxButtons.OK, MessageBoxIcon.Information)
    Else
        MessageBox.Show("The compression failed. Error: " + gdpicturePDFReducer.GetReducerStat(), "Optimizing PDF documents", MessageBoxButtons.OK, MessageBoxIcon.Error)
        'You can also check reported warnings or the page number when an error has been reported.
    End If
    Copy Code
    GdPicturePDFReducer gdpicturePDFReducer = new GdPicturePDFReducer();
    
    //PDFReducerConfiguration class provides different properties and options for the compression.
    gdpicturePDFReducer.PDFReducerConfiguration.Author = "GdPicture.NET PDF Reducer SDK";
    gdpicturePDFReducer.PDFReducerConfiguration.Producer = "GdPicture.NET 14";
    gdpicturePDFReducer.PDFReducerConfiguration.ProducerName = "Orpalis";
    gdpicturePDFReducer.PDFReducerConfiguration.Title = "Re-compress images";
    
    //When compressing your PDF files, you have the possibility to decide which version of PDF to use.
    gdpicturePDFReducer.PDFReducerConfiguration.OutputFormat = PDFReducerPDFVersion.PdfVersionRetainExisting;
    
    //By selecting required options through the PDFReducerConfiguration class you enable or disable the features you want to accent.
    
    //Recompressing images to obtain better compression ratio.
    gdpicturePDFReducer.PDFReducerConfiguration.RecompressImages = true;
    gdpicturePDFReducer.PDFReducerConfiguration.ImageQuality = PDFReducerImageQuality.ImageQualityHigh;
    
    //Reducing the size by decreasing the image resolution.
    gdpicturePDFReducer.PDFReducerConfiguration.DownscaleImages = true;
    gdpicturePDFReducer.PDFReducerConfiguration.DownscaleResolution = 200;
    
    long inputSize = new System.IO.FileInfo("input.pdf").Length;
    
    //Processing the specified document.
    GdPictureStatus status = gdpicturePDFReducer.ProcessDocument("input.pdf", "output.pdf");
    if (status == GdPictureStatus.OK)
    {
        long outputSize = new System.IO.FileInfo("output.pdf").Length;
        int ratio = 100 - (int)(outputSize * 100 / inputSize);
        MessageBox.Show("The compression ratio is " + ratio + "%.\n" + inputSize / 100 + "KB -> " + outputSize / 100 + "KB", "Optimizing PDF documents", MessageBoxButtons.OK, MessageBoxIcon.Information);
    }
    else
    {
        MessageBox.Show("The compression failed. Error: " + gdpicturePDFReducer.GetReducerStat(), "Optimizing PDF documents", MessageBoxButtons.OK, MessageBoxIcon.Error);
        //You can also check reported warnings or the page number when an error has been reported.
    }