This sample demonstrates how to mimic mail merge behavior to create a photo gallery using PowerPointWriter. |
The data for this sample is a file containing multiple images. The images will be programmatically added to a data table along with a description.
Our template presentation consists of a title slide and a content slide. The content slide will be repeated for each of our images.
The code below uses ExcelApplication to extract the data from the Excel spreadsheet into a DataTable. This code is also used in the ExcelWriter sample, Excel to DataTable Sample. It then passes the presentation to PowerPointTemplate and binds the data. The code uses [MaxRowsPerSlide|DatabindingProperties.MaxRowsPerSlide] to ensure the data fits neatly in the presentation. See Fitting Data on to Multiple Slides for more information.
private void MailMerge(){ using(PowerPointTemplate ppt = new PowerPointTemplate()){ ppt.Open(@"C:\..\..\PhotoGalleryTemplate.pptx"); string[] colNames = { "Title", "Subtitle" }; object[] values = { "My photo gallery", "Images from April vacation 1998" }; ppt.BindData(values, colNames, "PhotoGallery", ppt.CreateDataBindingProperties()); DataTable imagedt = GetImageData(); DataBindingProperties dataProps = ppt.CreateDataBindingProperties(); dataProps.MaxRowsPerSlide = 1; ppt.BindData(imagedt, "Image", dataProps); ppt.Process(); ppt.Save(@"C:..\..\outputs\PhotoGalleryOutput.pptx"); } } private DataTable GetImageData() { string filePath = @"C:..\..\inputs\"; DataTable imagedt = new DataTable(); imagedt.Columns.Add("ImageData", typeof(byte[])); imagedt.Columns.Add("Title"); imagedt.Columns.Add("Description"); imagedt.Rows.Add(new object[] { System.IO.File.ReadAllBytes(filePath + "Chrysanthemum.jpg"), "Chrysanthemum", "A beautiful red flower" }); imagedt.Rows.Add(new object[] { System.IO.File.ReadAllBytes(filePath + "Desert.jpg"), "Blistering Desert", "Red plateaus of the desert" }); imagedt.Rows.Add(new object[] { System.IO.File.ReadAllBytes(filePath + "Hydrangeas.jpg"), "Hydrangeas", "From a walk" }); imagedt.Rows.Add(new object[] { System.IO.File.ReadAllBytes(filePath + "Jellyfish.jpg"), "Jellyfish", "Spotted at the aquarium" }); imagedt.Rows.Add(new object[] { System.IO.File.ReadAllBytes(filePath + "Koala.jpg"), "Koala Bear", "From the zoo" }); imagedt.Rows.Add(new object[] { System.IO.File.ReadAllBytes(filePath + "Lighthouse.jpg"), "North Harbor Lighthouse", "An old lighthouse off the coast of my summer home" }); imagedt.Rows.Add(new object[] { System.IO.File.ReadAllBytes(filePath + "Penguins.jpg"), "Penguin Trio", "Emperor penguins that happened to stumble on the beach outside my hotel" }); imagedt.Rows.Add(new object[] { System.IO.File.ReadAllBytes(filePath + "Tulips.jpg"), "Golden Tulips", "Day 1 of my holiday in Holland" }); return imagedt; } |
Below is one of the resulting slides from our output file.
* Images: inputs.zip
* Template: PhotoGalleryTemplate.pptx
* Sample output: PhotoGalleryOutput.pptx