How to organize images automatically?

Hi,

I want to generate presentation based on images in database. I want to have 3 images per slide. Based on the number of images in database per component, the presentation should generate number of slides automatically and 3 images per slide. And in the presentation there might be multiple components. The images have image IDs as 0, 1, 2, 3 ....

For example, component A has 5 images and component B has 7 images, then component A will have 2 slides and component B will have 3 slides.

Please help! Thanks!

Hi Jane,

Thanks for your interest in Aspose.Slides.

Please visit following documentation link.

  1. Adding Slides to Presentation
  2. Adding Picture Frame to Slide
First you need to create presentation class object and add slides in a loop and add pictures to each slide.


The links don't work. And I know how to add pictures and slides, but I don't know how to loop through and get the correct number of pictures on corresponding slides. Can you share some sample code?

Thanks!

Hi Jane,

You can devise your own frame work to organize images belonging to various components from database in presentation slides using Aspose.Slides. I have worked on my end to help you a little and have written a code snippet that is capable of adding images from various components of database and at most three images per slides. The code will continue to add the 3 images per slides for any number of images in a particular component. I have used template presentation with a slide having three picture frames. I have used AlternativeText property of the picture frame for accurate identification. In the end the extra remaining picture frames for the images of particular component are also deleted. I.e. if a component has 4 images then three images will reside in first slide and 4th image will reside in 2nd slide. Since, I am using a template slide with three picture frames, so I have to delete the extra remaining picture frames for particular component as well. The code snippet will hopefully serve your purpose accurately.

public static void AddDbImage()

{

Presentation pres = new Presentation();

Presentation Template = new Presentation("D:\\Aspose Data\\ImageTemp.ppt");

Slide slide = Template.GetSlideByPosition(1);

Slide SrcSlide=null ;

System.Collections.SortedList sList = null;

Shape shp=null ;

PictureFrame pf;

Picture pic;

int picId = 0;

int iComp=3;//total compnents

int ImginComp=0;

int sldImgCounter = 0;

for(; iComp > 0; iComp--)

{

ImginComp=5;//These are images in any components

for (int i = 0; i < ImginComp; i++)

{

if (i%3== 0 )//If new component or the component has more than three images

{

sList = new System.Collections.SortedList();

//Cloning the selected slide at the end of another presentation file

SrcSlide=Template.CloneSlide(slide, pres.Slides.LastSlidePosition + 1, pres, sList);

sldImgCounter = 1;

}

if (sldImgCounter == 1)

shp = Aspose.Slides.Util.SlideUtil.FindShape(SrcSlide, "Image1");//Image1 is alternative text of picture frame

else if (sldImgCounter == 2)

shp = Aspose.Slides.Util.SlideUtil.FindShape(SrcSlide, "Image2");

else if (sldImgCounter == 3)

shp = Aspose.Slides.Util.SlideUtil.FindShape(SrcSlide, "Image3");

if (shp is PictureFrame)

{

//Here we are adding the image and replacing the images in desired PictureFrame

pf = (PictureFrame)shp;

pic = new Picture(pres, "C:\\demo.jpg");//Add Your desired image here

//Adding the picture object to pictures collection of the presentation

//After the picture object is added, the picture is given a uniqe picture Id

picId = pres.Pictures.Add(pic);

pf.PictureId = picId;

sldImgCounter++;

}

}

//If there are less then three images in slide then the rest of

// temaplate picture frames in that particular slide are to be deleted

if (sldImgCounter < 4)

{

for (int iTemp = sldImgCounter; iTemp <= 4; iTemp++)

{

shp = Aspose.Slides.Util.SlideUtil.FindShape(SrcSlide, "Image" + iTemp );

SrcSlide.Shapes.Remove(shp);

}

}

}

//Removing default slide

pres.Slides.RemoveAt(0);

pres.Write("NewPres.ppt");

}

Thanks and Regards,

Thank a lot. That worked.

Hi, I’m trying to modify the code to work in pptx, but seems to be very difficult for me. Mayb it will only take you a couple of minutes to do so. Can you please help?

I’ve tried very hard but still no luck. Please help. Thanks!

Hello Dear,

I have written the code for PPTX as well for your reference. Please use the following code snippet.

public static void AddDbImage()

{

PresentationEx pres = new PresentationEx();

PresentationEx Template = new PresentationEx("D:\\Aspose Data\\ImageTemp.pptx");

SlideEx TemplateSlide = Template.Slides[0];

SlideEx SrcSlide = null;

SlidesEx slides = pres.Slides;

ShapeEx shp = null;

PictureFrameEx pf;

int iComp = 3;//total compnents

int ImginComp = 0;

int sldImgCounter = 0;

int iSlideNum = 0;

for (; iComp > 0; iComp--)

{

ImginComp = 5;//These are images in any components

for (int i = 0; i < ImginComp; i++)

{

if (i % 3 == 0)//If new component or the component has more than three images

{

iSlideNum = slides.AddClone(TemplateSlide);

SrcSlide = pres.Slides[iSlideNum];

sldImgCounter = 1;

}

if (sldImgCounter == 1)

shp = Aspose.Slides.Util.SlideUtil.FindShape(SrcSlide,"Image1");//Image1 is alternative text of picture frame

else if (sldImgCounter == 2)

shp = Aspose.Slides.Util.SlideUtil.FindShape(SrcSlide, "Image2");

else if (sldImgCounter == 3)

shp = Aspose.Slides.Util.SlideUtil.FindShape(SrcSlide, "Image3");

if (shp is PictureFrameEx)

{

pf = (PictureFrameEx)shp;

//Here we are adding the image and replacing the images in desired PictureFrame

System.Drawing.Image img =(Image)new Bitmap("C:\\demo.jpg");

ImageEx imgx = pres.Images.AddImage(img);

pf.PictureFormat.Picture.Image = imgx;

sldImgCounter++;

}

}

//If there are less then three images in slide then the rest of

// temaplate picture frames in that particular slide are to be deleted

if (sldImgCounter < 4)

{

for (int iTemp = sldImgCounter; iTemp <= 4; iTemp++)

{

shp = Aspose.Slides.Util.SlideUtil.FindShape(SrcSlide, "Image" + iTemp);

SrcSlide.Shapes.Remove(shp);

}

}

}

//Removing default slide

pres.Slides.RemoveAt(0);

pres.Write("NewPres.ppt");

}

Thanks and Regards,

Hi, I've been working on this for several days. Could you please kindly enough to provide a sample VB code to add another set of images to each component? Now in your example there are 3 components, 5 images per conponents. How can I have 3 components, 5 group A images, 4 group B imgages per component (so total 9 images per component)? I will greatly appreciate if you can provide sample code in VB.

Hi Jane,

I have converted the code snippet for your kind reference. Please share if you may feel any issue.

Thanks and Regards,

Thanks a lot.