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 in a Document instance and open an output stream for the extracted image file.
- Get the target Page from the document and access its
Resources.Imagescollection. - Retrieve the required XImage object from that image collection by index.
- 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);
}
}