Extract Images from PDF using Java

Contents
[ ]

Extract images from PDF pages when you need to reuse embedded graphics, inspect document assets, or export images for downstream processing.

  1. Open the source PDF in a Document instance and open an output stream for the extracted image file.
  2. Get the target Page from the document and access its Resources.Images collection.
  3. Retrieve the required XImage object from that image collection by index.
  4. Call image.save(outputImage) to write the extracted image bytes to the target stream.
public static void extractImage(Path inputFile, Path outputFile) throws Exception {
    try (Document document = new Document(inputFile.toString());
         OutputStream outputImage = Files.newOutputStream(outputFile)) {
        XImage image = document.getPages().get_Item(1).getResources().getImages().get_Item(1);
        image.save(outputImage);
    }
}