* @throws IOException
*/
public static List<File> convertTiffFileToPngFiles(File tiffFile, File tmpDirectory) throws IOException {
tmpDirectory.mkdirs();
List<File> list = new ArrayList<File>();
RandomAccessFileOrArray ra = new RandomAccessFileOrArray(tiffFile.getCanonicalPath());
int pageCount = TiffImage.getNumberOfPages(ra);
for (int i = 0; i < pageCount; ++i) {
Image img = TiffImage.getTiffImage(ra, i + 1);
if (img != null) {
try {
// First - create a single PDF page from the tiff page.
Document document = new Document(PageSize.A4, 0.0f, 0.0f, 0.0f, 0.0f);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PdfWriter.getInstance(document, baos);
document.open();
document.add(img);
document.close();
// Second - read the memory based PDF file and read the image. Write
// this into PNG file.
PdfReader reader = new PdfReader(baos.toByteArray());
for (int j = 0; j < reader.getXrefSize(); j++) {
PdfObject pdfobj = reader.getPdfObject(j);
if (pdfobj != null) {
if (pdfobj.isStream()) {
PdfStream stream = (PdfStream) pdfobj;
PdfObject pdfsubtype = stream.get(PdfName.SUBTYPE);
if (pdfsubtype != null) {
if (pdfsubtype.toString().equals(PdfName.IMAGE.toString())) {
PdfImageObject image = new PdfImageObject((PRStream) stream);
BufferedImage bufferedImage = image.getBufferedImage();
if (bufferedImage != null) {
String pngName = IdUtils.getUniqueID() + ".png";
File pngFile = new File(tmpDirectory, pngName);
FileOutputStream out = new FileOutputStream(pngFile);
ImageIO.write(bufferedImage, "png", out);
out.close();
list.add(pngFile);
}
}
}
}
}
}
reader.close();
} catch (Exception exception) {
// Ignore - the page may be corrupt.
}
}
}
ra.close();
return list;
}