I have created Powerpoint using images from folder, and everything works fine, what i want to do is insert 4 images per slide , i referred the below link for doing the presentation and it works like charm MSDN
however there is a code where it says to insert multiple images per slide we have to change the hard coded relationship id to more dynamic ids,
// Create a slide part for the new slide.
var slidePart = presentationPart.AddNewPart<SlidePart>(relId);
GenerateSlidePart(imageFileNameNoPath, imageFileNameNoPath,
imageWidthEMU, imageHeightEMU).Save(slidePart);
// Add the relationship between the slide and the
// slide layout.
slidePart.AddPart<SlideLayoutPart>(slideLayoutPart);
// Create an image part for the image used by the new slide.
// A hardcoded relationship id is used for the image part since
// there is only one image per slide. If more than one image
// was being added to the slide, an approach similar to that
// used before for the slide part relationship id could be
// followed, where the image part relationship id could be
// incremented for each image part.
var imagePart = slidePart.AddImagePart(ImagePartType.Jpeg,"relId1");
GenerateImagePart(imagePart, imageBytes);
doing so corrupts the Presentation, below is the modified function
using (PresentationDocument newDeck =
PresentationDocument.Open(newPresentation, true))
{
PresentationPart presentationPart = newDeck.PresentationPart;
// Reuse the slide master part. This code assumes that the
// template presentation being used has at least one
// master slide.
var slideMasterPart = presentationPart.SlideMasterParts.First();
// Reuse the slide layout part. This code assumes that the
// template presentation being used has at least one
// slide layout.
var slideLayoutPart = slideMasterPart.SlideLayoutParts.First();
// If the new presentation doesn't have a SlideIdList element
// yet then add it.
if (presentationPart.Presentation.SlideIdList == null)
presentationPart.Presentation.SlideIdList = new SlideIdList();
// Loop through each image file creating slides
// in the new presentation.
int imageNo = 0;
SlidePart slidePart = null;
foreach (string imageFileNameWithPath in imageFileNames)
{
imageFileNameNoPath =
Path.GetFileNameWithoutExtension(imageFileNameWithPath);
// Create a unique relationship id based on the current
// slide id.
relId = "rel" + currentSlideId;
imageRelId = "relId" + RandomString(5); //function to generate random string of length 5
// Get the bytes, type and size of the image.
ImagePartType imagePartType = ImagePartType.Png;
byte[] imageBytes = GetImageData(imageFileNameWithPath,
ref imagePartType, ref imageWidthEMU, ref imageHeightEMU);
if (imageNo % 4 == 0)
{
// Create a slide part for the new slide.
slidePart = presentationPart.AddNewPart<SlidePart>(relId);
GenerateSlidePart(imageFileNameNoPath, imageFileNameNoPath,
imageWidthEMU, imageHeightEMU,imageRelId).Save(slidePart);
// Add the relationship between the slide and the
// slide layout.
slidePart.AddPart<SlideLayoutPart>(slideLayoutPart);
}
// Create an image part for the image used by the new slide.
// A hardcoded relationship id is used for the image part since
// there is only one image per slide. If more than one image
// was being added to the slide an approach similar to that
// used above for the slide part relationship id could be
// followed, where the image part relationship id could be
// incremented for each image part.
var imagePart = slidePart.AddImagePart(ImagePartType.Jpeg,
imageRelId);
GenerateImagePart(imagePart, imageBytes);
if (imageNo % 4 == 0)
{
// Add the new slide to the slide list.
slideId = new SlideId();
slideId.RelationshipId = relId;
slideId.Id = currentSlideId;
presentationPart.Presentation.SlideIdList.Append(slideId);
// Increment the slide id;
currentSlideId++;
}
imageNo++;
}
// Save the changes to the slide master part.
slideMasterPart.SlideMaster.Save();
//OpenXmlUtils.DeleteSlide(presentationPart, presentationPart.GetSlidePartsInOrder().Last());
// Save the changes to the new deck.
presentationPart.Presentation.Save();
}
here is the function to get random string
private static string RandomString(int length)
{
const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
var random = new Random();
return new string(Enumerable.Repeat(chars, length)
.Select(s => s[random.Next(s.Length)]).ToArray());
}