Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Last active September 24, 2021 10:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aspose-com-gists/d128eb81b15aa2d87d6c08f68afc0078 to your computer and use it in GitHub Desktop.
Save aspose-com-gists/d128eb81b15aa2d87d6c08f68afc0078 to your computer and use it in GitHub Desktop.
Create and manipulate Tables in PowerPoint using C#
// Load presentation
using (Presentation pres = new Presentation("UpdateExistingTable.pptx"))
{
// Access the first slide
ISlide sld = pres.Slides[0];
// Initialize null TableEx
ITable tbl = null;
// Iterate through the shapes and set a reference to the table found
foreach (IShape shp in sld.Shapes)
if (shp is ITable)
tbl = (ITable)shp;
// Set the text of the first column of second row
tbl[0, 1].TextFrame.Text = "New";
//Write the PPTX to Disk
pres.Save("table1_out.pptx", Aspose.Slides.Export.SaveFormat.Pptx);
}
// Create or load presentation
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 and text for each cell
for (int row = 0; row < tbl.Rows.Count; row++)
{
for (int cell = 0; cell < tbl.Rows[row].Count; cell++)
{
// Add text to the cell
tbl.Rows[row][cell].TextFrame.Text = "Cells_" + 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;
}
}
// Save PPTX to disk
pres.Save("table.pptx", SaveFormat.Pptx);
// Create or load presentation
Presentation presentation = new Presentation();
// Get reference of the slide
ISlide slide = presentation.Slides[0];
// Get reference of the table
ITable someTable = presentation.Slides[0].Shapes[0] as ITable; // let's say that the first shape on the first slide is a table
// Set table cells' font height
PortionFormat portionFormat = new PortionFormat();
portionFormat.FontHeight = 25;
someTable.SetTextFormat(portionFormat);
// Set table cells' text alignment and right margin in one call
ParagraphFormat paragraphFormat = new ParagraphFormat();
paragraphFormat.Alignment = TextAlignment.Right;
paragraphFormat.MarginRight = 20;
someTable.SetTextFormat(paragraphFormat);
// Set table cells' text vertical type
TextFrameFormat textFrameFormat = new TextFrameFormat();
textFrameFormat.TextVerticalType = TextVerticalType.Vertical;
someTable.SetTextFormat(textFrameFormat);
// Save presentation
presentation.Save("result.pptx", SaveFormat.Pptx);
// Load presentation
using (Presentation pres = new Presentation("presentation.pptx"))
{
// Get reference of the table
ITable table = (ITable)pres.Slides[0].Shapes[0];
Console.WriteLine($"Lock aspect ratio set: {table.ShapeLock.AspectRatioLocked}");
// Lock aspect ratio
table.ShapeLock.AspectRatioLocked = !table.ShapeLock.AspectRatioLocked; // invert
Console.WriteLine($"Lock aspect ratio set: {table.ShapeLock.AspectRatioLocked}");
// Save presentation
pres.Save("pres-out.pptx", SaveFormat.Pptx);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment