Extract Images from PDF using Java
Contents
[
Hide
]
Extract images from PDF pages when you need to reuse embedded graphics, inspect document assets, or export images for downstream processing.
- Open the source PDF Document.
- Open an output stream for the extracted image file.
- Get the target XImage from the target Page resources collection.
- 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);
}
}