Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migration of unmigrated content due to installation of a new plugin

...

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.

...

}
Note

This

sample

stores

some

of

the

data

in

a

CSV

file,

which

is

available

for

download

under

*

Downloads

*

.

The

CSV

parser

used

in

the

example

code

was

developed

by

Andrew

Rissing

and

can

be

downloaded

from

[

Code

Project

|http://www.codeproject.com/KB/database/GenericParser.aspx].{note} h3. The Template Our template presentation contains a single slide with 2 data markers. !template.png! h3. The Code The code below uses PowerPointApplication to copy the slide in our template, then passes the presentation to PowerPointTemplate to bind the data. {newcode}

.

The Template

Our template presentation contains a single slide with 2 data markers.
Image Added

The Code

The code below uses PowerPointApplication to copy the slide in our template, then passes the presentation to PowerPointTemplate to bind the data.

Code Block


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
    }

{newcode}
h3. Result
The resulting output is a presentation containing 2 slides with the same layout but different data.
!output.png!

h1. Downloads
data:[^Team.csv] [^Agile.csv]
template:[^template.pptx]
output: [^output.pptx]

Result

The resulting output is a presentation containing 2 slides with the same layout but different data.
Image Added

Downloads

data:Team.csv Agile.csv
template:template.pptx
output: output.pptx