Extract images to files

Here are the steps to extract images to files:

  • Instantiate Parser object for the initial document;
  • Call getImages method and obtain collection of PageImageArea objects;
  • Check if collection isn’t null (images extraction is supported for the document);
  • Iterate through the collection and save image contents to the file.

The following example shows how to save images to files:

// Create an instance of Parser class
try (Parser parser = new Parser(Constants.SampleZip)) {
    // Extract images from document
    Iterable<PageImageArea> images = parser.getImages();

    // Check if images extraction is supported
    if (images == null) {
        System.out.println("Page images extraction isn't supported");
        return;
    }

    // Create the options to save images in PNG format
    ImageOptions options = new ImageOptions(ImageFormat.Png);

    int imageNumber = 0;
    // Iterate over images
    for (PageImageArea image : images)
    {
        // Save the image to the png file
        image.save(Constants.getOutputFilePath(String.format("%d.png", imageNumber)), options);

        imageNumber++;
    }
}

More resources

GitHub examples

You may easily run the code above and see the feature in action in our GitHub examples:

Free online document parser App

Along with full featured Java library we provide simple, but powerful free Apps.

You are welcome to extract data from PDF, DOC, DOCX, PPT, PPTX, XLS, XLSX, Emails and more with our free online Free Online Document Parser App.