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 Document.
  2. Open an output stream for the extracted image file.
  3. Get the target XImage from the target Page resources collection.
  4. Save the extracted XImage to the output 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);
    }
}