title: Using pdfbox to convert a color PDF to a b/w tiff
body: <p>I am have a bit of a problem converting some color PDFs to tiff images. The PDFs I am having problems with have hand written signatures written in blue ink. These signatures do not appear in the generated binary tiffs. I suspect there is a threshold value somewhere to determine which pixels will be black and which will be white.</p>
  
  <pre><code>@SuppressWarnings("serial")
  private static void convertPdfToTiff(final File pdf, final File tif) throws Exception {
      try 
      {
          final Iterator&lt;ImageWriter&gt; imageWriterIterator = ImageIO.getImageWritersByFormatName("TIF");
          final ImageWriter imageWriter = imageWriterIterator.hasNext() ? imageWriterIterator.next() : null;
  
          final TIFFImageWriteParam writeParam = new TIFFImageWriteParam(Locale.getDefault());
          writeParam.setCompressionMode(TIFFImageWriteParam.MODE_EXPLICIT);
          writeParam.setCompressionType("LZW");
  
          PDDocument pdfDocument = PDDocument.load(pdf);
          PDFRenderer pdfRenderer = new PDFRenderer(pdfDocument);
  
          OutputStream out = new FileOutputStream(tif);
          final BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(out);
          final ImageOutputStream imageOutputStream = ImageIO.createImageOutputStream(bufferedOutputStream);
          imageWriter.setOutput(imageOutputStream);
          imageWriter.prepareWriteSequence(null);
  
          int pageCounter = 0;
          for (PDPage page : pdfDocument.getPages()) 
          {
              BufferedImage image = pdfRenderer.renderImageWithDPI(pageCounter, 300, ImageType.BINARY);
  
              final IIOImage s = new IIOImage(image, null, new TIFFImageMetadata(new TIFFIFD(new Vector&lt;BaselineTIFFTagSet&gt;() 
              {
                  {
                         add(BaselineTIFFTagSet.getInstance());
                  }
              }))) 
  
              {   
                  {
                         final TIFFImageMetadata tiffMetadata = (TIFFImageMetadata) getMetadata();
                         final TIFFIFD rootIFD = tiffMetadata.getRootIFD();
                         final BaselineTIFFTagSet base = BaselineTIFFTagSet.getInstance();
                         rootIFD.addTIFFField(new TIFFField(base.getTag(BaselineTIFFTagSet.TAG_X_RESOLUTION), TIFFTag.TIFF_RATIONAL, 1, new long[][] { { 300, 1 } }));
                         rootIFD.addTIFFField(new TIFFField(base.getTag(BaselineTIFFTagSet.TAG_Y_RESOLUTION), TIFFTag.TIFF_RATIONAL, 1, new long[][] { { 300, 1 } }));
                  }
              };
  
              imageWriter.writeToSequence(s, writeParam);
              pageCounter++;
          }
  
          imageWriter.dispose();
          imageOutputStream.flush();
          imageOutputStream.close();
          bufferedOutputStream.flush();
          bufferedOutputStream.close();
          pdfDocument.close();
          out.flush();
          out.close();
      } 
      catch (Exception e) 
      {
          e.printStackTrace();
          throw e;
      }
  }
  </code></pre>