Creating a TIF file with a resolution of X ppi from a PDFBox PDPage

Submitted by Jochus on Sun, 25/11/2012 - 22:54 | Posted in: Java

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

Submitted by Tilman (not verified) on Thu, 27/03/2014 - 20:42
 

All this, and more, is part of ImageIOUtil.writeImage() in the (unreleased) 2.0 version of PDFBOX.

Submitted by Jochus on Thu, 27/03/2014 - 20:46
 

Aha, thanks for sharing! :-)

Add new comment

The content of this field is kept private and will not be shown publicly.

Full HTML

  • Lines and paragraphs break automatically.
  • You can caption images (data-caption="Text"), but also videos, blockquotes, and so on.
  • Web page addresses and email addresses turn into links automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <bash>, <cpp>, <css>, <html5>, <java>, <javascript>, <php>, <sql>, <xml>. The supported tag styles are: <foo>, [foo].
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.