Reference Guide
Programming / PDF Optimization & MRC / Optimizing PDF documents in general
In This Topic
    Optimizing PDF documents in general
    In This Topic

    PDF Optimization is about serializing several compression algorithms to surpass the limitations of some compression schemes while removing unwanted or unused objects and applying several other techniques when necessary. The GdPicturePDFReducer class provides innovative and highly sophisticated techniques to help anyone dramatically reduce the file size of PDF documents. With the help of PDFReducerConfiguration class, it is allowed to apply selected features to address all areas of compression and optimization, with a focus on font optimization, data compression, and image analysis.

    This example focuses on general aspects of PDF optimization such as content removal and fonts optimization. Further topic explanations are covered in details in this beneficial article.

    The usage of the GdPicturePDFReducer class is effortless and straightforward. Here is the basic workflow.

    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 = "PDF Optimization"
    
    '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.
    
    'Content removal options - both interactive and document content.
    gdpicturePDFReducer.PDFReducerConfiguration.RemoveAnnotations = True
    gdpicturePDFReducer.PDFReducerConfiguration.RemoveBlankPages = True
    gdpicturePDFReducer.PDFReducerConfiguration.RemoveBookmarks = True
    gdpicturePDFReducer.PDFReducerConfiguration.RemoveEmbeddedFiles = True
    gdpicturePDFReducer.PDFReducerConfiguration.RemoveFormFields = True
    gdpicturePDFReducer.PDFReducerConfiguration.RemoveHyperlinks = True
    gdpicturePDFReducer.PDFReducerConfiguration.RemoveJavaScript = True
    gdpicturePDFReducer.PDFReducerConfiguration.RemoveMetadata = True
    gdpicturePDFReducer.PDFReducerConfiguration.RemovePageThumbnails = True
    
    'Greatly optimizes output file size by focusing on fonts.
    gdpicturePDFReducer.PDFReducerConfiguration.PackFonts = True
    
    'Packing the document content before saving.
    gdpicturePDFReducer.PDFReducerConfiguration.PackDocument = True
    
    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 = "PDF Optimization";
    
    //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.
    
    //Content removal options - both interactive and document content.
    gdpicturePDFReducer.PDFReducerConfiguration.RemoveAnnotations = true;
    gdpicturePDFReducer.PDFReducerConfiguration.RemoveBlankPages = true;
    gdpicturePDFReducer.PDFReducerConfiguration.RemoveBookmarks = true;
    gdpicturePDFReducer.PDFReducerConfiguration.RemoveEmbeddedFiles = true;
    gdpicturePDFReducer.PDFReducerConfiguration.RemoveFormFields = true;
    gdpicturePDFReducer.PDFReducerConfiguration.RemoveHyperlinks = true;
    gdpicturePDFReducer.PDFReducerConfiguration.RemoveJavaScript = true;
    gdpicturePDFReducer.PDFReducerConfiguration.RemoveMetadata = true;
    gdpicturePDFReducer.PDFReducerConfiguration.RemovePageThumbnails = true;
    
    //Greatly optimizes output file size by focusing on fonts.
    gdpicturePDFReducer.PDFReducerConfiguration.PackFonts = true;
    
    //Packing the document content before saving.
    gdpicturePDFReducer.PDFReducerConfiguration.PackDocument = true;
    
    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.
    }