Reference Guide
Programming / PDF Optimization & MRC / Optimizing PDF documents by controlling image compressions
In This Topic
    Optimizing PDF documents by controlling image compressions
    In This Topic

    The PDF specification allows for seven compression schemes, all of which can be used to compress images. Two of them offer particularly exciting optimization opportunities: JBIG2 methods for bitonal images (usually black and white) and JPEG2000 for 24-bit color and 8-bit grayscale images. If it is necessary to recompress images in an existing PDF file with these specific compression schemes, the GdPicturePDFReducer class offers to take full control over the process. With the help of PDFReducerConfiguration class, you only need to select required options for the compression. Both pros and cons of these compression schemes are discussed in details in this beneficial article.

    This example shows the usage of the GdPicturePDFReducer class, effortless and straightforward for this purpose.

    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 = "Image compressions"
    
    '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.
    
    'Automatic color detection.
    gdpicturePDFReducer.PDFReducerConfiguration.EnableColorDetection = True
    
    'Repairing characters.
    gdpicturePDFReducer.PDFReducerConfiguration.EnableCharRepair = True
    
    'Controlling the image compression.
    gdpicturePDFReducer.PDFReducerConfiguration.EnableJPEG2000 = True 'for colored images
    gdpicturePDFReducer.PDFReducerConfiguration.EnableJBIG2 = True 'for bitonal images
    gdpicturePDFReducer.PDFReducerConfiguration.JBIG2PMSThreshold = 0.65F
    
    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 = "Image compressions";
    
    //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.
    
    //Automatic color detection.
    gdpicturePDFReducer.PDFReducerConfiguration.EnableColorDetection = true;
    
    //Repairing characters.
    gdpicturePDFReducer.PDFReducerConfiguration.EnableCharRepair = true;
    
    //Controlling the image compression.
    gdpicturePDFReducer.PDFReducerConfiguration.EnableJPEG2000 = true; //for colored images
    gdpicturePDFReducer.PDFReducerConfiguration.EnableJBIG2 = true; //for bitonal images
    gdpicturePDFReducer.PDFReducerConfiguration.JBIG2PMSThreshold = 0.65f;
    
    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.
    }