Manage Presentation Tables in .NET
Introduction
Tables in PowerPoint organize information into rows and columns, making it easier to read and compare values.
Aspose.Slides provides the Table class, ITable interface, Cell class, ICell interface, and other types to allow you to create, update, and manage tables in presentations.
Create a Table from Scratch
Create a table by specifying its position, column widths, and row heights. After adding it to a slide, you can format cell borders, merge cells, and insert text.
- Create an instance of the Presentation class.
- Get a reference to the slide by its index.
- Define an array of column widths in points.
- Define an array of row heights in points.
- Add an ITable object to the slide through the AddTable method.
- Iterate through each ICell to apply formatting to the top, bottom, right, and left borders.
- Merge the first two cells of the table’s first row.
- Access the merged cell through its TextFrame property.
- Set the text in the merged cell.
- Save the modified presentation.
The example below creates a table with three columns and five rows at (100, 50) points. It applies red borders with a width of 5 points, merges the first two cells in the first row, and saves the result as table.pptx.
using System.Drawing;
using Aspose.Slides;
using Aspose.Slides.Export;
using var presentation = new Presentation();
var slide = presentation.Slides[0];
var columnWidths = new double[] { 50, 50, 50 };
var rowHeights = new double[] { 50, 30, 30, 30, 30 };
var table = slide.Shapes.AddTable(100, 50, columnWidths, rowHeights);
foreach (var row in table.Rows)
{
foreach (var cell in row)
{
var cellFormat = cell.CellFormat;
cellFormat.BorderTop.FillFormat.FillType = FillType.Solid;
cellFormat.BorderTop.FillFormat.SolidFillColor.Color = Color.Red;
cellFormat.BorderTop.Width = 5;
cellFormat.BorderBottom.FillFormat.FillType = FillType.Solid;
cellFormat.BorderBottom.FillFormat.SolidFillColor.Color = Color.Red;
cellFormat.BorderBottom.Width = 5;
cellFormat.BorderLeft.FillFormat.FillType = FillType.Solid;
cellFormat.BorderLeft.FillFormat.SolidFillColor.Color = Color.Red;
cellFormat.BorderLeft.Width = 5;
cellFormat.BorderRight.FillFormat.FillType = FillType.Solid;
cellFormat.BorderRight.FillFormat.SolidFillColor.Color = Color.Red;
cellFormat.BorderRight.Width = 5;
}
}
table.MergeCells(table[0, 0], table[1, 0], false);
table[0, 0].TextFrame.Text = "Merged Cells";
presentation.Save("table.pptx", SaveFormat.Pptx);
Numbering in a Standard Table
In a standard table, cell indices are zero-based and use the order (column, row). The first cell is indexed as (0, 0).
For example, the cells in a table with 4 columns and 4 rows are numbered this way:
| (0, 0) | (1, 0) | (2, 0) | (3, 0) |
|---|---|---|---|
| (0, 1) | (1, 1) | (2, 1) | (3, 1) |
| (0, 2) | (1, 2) | (2, 2) | (3, 2) |
| (0, 3) | (1, 3) | (2, 3) | (3, 3) |
This example creates the 4 × 4 table illustrated above, with column widths and row heights of 70 points and red cell borders with a width of 5 points. The coordinates illustrate cell indices; the example leaves the cells empty and saves the table as StandardTables_out.pptx.
using System.Drawing;
using Aspose.Slides;
using Aspose.Slides.Export;
using var presentation = new Presentation();
var slide = presentation.Slides[0];
var columnWidths = new double[] { 70, 70, 70, 70 };
var rowHeights = new double[] { 70, 70, 70, 70 };
var table = slide.Shapes.AddTable(100, 50, columnWidths, rowHeights);
foreach (var row in table.Rows)
{
foreach (var cell in row)
{
var cellFormat = cell.CellFormat;
cellFormat.BorderTop.FillFormat.FillType = FillType.Solid;
cellFormat.BorderTop.FillFormat.SolidFillColor.Color = Color.Red;
cellFormat.BorderTop.Width = 5;
cellFormat.BorderBottom.FillFormat.FillType = FillType.Solid;
cellFormat.BorderBottom.FillFormat.SolidFillColor.Color = Color.Red;
cellFormat.BorderBottom.Width = 5;
cellFormat.BorderLeft.FillFormat.FillType = FillType.Solid;
cellFormat.BorderLeft.FillFormat.SolidFillColor.Color = Color.Red;
cellFormat.BorderLeft.Width = 5;
cellFormat.BorderRight.FillFormat.FillType = FillType.Solid;
cellFormat.BorderRight.FillFormat.SolidFillColor.Color = Color.Red;
cellFormat.BorderRight.Width = 5;
}
}
presentation.Save("StandardTables_out.pptx", SaveFormat.Pptx);
Access an Existing Table
Tables are stored in a slide’s shape collection. Iterate through the shapes to locate a table, then use the ITable interface to read or update its cells.
- Load the presentation using the Presentation class.
- Get a reference to the slide containing the table by its index.
- Iterate through the IShape objects and stop when a table is found. If the slide contains several tables, use AlternativeText to identify the one you need.
- Update the text in the target cell.
- Save the modified presentation.
The example below opens UpdateExistingTable.pptx and finds the first table on the first slide. It sets the cell at column 0, row 1 to New and saves the result as table1_out.pptx. The input must contain at least one slide, and the first table on that slide must have at least one column and two rows.
using Aspose.Slides;
using Aspose.Slides.Export;
using var presentation = new Presentation("UpdateExistingTable.pptx");
var slide = presentation.Slides[0];
ITable? table = null;
foreach (var shape in slide.Shapes)
{
if (shape is ITable candidateTable)
{
table = candidateTable;
break;
}
}
table![0, 1].TextFrame.Text = "New";
presentation.Save("table1_out.pptx", SaveFormat.Pptx);
To resize a row in an existing table and understand why its actual height can exceed the requested minimum, see Control Row Height.
Find the Cell That Owns a Text Frame
When generic text-processing code receives an ITextFrame from a table, use the ITextFrame.ParentCell property to retrieve the owning ICell. For a table-cell text frame, ITextFrame.ParentCell is set and ITextFrame.ParentShape is null, even though the table itself is a shape.
The cell coordinates are available through the read-only ICell.FirstColumnIndex and ICell.FirstRowIndex properties. ITextFrame.ParentCell is also read-only: it provides navigation to the owner but does not change ownership. Always check the returned cell for null before using it.
For a complete example that identifies table-cell and shape owners, including shapes associated with SmartArt nodes, see Search and Replace Text.
Align Text in a Table
You can control the vertical anchoring and text direction of individual table cells. The example in this section centers text within the first cell and rotates it by 270 degrees.
- Create an instance of the Presentation class.
- Get a reference to the slide by its index.
- Add an ITable object to the slide.
- Access an ITextFrame object from the table.
- Access the first IParagraph and set its text and color.
- Set the cell’s TextAnchorType and TextVerticalType.
- Save the modified presentation.
This example creates a 4 × 4 table with column widths of 120 points and row heights of 100 points. It formats the text in cell (0, 0), adds values to the remaining cells in the first row, and saves the result as Vertical_Align_Text_out.pptx.
using System.Drawing;
using Aspose.Slides;
using Aspose.Slides.Export;
using var presentation = new Presentation();
var slide = presentation.Slides[0];
var columnWidths = new double[] { 120, 120, 120, 120 };
var rowHeights = new double[] { 100, 100, 100, 100 };
var table = slide.Shapes.AddTable(100, 50, columnWidths, rowHeights);
table[1, 0].TextFrame.Text = "10";
table[2, 0].TextFrame.Text = "20";
table[3, 0].TextFrame.Text = "30";
var cell = table[0, 0];
var paragraph = cell.TextFrame.Paragraphs[0];
var portion = paragraph.Portions[0];
portion.Text = "Text here";
portion.PortionFormat.FillFormat.FillType = FillType.Solid;
portion.PortionFormat.FillFormat.SolidFillColor.Color = Color.Black;
cell.TextAnchorType = TextAnchorType.Center;
cell.TextVerticalType = TextVerticalType.Vertical270;
presentation.Save("Vertical_Align_Text_out.pptx", SaveFormat.Pptx);
Set Text Formatting on the Table Level
Use SetTextFormat to apply text formatting to all cells in a table. Its overloads accept portion, paragraph, and text frame formatting, so you can set these properties without iterating through individual cells.
- Load the presentation using the Presentation class.
- Get a reference to the slide by its index.
- Access an ITable object from the slide.
- Set the FontHeight for the text.
- Set the Alignment and MarginRight.
- Set the TextVerticalType.
- Save the modified presentation.
The example below opens table.pptx, which must contain at least one slide with a table as its first shape. It sets the font size to 25 points, right-aligns paragraphs with a right margin of 20 points, and makes the text vertical. The formatted presentation is saved as result.pptx.
using Aspose.Slides;
using Aspose.Slides.Export;
using var presentation = new Presentation("table.pptx");
var slide = presentation.Slides[0];
var table = (ITable)slide.Shapes[0];
var portionFormat = new PortionFormat();
portionFormat.FontHeight = 25;
table.SetTextFormat(portionFormat);
var paragraphFormat = new ParagraphFormat();
paragraphFormat.Alignment = TextAlignment.Right;
paragraphFormat.MarginRight = 20;
table.SetTextFormat(paragraphFormat);
var textFrameFormat = new TextFrameFormat();
textFrameFormat.TextVerticalType = TextVerticalType.Vertical;
table.SetTextFormat(textFrameFormat);
presentation.Save("result.pptx", SaveFormat.Pptx);
Get Table Style Properties
Use StylePreset to read or assign a table’s preset style. This example applies TableStylePreset.DarkStyle1 to one table, prints the preset name, and assigns the same preset to a second table. Both tables are saved in table-style.pptx.
using System;
using Aspose.Slides;
using Aspose.Slides.Export;
using var presentation = new Presentation();
var slide = presentation.Slides[0];
var columnWidths = new double[] { 100, 150 };
var rowHeights = new double[] { 5, 5, 5 };
var table = slide.Shapes.AddTable(10, 10, columnWidths, rowHeights);
table.StylePreset = TableStylePreset.DarkStyle1;
var stylePreset = table.StylePreset;
Console.WriteLine($"Table style preset: {stylePreset}");
var anotherTable = slide.Shapes.AddTable(10, 100, columnWidths, rowHeights);
anotherTable.StylePreset = stylePreset;
presentation.Save("table-style.pptx", SaveFormat.Pptx);
Lock Aspect Ratio of a Table
A table’s aspect ratio is the ratio of its width to its height. Use AspectRatioLocked to lock this ratio for a table.
The example below opens pres.pptx, which must contain at least one slide with a table as its first shape. It prints the current lock state, enables the aspect ratio lock, prints the updated state (True), and saves the result as pres-out.pptx.
using System;
using Aspose.Slides;
using Aspose.Slides.Export;
using var presentation = new Presentation("pres.pptx");
var slide = presentation.Slides[0];
var table = (ITable)slide.Shapes[0];
Console.WriteLine($"Lock aspect ratio set: {table.ShapeLock.AspectRatioLocked}");
table.ShapeLock.AspectRatioLocked = true;
Console.WriteLine($"Lock aspect ratio set: {table.ShapeLock.AspectRatioLocked}");
presentation.Save("pres-out.pptx", SaveFormat.Pptx);
FAQ
Can I enable right-to-left (RTL) reading direction for an entire table and the text in its cells?
Yes. The table exposes a RightToLeft property, and paragraphs have ParagraphFormat.RightToLeft. Using both ensures the correct RTL order and rendering inside cells.
How can I prevent users from moving or resizing a table in the final file?
Use shape locks to disable moving, resizing, selection, etc. These locks apply to tables as well.
Is inserting an image inside a cell as a background supported?
Yes. You can set a picture fill for a cell; the image will cover the cell area according to the chosen mode (stretch or tile).