How to reduce PDF file size programmatically in Java?

Also change the PdfCopy to PdfSmartCopy. It will eliminate duplicate streams which have the same hash (md5).


use iText

PdfReader reader = new PdfReader(new FileInputStream("input.pdf"));
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream("output.pdf"));
int total = reader.getNumberOfPages() + 1;
for ( int i=1; i<total; i++) {
   reader.setPageContent(i + 1, reader.getPageContent(i + 1));
}
stamper.setFullCompression();
stamper.close();

With writer.setFullCompression() you already compressed file as much as possible. With iText you can't do anything more.

Tags:

Java

Pdf

Itext