Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Created June 17, 2021 14:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aspose-com-gists/29b1a144486739b89071d689db85eef0 to your computer and use it in GitHub Desktop.
Save aspose-com-gists/29b1a144486739b89071d689db85eef0 to your computer and use it in GitHub Desktop.
Extract Images from PDF in Java
// Load PDF document
Document pdfDocument = new Document("ImagetoPDF.pdf");
// Loop through pages
for (Page page : pdfDocument.getPages()) {
int imageCounter = 1;
// Loop through images
for (XImage xImage : page.getResources().getImages()) {
try {
// Create file stream
java.io.FileOutputStream outputImage = new java.io.FileOutputStream(
page.getNumber() + "_" + imageCounter + ".jpg");
// Save output image
xImage.save(outputImage);
// Close the stream
outputImage.close();
} catch (java.io.FileNotFoundException e) {
// TODO: handle exception
e.printStackTrace();
} catch (java.io.IOException e) {
// TODO: handle exception
e.printStackTrace();
}
imageCounter++;
}
// Reset counter
imageCounter=1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment