Skip to main content

ImageCompressionOptions Class

Contains image compression settings.

Declaration

public class ImageCompressionOptions extends JavaObject

Remarks

Use the ImageCompressionOptions class to configure image compression when you optimize a PDF document. Pass a configured ImageCompressionOptions object to the OptimizationOptions.setImageCompressionOptions method to control image quality and file size.

The default settings use JPEG compression for color and grayscale images, CCITT Group 4 compression for bitonal images, and a JPEG quality value of 100.

The following code snippet optimizes images with different compression settings:

package pdf;

import com.devexpress.docs.pdf.*;

import java.nio.channels.*;
import java.nio.file.*;

public class Main {
    public static void main(String[] args) throws Exception {
        try (FileChannel channel = FileChannel.open(Path.of("Document.pdf"));
             PdfDocument pdfDocument = new PdfDocument(channel)) {

            // Configure image compression options.
            ImageCompressionOptions imageCompressionOptions = new ImageCompressionOptions();

            // Configure general compression settings.
            imageCompressionOptions.setCompressionType(ImageCompressionType.JPEG);
            imageCompressionOptions.setJpegQuality(75);
            imageCompressionOptions.setDownsamplingResolution(150);

            // Convert color images to grayscale.
            imageCompressionOptions.setConvertGrayscale(true);

            // Configure compression for grayscale images.
            imageCompressionOptions.setGrayscaleCompressionType(GrayscaleImageCompressionType.JPEG);

            // Configure compression for bitonal images.
            imageCompressionOptions.setBitonalCompressionType(BitonalImageCompressionType.CCITT_GROUP_4);

            // Compress images.
            pdfDocument.compressImages(imageCompressionOptions);

            try (WritableByteChannel writableByteChannel =
                    FileChannel.open(Path.of("Document_Optimized.pdf"),
                        StandardOpenOption.CREATE,
                        StandardOpenOption.WRITE,
                        StandardOpenOption.TRUNCATE_EXISTING)) {

                pdfDocument.save(writableByteChannel);
            }
        }
    }
}

Refer to the following help topic for additional information: Optimize PDF Documents.

Inherited Members

com.devexpress.system.JavaObject.clone()
com.devexpress.system.JavaObject.equals(java.lang.Object)
com.devexpress.system.JavaObject.hashCode()
com.devexpress.system.JavaObject.initFields()
com.devexpress.system.JavaObject.memberwiseClone()
com.devexpress.system.JavaObject.toString()
java.lang.Object.finalize()
java.lang.Object.getClass()
java.lang.Object.notify()
java.lang.Object.notifyAll()
java.lang.Object.wait()
java.lang.Object.wait(long)
java.lang.Object.wait(long,int)

Inheritance

Object
com.devexpress.system.JavaObject
ImageCompressionOptions

ImageCompressionOptions()

Initializes a new instance of the ImageCompressionOptions class.

Declaration

public ImageCompressionOptions()

Methods

getBitonalCompressionType() Method

Returns the compression type for bitonal images.

Declaration

public BitonalImageCompressionType getBitonalCompressionType()

Returns

Type Description
BitonalImageCompressionType

The compression algorithm applied to bitonal images.

getCompressionType() Method

Returns the compression type for color images.

Declaration

public ImageCompressionType getCompressionType()

Returns

Type Description
ImageCompressionType

The compression algorithm applied to images.

getConvertGrayscale() Method

Returns whether color images are converted to grayscale.

Declaration

public boolean getConvertGrayscale()

Returns

Type Description
boolean

true if color images are converted to grayscale before compression; otherwise, false.

getDownsamplingResolution() Method

Returns the target resolution for image downsampling.

Declaration

public float getDownsamplingResolution()

Returns

Type Description
float

The target image resolution, in DPI.

getGrayscaleCompressionType() Method

Returns the compression type for grayscale images.

Declaration

public GrayscaleImageCompressionType getGrayscaleCompressionType()

Returns

Type Description
GrayscaleImageCompressionType

The compression algorithm applied to grayscale images.

getIgnoreSizeReduction() Method

Returns whether to ignore compression results that increase the document size.

Declaration

public boolean getIgnoreSizeReduction()

Returns

Type Description
boolean

true if the PDF document is updated when compression does not reduce the file size; otherwise, false.

getInterpolationMode() Method

Returns the interpolation mode used to resize images.

Declaration

public DXInterpolationMode getInterpolationMode()

Returns

Type Description
DXInterpolationMode

The interpolation mode used when images are downsampled.

getJpegQuality() Method

Returns the JPEG compression quality.

Declaration

public int getJpegQuality()

Returns

Type Description
int

The JPEG compression quality level. The value ranges from 0 to 100.

setBitonalCompressionType(BitonalImageCompressionType value) Method

Sets the compression type for bitonal images.

Declaration

public void setBitonalCompressionType(BitonalImageCompressionType value)

Parameters

Name Type Description
value BitonalImageCompressionType

The compression algorithm applied to bitonal images.

setCompressionType(ImageCompressionType value) Method

Sets the compression type for color images.

Declaration

public void setCompressionType(ImageCompressionType value)

Parameters

Name Type Description
value ImageCompressionType

The compression algorithm applied to images.

setConvertGrayscale(boolean value) Method

Sets whether to convert color images to grayscale.

Declaration

public void setConvertGrayscale(boolean value)

Parameters

Name Type Description
value boolean

true to convert color images to grayscale before compression; otherwise, false.

setDownsamplingResolution(float value) Method

Sets the target resolution for image downsampling.

Declaration

public void setDownsamplingResolution(float value)

Parameters

Name Type Description
value float

The target image resolution, in DPI.

setGrayscaleCompressionType(GrayscaleImageCompressionType value) Method

Sets the compression type for grayscale images.

Declaration

public void setGrayscaleCompressionType(GrayscaleImageCompressionType value)

Parameters

Name Type Description
value GrayscaleImageCompressionType

The compression algorithm applied to grayscale images.

setIgnoreSizeReduction(boolean value) Method

Sets whether to ignore compression results that increase the document size.

Declaration

public void setIgnoreSizeReduction(boolean value)

Parameters

Name Type Description
value boolean

true to update the PDF document when compression does not reduce the file size; otherwise, false.

setInterpolationMode(DXInterpolationMode value) Method

Sets the interpolation mode used to resize images.

Declaration

public void setInterpolationMode(DXInterpolationMode value)

Parameters

Name Type Description
value DXInterpolationMode

The interpolation mode used when images are downsampled.

setJpegQuality(int value) Method

Sets the JPEG compression quality. A higher value preserves image quality but produces larger files.

Declaration

public void setJpegQuality(int value)

Parameters

Name Type Description
value int

The JPEG compression quality level. The value ranges from 0 to 100.