MS PowerPoint presentations allow you to create slides containing text, images, charts, animations, and other elements. A variety of additional formatting options can make your presentation more attractive. In this article, you’ll learn how to create such presentations programmatically, and you’ll learn how to create PPTX presentations with text, tables, images, and charts using C#.

  • Create a PowerPoint presentation
  • Open an existing PowerPoint presentation
  • Add slides to your presentation
  • Adds text to the slides of your presentation
  • Create tables in your presentation
  • Create charts in your presentation

Aspose.Slides for.net is a presentation operation API that allows you to create and manipulate PowerPoint documents from.NET applications. The API provides almost every possible feature needed to implement basic and advanced PowerPoint automation features.

>> You can download the latest version of Aspose.Slides for the test experience.

Create a PowerPoint presentation in C#

Let’s first create an empty PowerPoint presentation using Aspose.Slides for.net. Here are the steps to do this.

  • Create an instance of the Presentation class.
  • Save it as PPTX using the present.save (String, SaveFormat) method.

The following code example shows how to create a PowerPoint presentation in C#.

// Instantiate a Presentation object that represents a presentation file
using (Presentation presentation = new Presentation())
{
    // Get the first slide
    ISlide slide = presentation.Slides[0];

    // Add content to slide...
    
    // Save presentation
    presentation.Save("NewPresentation.pptx", SaveFormat.Pptx);
}
Copy the code

Open an existing PowerPoint presentation in C#

Open an existing PowerPoint presentation with no extra effort. All you need to do is supply the path to the PPTX file to the Presentation class constructor. The following code example shows how to open an existing PPTX presentation.

// Opening the presentation file by passing the file path to the constructor of Presentation class
Presentation pres = new Presentation("OpenPresentation.pptx");

// Printing the total number of slides in the presentation
System.Console.WriteLine(pres.Slides.Count.ToString());
Copy the code

Add slides to your presentation in C#

Once you have created your presentation, you can start adding slides to it. Here are the steps to add Slides to a presentation using Aspose.Slides for.NET.

  • Create an instance of the Presentation class.
  • The ISlideCollection class is instantiated by setting a reference to the Presentations.Slides property.
  • Add empty slides to the presentation using the slide.addemptyslide (ILayoutSlide) method exposed by the ISlideCollection object
  • Save the Presentation file using the present.save (String, SaveFormat) method.

The following code example shows how to add slides to a PowerPoint presentation using C#.

// Instantiate Presentation class that represents the presentation file using (Presentation pres = new Presentation()) {  // Instantiate SlideCollection calss ISlideCollection slds = pres.Slides; for (int i = 0; i < pres.LayoutSlides.Count; i++) { // Add an empty slide to the Slides collection slds.AddEmptySlide(pres.LayoutSlides[i]); } // Save the PPTX file to the Disk pres.Save("EmptySlide_out.pptx", SaveFormat.Pptx); }Copy the code

Insert text in the slide using C#

Now we can add content to the slides in our PowerPoint presentation. Let’s start by adding a piece of text to the slide using the following steps.

  • Create a new Presentation using the Presentation class.
  • Get a reference to the slides in the presentation.
  • Add IAutoShape with ShapeType as rectangle in a specified position of slide.
  • Gets a reference to the newly added IAutoShape object.
  • Adds TextFrame to the optional graph that contains the default text.
  • Save the presentation as a PPTX file.

The following code example shows how to add text to a slide in a presentation using C#.

// Instantiate PresentationEx// Instantiate PresentationEx
using (Presentation pres = new Presentation())
{
    // Get the first slide
    ISlide sld = pres.Slides[0];

    // Add an AutoShape of Rectangle type
    IAutoShape ashp = sld.Shapes.AddAutoShape(ShapeType.Rectangle, 150, 75, 150, 50);

    // Add TextFrame to the Rectangle
    ashp.AddTextFrame(" ");

    // Accessing the text frame
    ITextFrame txtFrame = ashp.TextFrame;

    // Create the Paragraph object for text frame
    IParagraph para = txtFrame.Paragraphs[0];

    // Create Portion object for paragraph
    IPortion portion = para.Portions[0];

    // Set Text
    portion.Text = "Aspose TextBox";

    // Save the presentation to disk
    pres.Save("presentation.pptx", Aspose.Slides.Export.SaveFormat.Pptx);
}
Copy the code

Create tables in your presentation using C#

Asposed.Slides for.net provides an easy way to create tables in a presentation document. Here are its steps.

  • Create an instance of the Presentation class.
  • Use the slide’s index to get a reference to the slide.
  • Defines an array of columns with widths and rows with heights.
  • Add the table to the Slide using the slide.Shapes.addtable () method exposed by the IShapes object and get a reference to the table in the ITable instance.
  • Iterate through each cell to apply the format.
  • Add Text to the cell using the table.rows [][].textFrame.text property.
  • Save the presentation as a PPTX file.

The following code example shows how to create a table in a slide of a PowerPoint presentation.

// Instantiate Presentation class that represents PPTX file
Presentation pres = new Presentation();

// Access first slide
ISlide sld = pres.Slides[0];

// Define columns with widths and rows with heights
double[] dblCols = { 50, 50, 50 };
double[] dblRows = { 50, 30, 30, 30, 30 };

// Add table shape to slide
ITable tbl = sld.Shapes.AddTable(100, 50, dblCols, dblRows);

// Set border format for each cell
for (int row = 0; row < tbl.Rows.Count; row++) { for (int cell = 0; cell < tbl.Rows[row].Count; cell++) { tbl.Rows[row][cell].CellFormat.BorderTop.FillFormat.FillType = FillType.Solid; tbl.Rows[row][cell].CellFormat.BorderTop.FillFormat.SolidFillColor.Color = Color.Red; tbl.Rows[row][cell].CellFormat.BorderTop.Width = 5; tbl.Rows[row][cell].CellFormat.BorderBottom.FillFormat.FillType = (FillType.Solid); tbl.Rows[row][cell].CellFormat.BorderBottom.FillFormat.SolidFillColor.Color= Color.Red; tbl.Rows[row][cell].CellFormat.BorderBottom.Width =5; tbl.Rows[row][cell].CellFormat.BorderLeft.FillFormat.FillType = FillType.Solid; tbl.Rows[row][cell].CellFormat.BorderLeft.FillFormat.SolidFillColor.Color =Color.Red; tbl.Rows[row][cell].CellFormat.BorderLeft.Width = 5; tbl.Rows[row][cell].CellFormat.BorderRight.FillFormat.FillType = FillType.Solid; tbl.Rows[row][cell].CellFormat.BorderRight.FillFormat.SolidFillColor.Color = Color.Red; tbl.Rows[row][cell].CellFormat.BorderRight.Width = 5; } } // Merge cells 1 & 2 of row 1 tbl.MergeCells(tbl.Rows[0][0], tbl.Rows[1][1], false); // Add text to the merged cell tbl.Rows[0][0].TextFrame.Text = "Merged Cells"; // Save PPTX to Disk pres.Save("table.pptx", SaveFormat.Pptx);
Copy the code

Create charts in your presentation using C#

Here are the steps to add a chart to a PowerPoint presentation using C#.

  • Create an instance of the Presentation class.
  • Get a reference to the slides by index.
  • Add a chart of the desired type using the islide.Shapes.AddChart(ChartType, Single, Single, Single) method.
  • Add chart titles.
  • Access the chart data worksheet.
  • Clear all default series and categories.
  • Add new series and categories.
  • Add new chart data to the chart series.
  • Sets the fill color for the chart series.
  • Add chart series labels.
  • Save the presentation as a PPTX file.

The following code example shows how to add charts to a presentation using C#.

// Instantiate Presentation class that represents PPTX file
Presentation pres = new Presentation();

// Access first slide
ISlide sld = pres.Slides[0];

// Add chart with default data
IChart chart = sld.Shapes.AddChart(ChartType.ClusteredColumn, 0, 0, 500, 500);

// Setting chart Title
// Chart.ChartTitle.TextFrameForOverriding.Text = "Sample Title";
chart.ChartTitle.AddTextFrameForOverriding("Sample Title");
chart.ChartTitle.TextFrameForOverriding.TextFrameFormat.CenterText = NullableBool.True;
chart.ChartTitle.Height = 20;
chart.HasTitle = true;

// Set first series to Show Values
chart.ChartData.Series[0].Labels.DefaultDataLabelFormat.ShowValue = true;

// Setting the index of chart data sheet
int defaultWorksheetIndex = 0;

// Getting the chart data worksheet
IChartDataWorkbook fact = chart.ChartData.ChartDataWorkbook;

// Delete default generated series and categories
chart.ChartData.Series.Clear();
chart.ChartData.Categories.Clear();
int s = chart.ChartData.Series.Count;
s = chart.ChartData.Categories.Count;

// Adding new series
chart.ChartData.Series.Add(fact.GetCell(defaultWorksheetIndex, 0, 1, "Series 1"), chart.Type);
chart.ChartData.Series.Add(fact.GetCell(defaultWorksheetIndex, 0, 2, "Series 2"), chart.Type);

// Adding new categories
chart.ChartData.Categories.Add(fact.GetCell(defaultWorksheetIndex, 1, 0, "Caetegoty 1"));
chart.ChartData.Categories.Add(fact.GetCell(defaultWorksheetIndex, 2, 0, "Caetegoty 2"));
chart.ChartData.Categories.Add(fact.GetCell(defaultWorksheetIndex, 3, 0, "Caetegoty 3"));

// Take first chart series
IChartSeries series = chart.ChartData.Series[0];

// Now populating series data

series.DataPoints.AddDataPointForBarSeries(fact.GetCell(defaultWorksheetIndex, 1, 1, 20));
series.DataPoints.AddDataPointForBarSeries(fact.GetCell(defaultWorksheetIndex, 2, 1, 50));
series.DataPoints.AddDataPointForBarSeries(fact.GetCell(defaultWorksheetIndex, 3, 1, 30));

// Setting fill color for series
series.Format.Fill.FillType = FillType.Solid;
series.Format.Fill.SolidFillColor.Color = Color.Red;


// Take second chart series
series = chart.ChartData.Series[1];

// Now populating series data
series.DataPoints.AddDataPointForBarSeries(fact.GetCell(defaultWorksheetIndex, 1, 2, 30));
series.DataPoints.AddDataPointForBarSeries(fact.GetCell(defaultWorksheetIndex, 2, 2, 10));
series.DataPoints.AddDataPointForBarSeries(fact.GetCell(defaultWorksheetIndex, 3, 2, 60));

// Setting fill color for series
series.Format.Fill.FillType = FillType.Solid;
series.Format.Fill.SolidFillColor.Color = Color.Green;

// First label will be show Category name
IDataLabel lbl = series.DataPoints[0].Label;
lbl.DataLabelFormat.ShowCategoryName = true;

lbl = series.DataPoints[1].Label;
lbl.DataLabelFormat.ShowSeriesName = true;

// Show value for third label
lbl = series.DataPoints[2].Label;
lbl.DataLabelFormat.ShowValue = true;
lbl.DataLabelFormat.ShowSeriesName = true;
lbl.DataLabelFormat.Separator = "/";
            
// Save presentation with chart
pres.Save("AsposeChart_out.pptx", SaveFormat.Pptx);
Copy the code

If you have any questions or requirements, please feel free to join the Aspose Technology Exchange Group (761297826), we are happy to provide you with inquiries and consultation.