C# .NET - Create PPT Slides through C#.net - Asked By vivek kumar on 11-Dec-08 06:54 AM

sir,

    I want to create ppt slide , when i am click on button. And also i want to transfer data from sql server to different slides...

   PLEASE HELP ME .


Re :: Create PPT Slides using C# - Shailendrasinh Parmar replied to vivek kumar on 11-Dec-08 07:00 AM

Here is the code to create Powerpoint Slide show

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using PowerPoint;
using Graph;
using Microsoft.Office.Core;

private static PowerPoint.Presentation _pPres = null;
private static PowerPoint.ApplicationClass _pApp = null;
private int noofSalespersons;

protected void Page_Load(object sender, EventArgs e)
{
_pApp = new ApplicationClass();
//Graph.Chart objChart;
int noofSlides;
noofSalespersons =12;
string sTemplateFile = Server.MapPath("Resource\\Template.ppt");
_pPres = _pApp.Presentations.Open(sTemplateFile, MsoTriState.msoTrue, MsoTriState.msoTrue, MsoTriState.msoFalse);
noofSlides = (noofSalespersons / 10) + 1;
if (noofSalespersons%10>0)
{
noofSlides += 1;
}
int slideIndex=1;
while (_pPres.Slides.Count!=noofSlides)
{
_pPres.Slides.InsertFromFile("D:\\Template.ppt", slideIndex, 1, 1);
slideIndex += 1;
}

Chart chart;
Graph.DataSheet dataSheet;
for (int i = 1; i <= slideIndex; i++)
{
PowerPoint.Slide slide = _pPres.Slides._Index(i) as Slide;
if (i==1)

{

PowerPoint.Shape shape = slide.Shapes[1];


chart = shape.OLEFormat.Object as Graph.Chart;

dataSheet = chart.Application.DataSheet;

dataSheet.Cells[2, 2] = "50";
dataSheet.Cells[2, 3] = "40";
dataSheet.Cells[2, 4] = "50";
dataSheet.Cells[2, 5] = "50";
dataSheet.Cells[3, 2] = "60";
dataSheet.Cells[3, 3] = "70";
dataSheet.Cells[3, 4] = "80";
dataSheet.Cells[3, 5] = "60";
//dataSheet.Cells[3, 6] = "0";
dataSheet.Cells[4, 2] = "50";
dataSheet.Cells[4, 3] = "40";
dataSheet.Cells[4, 4] = "50";
dataSheet.Cells[4, 5] = "50";
//dataSheet.Cells[4, 6] = "0";
chart.Application.Update();
dataSheet = null;
chart = null;

}

ArrayList listShapeName = new ArrayList();
ArrayList listAutoShapes = new ArrayList();
ArrayList listTextBox = new ArrayList();
ArrayList listPlaceHolder = new ArrayList();
int reminingShapes;

reminingShapes = noofSalespersons % 10;

if (i>=2)
{
if ((i == slideIndex) && (reminingShapes>0))
{
int remingCount = 0;
for (int k = 1; k <= slide.Shapes.Count; k++)
{
PowerPoint.Shape shape = slide.Shapes[k];
//string shapeName = shape.Name;
//listShapeName.Add(shapeName);
if (shape.Type == MsoShapeType.msoEmbeddedOLEObject)
{
remingCount += 1;
if (remingCount>reminingShapes)
{
//slide.Shapes[k].Delete();
listShapeName.Add(k);
}

}

}

for (int m = listShapeName.Count - 1; m >= 0; m--)

{

slide.Shapes[listShapeName[m]].Delete();

}

listShapeName.Clear();

for (int k = 1; k <= slide.Shapes.Count; k++)

{

PowerPoint.Shape shape = slide.Shapes[k];


if (reminingShapes < 6)

{

if (shape.Type == MsoShapeType.msoTextBox)

{

listTextBox.Add(k);

}

}

}

if (listTextBox.Count>0)

{

for (int m = listTextBox.Count - 1; m >= listTextBox.Count - 4; m--)

{

slide.Shapes[listTextBox[m]].Delete();

}

listTextBox.Clear();

}


for (int k = 1; k <= slide.Shapes.Count; k++)

{

PowerPoint.Shape shape = slide.Shapes[k];

if (reminingShapes < 6)

{

if (shape.Type == MsoShapeType.msoAutoShape)

{

listAutoShapes.Add(k);

}

}

}

if (listAutoShapes.Count>0)

{

for (int m = listAutoShapes.Count - 1; m >= listAutoShapes.Count - 4; m--)

{

slide.Shapes[listAutoShapes[m]].Delete();

}

listAutoShapes.Clear();

}

}

int z = 0;

int bottom = 0;

for (int k = 1; k <= slide.Shapes.Count; k++)

{

PowerPoint.Shape shape = slide.Shapes[k];

string shapeName= shape.Name;

if (shape.Type==MsoShapeType.msoEmbeddedOLEObject)

{

listShapeName.Add(shapeName);


chart =shape.OLEFormat.Object as Graph.Chart;

dataSheet = chart.Application.DataSheet;

//if (k % 2 == 0)

//{

z = z + 1;


//shape.Top = newHeight;

//shape.IncrementTop(200);//(200-shape.Height) + shape.Top);

shape.Height = ((42 - (2 * z)) * 200) / (40);

//shape.Width = 90;

dataSheet.Cells[2, 2] = (((11-z) * 100) / 100).ToString();

dataSheet.Cells[3, 2] = (((11-z) * 100) / 100).ToString();

dataSheet.Cells[4, 2] = (((11-z) * 100) / 100).ToString();

dataSheet.Cells[5, 2] = (((11-z) * 100) / 100).ToString();

//shape.Width = 90;


//}

//else

//{


// //shape.Width = 86;

// shape.Height = 200;

// //newHeight = shape.Top;

// dataSheet.Cells[2, 2] = ((80 * 100) / 260).ToString();

// dataSheet.Cells[3, 2] = ((70 * 100) / 260).ToString();

// dataSheet.Cells[4, 2] = ((60 * 100) / 260).ToString();

// dataSheet.Cells[5, 2] = ((50 * 100) / 260).ToString();

//}

chart.Application.Update();

dataSheet = null;

chart = null;

}
}
}

_pPres.SaveAs("d:\\Vijay\\Powerpoint\\Sample2.ppt", PpSaveAsFileType.ppSaveAsPresentation, Microsoft.Office.Core.MsoTriState.msoFalse);

_pPres.Close();

_pApp.Quit();

GC.Collect();

}


See the original article at :: http://www.dotnetspider.com/forum/157867-Create-power-point-slide-charts-using-c-net.aspx

Hope this helps.

Re :: Create PPT Slides using C# [For Window Application] - Shailendrasinh Parmar replied to vivek kumar on 11-Dec-08 07:03 AM

See the following article for creating slideshow in C#, but it for Window Application

http://www.eggheadcafe.com/tutorials/aspnet/86c1636b-0197-4d86-a452-e18866abd2cc/c--power-point-slide-sh.aspx

Hope it helps.

QUESTION - vivek kumar replied to Shailendrasinh Parmar on 11-Dec-08 07:03 AM

SIR,

   BUT I WANT TO USE  SQL QUERY TO TRANSFER DATA FROM DATABASE TO SLIDES.

PLEASE HELP ME

TRY THIS - selva murugan replied to vivek kumar on 11-Dec-08 07:03 AM

http://www.dotnetspider.com/forum/157867-Create-power-point-slide-charts-using-c-net.aspx

http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=3573445&SiteID=1

TRY THIS - C_A P replied to vivek kumar on 12-Dec-08 02:13 AM

Create a new project, for simplicity, create a Windows application. Go ahead and right click on References in the Solution Explorer, and select Add Reference… When the Add Reference window comes up, select the COM tab. This will list all Component names which are available on your machine. Since we are going to use Microsoft PowerPoint , you will scroll down until you find: Microsoft Power point 11.0 Object Library. You can even do the same thing with PIAs. In the code, I have used PIA.

I have created a very simple wrapper class for the PowerPoint object model. The code will be given below. This project also has the error provider functionality to make sure that the user will enter values for all the fields.

Create button click event. Here, we check whether all fields are filled and then we call the CreateFile() function in the PowerPoint wrapper class.

Collapse
private void btnCreate_Click(object sender, System.EventArgs e)
{
bool filled = true;

if ( ( txtName.Text).Trim().Length.Equals(0) )
{
errorProvider.SetError(txtName,"cannot be empty" );
filled = false;
}
if( ( txtAge.Text ).Trim().Length.Equals(0) )
{
errorProvider.SetError( txtAge,"cannot be empty" );
filled = false;
}
if( ( txtDesignation.Text ).Trim().Length.Equals(0) )
{
errorProvider.SetError( txtDesignation,"cannot be empty" );
filled = false;
}
if( ( txtPlace.Text ).Trim().Length.Equals(0) )
{
errorProvider.SetError( txtPlace,"cannot be empty" );
filled = false;
}
if( ( txtEmail.Text ).Trim().Length.Equals(0) )
{
errorProvider.SetError( txtEmail,"cannot be empty" );
filled = false;
}
if( ( txtCompany.Text ).Trim().Length.Equals(0) )
{
errorProvider.SetError( txtCompany,"cannot be empty" );
filled = false;
}

if ( filled == true )
{
PPTAuto ppt = new PPTAuto();
StringCollection array = new StringCollection ();

array.Insert(0,txtName.Text);
array.Insert(1,txtAge.Text);
array.Insert(2,txtDesignation.Text);
array.Insert(3,txtCompany.Text);
array.Insert(4,txtPlace.Text);
array.Insert(5,txtEmail.Text);

ppt.CreateFile(array);
}
}

PowerPoint File Creation Code

Collapse
public void CreateFile(StringCollection array)
{
String strTemplate;
// getting one Microsoft template
strTemplate = "C:\\Program Files\\Microsoft Office\\Templates\\
Presentation Designs\\Glass Layers.pot"
;

//Create a new presentation based on a template.
objApp = new PowerPoint.Application();
objApp.Visible = MsoTriState.msoTrue;
objPresSet = objApp.Presentations;
objPres = objPresSet.Open(strTemplate,
MsoTriState.msoFalse, MsoTriState.msoTrue, MsoTriState.msoTrue);

objSlides = objPres.Slides;

// Adding a slide
objSlide = objSlides.Add(1,PowerPoint.PpSlideLayout.ppLayoutTitleOnly);

// adding different text boxes
objSlide.Shapes.AddTextbox
(MsoTextOrientation.msoTextOrientationHorizontal,100,100,500,80);
objSlide.Shapes.AddTextbox
(MsoTextOrientation.msoTextOrientationHorizontal,100,150,500,80);
objSlide.Shapes.AddTextbox
(MsoTextOrientation.msoTextOrientationHorizontal,100,200,500,80);
objSlide.Shapes.AddTextbox
(MsoTextOrientation.msoTextOrientationHorizontal,100,250,500,80);
objSlide.Shapes.AddTextbox
(MsoTextOrientation.msoTextOrientationHorizontal,100,300,500,80);
objSlide.Shapes.AddTextbox
(MsoTextOrientation.msoTextOrientationHorizontal,100,350,500,80);
objSlide.Shapes.AddTextbox
(MsoTextOrientation.msoTextOrientationHorizontal,100,400,500,80);

objTextRng = objSlide.Shapes[1].TextFrame.TextRange;
objTextRng.Text = "These are my details";
objTextRng.Font.Name = "Arial";
objTextRng.Font.Size = 20;

objTextRng = objSlide.Shapes[2].TextFrame.TextRange;
objTextRng.Text =array[0] ;
objTextRng.Font.Name = "Arial";
objTextRng.Font.Size = 20;

objTextRng = objSlide.Shapes[3].TextFrame.TextRange;
objTextRng.Text =array[1] ;
objTextRng.Font.Name = "Arial";
objTextRng.Font.Size = 20;

objTextRng = objSlide.Shapes[4].TextFrame.TextRange;
objTextRng.Text =array[2] ;
objTextRng.Font.Name = "Arial";
objTextRng.Font.Size = 20;

objTextRng = objSlide.Shapes[5].TextFrame.TextRange;
objTextRng.Text =array[3] ;
objTextRng.Font.Name = "Arial";
objTextRng.Font.Size = 20;

objTextRng = objSlide.Shapes[6].TextFrame.TextRange;
objTextRng.Text =array[4] ;
objTextRng.Font.Name = "Arial";
objTextRng.Font.Size = 20;

objTextRng = objSlide.Shapes[7].TextFrame.TextRange;
objTextRng.Text =array[5] ;
objTextRng.Font.Name = "Arial";
objTextRng.Font.Size = 20;
}
TRYTHIS LINK - C_A P replied to vivek kumar on 12-Dec-08 02:17 AM
http://cs.senecac.on.ca/~sql710/lectures/sql710week5.ppt

http://www.cs.ucl.ac.uk/staff/C.Clack/3C13/powerpoints/databasesIIProgrammaticSQL.ppt