Skip to main content
You are viewing help content for pre-release software. This document and the features it describes are subject to change.
All docs
V25.2
  • Optimize Documents with DevExpress PDF Document API

    • 4 minutes to read

    The PDF Document API allows you to optimize PDF documents to reduce file size. You can compress images with different quality settings and resolutions, and remove unnecessary elements such as metadata, attachments, bookmarks, thumbnails, JavaScript actions, and logical structure information.

    This topic contains API reference documentation and C#/VB.NET code examples that perform the following actions:

    Compress Images

    Use the OptimizeDocument(PdfImageCompressionOptions) method to reduce file size by compressing images. Create a PdfImageCompressionOptions object and configure the following settings:

    General Compression Settings

    • CompressionType — chooses between lossy (JPEG) or lossless (Flate) compression.
    • JpegQuality — sets JPEG image quality from 0 (smallest file, lowest quality) to 100 (largest file, highest quality).
    • DownsamplingResolution — reduces image resolution.
    • InterpolationMode — controls how images are resampled when the resolution changes.

    Specialized Compression for Different Image Types

    • GrayscaleCompressionType — specifies the compression algorithm for grayscale images (JPEG or Flate).
    • BitonalCompressionType — specifies the compression algorithm for bitonal (black and white) images (CCITT Group 3, CCITT Group 4, or Flate).
    • ConvertGrayscale — converts all color images to grayscale to reduce file size.

    Example

    The following code snippet compresses images in a PDF document with specialized compression algorithms for different image types:

    using DevExpress.Pdf;
    
    using (PdfDocumentProcessor processor = new PdfDocumentProcessor()) {
        // Load the document
        processor.LoadDocument("Document.pdf");
    
        // Configure image compression options
        var options = new PdfImageCompressionOptions() {
            // General compression settings
            CompressionType = PdfImageCompressionType.Jpeg,
            JpegQuality = 75,
            DownsamplingResolution = 150,
    
            // Convert color images to grayscale
            ConvertGrayscale = true,
    
            // Compression for grayscale images
            GrayscaleCompressionType = PdfGrayscaleImageCompressionType.Jpeg,
    
            // Compression for bitonal (black and white) images
            BitonalCompressionType = PdfBitonalImageCompressionType.CCITTGroup4
        };
    
        // Optimize the document
        processor.OptimizeDocument(options);
    
        // Save the result
        processor.SaveDocument("Document.optimized.pdf");
    }
    

    Remove Unnecessary Content

    Use the OptimizeDocument(PdfOptimizationOptions) method to optimize file size by removing unused elements. Create a PdfOptimizationOptions object and specify elements to remove:

    You can also compress images as part of comprehensive optimization. To do this, assign a PdfImageCompressionOptions object to the ImageCompressionOptions property.

    Example

    The following code snippet removes metadata, attachments, and compresses images:

    using DevExpress.Pdf;
    
    using (PdfDocumentProcessor processor = new PdfDocumentProcessor()) {
        // Load the document
        processor.LoadDocument("Document.pdf");
    
        // Configure optimization options
        var options = new PdfOptimizationOptions() {
            // Remove unnecessary elements
            RemoveMetadata = true,
            RemoveAttachments = true,
            RemoveThumbnails = true,
            // Compress images
            ImageCompressionOptions = new PdfImageCompressionOptions() {
                CompressionType = PdfImageCompressionType.Jpeg,
                JpegQuality = 80,
            }
        };
    
        // Optimize the document
        processor.OptimizeDocument(options);
    
        // Save the result
        processor.SaveDocument("Document.optimized.pdf");
    }
    

    Reduce File Size with Object Streams

    Activate the UseObjectStreams option to use object streams when saving PDF documents. Object streams (PDF 1.5+) pack multiple PDF objects into a single compressed stream to reduce file size.

    Set the UseObjectStreams property to true in a PdfSaveOptions object and pass it to the SaveDocument method.

    Example

    The following code snippet merges multiple PDF files into a single document and uses object streams to reduce the output file size:

    using DevExpress.Pdf;
    
    using (PdfDocumentProcessor processor = new PdfDocumentProcessor()) {
        // Create an empty document with object streams enabled
        var saveOptions = new PdfSaveOptions() {
            UseObjectStreams = true
        };
        processor.CreateEmptyDocument("Merged.pdf", saveOptions);
    
        // Append multiple PDF files
        processor.AppendDocument("Document1.pdf");
        processor.AppendDocument("Document2.pdf");
        processor.AppendDocument("Document3.pdf");
    }