Image Detection/extraction in Java

I am trying to extract images from my .doc/.docx file using below lines of code.

for (Shape shape : (Iterable<Shape>) shapes)
{
    if (shape.hasImage())
    {
        String imageFileName = java.text.MessageFormat.format(
                        "Aspose.Images.{0}{1}", imageIndex, FileFormatUtil
                                        .imageTypeToExtension(shape.getImageData()
                                                        .getImageType()));
        shape.getImageData().save(dataDir + imageFileName);

        imageIndex++;
    }
}

Can someone confirm weather shape.hasImage() can detect all types of images , especially the ones with below extensions

‘.dng’, ‘.eps’, ‘.tiff’, ‘.tif’, ‘.png’, ‘.jpeg’, ‘.jpg’, ‘.wmf’, ‘.emf’, ‘.svg’

@saheeraeranhikkal Yes, your code is correct and will extract image data properly. You should note that ImageType enum is limited to the following formats: EMF, WMF, PICT, JPEG, PNG, BMP. Regarding the image types you have listed:

  • .DNG image can be extracted, but Aspose.Words does not detect its format, so ImageData.getImageType() will return ImageType.UNKNOWN. So in this case you need your own mechanism to detect format of such images.
  • The same applies to .EPS format.
  • .TIFF and .TIF are both extensions of the same format. It’s format also is detected as ImageType.UNKNOWN
  • .PNG is detects as ImageType.PNG.
  • .JPG and .JPEG are detected as ImageType.JPEG.
  • .WMF images are detected as ImageType.WMF.
  • .EMF images are detected as ImageType.EMF.
  • .SVG unfortunately, extracting SVG images via ImageData is not yet supported. This feature request is logged as WORDSNET-23911. However, you can render shape to SVG using code like this:
shape.getShapeRenderer().Save("C:\\Temp\\out.svg", new ImageSaveOptions(SaveFormat.SVG));
1 Like

Thank you alexey :slightly_smiling_face:

1 Like