Search code examples
javaimagepdfpdfboxjpeg2000

How to change the filter of an image in a PDF file


I'm building a tool to compress PDF files, and using pdfbox. I have some images with the DCTDecode + FlateDecode filter and I'd like to experiment with the JPXDecode filter to see if it occupies less space.

I've seen some code using iText, but how to do it with pdfbox?. I've found no documentation how to do so.


Solution

  • With pdfbox it is possible to compress all images, by using a custom COSWriter that handles all image streams and recodes them with the JPXDecode filter. pdfbox isn't able to do so, but the JAI library with a plugin can generate a JPEG2000 image. Compression factor is configurable, and high compression ratios can be achieved without losing too much quality.

    By using in addition the FlateDecode filter, a little more compression can be obtained with no quality loss.