Reduce image size (bytes) in ITextPDF

2019-05-19 16:10发布

问题:

I'm using the itext PDF library to build a very image-intensive PDF document in Java. Each page has a dozen images on it. The original source images are very high resolution, and I'm using scaleToFit to render the image to the size I need.

The problem I have is that the PDF document is still very large. My understanding is that the entire original high resolution image is being included, and the scaling I'm using only affects the actual rendering, not the size of the image that's included in the file.

I've verified this by removing the scaling — the pages were rendered with the high resolution images overlapping each other and the edge of pages, and the PDF was the same size as when the scaling was in place.

So, here's the question — how can I reduce the size of the PDF file by scaling down each image? If I lose a little bit of image quality that's ok. Rescaling the source images manually will be difficult.

回答1:

So I've found a way to do it. I now load the image into a BufferedImage, and then scale that using the hints found here: how do I scale a BufferedImage.

This gives me a BufferedImage — I then convert this into an iText image using

        Image returnedImage = Image.getInstance ( pcb, bufferedImage, quality );

Where quality is currently 0.6. That's acceptable for the work I'm doing.



标签: java itext