Table

Table (7)

Thursday, 28 November 2019 06:48

Java set table style for the PowerPoint table

Written by support iceblue

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

Tuesday, 26 March 2019 04:04

Align Text in PowerPoint Table in Java

Written by support iceblue

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

Tuesday, 12 February 2019 07:30

Insert an Image to a PowerPoint Table in Java

Written by support iceblue

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

This article demonstrates how to access a table in an existing PowerPoint document and how to merge and split cells in the table by using Spire.Presentation for Java.

Here is the screenshot of the input file.

Merge and Split Table Cells in PowerPoint in Java

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

public class MergeAndSplitCells {

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

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

        //load a sample PowerPoint file
        presentation.loadFromFile("C:/Users/Administrator/Desktop/sample.pptx");

        //declare ITable variable
        ITable table = null;

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

                //merge the cells between cell[0,0] and cell[2,0]
                table.mergeCells(table.get(0, 0), table.get(2, 0), false);

                //merge the cells between  cell[0,1] and cell[0,3]
                table.mergeCells(table.get(0, 1), table.get(0, 3), false);

                //split cell[2,3] to 2 columns
                table.get(2,3).Split(1,2);
            }
        }

        //save to file
        presentation.saveToFile("MergeAndSplitCells.pptx", FileFormat.PPTX_2010);
    }
}

Output

Merge and Split Table Cells in PowerPoint in Java

Friday, 21 December 2018 06:46

Create Table in PowerPoint in Java

Written by support iceblue

This article demonstrates how to create a table and set table style in a PowerPoint document using Spire.Presentation for Java.

import com.spire.presentation.*;

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

        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 
        ITable table = presentation.getSlides().get(0).getShapes().appendTable((float) presentation.getSlideSize().getSize().getWidth() / 2 - 275, 90, widths, heights);
        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 font
                table.get(j, i).getTextFrame().getParagraphs().get(0).getTextRanges().get(0).setLatinFont(new TextFont("Arial Narrow"));
            }
        }
        //Set the alignment of the first table row to Center
        for (int i = 0; i < 5; i++) {
            table.get(i, 0).getTextFrame().getParagraphs().get(0).setAlignment(TextAlignmentType.CENTER);
        }
        //Set the style of table
        table.setStylePreset(TableStylePreset.LIGHT_STYLE_3_ACCENT_1);

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

Output:

Create Table in PowerPoint in Java