Find and Replace images in Slides

Hi,


I’m trying to find and replace specific images. How can I do this. I have already manage to do this in aspose word by doing the following

NodeCollection shapeCollection = document.getChildNodes(NodeType.SHAPE, true);

shapeCollection.forEach(shape -> {

if (shape.isImage()) {

if (shape.getName().equals(“picture name”)) {
try {
shape.getImageData().setImageBytes(imageByte);

} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});

I cant do the same in aspose slides. Please help. Thank You

Hi Ravindu,


Thank you for posting.

I have observed your comments and like to share with you that there is an issue appearing while replacing the image on slide. A ticket with ID SLIDESJAVA-35205 has been logged into our issue management system for further investigation and resolution of the issue. This thread has been linked with the issue and we will update you as soon as the issue will be fixed.

Best Regards,

Hi,


So is there any other way I can find a image and replace it using Aspose Slide?? Getting all images as a collection and replacing works.
presentation.getImages().forEach(image->{
image.replaceImage(newImageData);
});

But above code will replace all images in slides cause there is no way of identifying each images.

Hi Ravindu,

I have observed your comments and like to request you to please try using following sample code on your end to serve the purpose, and then share your kind feedback with us.

Presentation pres = new Presentation("D:\\replacepic.pptx");

for (ISlide slide : pres.getSlides())
{
    for (IShape shape : slide.getShapes())
    {
        if (shape instanceof PictureFrame)
        {
            IPictureFrame pf = (IPictureFrame) shape;
            if (pf.getAlternativeText().compareTo("test") == 0)
            {
                IPPImage imgx = null;
                try
                {
                    imgx = pres.getImages().addImage(new FileInputStream(new File("C:\\test.JPG")));
                    IPPImage oldImage = pf.getPictureFormat().getPicture().getImage();
                    oldImage.replaceImage(imgx);
                }
                catch (Exception ex)
                {
                    ex.printStackTrace();
                }
            }
        }
    }
}

//Write the PPTX file to disk
pres.save("D:\\test.pptx", SaveFormat.Pptx);

A picture is contained within a picture frame, which is basically a shape. A shape could be distinguished by the AlternativeText of that shape, as explained in this documentation article. Then the image is being replaced as in the code sample above. I have also attached the sample presentation for your kind reference.

I hope this will be helpful. Please share if I may help you further in this regard.

Best Regards,

Hi,

I was able to do this by this,

presentation.getSlides().forEach(slide -> {
slide.getShapes().forEach(shape -> {

IPPImage s = presentation.getImages().addImage(b);
if (shape.getName().equals(documentTagDTO.getName())) {
if (shape instanceof PictureFrame) {
com.aspose.slides.PictureFrame picFrame = (PictureFrame) shape;
picFrame.getPictureFormat().getPicture().setImage(s);
}
}

shape.getFillFormat().getPictureFillFormat().setPictureFillMode(PictureFillMode.Tile);
IPPImage value = presentation.getImages().addImage(b);
shape.getFillFormat().getPictureFillFormat().getPicture().setImage(value);
});
});

Thanks for your help…

Hi Ravindu,


Thanks for your valuable feedback.

We are glad that your issue is resolved and things have started working on your end.

Please feel free to contact us if we can be of any help to you.

Best Regards,

The issues you have found earlier (filed as SLIDESJAVA-35205) have been fixed in this update.


This message was posted using Notification2Forum from Downloads module by Aspose Notifier.