How to Extract Images from Word File in Java

In this quick tutorial, we will learn how to extract images from Word file in Java. It contains all the information required to establish the environment, details of the API usage for writing the application, and a runnable sample code that demonstrates the process to extract pictures from Word document in Java. You will also learn different options to customize the process while loading the source DOCX file and extracting different images from it like PNG, JPEG, etc.

Steps to Extract Images From Word in Java

  1. Establish the environment to add Aspose.Words for Java for extracting images
  2. Load the source Word file with images into the Document class object
  3. Get the shapes collection using the getChildNodes() method in the Document class
  4. Iterate through all the shapes in the collection
  5. Check if the shape has an image using the hasImage() method in the Shape class
  6. Save the image as a separate file on the disk

These steps explain the process to extract images from Word file in Java. First, the resources are shared for the environment establishment followed by the introduction to the classes, methods, and properties required to accomplish the task like the Document class is used to load the source Word file, and getChildNodes() method is used to extract all the images from it. Finally, each element of the collection is tested for having an image, and if found saved on the disk as a separate image file.

Code to Extract Images from Word Document in Java

This sample code demonstrates the process to extract all images from Word document in Java. After loading the source Word file, getChildNodes() method is used by providing NodeType.SHAPE value as an argument that returns the set of all the shapes in the document as the Shape class has a method hasImage() that verifies the existence of the image. Once the image is detected, its extension is retrieved using the shape.getImageData().getImageType() and used in the output image name along with the option to filter the images based on their types.

In this tutorial, we have learned how to insert a picture in MS Word using Java. If you want to learn how to create a Word document from scratch, you may refer to the article how to create Word document using Java.

 English