title: Multipage Tiff compression
body: <p>I've seen a few questions regarding multipage tiffs and a few questions about compression, but none (that I've seen) linking the two. <a href="https://stackoverflow.com/questions/9539057/convert-java-awt-image-to-tiff-byte-array-with-compression">This question</a> is as close as I've seen and puts me incredibly close, so I hope. I went into the Oracle forum thread mentioned (it is talking about a multipage PDF to TIFF with compression) and I think I'm closish to having the code finished to do this. Can anyone assist? I'm going to remove the try/catches to try and shorten this down a hair (basically all they did was output a message in console and return false).</p>
  
  <pre><code> public static boolean CompressedTiff(List&lt;BufferedImage&gt; images, File path)
   {
      if (!path.getParentFile().exists())
           path.getParentFile().mkdirs();
      path.createNewFile();
      ImageOutputStream ios;
           ios = ImageIO.createImageOutputStream(path);
  
      Iterator&lt;ImageWriter&gt; imageWriters = ImageIO.getImageWritersByFormatName("TIFF");
      ImageWriter writer = (ImageWriter)imageWriters.next();
      writer.setOutput(ios);
      TIFFImageWriteParam writeParam = (TIFFImageWriteParam)writer.getDefaultWriteParam();
      writeParam.setCompressionMode(2);
      writeParam.setCompressionType("LZW"); 
      writer.prepareWriteSequence(null);
  
      for(int i = 0; i &lt; images.size(); i++)
      {
          ImageTypeSpecifier spec = ImageTypeSpecifier.createFromRenderedImage(images.get(i));
          javax.imageio.metadata.IIOMetadata metadata = writer.getDefaultImageMetadata(spec, writeParam);
          IIOImage iioImage = new IIOImage(images.get(i), null, metadata);
          writer.writeToSequence(iioImage, writeParam);
          images.get(i).flush();//modified after release.
  
          images.get(i).flush();
          writer.endWriteSequence();
          ios.flush();
          writer.dispose();
          ios.close();
      }
      return true;
  
  }
  </code></pre>
  
  <p>It failed out on the next pass at writer.writeToSequence saying I needed to call prepareWriteSequence. I changed it to </p>
  
  <pre><code> writer.prepareWriteSequence(metadata);
   writer.writeToSequence(iioImage, writeParam);
  </code></pre>
  
  <p>also removed the earlier writer.prepareWriteSequence(null);</p>
  
  <p>and it appears to be navigating the files properly, however, the output isn't of any type of renderable tif. Multipage or otherwise.</p>
  
  <p>I have JAI installed so if it is possible to use that in some way to achieve the compressed image, that'd be fantastic. The code I am using that generates a TIFF is using this, but I haven't seen anything that worked as far as adding compression to the pages.</p>
  
  <p>edit: I added a bunch of ios.flush(); ios.close(); calls in the catch blocks and it prevents the non-renderable TIFF issue. However, it isn't adding any page beyond the first.</p>