Find image type

How do you find the image type when extracting an image from a PDF with Aspose.PDF for java.

@james.stratton

Thanks for contacting support.

Images in the PDF document actually stored as a stream of pixels coded with using a specific filter and parameters. But they can be stored in a the required format for the purpose of standardization. By using pdf.getPages().get_Item(1).getResources().getImages().get_Item(1) we get pixel map that by default can be saved as JPEG image and you may store it with your desired file format. Please check following code snippet to save an image with PNG format.

FileOutputStream stream = new FileOutputStream(dataDir + "test.png");
document.getPages().get_Item(13).getResources().getImages().get_Item(1).save(stream, ImageType.getPng());
stream.close();

Please also note that the index of page or image cannot be 0:
.getPages().get_Item(0)
and
.getImages().get_Item(0)
else following exception will occur:

index should be in the range [1..n] where n equals to the pages count.
index should be in the range [1..n] where n equals to the images count.

In case of any further assistance, please feel free to let us know.

Hi,
I want to extract images from pdf and save image in jpg or png based on its original type, how can get its own original type?

@lucyqjz

As shared earlier in this forum thread that PDF does not store this information in it. The images are stored in the PDF document as a stream of pixels coded with using specific filter parameters. You can however use the above shared code snippet to extract images and save them into your desired image format.