In order to have the correct ppi in the TIF file, you need to specify IIOMetadata for the TIF file and pass it to the constructor of the IIOImage.
In the example below, you can see the creation of this metadata in the function: createMetadata()
... PDDocument document = null; try { // Set main options int imageType = BufferedImage.TYPE_BYTE_BINARY; int resolution = X; int maxPage = Integer.MAX_VALUE; // Create TIFF writer ImageWriter writer = null; ImageWriteParam writerParams = null; try { TIFFImageWriterSpi tiffspi = new TIFFImageWriterSpi(); writer = tiffspi.createWriterInstance(); writerParams = writer.getDefaultWriteParam(); writerParams.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); writerParams.setCompressionType("CCITT T.6"); writerParams.setCompressionQuality(1.0f); } catch (Exception ex) { throw new BusinessException("Could not load the TIFF writer", ex); } // Create metadata for Java IO ImageWriter IIOMetadata metadata = createMetadata(writer, writerParams, resolution); // Load the pdf file document = PDDocument.load(file.getFile()); List<PDPage> pages = (List<PDPage>) document.getDocumentCatalog().getAllPages(); // Loop over the pages for (int i = 0; i < pages.size() && i <= maxPage; i++) { // Write page to image file File outputFile = null; ImageOutputStream ios = null; try { outputFile = new File(file.getFile().getCanonicalPath() + "." + i); ios = ImageIO.createImageOutputStream(outputFile); writer.setOutput(ios); writer.write(null, new IIOImage(pages.get(i).convertToImage(imageType, resolution), null, metadata), writerParams); } catch (Exception ex) { throw new BusinessException("Could not write the TIFF file", ex); } finally { ios.close(); } // do something with TIF file } } catch (Exception ex) { throw new BusinessException("Error while converting PDF to TIFF", ex); } finally { try { if (document != null) { document.close(); } } catch (Exception ex) { log.error("Error while closing PDF document", ex); } } ... // Internal methods private IIOMetadata createMetadata(ImageWriter writer, ImageWriteParam writerParams, int resolution) throws IIOInvalidTreeException { // Get default metadata from writer ImageTypeSpecifier type = writerParams.getDestinationType(); IIOMetadata meta = writer.getDefaultImageMetadata(type, writerParams); // Convert default metadata to TIFF metadata TIFFDirectory dir = TIFFDirectory.createFromMetadata(meta); // Get {X,Y} resolution tags BaselineTIFFTagSet base = BaselineTIFFTagSet.getInstance(); TIFFTag tagXRes = base.getTag(BaselineTIFFTagSet.TAG_X_RESOLUTION); TIFFTag tagYRes = base.getTag(BaselineTIFFTagSet.TAG_Y_RESOLUTION); // Create {X,Y} resolution fields TIFFField fieldXRes = new TIFFField(tagXRes, TIFFTag.TIFF_RATIONAL, 1, new long[][] { { resolution, 1 } }); TIFFField fieldYRes = new TIFFField(tagYRes, TIFFTag.TIFF_RATIONAL, 1, new long[][] { { resolution, 1 } }); // Add {X,Y} resolution fields to TIFFDirectory dir.addTIFFField(fieldXRes); dir.addTIFFField(fieldYRes); // Return TIFF metadata so it can be picked up by the IIOImage return dir.getAsMetadata(); }
Comments
All this, and more, is part of ImageIOUtil.writeImage() in the (unreleased) 2.0 version of PDFBOX.
Aha, thanks for sharing! :-)
Add new comment