Manage Presentation Tables in Java

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.

  1. Create an instance of the Presentation class.
  2. Get a reference to the slide by its index.
  3. Define an array of column widths in points.
  4. Define an array of row heights in points.
  5. Add an ITable object to the slide through the addTable method.
  6. Iterate through each ICell to apply formatting to the top, bottom, right, and left borders.
  7. Merge the first two cells of the table’s first row.
  8. Access the merged cell through its getTextFrame method.
  9. Set the text in the merged cell.
  10. 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.

import com.aspose.slides.*;
import java.awt.Color;

Presentation presentation = new Presentation();
try {
    ISlide slide = presentation.getSlides().get_Item(0);

    double[] columnWidths = { 50, 50, 50 };
    double[] rowHeights = { 50, 30, 30, 30, 30 };
    ITable table = slide.getShapes().addTable(100, 50, columnWidths, rowHeights);

    for (IRow row : table.getRows())
    {
        for (ICell cell : row)
        {
            ICellFormat cellFormat = cell.getCellFormat();
            cellFormat.getBorderTop().getFillFormat().setFillType(FillType.Solid);
            cellFormat.getBorderTop().getFillFormat().getSolidFillColor().setColor(Color.RED);
            cellFormat.getBorderTop().setWidth(5);

            cellFormat.getBorderBottom().getFillFormat().setFillType(FillType.Solid);
            cellFormat.getBorderBottom().getFillFormat().getSolidFillColor().setColor(Color.RED);
            cellFormat.getBorderBottom().setWidth(5);

            cellFormat.getBorderLeft().getFillFormat().setFillType(FillType.Solid);
            cellFormat.getBorderLeft().getFillFormat().getSolidFillColor().setColor(Color.RED);
            cellFormat.getBorderLeft().setWidth(5);

            cellFormat.getBorderRight().getFillFormat().setFillType(FillType.Solid);
            cellFormat.getBorderRight().getFillFormat().getSolidFillColor().setColor(Color.RED);
            cellFormat.getBorderRight().setWidth(5);
        }
    }

    table.mergeCells(table.get_Item(0, 0), table.get_Item(1, 0), false);
    table.get_Item(0, 0).getTextFrame().setText("Merged Cells");

    presentation.save("table.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

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.

import com.aspose.slides.*;
import java.awt.Color;

Presentation presentation = new Presentation();
try {
    ISlide slide = presentation.getSlides().get_Item(0);

    double[] columnWidths = { 70, 70, 70, 70 };
    double[] rowHeights = { 70, 70, 70, 70 };
    ITable table = slide.getShapes().addTable(100, 50, columnWidths, rowHeights);

    for (IRow row : table.getRows())
    {
        for (ICell cell : row)
        {
            ICellFormat cellFormat = cell.getCellFormat();
            cellFormat.getBorderTop().getFillFormat().setFillType(FillType.Solid);
            cellFormat.getBorderTop().getFillFormat().getSolidFillColor().setColor(Color.RED);
            cellFormat.getBorderTop().setWidth(5);

            cellFormat.getBorderBottom().getFillFormat().setFillType(FillType.Solid);
            cellFormat.getBorderBottom().getFillFormat().getSolidFillColor().setColor(Color.RED);
            cellFormat.getBorderBottom().setWidth(5);

            cellFormat.getBorderLeft().getFillFormat().setFillType(FillType.Solid);
            cellFormat.getBorderLeft().getFillFormat().getSolidFillColor().setColor(Color.RED);
            cellFormat.getBorderLeft().setWidth(5);

            cellFormat.getBorderRight().getFillFormat().setFillType(FillType.Solid);
            cellFormat.getBorderRight().getFillFormat().getSolidFillColor().setColor(Color.RED);
            cellFormat.getBorderRight().setWidth(5);
        }
    }

    presentation.save("StandardTables_out.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

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.

  1. Load the presentation using the Presentation class.
  2. Get a reference to the slide containing the table by its index.
  3. Iterate through the IShape objects and stop when a table is found. If the slide contains several tables, use getAlternativeText to identify the one you need.
  4. Update the text in the target cell.
  5. 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.

import com.aspose.slides.*;

Presentation presentation = new Presentation("UpdateExistingTable.pptx");
try {
    ISlide slide = presentation.getSlides().get_Item(0);
    ITable table = null;

    for (IShape shape : slide.getShapes()) {
        if (shape instanceof ITable) {
            table = (ITable) shape;
            break;
        }
    }

    if (table != null) {
        table.get_Item(0, 1).getTextFrame().setText("New");
        presentation.save("table1_out.pptx", SaveFormat.Pptx);
    }
} finally {
    presentation.dispose();
}

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.getParentCell method to retrieve the owning ICell. For a table-cell text frame, ITextFrame.getParentCell returns the owner and ITextFrame.getParentShape returns null, even though the table itself is a shape.

The cell coordinates are available through the read-only ICell.getFirstColumnIndex and ICell.getFirstRowIndex methods. ITextFrame.getParentCell also provides read-only navigation: it returns 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.

  1. Create an instance of the Presentation class.
  2. Get a reference to the slide by its index.
  3. Add an ITable object to the slide.
  4. Access an ITextFrame object from the table.
  5. Access the first IParagraph and set its text and color.
  6. Set the cell’s vertical anchoring and text direction using setTextAnchorType and setTextVerticalType.
  7. 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.

import com.aspose.slides.*;
import java.awt.Color;

Presentation presentation = new Presentation();
try {
    ISlide slide = presentation.getSlides().get_Item(0);

    double[] columnWidths = { 120, 120, 120, 120 };
    double[] rowHeights = { 100, 100, 100, 100 };
    ITable table = slide.getShapes().addTable(100, 50, columnWidths, rowHeights);

    table.get_Item(1, 0).getTextFrame().setText("10");
    table.get_Item(2, 0).getTextFrame().setText("20");
    table.get_Item(3, 0).getTextFrame().setText("30");

    ITextFrame textFrame = table.get_Item(0, 0).getTextFrame();
    IParagraph paragraph = textFrame.getParagraphs().get_Item(0);

    IPortion portion = paragraph.getPortions().get_Item(0);
    portion.setText("Text here");
    portion.getPortionFormat().getFillFormat().setFillType(FillType.Solid);
    portion.getPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.BLACK);

    ICell cell = table.get_Item(0, 0);
    cell.setTextAnchorType(TextAnchorType.Center);
    cell.setTextVerticalType(TextVerticalType.Vertical270);

    presentation.save("Vertical_Align_Text_out.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

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.

  1. Load the presentation using the Presentation class.
  2. Get a reference to the slide by its index.
  3. Access an ITable object from the slide.
  4. Set the font size using setFontHeight for the text.
  5. Set paragraph alignment and the right margin using setAlignment and setMarginRight.
  6. Set the text direction using setTextVerticalType.
  7. 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.

import com.aspose.slides.*;

Presentation presentation = new Presentation("table.pptx");
try {
    ISlide slide = presentation.getSlides().get_Item(0);
    ITable table = (ITable) slide.getShapes().get_Item(0);

    PortionFormat portionFormat = new PortionFormat();
    portionFormat.setFontHeight(25);
    table.setTextFormat(portionFormat);

    ParagraphFormat paragraphFormat = new ParagraphFormat();
    paragraphFormat.setAlignment(TextAlignment.Right);
    paragraphFormat.setMarginRight(20);
    table.setTextFormat(paragraphFormat);

    TextFrameFormat textFrameFormat = new TextFrameFormat();
    textFrameFormat.setTextVerticalType(TextVerticalType.Vertical);
    table.setTextFormat(textFrameFormat);

    presentation.save("result.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

Get Table Style Properties

Use getStylePreset to read a table’s preset style and setStylePreset to assign it. This example applies TableStylePreset.DarkStyle1 to one table, prints the preset value, and assigns the same preset to a second table. Both tables are saved in table-style.pptx.

import com.aspose.slides.*;

Presentation presentation = new Presentation();
try {
    ISlide slide = presentation.getSlides().get_Item(0);

    double[] columnWidths = { 100, 150 };
    double[] rowHeights = { 5, 5, 5 };
    ITable table = slide.getShapes().addTable(10, 10, columnWidths, rowHeights);
    table.setStylePreset(TableStylePreset.DarkStyle1);

    int stylePreset = table.getStylePreset();
    System.out.println("Table style preset: " + stylePreset);

    ITable anotherTable = slide.getShapes().addTable(10, 100, columnWidths, rowHeights);
    anotherTable.setStylePreset(stylePreset);

    presentation.save("table-style.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

Lock Aspect Ratio of a Table

A table’s aspect ratio is the ratio of its width to its height. Use setAspectRatioLocked 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.

import com.aspose.slides.*;

Presentation presentation = new Presentation("pres.pptx");
try {
    ISlide slide = presentation.getSlides().get_Item(0);

    ITable table = (ITable) slide.getShapes().get_Item(0);
    System.out.println("Lock aspect ratio set: " + table.getGraphicalObjectLock().getAspectRatioLocked());

    table.getGraphicalObjectLock().setAspectRatioLocked(true);
    System.out.println("Lock aspect ratio set: " + table.getGraphicalObjectLock().getAspectRatioLocked());

    presentation.save("pres-out.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

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 setRightToLeft method, and paragraphs have ParagraphFormat.setRightToLeft. 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).