This article guides on how to create a table in PowerPoint using C#. It provides all the details to establish the environment, a step-by-step process to create and fill a table, and a runnable sample code that demonstrates how to insert a table in a slide using C#. You will also learn to format the text in the table and save the resultant presentation on the disk in PPT, PPTX, or any other format supported by MS PowerPoint.
Steps to Create a Table in PowerPoint using C#
- Establish the environment to add Aspose.Slides for .NET to add a table
- Create a new presentation using the Presentation class and access its first slide
- Add a table in the slide having the defined heights for rows and columns using the AddTable() method
- Iterate through each row and cell of the newly added table
- Set some text in each cell and sets its font
- Save the presentation in the PPT format
These steps explain how to make table in PowerPoint using C#. First, you may create a presentation and get access to the first slide in it that contains a collection of shapes by default. In the next steps create a table by providing the X and Y coordinates for the top left position of the text in a cell along with the integers array describing the rows’ height and columns’ width. In the final steps, create an ITextFrame class object and set the text paragraph formatting as per your requirements before saving the output file.
Code to Add Table in PowerPoint using C#
using Aspose.Slides; | |
using Aspose.Slides.Export; | |
namespace AsposeProjects | |
{ | |
class Program | |
{ | |
static void Main(string[] args) // Main function to add table in a slide using C# | |
{ | |
// Initialize license | |
License lic = new License(); | |
lic.SetLicense("Aspose.Total.lic"); | |
// Instantiate a new presentation | |
Presentation presentation = new Presentation(); | |
// Access the first slide from the default collection | |
ISlide sld = presentation.Slides[0]; | |
// Specify the rows heights and columns widths | |
double[] columnsWidths = { 45, 45, 45 }; | |
double[] rowsHeights = { 45, 26, 26, 26, 26 }; | |
// Insert a new table | |
Aspose.Slides.ITable table = sld.Shapes.AddTable(55, 55, columnsWidths, rowsHeights); | |
// Fill the table and set the font | |
foreach (IRow row in table.Rows) | |
{ | |
foreach (ICell cell in row) | |
{ | |
// Access the cell's text frame | |
ITextFrame textFormat = cell.TextFrame; | |
// Set text in the cell | |
textFormat.Text = "Data " + cell.FirstRowIndex.ToString() + cell.FirstColumnIndex.ToString(); | |
// Set text font | |
textFormat.Paragraphs[0].Portions[0].PortionFormat.FontHeight = 10; | |
textFormat.Paragraphs[0].ParagraphFormat.Bullet.Type = BulletType.None; | |
} | |
} | |
// Save the presentation on the disk | |
presentation.Save("PresentationTable.ppt", SaveFormat.Ppt); | |
System.Console.WriteLine("Done"); | |
} | |
} | |
} |
This tutorial has described how will you insert a table in a presentation using C#. If you want to learn the process to secure a PowerPoint presentation refer to the article on how to secure PowerPoint presentation in C#.