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
- Create a Table in PowerPoint Presentation
- Access a Table in a Presentation using Java
- Format Text in PowerPoint Tables
- Lock Aspect Ratio of the Tables
Java API to Create and Manipulate Tables in PowerPoint
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 Presentation 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 presentation from scratch using 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 create a table in a PowerPoint presentation.
The following screenshot shows the table that we have created using the above code.
Access a Table in a Presentation 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.
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.
Lock Aspect Ratio of Tables in PowerPoint using Java
You can also lock the aspect ratio of the tables in PowerPoint presentations 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 code sample shows how to lock the aspect ratio of the table in PowerPoint presentations.
Get a Free API 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 presentations 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.