Learn how to extract images from PowerPoint PPT presentations in Java: https://blog.aspose.com/2022/01/13/extract-images-from-ppt-in-java/
Last active
February 3, 2022 15:47
-
-
Save aspose-com-gists/06934ffb1e5c49469ed7cca4e80ce1ca to your computer and use it in GitHub Desktop.
Extract Images in PowerPoint PPT 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 the presentation | |
Presentation pres = new Presentation("presentation.pptx"); | |
com.aspose.slides.IPPImage img = null; | |
com.aspose.slides.IPPImage backImage = null; | |
String ImagePath = "BackImage_"; | |
int slideIndex = 0; | |
String imageType = ""; | |
for (int i = 0; i < pres.getSlides().size(); i++) { | |
slideIndex++; | |
// Access slide | |
ISlide sl = pres.getSlides().get_Item(i); | |
// Check if background is filled with picture | |
if (sl.getBackground().getFillFormat().getFillType() == FillType.Picture) { | |
// Get the background image | |
backImage = sl.getBackground().getFillFormat().getPictureFillFormat().getPicture().getImage(); | |
// Save picture | |
BufferedImage image = backImage.getSystemImage(); | |
imageType = backImage.getContentType(); | |
imageType = imageType.substring(imageType.indexOf("/") + 1, imageType.length()); | |
try { | |
ImageIO.write(image, imageType, | |
new File(ImagePath + "Slide_" + slideIndex + "." + imageType.toString())); | |
} catch (IOException ex) { | |
// read exception message | |
} | |
} else { | |
// check if slide layout has image background | |
if (sl.getLayoutSlide().getBackground().getFillFormat().getFillType() == FillType.Picture) { | |
// Get the background image | |
backImage = sl.getLayoutSlide().getBackground().getFillFormat().getPictureFillFormat().getPicture() | |
.getImage(); | |
// Save image | |
BufferedImage image = backImage.getSystemImage(); | |
imageType = backImage.getContentType(); | |
imageType = imageType.substring(imageType.indexOf("/") + 1, imageType.length()); | |
try { | |
ImageIO.write(image, imageType, | |
new File(ImagePath + "LayoutSlide_" + slideIndex + "." + imageType.toString())); | |
} catch (IOException ex) { | |
// read exception message | |
} | |
} | |
} | |
} |
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 the presentation | |
Presentation pres = new Presentation("presentation.pptx"); | |
int imageIndex = 1; | |
String imageType = ""; | |
String imagePath = "Image_"; | |
// Loop through images | |
for (IPPImage image : pres.getImages()) { | |
// Get image type | |
imageType = image.getContentType(); | |
imageType = imageType.substring(imageType.indexOf("/") + 1, imageType.length()); | |
// Save image | |
try { | |
ImageIO.write(image.getSystemImage(), imageType, new File(imagePath + imageIndex + "." + imageType.toString())); | |
} catch (IOException ex) { | |
System.out.println(ex.getMessage()); | |
} | |
imageIndex++; | |
} |
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 the presentation | |
Presentation pres = new Presentation("presentation.pptx"); | |
com.aspose.slides.IPPImage img = null; | |
int slideIndex = 0; | |
String imageType = ""; | |
boolean isImageFound = false; | |
// Loop through slides | |
for (int i = 0; i < pres.getSlides().size(); i++) { | |
slideIndex++; | |
// Access slide | |
ISlide sl = pres.getSlides().get_Item(i); | |
for (int j = 0; j < sl.getShapes().size(); j++) { | |
// Access the shape | |
IShape sh = sl.getShapes().get_Item(j); | |
// Check if it is an auto shape | |
if (sh instanceof IAutoShape) { | |
IAutoShape ashp = (IAutoShape) sh; | |
if (ashp.getFillFormat().getFillType() == FillType.Picture) { | |
img = ashp.getFillFormat().getPictureFillFormat().getPicture().getImage(); | |
imageType = img.getContentType(); | |
imageType = imageType.substring(0, imageType.indexOf("/") + 1); | |
isImageFound = true; | |
} | |
} | |
// If shape is a picture frame | |
else if (sh instanceof IPictureFrame) { | |
IPictureFrame pf = (IPictureFrame) sh; | |
img = pf.getPictureFormat().getPicture().getImage(); | |
imageType = img.getContentType(); | |
imageType = imageType.substring(imageType.indexOf("/") + 1, imageType.length()); | |
isImageFound = true; | |
} | |
// Set the desired picture format | |
if (isImageFound) { | |
try { | |
ImageIO.write(img.getSystemImage(), imageType, | |
new File("Slide_" + slideIndex + "_Shape_" + j + "." + imageType)); | |
} catch (IOException ex) { | |
// Exception | |
} | |
} | |
isImageFound = false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment