News Category

Table

Table (9)

This article demonstrates how to evenly distribute the rows and columns of a PowerPoint table using Spire.Presentation for Java.

The input document:

Evenly Distribute Rows and Columns in PowerPoint Table in Java

import com.spire.presentation.FileFormat;
import com.spire.presentation.ISlide;
import com.spire.presentation.ITable;
import com.spire.presentation.Presentation;

public class DistributeRowsAndColumns {
    public static void main(String []args) throws Exception {
        //Create a Presentation instance
        Presentation ppt = new Presentation();
        //Load the PowerPoint document
        ppt.loadFromFile("Input.pptx");

        //Get the first slide
        ISlide slide = ppt.getSlides().get(0);
        //Get the table in the slide
        ITable table = (ITable) slide.getShapes().get(0);
        //Distribute table rows
        table.distributeRows(0,4);
        //Distribute table columns
        table.distributeColumns(0,4);

        //Save the result document
        ppt.saveToFile("DistributeRowsAndColumns.pptx", FileFormat.PPTX_2013);
    }

The output document:

Evenly Distribute Rows and Columns in PowerPoint Table in Java

This article demonstrates how to horizontally and vertically align a table in a slide using Spire.Presentation for Java.

Below is a screenshot of the sample document.

Set Table Alignment in Slides in Java

import com.spire.presentation.FileFormat;
import com.spire.presentation.ITable;
import com.spire.presentation.Presentation;
import com.spire.presentation.ShapeAlignmentEnum;

public class AlignTable {

    public static void main(String[] args) throws Exception {

        //Create a Presentation object
        Presentation presentation = new Presentation();

        //Load the sample PowerPoint document contain
        presentation.loadFromFile("C:\\Users\\Administrator\\Desktop\\sample.pptx");

        //Declare a ITable variable
        ITable table = null;

        //Loop through the shapes in the first slide
        for (Object shape: presentation.getSlides().get(0).getShapes()
             ) {

            //Check if shape is an instance of ITable
            if (shape instanceof ITable)
            {
                //Convert shape to table
                table =(ITable)shape;
            }
        }

        //Horizontally align table to center
        table.setShapeAlignment(ShapeAlignmentEnum.ShapeAlignment.AlignCenter);

        //Vertically align table to middle
        table.setShapeAlignment(ShapeAlignmentEnum.ShapeAlignment.AlignMiddle);

        //Save to file
        presentation.saveToFile("AlignTable.pptx", FileFormat.PPTX_2013);
    }
}

Set Table Alignment in Slides in Java

With Spire.Presentation for Java, we can format a table by applying preset table styles. This article will demonstrate how to apply predefined styles to a table on presentation slides.

Firstly, view the table on the PowerPoint document:

Java set table style for the PowerPoint table

import com.spire.presentation.ITable;
import com.spire.presentation.Presentation;
import com.spire.presentation.FileFormat;
import com.spire.presentation.TableStylePreset;

public class SetTableStyle {
    public static void main(String[] args) throws Exception {

        //Create a PPT document and load file
        Presentation presentation = new Presentation();
        presentation.loadFromFile("Sample.pptx");

        ITable table = null;

        for (Object shape : presentation.getSlides().get(0).getShapes()) {
            if (shape instanceof ITable) {
                table = (ITable) shape;
                //Set the table style from TableStylePreset and apply it to selected table
                table.setStylePreset(TableStylePreset.MEDIUM_STYLE_1_ACCENT_2);
            }
        }
        //Save the file
       presentation.saveToFile("SetTableStyle.pptx", FileFormat.PPTX_2010);

    }
}

Effective screenshot after updating the table style:

Java set table style for the PowerPoint table

This article demonstrates how to add or delete rows and columns in a PowerPoint table using Spire.Presentation for Java.

Here is a screenshot of the sample PowerPoint file:

Add or Delete Rows and Columns from PowerPoint Table in Java

Add a row and a column

import com.spire.presentation.*;

public class AddRowAndColumn {

    public static void main(String[] args) throws Exception {

        //load the sample PowerPoint file
        Presentation presentation = new Presentation();
        presentation.loadFromFile("C:\\Users\\Administrator\\Desktop\\input.pptx");

        //get the table in the document
        ITable table = null;
        for (Object shape : presentation.getSlides().get(0).getShapes()) {
            if (shape instanceof ITable) {
                table = (ITable) shape;

                //add the last row to the end of the table as a new row
                int rowCount = table.getTableRows().getCount();
                TableRow row = table.getTableRows().get(rowCount - 1);
                table.getTableRows().append(row);

                //get the new row and set the text for each cell
                rowCount = table.getTableRows().getCount();
                row = table.getTableRows().get(rowCount - 1);
                row.get(0).getTextFrame().setText("America");
                row.get(1).getTextFrame().setText("Washington");
                row.get(2).getTextFrame().setText("North America");
                row.get(3).getTextFrame().setText("9372610");

                //add the last column to the end of the table as a new column
                int colCount = table.getColumnsList().getCount();
                TableColumn column =table.getColumnsList().get(colCount-1);
                table.getColumnsList().add(column);

                //get the new column and set the text for each cell
                colCount = table.getColumnsList().getCount();
                column = table.getColumnsList().get(colCount-1);
                column.get(0).getTextFrame().setText("Population");
                column.get(1).getTextFrame().setText("32370000");
                column.get(2).getTextFrame().setText("7350000");
                column.get(3).getTextFrame().setText("15040000");
                column.get(4).getTextFrame().setText("26500000");
                column.get(5).getTextFrame().setText("329740000");
            }
        }
        
        //save the document
        presentation.saveToFile("output/AddRowAndColumn.pptx", FileFormat.PPTX_2013);
    }
}

Add or Delete Rows and Columns from PowerPoint Table in Java

Delete a row and a column

import com.spire.presentation.FileFormat;
import com.spire.presentation.ITable;
import com.spire.presentation.Presentation;

public class DeleteRowAndColumn {

    public static void main(String[] args) throws Exception {

        //load the sample PowerPoint file
        Presentation presentation = new Presentation();
        presentation.loadFromFile("C:\\Users\\Administrator\\Desktop\\input.pptx");

        //get the table in the document
        ITable table = null;
        for (Object shape : presentation.getSlides().get(0).getShapes()) {
            if (shape instanceof ITable) {
                table = (ITable) shape;
                
                //delete the second column
                table.getColumnsList().removeAt(1, false);
                
                //delete the second row
                table.getTableRows().removeAt(1, false);
            }
        }
        
        //save the document
        presentation.saveToFile("output/DeleteRowAndColumn.pptx", FileFormat.PPTX_2013);
    }
}

Add or Delete Rows and Columns from PowerPoint Table in Java

This article demonstrates how to align text within a table cell in PowerPoint using Spire.Presenatation for Java.

Code Snippets

import com.spire.presentation.*;

public class AlignTextInTableCell {

    public static void main(String[] args) throws Exception {

        //create a PowerPoint file
        Presentation presentation = new Presentation();

        //add a table
        Double[] widths = new Double[]{100d, 200d, 100d, 100d};
        Double[] heights = new Double[]{30d, 70d, 70d};
        ITable table = presentation.getSlides().get(0).getShapes().appendTable(30,80, widths, heights);
        table.setStylePreset(TableStylePreset.NONE);

        //set the horizontal alignment for the cells in the first row
        table.get(0, 0).getTextFrame().setText("Left");
        table.get(0, 0).getTextFrame().getParagraphs().get(0).setAlignment(TextAlignmentType.LEFT);
        table.get(1, 0).getTextFrame().setText("Horizontal Center");
        table.get(1, 0).getTextFrame().getParagraphs().get(0).setAlignment(TextAlignmentType.CENTER);
        table.get(2, 0).getTextFrame().setText("Right");
        table.get(2, 0).getTextFrame().getParagraphs().get(0).setAlignment(TextAlignmentType.RIGHT);
        table.get(3, 0).getTextFrame().setText("Justify");
        table.get(3, 0).getTextFrame().getParagraphs().get(0).setAlignment(TextAlignmentType.JUSTIFY);

        //Set the vertical alignment for the cells in the second row
        table.get(0, 1).getTextFrame().setText("Top");
        table.get(0, 1).setTextAnchorType(TextAnchorType.TOP);
        table.get(1, 1).getTextFrame().setText("Vertical Center");
        table.get(1, 1).setTextAnchorType(TextAnchorType.CENTER);
        table.get(2, 1).getTextFrame().setText("Bottom");
        table.get(2, 1).setTextAnchorType(TextAnchorType.BOTTOM);

        //set the both horizontal and vertical alignment for the cells in the third row
        table.get(0, 2).getTextFrame().setText("Top Left");
        table.get(0, 2).getTextFrame().getParagraphs().get(0).setAlignment(TextAlignmentType.LEFT);
        table.get(0, 2).setTextAnchorType(TextAnchorType.TOP);
        table.get(1, 2).getTextFrame().setText("Center");
        table.get(1, 2).getTextFrame().getParagraphs().get(0).setAlignment(TextAlignmentType.CENTER);
        table.get(1, 2).setTextAnchorType(TextAnchorType.CENTER);
        table.get(2, 2).getTextFrame().setText("Bottom Right");
        table.get(2, 2).getTextFrame().getParagraphs().get(0).setAlignment(TextAlignmentType.RIGHT);
        table.get(2, 2).setTextAnchorType(TextAnchorType.BOTTOM);

        //save to file
        presentation.saveToFile("output/Alignment.pptx", FileFormat.PPTX_2013);
        presentation.dispose();
    }
}

Output

Align Text in PowerPoint Table in Java

This article demonstrates how to set the row height and column width of an existing table in a PowerPoint document using Spire.Presentation for Java.

Below is the screenshot of the input PowerPoint document:

Set Table Row Height and Column Width in PowerPoint in Java

import com.spire.presentation.FileFormat;
import com.spire.presentation.ISlide;
import com.spire.presentation.ITable;
import com.spire.presentation.Presentation;

public class Table_Row_Height_and_Column_Width {
    public static void main(String[] args) throws Exception {
        //Load the PowerPoint document
        Presentation ppt = new Presentation();
        ppt.loadFromFile("Table.pptx");

        //Get the first slide
        ISlide slide = ppt.getSlides().get(0);

        //Get the first table in the slide
        ITable table = (ITable) slide.getShapes().get(0);

        //Change the height of the first table row and the width of the first table column
        table.getTableRows().get(0).setHeight(100);
        table.getColumnsList().get(0).setWidth(250);

        //Save the document
        ppt.saveToFile("Output.pptx", FileFormat.PPTX_2013);
        ppt.dispose();
    }
}

Output:

Set Table Row Height and Column Width in PowerPoint in Java

This article demonstrates how to insert an image to a table cell in PowerPoint using Spire.Presentataion for Java.

import com.spire.presentation.FileFormat;
import com.spire.presentation.ITable;
import com.spire.presentation.Presentation;
import com.spire.presentation.drawing.FillFormatType;
import com.spire.presentation.drawing.IImageData;
import com.spire.presentation.drawing.PictureFillType;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.FileInputStream;

public class InsertImageToTableCell {

    public static void main(String[] args) throws Exception {

        //create a Presentation object and load an example PowerPoint file
        Presentation presentation = new Presentation();
        presentation.loadFromFile("C:/Users/Administrator/Desktop/example.pptx");

        //append a table to the first slide
        Double[] widths = new Double[]{100d,100d};
        Double[] heights = new Double[]{100d,100d};
        ITable table = presentation.getSlides().get(0).getShapes().appendTable(100,100, widths, heights);

        //insert an image to the cell(0,0)
        table.get(0,0).getFillFormat().setFillType(FillFormatType.PICTURE);
        table.get(0,0).getFillFormat().getPictureFill().setFillType(PictureFillType.STRETCH);
        BufferedImage bufferedImage = ImageIO.read(new FileInputStream("C:/Users/Administrator/Desktop/logo.png"));
        IImageData imageData = presentation.getImages().append(bufferedImage);
        table.get(0,0).getFillFormat().getPictureFill().getPicture().setEmbedImage(imageData);

        //save to file
        presentation.saveToFile("InsertImageToCell.pptx", FileFormat.PPTX_2013);
    }
}

Insert an Image to a PowerPoint Table in Java

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.4.5</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();
    }
}

Java: Merge and Split Table Cells in PowerPoint

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();
    }
}

Java: Merge and Split Table Cells in PowerPoint

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.

Tables in PowerPoint are a valuable tool for organizing and presenting data in a clear and concise manner. When creating business presentations or financial reports, you can insert tables to present data effectively and make your presentations more impactful and attractive to your audience. This article will demonstrate how to programmatically add a table to a presentation slide 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.4.5</version>
    </dependency>
</dependencies>
    

Insert a Table in PowerPoint in Java

To create a table in a specified PowerPoint slide, you can use the ISlide.getShapes().appendTable() method. With Spire.Presentation for Java, you are also allowed to format the style of the table. The following are the detailed steps.

  • Create a Presentation instance.
  • Get a specified slide using Presentation.getSlides().get() method.
  • Define two double arrays to specify the number and size of rows and columns in the table.
  • Add a table with the specified number and size of rows and columns to the slide using ISlide.getShapes().appendTable() method.
  • Define some data and then then fill the table with the data using ITable.get().getTextFrame().setText() method.
  • Set the text font and alignment of the table.
  • Set a built-in table style using ITable.setStylePreset() method.
  • Save the result document using Presentation.saveToFile() method.
  • Java
import com.spire.presentation.*;

public class AddTable {
    public static void main(String[] args) throws Exception {
        //Create a Presentation instance
        Presentation presentation = new Presentation();

        //Get the first slide
        ISlide slide = presentation.getSlides().get(0);

        //Define two double arrays to specify the number and size of rows and columns in the table
        Double[] widths = new Double[]{100d, 100d, 150d, 100d, 100d};
        Double[] heights = new Double[]{15d, 15d, 15d, 15d, 15d, 15d, 15d, 15d, 15d, 15d, 15d, 15d, 15d};


        //Add a table with the specified number and size of rows and columns to the slide
        ITable table = slide.getShapes().appendTable((float) presentation.getSlideSize().getSize().getWidth() / 2 - 275, 90, widths, heights);

        //Specify table data
        String[][] dataStr = new String[][]
                {
                        {"Name", "Capital", "Continent", "Area", "Population"},
                        {"Venezuela", "Caracas", "South America", "912047", "19700000"},
                        {"Bolivia", "La Paz", "South America", "1098575", "7300000"},
                        {"Brazil", "Brasilia", "South America", "8511196", "150400000"},
                        {"Canada", "Ottawa", "North America", "9976147", "26500000"},
                        {"Chile", "Santiago", "South America", "756943", "13200000"},
                        {"Colombia", "Bagota", "South America", "1138907", "33000000"},
                        {"Cuba", "Havana", "North America", "114524", "10600000"},
                        {"Ecuador", "Quito", "South America", "455502", "10600000"},
                        {"Paraguay", "Asuncion", "South America", "406576", "4660000"},
                        {"Peru", "Lima", "South America", "1285215", "21600000"},
                        {"Jamaica", "Kingston", "North America", "11424", "2500000"},
                        {"Mexico", "Mexico City", "North America", "1967180", "88600000"}
                };

        //Add data to table
        for (int i = 0; i < 13; i++) {
        for (int j = 0; j < 5; j++) {
        //Fill the table with data
        table.get(j, i).getTextFrame().setText(dataStr[i][j]);

        //Set the text font
        table.get(j, i).getTextFrame().getParagraphs().get(0).getTextRanges().get(0).setLatinFont(new TextFont("Calibri"));

        //Set the text alignment of the table
        table.get(j, i).getTextFrame().getParagraphs().get(0).setAlignment(TextAlignmentType.CENTER);
            }
        }

        //Set the table style
        table.setStylePreset(TableStylePreset.LIGHT_STYLE_3_ACCENT_1);

        //Save the result document
        presentation.saveToFile("AddTable.pptx", FileFormat.PPTX_2013);
    }
}

Java: Create Tables in PowerPoint

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.