Sample of the Week:
Despite the near ubiquitous use of PDF to exchange documents, there are still rooms… and rooms… and rooms of paper documents in cold storage in offices all over the world. Often times, the first stop on the way to becoming useful again, these documents are scanned to TIFF and stored on massive hard drives. This article is going to be far shorter than my usual Sample of the Week articles because converting these TIFF files to PDF using the Datalogics PDF Java Toolkit is incredibly simple. The developer simply needs to convert a standard Java BufferedImage to a PDFXObjectImage before placement on a PDF page; this also works for any kind of image, not just TIFF. The fun part is figuring out what size page to put it on.
The image portion of an image file really only knows about it’s width and height. To calculate the pixel density, you need to look at the metadata associated with that image. In the Gist below we assume that the images have square pixels so we just use the “HorizontalPixelSize” for my calculation. PDF pages are measured in points (72 per inch) so in order to determine the right size PDF page to add our image to, we need to know the height, width, and pixel density… a little bit of math later and we have our page size.
This Gist uses a multi-page TIFF file as the input source but the code can be easily modified to accept multiple separate image files. If you plan on converting other image formats to PDF and you want to retain the same pixel density as the original image, the metadata will be different so that portion of the code will need to change as well.
You’ll also need at least an evaluation version of the Datalogics PDF Java Toolkit to run this code as well as the Java Advanced Imaging ImageIO Tools for reading the TIFF files.