Back to Devexpress

Optimize Documents with DevExpress PDF Document API

officefileapi-405623-pdf-document-api-document-manipulation-optimize-documents.md

latest9.2 KB
Original Source

Optimize Documents with DevExpress PDF Document API

  • Nov 04, 2025
  • 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:

csharp
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");
}
vb
Imports DevExpress.Pdf

Using processor As New PdfDocumentProcessor()
    ' Load the document
    processor.LoadDocument("Document.pdf")

    ' Configure image compression options
    Dim options As New PdfImageCompressionOptions() With {
        .CompressionType = PdfImageCompressionType.Jpeg,
        .JpegQuality = 75,
        .DownsamplingResolution = 150,
        .ConvertGrayscale = True,
        .GrayscaleCompressionType = PdfGrayscaleImageCompressionType.Jpeg,
        .BitonalCompressionType = PdfBitonalImageCompressionType.CCITTGroup4
    }

    ' Optimize the document
    processor.OptimizeDocument(options)

    ' Save the result
    processor.SaveDocument("Document.optimized.pdf")
End Using

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:

csharp
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");
}
vb
Imports DevExpress.Pdf

Using processor As New PdfDocumentProcessor()
    ' Load the document
    processor.LoadDocument("Document.pdf")

    ' Configure optimization options
    Dim options As New PdfOptimizationOptions() With {
        ' Remove unnecessary elements
        .RemoveMetadata = True,
        .RemoveAttachments = True,
        .RemoveThumbnails = True,
        ' Compress images
        .ImageCompressionOptions = New PdfImageCompressionOptions() With {
            .CompressionType = PdfImageCompressionType.Jpeg,
            .JpegQuality = 80
        }
    }

    ' Optimize the document
    processor.OptimizeDocument(options)

    ' Save the result
    processor.SaveDocument("Document.optimized.pdf")
End Using

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:

csharp
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");
}
vb
Imports DevExpress.Pdf

Using processor As New PdfDocumentProcessor()
    ' Create an empty document with object streams enabled
    Dim saveOptions As New PdfSaveOptions() With {
        .UseObjectStreams = True
    }
    processor.CreateEmptyDocument("Merged.pdf", saveOptions)

    ' Append multiple PDF files
    processor.AppendDocument("Document1.pdf")
    processor.AppendDocument("Document2.pdf")
    processor.AppendDocument("Document3.pdf")
End Using