Manage Cells
Identify Merged Table Cell
- Create an instance of the Presentation class.
- Get the table from the first slide.
- Iterate through the table’s rows and columns to find merge cells.
- Print message when merged cells are found.
This Java code shows you how to identify merged table cells in a presentation:
Presentation pres = new Presentation("SomePresentationWithTable.pptx");
try {
ITable table = (ITable)pres.getSlides().get_Item(0).getShapes().get_Item(0); // assuming that Slide#0.Shape#0 is a table
for (int i = 0; i < table.getRows().size(); i++)
{
for (int j = 0; j < table.getColumns().size(); j++)
{
ICell currentCell = table.getRows().get_Item(i).get_Item(j);
if (currentCell.isMergedCell())
{
System.out.println(String.format("Cell %d;%d is a part of merged cell with RowSpan=%d and ColSpan=%d starting from Cell %d;%d.",
i, j, currentCell.getRowSpan(), currentCell.getColSpan(), currentCell.getFirstRowIndex(), currentCell.getFirstColumnIndex()));
}
}
}
} finally {
if (pres != null) pres.dispose();
}
Remove Table Cells Border
- Create an instance of the Presentation class.
- Get a slide’s reference through its index.
- Define an array of columns with width.
- Define an array of rows with height.
- Add a table to the slide through the addTable method.
- Iterate through every cell to clear the top, bottom, right, and left borders.
- Save the modified presentation as a PPTX file.
This Java code shows you how to remove the borders from table cells:
// Instantiates Presentation class that represents a PPTX file
Presentation pres = new Presentation();
try {
// Accesses the first slide
Slide sld = (Slide)pres.getSlides().get_Item(0);
// Defines columns with widths and rows with heights
double[] dblCols = { 50, 50, 50, 50 };
double[] dblRows = { 50, 30, 30, 30, 30 };
// Adds table shape to slide
ITable tbl = sld.getShapes().addTable(100, 50, dblCols, dblRows);
// Sets the border format for each cell
for (IRow row : tbl.getRows())
{
for (ICell cell : row)
{
cell.getCellFormat().getBorderTop().getFillFormat().setFillType(FillType.NoFill);
cell.getCellFormat().getBorderBottom().getFillFormat().setFillType(FillType.NoFill);
cell.getCellFormat().getBorderLeft().getFillFormat().setFillType(FillType.NoFill);
cell.getCellFormat().getBorderRight().getFillFormat().setFillType(FillType.NoFill);
}
}
// Writes the PPTX to disk
pres.save("table_out.pptx", SaveFormat.Pptx);
} finally {
if (pres != null) pres.dispose();
}
Numbering in Merged Cells
If we merge 2 pairs of cells (1, 1) x (2, 1) and (1, 2) x (2, 2), the resulting table will be numbered. This Java code demonstrates the process:
// Instantiates Presentation class that represents a PPTX file
Presentation pres = new Presentation();
try {
// Accesses first slide
ISlide sld = pres.getSlides().get_Item(0);
// Defines columns with widths and rows with heights
double[] dblCols = { 70, 70, 70, 70 };
double[] dblRows = { 70, 70, 70, 70 };
// Adds a table shape to the slide
ITable tbl = sld.getShapes().addTable(100, 50, dblCols, dblRows);
// Sets the border format for each cell
for (IRow row : tbl.getRows())
{
for (ICell cell : row)
{
cell.getCellFormat().getBorderTop().getFillFormat().setFillType(FillType.Solid);
cell.getCellFormat().getBorderTop().getFillFormat().getSolidFillColor().setColor(Color.RED);
cell.getCellFormat().getBorderTop().setWidth(5);
cell.getCellFormat().getBorderBottom().getFillFormat().setFillType(FillType.Solid);
cell.getCellFormat().getBorderBottom().getFillFormat().getSolidFillColor().setColor(Color.RED);
cell.getCellFormat().getBorderBottom().setWidth(5);
cell.getCellFormat().getBorderLeft().getFillFormat().setFillType(FillType.Solid);
cell.getCellFormat().getBorderLeft().getFillFormat().getSolidFillColor().setColor(Color.RED);
cell.getCellFormat().getBorderLeft().setWidth(5);
cell.getCellFormat().getBorderRight().getFillFormat().setFillType(FillType.Solid);
cell.getCellFormat().getBorderRight().getFillFormat().getSolidFillColor().setColor(Color.RED);
cell.getCellFormat().getBorderRight().setWidth(5);
}
}
// Merges cells (1, 1) x (2, 1)
tbl.mergeCells(tbl.get_Item(1, 1), tbl.get_Item(2, 1), false);
// Merges cells (1, 2) x (2, 2)
tbl.mergeCells(tbl.get_Item(1, 2), tbl.get_Item(2, 2), false);
pres.save("MergeCells_out.pptx", SaveFormat.Pptx);
} finally {
if (pres != null) pres.dispose();
}
We then merge the cells further by merging (1, 1) and (1, 2). The result is a table containing a large merged cell in its center:
// Instantiates Presentation class that represents a PPTX file
Presentation pres = new Presentation();
try {
// Accesses first slide
ISlide sld = pres.getSlides().get_Item(0);
// Defines columns with widths and rows with heights
double[] dblCols = { 70, 70, 70, 70 };
double[] dblRows = { 70, 70, 70, 70 };
// Adds a table shape to the slide
ITable tbl = sld.getShapes().addTable(100, 50, dblCols, dblRows);
// Sets the border format for each cell
for (IRow row : tbl.getRows())
{
for (ICell cell : row)
{
cell.getCellFormat().getBorderTop().getFillFormat().setFillType(FillType.Solid);
cell.getCellFormat().getBorderTop().getFillFormat().getSolidFillColor().setColor(Color.RED);
cell.getCellFormat().getBorderTop().setWidth(5);
cell.getCellFormat().getBorderBottom().getFillFormat().setFillType(FillType.Solid);
cell.getCellFormat().getBorderBottom().getFillFormat().getSolidFillColor().setColor(Color.RED);
cell.getCellFormat().getBorderBottom().setWidth(5);
cell.getCellFormat().getBorderLeft().getFillFormat().setFillType(FillType.Solid);
cell.getCellFormat().getBorderLeft().getFillFormat().getSolidFillColor().setColor(Color.RED);
cell.getCellFormat().getBorderLeft().setWidth(5);
cell.getCellFormat().getBorderRight().getFillFormat().setFillType(FillType.Solid);
cell.getCellFormat().getBorderRight().getFillFormat().getSolidFillColor().setColor(Color.RED);
cell.getCellFormat().getBorderRight().setWidth(5);
}
}
// Merges cells (1, 1) x (2, 1)
tbl.mergeCells(tbl.get_Item(1, 1), tbl.get_Item(2, 1), false);
// Merges cells (1, 2) x (2, 2)
tbl.mergeCells(tbl.get_Item(1, 2), tbl.get_Item(2, 2), false);
// Merges cells (1, 1) x (1, 2)
tbl.mergeCells(tbl.get_Item(1, 1), tbl.get_Item(1, 2), true);
//Writes the PPTX file to disk
pres.save("MergeCells_out.pptx", SaveFormat.Pptx);
} finally {
if (pres != null) pres.dispose();
}
Numbering in Splitted Cell
In previous examples, when table cells got merged, the numeration or number system in other cells did not change.
This time, we take a regular table (a table without merged cells) and then try to split cell (1,1) to get a special table. You may want to pay attention to this table’s numbering, which may be considered strange. However, that is the way Microsoft PowerPoint numerates table cells and Aspose.Slides does the same thing.
This Java code demonstrates the process we described:
// Instantiates the Presentation class that represents a PPTX file
Presentation pres = new Presentation();
try {
// Accesses the first slide
ISlide sld = pres.getSlides().get_Item(0);
// Defines columns with widths and rows with heights
double[] dblCols = { 70, 70, 70, 70 };
double[] dblRows = { 70, 70, 70, 70 };
// Adds a table shape to the slide
ITable tbl = sld.getShapes().addTable(100, 50, dblCols, dblRows);
// Sets the border format for each cell
for (IRow row : tbl.getRows())
{
for (ICell cell : row)
{
cell.getCellFormat().getBorderTop().getFillFormat().setFillType(FillType.Solid);
cell.getCellFormat().getBorderTop().getFillFormat().getSolidFillColor().setColor(Color.RED);
cell.getCellFormat().getBorderTop().setWidth(5);
cell.getCellFormat().getBorderBottom().getFillFormat().setFillType(FillType.Solid);
cell.getCellFormat().getBorderBottom().getFillFormat().getSolidFillColor().setColor(Color.RED);
cell.getCellFormat().getBorderBottom().setWidth(5);
cell.getCellFormat().getBorderLeft().getFillFormat().setFillType(FillType.Solid);
cell.getCellFormat().getBorderLeft().getFillFormat().getSolidFillColor().setColor(Color.RED);
cell.getCellFormat().getBorderLeft().setWidth(5);
cell.getCellFormat().getBorderRight().getFillFormat().setFillType(FillType.Solid);
cell.getCellFormat().getBorderRight().getFillFormat().getSolidFillColor().setColor(Color.RED);
cell.getCellFormat().getBorderRight().setWidth(5);
}
}
// Merges cells (1, 1) x (2, 1)
tbl.mergeCells(tbl.get_Item(1, 1), tbl.get_Item(2, 1), false);
// Merges cells (1, 2) x (2, 2)
tbl.mergeCells(tbl.get_Item(1, 2), tbl.get_Item(2, 2), false);
// Splits cell (1, 1)
tbl.get_Item(1, 1).splitByWidth(tbl.get_Item(2, 1).getWidth() / 2);
//Writes the PPTX file to disk
pres.save("SplitCells_out.pptx", SaveFormat.Pptx);
} finally {
if (pres != null) pres.dispose();
}
Change Table Cell Background Color
This Java code shows you how to change a table cell’s background color:
Presentation presentation = new Presentation();
try {
ISlide slide = presentation.getSlides().get_Item(0);
double[] dblCols = { 150, 150, 150, 150 };
double[] dblRows = { 50, 50, 50, 50, 50 };
// create a new table
ITable table = slide.getShapes().addTable(50, 50, dblCols, dblRows);
// set the background color for a cell
ICell cell = table.get_Item(2, 3);
cell.getCellFormat().getFillFormat().setFillType(FillType.Solid);
cell.getCellFormat().getFillFormat().getSolidFillColor().setColor(Color.RED);
presentation.save("cell_background_color.pptx", SaveFormat.Pptx);
} finally {
if (presentation != null) presentation.dispose();
}
Add Image Inside Table Cell
- Create an instance of the Presentation class.
- Get a slide’s reference through its index.
- Define an array of columns with width.
- Define an array of rows with height.
- Add a table to the slide through the AddTable method.
- Create a
Images
object to hold the image file. - Add the
IImage
image toIPPImage
Object. - Set the
FillFormat
for the Table Cell toPicture
. - Add the image to the table’s first cell.
- Save the modified presentation as a PPTX file
This Java code shows you how to place an image inside a table cell when creating a table:
// Instantiates the Presentation class that represents a PPTX file
Presentation pres = new Presentation();
try {
// Accesses the first slide
ISlide islide = pres.getSlides().get_Item(0);
// Defines columns with widths and rows with heights
double[] dblCols = {150, 150, 150, 150};
double[] dblRows = {100, 100, 100, 100, 90};
// Adds a table shape to the slide
ITable tbl = islide.getShapes().addTable(50, 50, dblCols, dblRows);
// Create an IPPImage object using the image file
IPPImage picture;
IImage image = Images.fromFile("image.jpg");
try {
picture = pres.getImages().addImage(image);
} finally {
if (image != null) image.dispose();
}
// Adds the image to the first table cell
ICellFormat cellFormat = tbl.get_Item(0, 0).getCellFormat();
cellFormat.getFillFormat().setFillType(FillType.Picture);
cellFormat.getFillFormat().getPictureFillFormat().setPictureFillMode(PictureFillMode.Stretch);
cellFormat.getFillFormat().getPictureFillFormat().getPicture().setImage(picture);
// Saves the PPTX file to Disk
pres.save("Image_In_TableCell_out.pptx", SaveFormat.Pptx);
} catch (IOException e) {
} finally {
if (pres != null) pres.dispose();
}