Align Text in 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