How to replace image in slide by image name or id?

Hello,


I am generating slides from Aspose dynamically based on my collection values. I have one templates and selecting each slide and replacing values from collection and generating new slide.

It works fine with text but I am not able to render image based on slide selections. I want to get old image from selected slide and replace with new image.

Kindly let me know how to get old image in particular slide not in whole presentations and how to replace this with new image by path.

Thanks,
Devanand

Hi Devanand,


I have observed your requirements. I have shared a piece of code in a text file with you. This piece of code will help you to achieve your requirements. Please share feedback with us if there is still an issue.

Best Regards,

Hello Adnan Ahmad ,


Thanks for reply.

It seems your code is useful in Java. I have been using c#.Net where i am not able to get references for given methods and it looks like searching images in presentation not in slide.

My requirement is I am rending a template (PPT file) and populating data into the slide based on my data collection so I need to render each slide (same slide can be repeat multiple times) and replace data into text frame and image.

eg.
foreach (IShape shape in slide3.Shapes)
{
switch (shape.Name)
{
case “Image”:
//This replaces in entire presentations based on index, which replaces all images
System.Drawing.Image newImage = System.Drawing.Image.FromFile(dataDir + " imageTest.jpg");
IPPImage oldImage = prenTemplate.Images[2]; // Selecting from presentation not from slide3
oldImage.ReplaceImage(newImage);
}
}
}

Kindly let me know how to get image from slide3 and replace with new image by image path.

Thanks,
Devanand

Please find attached sample code for your reference.

Hi Devanand,


I have observed your comments. I like to share that you can find image by name using Alternative text property in a slide. Please follow guidelines on this link for finding shape with alternative text.

Best Regards,

Hi Adnan,


Thanks for your help and reply. I tried this code earlier and anyway I’m able to access image from presentation/slide. But the issue is I am generating new presentation by filling data in existing template (PPT file). So I have to replace data in slide and repeat it based on data size.

For eg. Template has 1 slide but output slide would be multiply of each slide. It could be 5 for example. This case image index is always same for same image. If I replace image for slide from template and repeat this 5 times so all output will have same five images because I replace by index as below.

case “ImageTestname”:
Image newImage = Image.FromFile(dataDir + “img” + imgNumber + “.jpg”);
IPPImage oldImage = prenTemplate.Images[2];
oldImage.ReplaceImage(newImage);
imgNumber++;
break;

Please find sample template PPT and output PPT, It will give you idea about my requirement. I need to generate as attached output from template.

Thanks,
Devanand



Hi Devanand,

I have observed your comments and like to share that you need to use slide cloning feature in order to copy slide from template presentation and then replace the image. I request you to please try using following sample code for your reference and modify this as per your requirement. It will give you overview of how to achieve the goal.

public static void TestImageReplace()
{
Presentation TemplatePres = new Presentation(“Template.pptx”);

Presentation TargetPres = new Presentation();

ISlide slide2Copy = TemplatePres.Slides[0];

ISlideCollection TargetPresSlides = TargetPres.Slides;

//Now Copying Slide from Template To Target
//We will copy 5 slides
ISlide slide = null;
for (int i = 0; i < 5; i++)
{
slide = TargetPresSlides.AddClone(slide2Copy);


// alternative text of the shape to be found
IShape shape = FindShape(slide, “ImageTestname”);

if (shape != null)
{
IPictureFrame picFrame = (IPictureFrame)shape;

Image newImage = Image.FromFile(“img.jpg”);
IPPImage oldImage = TargetPres.Images[2];
oldImage.ReplaceImage(newImage);

}
}


TargetPres.Save(“SavedPres.pptx”, SaveFormat.Pptx);
}

// Method implementation to find a shape in a slide using its alternative text
public static IShape FindShape(ISlide slide, string alttext)
{
// Iterating through all shapes inside the slide
for (int i = 0; i < slide.Shapes.Count; i++)
{
// If the alternative text of the slide matches with the required one then
// return the shape
if (slide.Shapes[i].AlternativeText.CompareTo(alttext) == 0)
return slide.Shapes[i];
}
return null;
}

Many Thanks,

Thanks a lot Mudassir Fayyaz…