Table of Contents

Setting up the Template

About templates and data markers

A PowerPointWriter template is an PowerPoint presentation that contains PowerPointWriter data markers. A data marker is a line of text beginning with %%= that specifies a database column, variable, or array to insert into the presentation. PowerPointWriter supports data markers embedded in text. Data markers are added to a presentation in PowerPoint and then bound to data sources in code. PowerPointWriter populates the data markers with values from the data sources when the code is executed.

This tutorial demonstrates how to use data markers to import single rows of data.

Data marker syntax

The basic syntax for a data marker is %%=[DataSourceName].[ColumnName], where DataSourceName is the name of the data source and ColumnName is the name of the column in the data source. You need to follow these rules when naming data markers:

For more specific information about creating data markers, see How to use Data Markers

Adding data markers to the the template

Our template consists of 5 slides populated with text and data markers.

In the downloadable SomeFileName.zip under ProjectProposal, there is a completed template file located in ProjectProposal/templates/part1_template.pptx.

1. Start with a blank .pptx file. Save the file as template. pptx.

2. Set the presentation theme by going to the Design tab.

3. Add 3 text boxes to the first slide and populate the one you want to use as the title with the data marker %%=Proposal.Name

In the other two text boxes, add the %%=Proposal.Date, %%=Proposal.ReviewStatus data markers. The final slide should look something like the one below.

4. Add a new slide to the presentation. This slide should consist of two text boxes, one for the title and a large one for the body of text. In the title text box, place the data marker %%=Proposal.Name. Note that this is the same as the data marker used on our first slide.

The rest of the slide will contain additional data about the proposal from the Proposal data source. The data markers we are adding are: %%=Proposal.Leader, %%=Proposal.Start, %%=Proposal.Estimate, %%= Proposal.Summary and %%=Proposal.ReviewStatus. Because PowerPointWriter supports data markers embedded in text, we only need one text box for this information.

The finished slide should look like the one below.

Adding a PowerPointWriter Reference in Visual Studio

In the sample code, the reference to SoftArtisans.OfficeWriter.PowerPointWriter.dll has already been added to the ProjectProposal project.

Create a .NET project and add a reference to the PowerPointWriter library.

  1. Open Visual Studio and create a .NET project.
  2. Add a reference to SoftArtisans.OfficeWriter.PowerPointWriter.dll

Writing the Code

There is a sample web application page Part1.aspx and code behind Part1.aspx.cs available in the ProjectProposal/ directory that shows the completed code.

1. Include the SoftArtisans.OfficeWriter.PowerPointWriter namespace in the code behind

using SoftArtisans.OfficeWriter.PowerPointWriter;

2. In the method that will actually create the presentation, instantiate the PowerPointTemplate object.

PowerPointTemplate pptt = new PowerPointTemplate();

3. Open the template file for the presentation.

pptt.Open(Page.MapPath("//templates//part1_template.pptx"));

4. Create a DatabindingProperties object for binding data to the template.

DataBindingProperties dataProps = pptt.CreateDataBindingProperties();

5. Create two arrays to hold the values for the presentation. The first is the array of values to add to the presentation. The second contains the column names. The column names values must match the data markers in the template.

//The values to display
 object[] valuesArray = { "Project Name", "Project Date", "Review", "Leader Name", "Start Date", "Cost Estimate", "Project Summary" };

//The column names are the same as the data markers
string[] columnNamesArray = {"Name", "Date", "ReviewStatus", "Leader", "Start", "Estimate", "Summary"};

6. Bind the data to the template. Make sure the data source name is the same as the name used in the template.

pptt.BindData(valuesArray, columnNamesArray, "Proposal", dataProps);

7. Call process to import the data into the template and save the file.

pptt.Process();

//Stream the output in the response as an attachment
pptt.Save(Page.Response, "Part1_Output.xlsx", false);

Final Code

PowerPointTemplate pptt = new PowerPointTemplate();
pptt.Open(Page.MapPath("//templates//part1_template.pptx"));
DataBindingProperties dataProps = pptt.CreateDataBindingProperties();
object[] valuesArray = { "Project Name", "Project Date", "Review", "Leader Name", "Start Date", "Cost Estimate", "Project Summary" };
string[] columnNamesArray = {"Name", "Date", "ReviewStatus", "Leader", "Start", "Estimate", "Summary"};
pptt.BindData(valuesArray, columnNamesArray, "Proposal", dataProps);
pptt.Process();
pptt.Save(Page.Response, "Part1_Output.xlsx", false)

Downloads

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

Next Steps

Continue on to Part 2 - Repeat Behavior and Delete Slide