Description

This sample demonstrates how to use PowerPointWriter's Image modifier to add images to your presentation. This sample covers importing images with specific dimensions, and using the PowerPointApplication object to align images on a slide.

The Data

The data for this sample consists of data regarding sales of certain products and an image showing the data on a chart.

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.

The Template

Our template presentation consists of a single slide with three data markers. Two of the data marker are placed in a table and will expand to fit the sales data. The third data marker, %%=Images.Chart(image(2)), is for our chart image. This data marker uses the image modifier with the scaling value 2. See [Importing Images] for more on scaling values. In this case, the imported image will be scaled to fit within the text box containing the data marker, but will retain its natural aspect ratio.

By default, text boxes will automatically take on the dimensions of the text inside of them. However, we want to set the text box to be the desired size for the imported picture. To allow re-sizing of the text box, use the _Do not Autofit_ option. To set _Do not Autofit_, right click on the text box and click Size and Position. On the text box tab, check the box marked Do not Autofit.

Our finished template will look like the following:

The Code

The code below uses PowerPointTemplate to import the images into the presentation. It then uses PowerPointApplication to align the chart on our slide.

public void ChartImageReport()
{
using (PowerPointTemplate pptt = new PowerPointTemplate())
{
pptt.Open(@"C:\Users\gabeg\Desktop\Internal Projects\PPTW Documentation\Testing Solutions\PPTWDoc\ChartImage\template.pptx");
//get the data to display with the chart
DataTable dtChart = GetCSVData(@"C:\Users\gabeg\Desktop\Internal Projects\PPTW Documentation\Testing Solutions\PPTWDoc\ChartImage\data.csv");

pptt.BindData(dtChart, "UnitSales", pptt.CreateDataBindingProperties());
//get the Chart
object[] chartImage = { System.IO.File.ReadAllBytes(@"C:\Users\gabeg\Desktop\Internal Projects\PPTW Documentation\Testing Solutions\PPTWDoc\ChartImage\chart.png") };
string[] chart = {"Chart"};
pptt.BindData(chartImage, chart, "Images", pptt.CreateDataBindingProperties());
pptt.Process();
//pass to Application object to align chart image
PowerPointApplication ppta = new PowerPointApplication();
Presentation pres = ppta.Open(pptt);
//Get a handle on the chart image
Slide slide1 = pres.Slides[0];
Pictures allPics = slide1.Pictures;
Picture pic = allPics[0];
int numPics = allPics.Count();
//Align the picture
pic.Align(Alignment.Middle);
ppta.Save(pres, Page.Response, "output.pptx", false);
}
///<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 slide is below:

Downloads

* Data:data.csvchart.png
* Template:template.pptx
* Sample output: output.pptx