Extracting Images from Presentation shapes
Contents
[
Hide
]
Images are added in slide background and shapes. Sometimes, it is required to extract the images added in the presentation shapes. The images are added in IPPImageCollection inside Presentation Document Object Model (DOM). This article covers the feature of accessing the images in presentation shape, extracting them from presentation collection and saving them in a file.
Extracting images from Presentation Shapes
In Aspose.Slides for Java, images can be added to slide shape and slide background. The images are added in IPPImageCollection of the presentation. In this example we will traverse through each shape inside every slide of presentation and see if there is any image added in slide shape. If the image will be found for any shape, we will extract that and will save it in file.The following code snippet will serve the purpose.
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
//Accessing the presentation | |
String path = "D:\\Aspose Data\\"; | |
//Accessing the presentation | |
Presentation pres = new Presentation(path + "ExtractImages.pptx"); | |
com.aspose.slides.IPPImage img = null; | |
com.aspose.slides.IPPImage Backimg = null; | |
int slideIndex = 0; | |
String ImageType = ""; | |
boolean ifImageFound = false; | |
for (int i = 0; i < pres.getSlides().size(); i++) | |
{ | |
slideIndex++; | |
//Accessing the first slide | |
ISlide sl = pres.getSlides().get_Item(i); | |
//Accessing the first slide Slide sl = pres.getSlideByPosition(i); | |
if (sl.getBackground().getFillFormat().getFillType() == FillType.Picture) | |
{ | |
//Getting the back picture | |
Backimg = sl.getBackground().getFillFormat().getPictureFillFormat().getPicture().getImage(); | |
//Saving picture | |
BufferedImage image=Backimg.getSystemImage(); | |
ImageType = Backimg.getContentType(); | |
//ImageType = ImageType.s.substring(0, ImageType.indexOf("/") + 1); | |
ImageType = ImageType.substring(ImageType.indexOf("/") + 1,ImageType.length()); | |
String ImagePath = path + "BackImage_"; | |
try { | |
ImageIO.write(image,ImageType, new File(ImagePath + "Slide_" + slideIndex+ "." +ImageType.toString())); | |
//Setting the desired picture format | |
} catch (IOException ex) { | |
Logger.getLogger(NewAPi.class.getName()).log(Level.SEVERE, null, ex); | |
} | |
} | |
else | |
{ | |
if (sl.getLayoutSlide().getBackground().getFillFormat().getFillType() == FillType.Picture) | |
{ | |
//Getting the back picture | |
Backimg = sl.getLayoutSlide().getBackground().getFillFormat().getPictureFillFormat().getPicture().getImage(); | |
BufferedImage image=Backimg.getSystemImage(); | |
ImageType = Backimg.getContentType(); | |
ImageType = ImageType.substring(ImageType.indexOf("/") + 1,ImageType.length()); | |
String ImagePath = path + "BackImage_"; | |
try { | |
ImageIO.write(image,ImageType, new File(ImagePath + "LayoutSlide_" + slideIndex+ "." +ImageType.toString())); | |
//Setting the desired picture format | |
} catch (IOException ex) { | |
Logger.getLogger(NewAPi.class.getName()).log(Level.SEVERE, null, ex); | |
} | |
} | |
} | |
for (int j = 0; j < sl.getShapes().size(); j++) | |
{ | |
// Accessing the shape with picture | |
IShape sh = sl.getShapes().get_Item(j); | |
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); | |
ifImageFound = true; | |
} | |
} | |
else if (sh instanceof IPictureFrame) | |
{ | |
IPictureFrame pf = (IPictureFrame)sh; | |
// if (pf.getFillFormat().getFillType() == FillType.Picture) | |
{ | |
img = pf.getPictureFormat().getPicture().getImage(); | |
ImageType = img.getContentType(); | |
ImageType = ImageType.substring(ImageType.indexOf("/") + 1,ImageType.length()); | |
ifImageFound = true; | |
} | |
} | |
// | |
//Setting the desired picture format | |
if (ifImageFound) | |
{ | |
//Format = GetImageFormat(ImageType); | |
String ImagePath = path + "Slides\\Image_"; | |
try { | |
ImageIO.write(img.getSystemImage(),ImageType, new File(ImagePath + "Slide_" + slideIndex + "_Shape_" + j + "." + ImageType)); | |
} catch (IOException ex) { | |
Logger.getLogger(NewAPi.class.getName()).log(Level.SEVERE, null, ex); | |
} | |
} | |
ifImageFound = false; | |
} | |
} | |