Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we compress PDF file size using iText?

I am using iText to read and manipulate PDF files(reports). I read in a PDF report using iText and I save each page as a separate PDF file. But I am unable to reduce the size of the generated PDF. Is there any compression technique or any other way to reduce the size of the PDF? Does pdfbox help in this way?

like image 537
MaheshVarma Avatar asked Nov 01 '25 16:11

MaheshVarma


1 Answers

You can try to set a compression level when using iText:

Document document = ...
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file));
writer.setCompressionLevel(9);

Level 9 is slowest, but gives you the best compression available in iText.


Please note, that the compression effect largely depends on the PDF content. If your PDF file contains large binary streams, such as images, the compression will have little to no effect on your document. Also, iText will never compress XMP metadata stream regardless of the configuration options.

like image 142
Jk1 Avatar answered Nov 03 '25 09:11

Jk1