title: Resizing images in PDF using iText: incompatible color conversion
body: <p>I'm trying to reduce the filesize of a few PDF's in Java. I've found a few techniques to do this. But resizing images seems most effective.
  I followed <a href="http://www.javabeat.net/articles/print.php?article_id=327" rel="nofollow">this piece of code</a> which uses iText.</p>
  
  <p>It works great on a few PDF's (reduction from 4.5MB to 800KB!) but some stayed unaffected.</p>
  
  <p>I altered the code a bit as follows, so it would accept more Stream objects (and hopefully compress more):</p>
  
  <pre><code>PdfReader reader = new PdfReader(original.getPath());
  int size = reader.getXrefSize();
  for(int i=0;i&lt;size;i++){
      PdfObject object = reader.getPdfObject(i);
      if (object == null || !object.isStream())
          continue;
      PRStream stream = (PRStream)object;
          PdfImageObject image = new PdfImageObject(stream);
          try{
          BufferedImage bi = image.getBufferedImage();
          //more stuff here
  </code></pre>
  
  <p>And it did! Some PDF's actually got compressed a lot more. However, I got a strange exception at some that didn't change at all:</p>
  
  <pre><code>javax.imageio.IIOException: Incompatible color conversion
      at com.sun.imageio.plugins.jpeg.JPEGImageReader.checkColorConversion(JPEGImageReader.java:927)
      at com.sun.imageio.plugins.jpeg.JPEGImageReader.readInternal(JPEGImageReader.java:1028)
      at com.sun.imageio.plugins.jpeg.JPEGImageReader.read(JPEGImageReader.java:984)
      at javax.imageio.ImageIO.read(ImageIO.java:1438)
      at javax.imageio.ImageIO.read(ImageIO.java:1342)
      at com.itextpdf.text.pdf.parser.PdfImageObject.getBufferedImage(PdfImageObject.java:302)
      at com.example.compression.App.main(App.java:56)
  </code></pre>
  
  <p>(App.java:56 being the line that says <code>image.getBufferedImage();</code>)</p>
  
  <p>Some research discovered, the Stream it was trying to open was a <strong>CMYK</strong> JPG.</p>
  
  <p>I'm wondering why these wouldn't open, and how I can resize these images anyway.
  Thanks</p>