Free Support Forum - aspose.com

java.lang.ClassCastException: com.aspose.imaging.fileformats.jpeg.JpegImage cannot be cast to com.aspose.imaging.fileformats.tiff.TiffImage

Attempting to merge multiple TIFF files into a single multi-page TIFF. I’ve taken code directly from the docs, but receive the following exception when trying to cast the return value of Image.load() to a TiffImage:


String dir = “/private/tmp/”;
String tiff1 = dir + “74801.tif”;
String tiff2 = dir + “74802.tif”;
String result = dir + “final.tif”;
TiffImage image1 = (TiffImage) Image.load(tiff1);
TiffImage image2 = (TiffImage) Image.load(tiff2);
TiffFrame frame = TiffFrame.copyFrame(image2.getActiveFrame());
image1.addFrame(frame);
image1.save(result);
/
java.lang.ClassCastException: com.aspose.imaging.fileformats.jpeg.JpegImage cannot be cast to com.aspose.imaging.fileformats.tiff.TiffImage
/
Hi,

Thank you for your inquiry.

Please use the following code snippet at your end to combine multiple TIFF images into one single multi-page TIFF image. In case of any issue, feel free to contact us along with sample input files and your code.

CODE:

String[] files = new String[] {"page1.tiff", "page2.tiff", "page3.tiff"};
Cache.setCacheType(CacheType.CacheOnDiskOnly);
TiffImage result = new TiffImage();
for(int i = 0; i < files.length; i++)
{
TiffImage source = (TiffImage)Image.load(files[i]);
TiffFrame [] frames = source.getFrames();
for(int index = 0; index < frames.length; index++)
{
result.addFrame(TiffFrame.createFrameFrom(frames[index], saveOptions));
}//end for
}//end for

result.save("result.tif");
result.dispose();

Thank you for the quick feedback. Unfortunately, I'm still getting the same error (com.aspose.imaging.fileformats.jpeg.JpegImage cannot be cast to com.aspose.imaging.fileformats.tiff.TiffImage) using the code below with the following package: aspose-imaging-16.10.jar. I have also tried the same code against package: aspose-imaging-3.9.0.jar.
I have attached the tiffs for reference.


   	String dir = "/private/tmp/";
String tiff1 = dir + "74801.tiff";
String tiff2 = dir + "74802.tiff";

TiffImage result = null;
try {
String[] files = new String[]{tiff1, tiff2};
TiffOptions tiffOptions = new TiffOptions(TiffExpectedFormat.Default);
Cache.setCacheType(CacheType.CacheOnDiskOnly);
for (int i = 0; i < files.length; i++) {
TiffImage source = (TiffImage) Image.load(files[i]);
TiffFrame[] frames = source.getFrames();
for (int index = 0; index < frames.length; index++) {
TiffFrame tiffFrame = TiffFrame.createFrameFrom(frames[index], tiffOptions);
if (index == 0) {
result = new TiffImage(tiffFrame);
} else {
result.addFrame(tiffFrame);
}
}
}
result.save("result.tif");
} catch(Exception ex) {
System.out.println(ex.getMessage());
} finally {
if (result != null) result.dispose();
}
Hi,

Thank you for writing us back and sharing sample.

This is to update you that we have evaluated the samples. It was found that the provided samples are not TIFF images rather these are JPEG images. Screenshot of image properties are attached for your reference.

Please visit the link Add Different Images as Separate Frames in a Multi-Page TIFF for details on how to convert more than one raster images to multi-page TIFF image.

Feel free to contact us in case of any query or comments.