Merging cells involves combining adjacent cells into a larger one. This allows users to create the title cells that span multiple columns or rows, providing greater design flexibility. 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 PowerPoint, users can merge and split table cells to adjust the structure and layout of tables. In this article, we will show you how to merge and split table cells in PowerPoint programmatically by using Spire.Presentation for Java.
Install Spire.Presentation for Java
First of all, you're required to add the Spire.Presentation.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.
<repositories> <repository> <id>com.e-iceblue</id> <name>e-iceblue</name> <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url> </repository> </repositories> <dependencies> <dependency> <groupId>e-iceblue</groupId> <artifactId>spire.presentation</artifactId> <version>9.9.2</version> </dependency> </dependencies>
Merge Table Cells in PowerPoint
Spire.Presentation for Java provides users with ITable.get(int columnIndex, int rowIndex) and ITable.mergeCells(Cell startCell, Cell endCell, boolean allowSplitting) methods 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.
- Declare ITable variable.
- Get the table from the first slide by looping through all shapes.
- Get the specific cells by using ITable.get(int columnIndex, int rowIndex) method and merge them by using ITable.mergeCells(Cell startCell, Cell endCell, boolean allowSplitting) method.
- Save the result file using Presentation.saveToFile() method.
- Java
import com.spire.presentation.FileFormat; import com.spire.presentation.ITable; import com.spire.presentation.Presentation; public class MergeCells { public static void main(String[] args) throws Exception { //Create an object of Presentation class Presentation presentation = new Presentation(); //Load a sample PowerPoint file presentation.loadFromFile("sample.pptx"); //Declare ITable variable ITable table = null; //Get the table from the first slide by looping through all shapes for (Object shape : presentation.getSlides().get(0).getShapes()) { if (shape instanceof ITable) { table = (ITable) shape; //Merge the cells from [0,0] to [3,0] table.mergeCells(table.get(0, 0), table.get(3, 0), false); //Merge the cells from [0,1] to [0,5] table.mergeCells(table.get(0, 1), table.get(0, 5), false); } } //Save the result file presentation.saveToFile("MergeCells.pptx", FileFormat.PPTX_2010); presentation.dispose(); } }
Split Table Cells in PowerPoint
Spire.Presentation for Java also supports users to get the specific cell and split it into smaller ones by calling ITable.get(int columnIndex, int rowIndex) and Cell.split(int RowCount, int ColunmCount) methods. The detailed steps are as follows.
- Create an object of Presentation class.
- Load a sample file using Presentation.loadFromFile() method.
- Declare ITable variable.
- Get the table from the first slide by looping through all shapes.
- Get the specific cell by using ITable.get(int columnIndex, int rowIndex) method 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.
- Java
import com.spire.presentation.FileFormat; import com.spire.presentation.ITable; import com.spire.presentation.Presentation; public class SplitCells { public static void main(String[] args) throws Exception { //Create an object of Presentation class Presentation presentation = new Presentation(); //Load a sample PowerPoint file presentation.loadFromFile("sample.pptx"); //Declare ITable variable ITable table = null; //Get the table from the first slide by looping through all shapes for (Object shape : presentation.getSlides().get(0).getShapes()) { if (shape instanceof ITable) { table = (ITable) shape; //Split the cell[2,2] to 2 rows and 2 columns table.get(2,2).split(2,2); } } //Save the result file presentation.saveToFile("SplitCells.pptx", FileFormat.PPTX_2010); 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.