Created
June 17, 2021 14:38
-
-
Save aspose-com-gists/29b1a144486739b89071d689db85eef0 to your computer and use it in GitHub Desktop.
Extract Images from PDF in Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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