
Tables are used to well organize the data in the form of rows and columns. Moreover, they summarize the data to be viewed and analyzed easily. MS PowerPoint also allows the presenters to create tables in the presentations. Accordingly, in this article, you will learn how to create and manipulate tables in PowerPoint presentations using Java.
- Java API to Create and Manipulate Tables in PowerPoint PPT
- Create a Table in PowerPoint PPT in Java
- Access a Table in a PPTX using Java
- Format Text in PowerPoint Tables
- Lock Aspect Ratio of the PPT Tables
Java API to Create and Manipulate Tables in PowerPoint PPT
To create and manipulate tables in PowerPoint presentations, we will use Aspose.Slides for Java. The API is designed to create, manipulate and convert PowerPoint and OpenOffice presentations. You can download the API’s JAR or install it using the following Maven configurations.
<repository>
<id>AsposeJavaAPI</id>
<name>Aspose Java API</name>
<url>http://repository.aspose.com/repo/</url>
</repository>
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-slides</artifactId>
<version>21.8</version>
<classifier>jdk16</classifier>
</dependency>
Create a Table in PowerPoint PPT using Java
Creating a table using Aspose.Slides for Java is as easy as a pie. The following steps demonstrate how to create a table in a PowerPoint PPT from scratch in Java.
- First, create a new presentation or load an existing one using Presentation class.
- Then, get reference of the desired slide into an ISlide object.
- Define the width and height of columns and rows respectively in double[] arrays.
- Insert a new table in presentation using ISlide.getShapes().addTable(float, float, double[], double[]) method.
- Get reference of the newly created table in an ITable object.
- Create a loop to iterate through the rows of the table.
- Create a nested loop to iterate through the cells of the table and in each iteration, perform the following operations.
- Set text of the cell using ITable.getRows().get_Item(rowIndex).get_Item(cellIndex).getTextFrame().setText(String) method.
- Get reference of the cell’s formatting into an ICellFormat object.
- Set cell’s border style, if required.
- Finally, save the presentation using Presentation.save(String, SaveFormat) method.
The following code sample shows how to add a table in a PowerPoint PPT in Java.
// Create or load presentation | |
Presentation pres = new Presentation(); | |
try { | |
// Access first slide | |
ISlide sld = pres.getSlides().get_Item(0); | |
// Define columns with widths and rows with heights | |
double[] dblCols = { 50, 50, 50 }; | |
double[] dblRows = { 50, 30, 30, 30, 30 }; | |
// Add table shape to slide | |
ITable tbl = sld.getShapes().addTable(100, 50, dblCols, dblRows); | |
// Set text and border format for each cell | |
for (int row = 0; row < tbl.getRows().size(); row++) { | |
for (int cell = 0; cell < tbl.getRows().get_Item(row).size(); cell++) { | |
// Set text | |
tbl.getRows().get_Item(row).get_Item(cell).getTextFrame().setText("Cell_" + cell); | |
// Set border | |
ICellFormat cellFormat = tbl.getRows().get_Item(row).get_Item(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); | |
} | |
} | |
// Save PPTX to Disk | |
pres.save("table.pptx", SaveFormat.Pptx); | |
} finally { | |
if (pres != null) | |
pres.dispose(); | |
} |
The following screenshot shows the table that we have created using the above code.

Access a Table in a PPT using Java
You can also access the tables in the existing PowerPoint presentations and manipulate them as required. The following are the steps to access the tables in a presentation.
- First, load an existing presentation using Presentation class.
- Then, get reference of the desired slide into an ISlide object.
- Create an instance of ITable and initialize it with null.
- Iterate through all IShape objects in ISlide.getShapes() collection.
- Filter the shapes of type ITable.
- Type cast the shape into ITable and manipulate it as required.
- Finally, save the presentation using Presentation.save(String, SaveFormat) method.
The following code sample shows how to access tables in a PowerPoint presentation using Java.
// Create or load presentation | |
Presentation pres = new Presentation("UpdateExistingTable.pptx"); | |
try { | |
// Access the first slide | |
ISlide sld = pres.getSlides().get_Item(0); | |
// Initialize ITable | |
ITable tbl = null; | |
// Iterate through the shapes and get a reference to the table found | |
for (IShape shp : sld.getShapes()) | |
{ | |
if (shp instanceof ITable) | |
{ | |
tbl = (ITable) shp; | |
// Set the text of the first column of second row | |
tbl.get_Item(0, 1).getTextFrame().setText("New"); | |
} | |
} | |
// Write the PPTX to disk | |
pres.save("table1_out.pptx", SaveFormat.Pptx); | |
} finally { | |
if (pres != null) pres.dispose(); | |
} |
Format Text in PowerPoint Tables using Java
Aspose.Slides for Java also allows you to set formatting of the tables quite easily, as demonstrated in the steps below.
- First, load an existing presentation using Presentation class.
- Then, get reference of the desired slide into an ISlide object.
- Retreive the reference of the desired table from slide into an instance of ITable class.
- Set formatting using PortionFormat, ParagraphFormat, and TextFrameFormat classes.
- Assign formatting to the table using ITable.setTextFormat() methods.
- Finally, save the presentation using Presentation.save(String, SaveFormat) method.
The following code sample shows how to set the formatting of the table in PowerPoint using Java.
// Load presentation | |
Presentation pres = new Presentation("simpletable.pptx"); | |
try { | |
// Get reference of the table | |
ITable someTable = (ITable) pres.getSlides().get_Item(0).getShapes().get_Item(0); | |
// Set table cells' font height | |
PortionFormat portionFormat = new PortionFormat(); | |
portionFormat.setFontHeight(25); | |
someTable.setTextFormat(portionFormat); | |
// Set table cells' text alignment and right margin in one call | |
ParagraphFormat paragraphFormat = new ParagraphFormat(); | |
paragraphFormat.setAlignment(TextAlignment.Right); | |
paragraphFormat.setMarginRight(20); | |
someTable.setTextFormat(paragraphFormat); | |
// Set table cells' text vertical type | |
TextFrameFormat textFrameFormat = new TextFrameFormat(); | |
textFrameFormat.setTextVerticalType(TextVerticalType.Vertical); | |
someTable.setTextFormat(textFrameFormat); | |
// Save presentation | |
pres.save("result.pptx", SaveFormat.Pptx); | |
} finally { | |
if (pres != null) pres.dispose(); | |
} |
Lock Aspect Ratio of Tables in PPTX using Java
You can also lock the aspect ratio of the tables in PowerPoint PPT using Java. The following are the steps to achieve this.
- First, load an existing presentation using Presentation class.
- Get reference of the desired slide into an ISlide object.
- Create a table or retrieve the reference of existing table into an ITable object.
- Lock the aspect ratio using ITable.getGraphicalObjectLock().setAspectRatioLocked(!ITable.getGraphicalObjectLock().getAspectRatioLocked()) method.
- Finally, save the presentation using Presentation.save(String, SaveFormat) method.
The following Java code sample shows how to lock the aspect ratio of the table in PowerPoint PPTX.
// Load presentation | |
Presentation pres = new Presentation("pres.pptx"); | |
try { | |
// Get reference of the table | |
ITable table = (ITable)pres.getSlides().get_Item(0).getShapes().get_Item(0); | |
System.out.println("Lock aspect ratio set: " + table.getGraphicalObjectLock().getAspectRatioLocked()); | |
// Lock aspect ratio | |
table.getGraphicalObjectLock().setAspectRatioLocked(!table.getGraphicalObjectLock().getAspectRatioLocked()); // invert | |
System.out.println("Lock aspect ratio set: " + table.getGraphicalObjectLock().getAspectRatioLocked()); | |
// Save presentation | |
pres.save("pres-out.pptx", SaveFormat.Pptx); | |
} finally { | |
if (pres != null) pres.dispose(); | |
} |
Java API to Create PowerPoint Tables - Get a Free License
You can use Aspose.Slides for Java without evaluation limitations by getting a free temporary license.
Conclusion
In this article, you have learned how to create tables in PowerPoint PPT using Java. Moreover, you have seen how to access the tables and set their formatting and aspect ratio programmatically. In addition, you can visit the documentation to explore more about Aspose.Slides for Java. Also, you can ask your questions via our forum.