1

How to achieve a compression level of PDF files in android comparable to that achieved by Adobe Reader, Quartz Pdf Context etc.?

I tried with iText but the compression level is about 400% less than Adobe Reader and 200% less than Quartz Pdf Context.

The code I use in iText (source iText Documentation) is:

public void compressPdf(String src, String dest) throws IOException, DocumentException {
    PdfReader reader = new PdfReader(src);
    PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest), PdfWriter.VERSION_1_5);
    stamper.getWriter().setCompressionLevel(9);
    int total = reader.getNumberOfPages() + 1;
    for (int i = 1; i < total; i++) {
        reader.setPageContent(i, reader.getPageContent(i));
    }
    stamper.setFullCompression();
    stamper.close();
    reader.close();
}
vitil
  • 256
  • 1
  • 3
  • 13
  • Try to find some diagnostic tools to tell you where your space is being taken up in the PDF files. – CommonsWare Jul 05 '15 at 23:48
  • *tried with iText* - how did you try? By default iText does not use maximum compression. – mkl Jul 06 '15 at 06:50
  • Thanks for the replies. CommonsWare: I'll try that and let you know. mkl : please see edited question – vitil Jul 06 '15 at 08:44
  • Your code only re-compresses the page content streams. Depending on the actual document structure they may be minute, e.g. if more or less all content is separated into form xobjects. Furthermore other programs may change image data to be smaller (at the possible cost of quality). – mkl Jul 06 '15 at 19:01
  • @CommonsWare I found out that there were several, though small uncompressed images in the pdf. Now I'm trying to make PDF image compression through iText. If you can help please answer to my new question http://stackoverflow.com/questions/31256810/compress-pdf-with-large-images-in-android – vitil Jul 06 '15 at 22:44

0 Answers0