Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Table of Contents

Table of Contents
maxLevel2

Intro

Note

This is Part 3 of a 3-part tutorial series for the Employee Training scenario. It is recommended that you complete Part 1 - Importing Data and Part 2 - The Continue Modifier before starting this section.

...

This part focuses on passing a presentation between PowerPointTemplate and PowerPointApplication in order to take advantage of the full range of features available with PowerPointWriter.

Adding to the Template

We will continue adding to our template from Part 2. The next part of our presentation will contain information about the Team the new employee will be working on.

...

This is the 8th and final slide in our template. This slide will be used as a template slide for 4 different data sets. To achieve this we will use the Slides.Copy method and the Slide data binding property.

Writing the Code

Info
titleFollowing the Sample

There is a downloadable PowerPointWriter_BasicTutorials.zip with completed templates and code. The completed example of the template is available under ProjectProposal/templates/part3_template.pptx. The code for this part of the tutorial can be found in Part3.aspx.cs.

We will start with an unprocessed template file containing all of the slides from the 3 parts of this tutorial.

  1. Create a PowerPointApplication object.
    Code Block
    
    PowerPointApplication ppta = new PowerPointApplication();
    

    # Set a Presentation object to our template file. This allows us to use all of the methods in the PowerPointApplication API including Slides.CopySlide.
    Code Block
    
    Presentation pres = ppta.Open(Page.MapPath("//templates//part3_template.pptx"));
    

    \The following steps will create 3 copies of the last slide.
    # Create a Slide object for the last slide in our template.
    Code Block
    
    Slide slideToCopy = pres.Slides[7];
    
  2. Call Slides.CopySlide three times, with each call placing the new slide at the end of the presentation.
    Code Block
    
    pres.Slides.CopySlide(slideToCopy, (pres.Slides.Count));
    pres.Slides.CopySlide(slideToCopy, (pres.Slides.Count));
    pres.Slides.CopySlide(slideToCopy, (pres.Slides.Count));
    

    \Now that our presentation contains all of the slides we want, we can pass it to PowerPointTemplate to import the data. See Passing Between Template and Application for more information.
    Code Block
    
    PowerPointTemplate pptt = new PowerPointTemplate();
    pptt.Open(ppta, pres);
    

    \We will reuse the code from Part 1 and Part 2 of this tutorial. We will take out the Save call and add then our new code for Part 3.
    Code Block
    
    //Code from Part 1
    PowerPointTemplate pptt = new PowerPointTemplate();
    pptt.Open(Page.MapPath("//templates//part1_template.pptx"));
    DataBindingProperties dataProps = pptt.CreateDataBindingProperties();
    string[] columnNamesArray = {"Logo"};
    Byte[] logoArray = System.IO.File.ReadAllBytes((Page.MapPath("//data//logo.png")));
    object[] valuesArray = {logoArray};
    pptt.BindData(valuesArray, columnNamesArray, "Company", dataProps);
    string[] employeeColNames = {"Name", "Team"};
                    object[] employeeValues = {"Amy Alberts", "Development"};
    pptt.BindData(employeeValues, employeeColNames, "Employee", dataProps);
    DataTable teamData = GetCSVData((Page.MapPath("//data//Team.csv")));
    pptt.BindData(teamData, "Team", dataProps);
     //Code from Part 2
    DataTable dtCompanyHistory = GetCSVData(Page.MapPath("//data//CompanyHistory.csv"));
    dataProps.MaxRowsPerSlide = 5;
    pptt.BindData(dtCompanyHistory, "CompanyHistory", dataProps);
    DataTable dtProducts = GetCSVData(Page.MapPath("//data//Products.csv"));
    DataBindingProperties dataProps2 = pptt.CreateDataBindingProperties();
    dataProps2.MaxRowsPerSlide = 2;
    pptt.BindData(dtProducts, "Products", dataProps2);
    

...


  1. \Next, we will bind the data for the slides that were added in part 3.
  2. Create an array containing the string "TrainingItem". Becuase all of our remaining slides use the same data markers, this array can be used as the column names array for multiple BindData calls.
    Code Block
    
    

...

  1. string[] 

...

  1. titleCol = 

...

2. We want to delete the last slide in our presentation, so we need to know how many slides there are. We can use Slides.Count to get this information.

Code Block

int numSlides = pres.Slides.Count;

3. Use the Slides.Delete method to delete the last slide. It takes an integer representing the index of the slide to delete as a parameter.

Code Block

pres.Slides.Delete(numSlides - 1);

4. Save the final presentation and stream it in the response.

...

  1. { "TrainingItem" };
    
  2. Get the data for the first slide in the 'Your Team' section of the presentation. The title of this slide is 'Team Members'.
    Code Block
    
    //Data for the TrainingItem data source
    object[] titleArrayTeam = { "Team Members" };
    //Get the Team members data
    DataTable dtTeamMembers = GetCSVData(Page.MapPath("//data//TeamMembers.csv"));
    
    In order to keep the 'Team Members' data from populating all of our copied slides, we need to set the Slide property to scope to the slide we want. In this case, it is the 8th slide in our presentation.
  3. Set the dataProps Slide property to be 7. Slide uses the 0-indexed position of the slide, so this will scop to slide 8.
    Code Block
    
    dataProps.Slide = 7;
    
  4. Bind the 'Team Members' data to the presentation. This will take two BindData calls, for the two data sources on the slide.
    Code Block
    
    pptt.BindData(titleArrayTeam, titleCol, "Department", dataProps);
    pptt.BindData(dtTeamMembers, "TrainingItems", dataProps);
    
  1. Follow these same steps for the remaining slides. Make sure to change the Slide property before each BindData call. The same DataBindingProperties object can be used for all the BindData calls.
Code Block


//The 9th slide will be 'Agile Development / Scrum'
object[] titleArrayAgile = { "Agile Development / Scrum" };
DataTable dtAgile = GetCSVData(Page.MapPath("//data//Agile.csv"));
dataProps.Slide = 8;
pptt.BindData(titleArrayAgile, titleCol, "Department", dataProps);
pptt.BindData(dtAgile, "TrainingItems", dataProps);

//The 10th slide will be 'REsources and Utilities'
object[] titleArrayResources = { "Resources and Utilities" };
DataTable dtResources = GetCSVData(Page.MapPath("//data//Resources.csv"));
dataProps.Slide = 9;
pptt.BindData(titleArrayResources, titleCol, "Department", dataProps);
pptt.BindData(dtResources, "TrainingItems", dataProps);

//The final slide will be 'Expectations'
object[] titleArrayExpectations = { "Expectations" };
DataTable dtExpectations = GetCSVData(Page.MapPath("//data//Expectations.csv"));
dataProps.Slide = 10;
pptt.BindData(titleArrayExpectations, titleCol, "Department", dataProps);
pptt.BindData(dtExpectations, "TrainingItems", dataProps);
  1. Finally, Process and Save the finished presentation. Congratulations, the tutorial is complete!
    Code Block
    
    pptt.Process();
    pptt.Save(Page.Response, "part3_OutPut.pptx", false);
    

Final Code

Note

For information on writing this code, see Part 1 - Importing Data and Part 2 - The Continue Modifier.

Code Block
//CodePowerPointApplication from Part 1
PowerPointTemplate pptt ppta = new PowerPointTemplatePowerPointApplication();
ppttPresentation pres = ppta.Open(Page.MapPath("//templates//part1part3_template.pptx"));
Slide slideToCopy = pres.Slides[7];
pres.Slides.CopySlide(slideToCopy, (pres.Slides.Count));
pres.Slides.CopySlide(slideToCopy, (pres.Slides.Count));
pres.Slides.CopySlide(slideToCopy, (pres.Slides.Count));
PowerPointTemplate pptt = new PowerPointTemplate();
pptt.Open(ppta, pres);
//Code from Part 1
 DataBindingProperties dataProps = pptt.CreateDataBindingProperties();
objectstring[] valuesArraycolumnNamesArray = { "Surgery ScheduleLogo", "January 1, 2014", "Under Review", "Pamela Blythe", "January 1, 2015", "6 months", "Software to schedule surgeries that require multiple resources such as surgeons, assistants, nurses, pre-op and post-op space, surgery theater, long-term recovery rooms." };
string[] columnNamesArray = { "Name", "Date", "ReviewStatus", "Leader", "Start", "Estimate", "Summary" };
pptt.BindData(valuesArray, columnNamesArray, "Proposal", dataProps);
//Code from Part 2
DataTable dtTeam };
Byte[] logoArray = System.IO.File.ReadAllBytes((Page.MapPath("//data//logo.png")));
object[] valuesArray = { logoArray };
pptt.BindData(valuesArray, columnNamesArray, "Company", dataProps);
string[] employeeColNames = { "Name", "Team" };
object[] employeeValues = { "Amy Alberts", "Development" };
pptt.BindData(employeeValues, employeeColNames, "Employee", dataProps);
DataTable teamData = GetCSVData((Page.MapPath("//data//Team.csv")));
pptt.BindData(teamData, "Team", dataProps);
//Code from Part 2
DataTable dtCompanyHistory = GetCSVData(Page.MapPath("//data//CompanyHistory.csv"));
dataProps.MaxRowsPerSlide = 5;
pptt.BindData(dtCompanyHistory, "CompanyHistory", dataProps);
DataTable dtProducts = GetCSVData(Page.MapPath("//data//Products.csv"));
DataBindingProperties dataProps2 = pptt.CreateDataBindingProperties();
dataProps2.MaxRowsPerSlide = 2;
pptt.BindData(dtProducts, "Products", dataProps2);
//add the data from part 3
string[] titleCol = { "TrainingItem" };
object[] titleArrayTeam = { "Team Members" };
DataTable dtTeamMembers = GetCSVData(Page.MapPath("//data//TeamMembers.csv"));
dataProps.Slide = 7;
pptt.BindData(titleArrayTeam, titleCol, "Department", dataProps);
pptt.BindData(dtTeamMembers, "TrainingItems", dataProps);
object[] titleArrayAgile = { "Agile Development / Scrum" };
DataTable dtAgile = GetCSVData(Page.MapPath("//data//TeamAgile.csv"));
DataTable dtCostdataProps.Slide = 8;
pptt.BindData(titleArrayAgile, titleCol, "Department", dataProps);
pptt.BindData(dtAgile, "TrainingItems", dataProps);
object[] titleArrayResources = { "Resources and Utilities" };
DataTable dtResources = GetCSVData(Page.MapPath("//data//CostResources.csv"));
dataProps.MaxRowsPerSlideSlide = 109;
pptt.BindData(dtTeamtitleArrayResources, titleCol, "TeamDepartment", dataProps);
pptt.BindData(dtCostdtResources, "CostTrainingItems", dataProps);
pptt.Process();
//Code from Part 3
PowerPointApplication ppta = new PowerPointApplication();
Presentation pres = ppta.Open(pptt);
pres.Slides.Delete(numSlides - 1)
ppta.Save(pres, object[] titleArrayExpectations = { "Expectations" };
DataTable dtExpectations = GetCSVData(Page.MapPath("//data//Expectations.csv"));
dataProps.Slide = 10;
pptt.BindData(titleArrayExpectations, titleCol, "Department", dataProps);
pptt.BindData(dtExpectations, "TrainingItems", dataProps);
pptt.Process();
pptt.Save(Page.Response, "Part2part3_OutputOutPut.pptx", false);

Downloads

You can download the code for the Basic PowerPointWriter Tutorials as a Visual Studio solution, which includes the Project Proposal.

...