title: compress pdf with large images via java
body: <p>Looking for a way to compress images in a pdf and to output a pdf for archiving. I cannot compress the images before creation as it would compromise the quality of the print. </p>
  
  <p>The size of each pdf is around 8MB with the bulk of this being made up of 2 images. Images are in png format and are brought into pdf during generation(3rd party generator used)</p>
  
  <p>Is there a way to compress these in java without using a 3rd party tool. I have tried with pdfbox, itext and a 3rd party exe(neevia), the 3rd party tool the only one that has given me any results so far(Down to around half a MB) but I do not want to relinquish control to an exe.
  Sample code is below.</p>
  
  <pre><code>import java.io.BufferedReader;
  import java.io.FileOutputStream;
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.InputStreamReader;
  import java.util.Arrays;
  
  import org.apache.pdfbox.exceptions.COSVisitorException;
  import org.apache.pdfbox.pdmodel.PDDocument;
  import org.apache.pdfbox.pdmodel.common.PDStream;
  
  import com.itextpdf.text.DocumentException;
  import com.itextpdf.text.pdf.PdfReader;
  import com.itextpdf.text.pdf.PdfStamper;
  import com.itextpdf.text.pdf.PdfWriter;
  
  public class compressPDF {
  
  public static void main (String[] args) throws IOException, DocumentException, COSVisitorException {
  
  
      /*
       * Using PDF Box
       */
  
      PDDocument doc; // = new PDDocument();
  
      doc = PDDocument.load("C:/_dev_env_/TEMP/compressPDF/TRPT_135002_1470_20131212_121423.PDF");
  
      PDStream stream= new PDStream(doc);
      stream.addCompression();
  
      doc.save("C:/_dev_env_/TEMP/compressPDF/compressed_pdfBox.pdf");
  
      doc.close();
  
      /*
       * Using itext
       */
  
      PdfReader reader = new PdfReader("C:/_dev_env_/TEMP/compressPDF/TRPT_135002_1470_20131212_121423.PDF");
  
      PdfStamper stamper = new PdfStamper(reader, new FileOutputStream("C:/_dev_env_/TEMP/compressPDF/compressed_Itext.pdf"), PdfWriter.VERSION_1_5);
      stamper.setFullCompression();
      stamper.getWriter().setCompressionLevel(50);
      int total = reader.getNumberOfPages() + 1;
      for (int i = 1; i &lt; total; i++) {
          reader.setPageContent(i, reader.getPageContent(i));
      }
      stamper.close();
      reader.close();
  
      /*
       * Using 3rd party - Neevia 
       */
      try {
      Process process = new ProcessBuilder("C:/Program Files (x86)/neeviaPDF.com/PDFcompress/cmdLine/CLcompr.exe","C:/_dev_env_/TEMP/compressPDF/TRPT_135002_1470_20131212_121423.PDF", "C:/_dev_env_/TEMP/compressPDF/compressed_Neevia.pdf").start();
      InputStream is = process.getInputStream();
      InputStreamReader isr = new InputStreamReader(is);
      BufferedReader br = new BufferedReader(isr);
      String line;
  
      System.out.printf("Output of running %s is:", Arrays.toString(args));
  
      while ((line = br.readLine()) != null) {
        System.out.println(line);
      }
      } catch (Exception e) {
          System.out.println(e);
      } finally {
          System.out.println("Created!!");
      }
  
  }
  
  }
  </code></pre>