Description
This sample demonstrates how to use PowerPointWriter's CopySlide method and Slide Data Binding Property to copy slides and bind them with different data. This makes it easy to programmatically create multiple slides with the same layout in your presentation.The Template
Our template presentation contains a single slide with 2 data markers.
The Code
The code below uses PowerPointApplication to copy the slide in our template, then passes the presentation to PowerPointTemplate to bind the data.
public void CopySlide(){ PowerPointApplication ppta = new PowerPointApplication(); Presentation pres = ppta.Open( "template.pptx" ); //Create a copy of the slide and add it to the end of the presentation Slide slideToCopy = pres.Slides[0]; pres.Slides.CopySlide(slideToCopy, (pres.Slides.Count)); //pass the presentation to PowerPointTemplate PowerPointTemplate pptt = new PowerPointTemplate(); pptt.Open(ppta, pres); //Set the column names - will be the same for both slides string [] titleCol = { "Title" }; string [] detailsCol = { "Details" }; //Create a Data Binding Property DataBindingProperties dataProps = pptt.CreateDataBindingProperties(); //Get the data for the first slide - Team Members //Data for the TrainingItem data source object [] titleArray = { "Team Members" }; DataTable dtTeam = GetCSVData( "Team.csv" ); //Set Slide to be the first slide in our presentation dataProps.Slide = 0; //Bind the Data pptt.BindData(titleArray, titleCol, "Training" , dataProps ); pptt.BindData(dtTeam, "TrainingItems" , dataProps); //Get the data for the second slide - Agile Development object [] titleArray2 = { "Agile Development / Scrum" }; DataTable dtAgile = GetCSVData( "Agile.csv" ); //Change our Data Binding Properties to scope to the second slide dataProps.Slide = 1; //Bind the Data - only the array of values changed pptt.BindData(titleArray2, titleCol, "Training" , dataProps); pptt.BindData(dtAgile, "TrainingItems" , dataProps); //Process the data pptt.Process(); pptt.Save( "output.pptx" ); } ///<summary> /// Uses a 3rd party generic CSV parser #region Utility Methods //Uses CSV reader System.Data.DataTable GetCSVData( string csvFileName) { DataTable dt; using (GenericParserAdapter parser = new GenericParserAdapter(csvFileName)) { parser.ColumnDelimiter = ',' ; parser.FirstRowHasHeader = true ; dt = parser.GetDataTable(); } return dt; } #endregion } |
Result
The resulting output is a presentation containing 2 slides with the same layout but different data.
Downloads
data:Team.csv Agile.csv
template:template.pptx
output: output.pptx