Merging and splitting table cells in PowerPoint are two common functions, mainly used to adjust the layout and structure of the table. Merging cells involves combining adjacent cells into a larger one. It allows users to create title cells that span multiple columns or rows. On the other hand, splitting cells means dividing a cell into several smaller ones, which is useful for creating detailed layouts or accommodating diverse content. In this article, we will show you how to merge and split table cells in PowerPoint programmatically by using Spire.Presentation for .NET.
Install Spire.Presentation for .NET
To begin with, you need to add the DLL files included in the Spire.Presentation for.NET package as references in your .NET project. The DLLs files can be either downloaded from this link or installed via NuGet.
PM> Install-Package Spire.Presentation
Merge Table Cells in PowerPoint
Spire.Presentation for .NET provides users with ITable[int columnIndex, int rowIndex] property and ITable.MergeCells(Cell startCell, Cell endCell, boolean allowSplitting) method to get and merge the specific cells. The detailed steps are as follows.
- Create an object of Presentation class.
- Load a sample file using Presentation.LoadFromFile() method.
- Get the table from the first slide by looping through all shapes.
- Get the specific cells by ITable[int columnIndex, int rowIndex] property and merge them by using ITable.MergeCells(Cell startCell, Cell endCell, boolean allowSplitting) method.
- Save the result file using Presentation.SaveToFile() method.
- C#
- VB.NET
using Spire.Presentation; namespace MergeCells { class Program { static void Main(string[] args) { //Create an object of Presentation class Presentation presentation = new Presentation(); //Load a PowerPoint presentation presentation.LoadFromFile("sample.pptx"); //Get the table from the first slide by looping through all shapes ITable table = null; foreach (IShape shape in presentation.Slides[0].Shapes) { if (shape is ITable) { table = (ITable)shape; //Merge the cells from [0,0] to [4,0] table.MergeCells(table[0, 0], table[4, 0], false); } } //Save the result document presentation.SaveToFile("MergeCells.pptx", FileFormat.Pptx2010); presentation.Dispose(); } } }
Split Table Cells in PowerPoint
Spire.Presentation for .NET also supports users to get the specific cell and split it into smaller ones by using ITable[int columnIndex, int rowIndex] property and Cell.Split(int RowCount, int ColunmCount) method. The detailed steps are as follows.
- Create an object of Presentation class.
- Load a sample file using Presentation.LoadFromFile() method.
- Get the table from the first slide by looping through all shapes.
- Get the specific cell by ITable[int columnIndex, int rowIndex] property and split it into 2 rows and 2 columns by using Cell.Split(int RowCount, int ColumnCount) method.
- Save the result file using Presentation.SaveToFile() method.
- C#
- VB.NET
using Spire.Presentation; namespace SplitCells { class Program { static void Main(string[] args) { //Create an object of Presentation class Presentation presentation = new Presentation(); //Load a PowerPoint presentation presentation.LoadFromFile("sample.pptx"); //Get the table from the first slide by looping through all shapes ITable table = null; foreach (IShape shape in presentation.Slides[0].Shapes) { if (shape is ITable) { table = (ITable)shape; //Split cell [2, 2] into 2 rows and 2 columns table[2, 2].Split(2, 2); } } //Save the result document presentation.SaveToFile("SplitCells.pptx", FileFormat.Pptx2013); presentation.Dispose(); } } }
Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.