How to reduce PDF file size in Java?

Document document = new Document(reader.getPageSizeWithRotation(1)); PdfCopy writer = new PdfCopy(document, new FileOutputStream(outFile)); document.open(); PdfImportedPage page = writer.getImportedPage(reader, ++i); writer.setFullCompression(); writer.addPage(page); document.close(); writer.close(); 

I use iText to split and merge PDF, I need your help to reduce (compress) the output PDF format programmatically. Please let me know the steps to achieve the same.

+9
source share
4 answers

With writer.setFullCompression() you have already compressed the file as much as possible. There is nothing more you can do with iText.

+5
source

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(); 
+6
source

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

+3
source

You can use ghostscript by calling exe with specific parameters to print your pdf using ghostscript pdfwriter (example: sDEVICE = pdfwrite -sOutputFile = myfile.pdf). There are several accepted parameters for compression or quality levels, etc. This can lead to optimization and file size reduction.

0
source

All Articles