Best practice to compress generated PDF using iText7

8.3k Views Asked by At

I have existing / source PDF source document and I copy selected pages from it and generate destination PDF with selected pages. Every page in existing / source document is scanned in different resolution and it varies in size:

  1. generated document with 4 pages => 175 kb
  2. generated document with 4 pages => 923 kb (I suppose this is because of higher scan resolution of each page in source document)

What would be best practice to compress this pages? Is there any code sample with compressing / reducing size of final PDF which consists of copied pages of source document in different resolution?

Kindest regards

1

There are 1 best solutions below

3
On BEST ANSWER

If you are just adding scans to a pdf document, it makes sense for the size of the resulting document to go up if you're using a high resolution image.

Keep in mind that iText is a pdf library. Not an image-manipulation library.

You could of course use regular old java to attempt to compress the images.

public static void writeJPG(BufferedImage bufferedImage, OutputStream outputStream, float quality) throws IOException
{
    Iterator<ImageWriter> iterator = ImageIO.getImageWritersByFormatName("jpg");
    ImageWriter imageWriter = iterator.next();
    ImageWriteParam imageWriteParam = imageWriter.getDefaultWriteParam();
    imageWriteParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
    imageWriteParam.setCompressionQuality(quality);
    ImageOutputStream imageOutputStream = new MemoryCacheImageOutputStream(outputStream);
    imageWriter.setOutput(imageOutputStream);
    IIOImage iioimage = new IIOImage(bufferedImage, null, null);
    imageWriter.write(null, iioimage, imageWriteParam);
    imageOutputStream.flush();
}

But really, putting scanned images into a pdf makes life so much more difficult. Imagine the people who have to handle that document after you. They open it, see text, try to select it, and nothing happens.

Additionaly, you might change the WriterProperties when creating your PdfWriter instance:

PdfWriter writer = new PdfWriter(dest,
    new WriterProperties().setFullCompressionMode(true));

Full compression mode will compress certain objects into an object stream, and it will also compress the cross-reference table of the PDF. Since most of the objects in your document will be images (which are already compressed), compressing objects won't have much effect, but if you have a large number of pages, compressing the cross-reference table may result in smaller PDF files.