Converting a text token to an image

Hi...I've wrestled with this for several days/hours and looked in all the documentation and these forums and cannot find the answer or figure it out...I'm in support too and I know how frustrating it is when a user asks a question that's been answered a million times before.

I have a PPT template and I use a search-and-replace program that I wrote that is based on the template.vb provided in the demos. I have tokens in the template like "[[companyname]]" and it is replaced with the real value.

How can I replace a token with an image, e.g., a logo file? I can create a new image on a slide, but I want it to be located in the same location as my "[[logo]]" token (or any other way that aspose.slides can understand.

I must be missing something easy because now I'm not sure I can see the forest through the trees anymore...

Any help/insight? Thanks a TON! Patrick.

Hello,

It will be easier if you want to replace the existing image with new image.

For example, you can place a logo image at your desired place on slide with desired dimensions. At runtime, you can access it and replace with newer one and it will not affect the place and dimensions you set in your template presentation.

Let me know, if it will solve your problem, I will try to provide you then a working example of it for your ease.

Shakeel...that would certainly work and if you have a working example I would REALLY appreciate it...will this work if there are 2 existing images on the single slide...I always wrestle with the accurate identification of multiple objects on one slide and that's one reason why I went with the token concept (as well as make it easier for someone else to design the layout).

Thank you a TON for any code/help. Patrick.

Dear Patrick,

Here is the source code. It reads the source presentation which has three logos (images) on a first slide.

Each of the image’s alternative text has been set to quickly access them programmatically using Slide.FindShape() method. Check the source presentation and read the instructions how to set the alternative text of any object/picture.

The code then reads three new images from disk and replaces all the logos on first slide with the newer images.

I have attached the source presentation, source images and output presentation.

Please see the source presentation; it has an instruction how to set alternative text of any shape.

C#

//Read the source presentation
Presentation srcPres = new Presentation(@"c:\source.ppt");

//Get the first slide
Slide sld = srcPres.GetSlideByPosition(1);

//Get three pictureframes by their alternative text
//i.e logo1, logo2, logo3
PictureFrame pfLogo1 = sld.FindShape("logo1") as PictureFrame;
PictureFrame pfLogo2 = sld.FindShape("logo2") as PictureFrame;
PictureFrame pfLogo3 = sld.FindShape("logo3") as PictureFrame;

//Read the three new pictures which will be used to replace
//logo1, logo2, logo3
Picture picLogo1 = new Picture(srcPres, @"c:\Sunset.jpg");
Picture picLogo2 = new Picture(srcPres, @"c:\Water lilies.jpg");
Picture picLogo3 = new Picture(srcPres, @"c:\Blue hills.jpg");

//Add above pictures in source presentation
//and note down their picture ids
int idPicLogo1 = srcPres.Pictures.Add(picLogo1);
int idPicLogo2 = srcPres.Pictures.Add(picLogo2);
int idPicLogo3 = srcPres.Pictures.Add(picLogo3);

//Set the new picture ids of above pictureframes
pfLogo1.PictureId = idPicLogo1;
pfLogo2.PictureId = idPicLogo2;
pfLogo3.PictureId = idPicLogo3;

//Save the output presentation on disk
srcPres.Write("c:\\output.ppt");

Hi There,

Can you give an example of this in regard to pptx files. I have an image that i want to replace, but I can not find a method like FindShape in the pptx api.

Regards
Jason

Hi Jason,

There is PictureFrameEx class in pptx api to deal with pictures, but it does not exposes properties/methods to access/modify the picture. We will investigate if it is possible for pptx and let you know.

Hello Jason,

Slide.FindShape method just iterate all shapes on a slide and compare string parameter with Shape.AlternativeText. AlternativeText property is not available for pptx format yet so you should use another way to find shape or image on a slide. You can try to use rectangle with text instead of image and search shape by this text. After you found it you can add new PictureFrameEx with the same coordinates.

AlternativeText property also will be available for pptx but little later.