Q: We are using jPDFOptimizer, your Java library to optimize PDF documents. We are getting inconsistent results when compressing using JPEG2000 with the quality parameter. Sometimes the images are well compressed with a quality of 0.8, sometimes we have to use a quality of 0.2 for what appears to be the same results.
A: This is a bit of a tricky issue, that really depends on what you are trying to achieve…
The issue starts with the fact that JPEG or JPEG 2000 images do not store the quality setting that they were originally compressed at, i.e. when we or any other software compresses a JPEG image, there is no place to store the value that was used for the quality setting.
This means that when you tell jPDFOptimizer to compress an image to JPEG to any quality (say 0.5), the image in the document might already be compressed at that quality and therefore there would be no size reduction. Since there is no way to detect the current quality, there is no way to know what new quality setting should be used to get an actual size reduction. Read more about how the JPEG2000 quality parameter is defined in jPDFOptimizer.
Going back to what you are trying to achieve:
Achieve Consistent Quality and DPI Output
If you want to have consistent quality and DPI output, then you should continue what you’re doing, i.e, set a given quality of JPEG compression and a maximum DPI. With this approach, it is possible that some images might end up being about the same size or even slightly larger than in the incoming PDF.
Reduce Size Regardless of Output
If you want to reduce size regardless of input, then you can do a couple of things:
- You can set a very low quality setting that will almost guarantee that you will get some size reduction. The trade-off here is that sometimes you will get images that will suffer in visuals.
- Or you can use an iterative approach, where you compress at an initial setting and then check if the output has been reduced in size. If not, you can go to a lower quality setting and check the output again, iterating until you get the desired size reduction. The trade-off in this approach is performance, as you might have to optimize the same PDF several times before getting the target output size.
These two items are really application logic, different applications will choose to go one way or the other, so we do not implement this logic in jPDFOptimizer. Our library provides the functions to handle the PDFs and images inside them, but depending on the problem that the application is trying to solve, it will need to choose a different approach.