This short tutorial guides you on how to insert a table in a slide using Java. It shares the necessary details to set the IDE for writing the application and detailed steps to perform this task. In the end, you will get a runnable sample code that demonstrates how to make table in PowerPoint using Java and saving the resultant presentation on the disk as PPTX, PPT, or in any other supported format without installing MS PowerPoint or any other third-party tool.
Steps to Add Table in PowerPoint using Java
- Set the IDE to use Aspose.Slides to insert a table
- Create a new presentation using the Presentation class and get access to the first slide
- Add a new table in the selected slide by using the addTable() method and providing the text position, and cells’ height and width
- Iterate through all the rows and set text along with the font settings
- Save the presentation on the disk having a table in it
The aforementioned steps explain how to create tables in PowerPoint using Java. All the necessary classes and methods are identified those are required to accomplish the task. The process is simple where a table is added to a slide and then its cells are filled with some sample text.
Code to Create a Table in PowerPoint using Java
/* | |
* To change this license header, choose License Headers in Project Properties. | |
* To change this template file, choose Tools | Templates | |
* and open the template in the editor. | |
*/ | |
package testslides; | |
import com.aspose.slides.BulletType; | |
import com.aspose.slides.ICell; | |
import com.aspose.slides.IRow; | |
import com.aspose.slides.ISlide; | |
import com.aspose.slides.ITable; | |
import com.aspose.slides.ITextFrame; | |
import com.aspose.slides.License; | |
import com.aspose.slides.Presentation; | |
public class AddTable { | |
public static void main(String[] args) throws Exception {//handle exception during adding any tablle | |
String path = "/Users/mudassirkhan/Documents/KnowledgeBase/TestData/"; | |
// Load the product license to create a table | |
License slidesTablelic = new License(); | |
slidesTablelic.setLicense(path + "Conholdate.Total.Product.Family.lic"); | |
// Create a new presentation | |
Presentation presentationForTable = new Presentation(); | |
// Access the first slide in the presentation | |
ISlide slide = presentationForTable.getSlides().get_Item(0); | |
// Define the arrays containing column widths and row heights | |
double[] dColumnsWidths = { 55, 55, 55 }; | |
double[] dRowsHeights = { 55, 36, 36, 36, 36 }; | |
// Add a new table with desired rows and columns | |
ITable newTable = slide.getShapes().addTable(60, 60, dColumnsWidths, dRowsHeights); | |
// Traverse through all the rows | |
for (IRow tableRow : newTable.getRows()) | |
{ | |
// Traverse through all the cells in the current row | |
for (ICell tableCell : tableRow) | |
{ | |
// Access the respective text frame of the cell | |
ITextFrame txtFormat = tableCell.getTextFrame(); | |
// Add some text to the cell | |
int iFirstRowIndex = tableCell.getFirstRowIndex(); | |
int iFirstColumnIndex = tableCell.getFirstColumnIndex(); | |
txtFormat.setText( "Text " + iFirstRowIndex + " " + iFirstColumnIndex); | |
// Set the font related property for the newly added text | |
txtFormat.getParagraphs().get_Item(0).getPortions().get_Item(0).getPortionFormat().setFontHeight(10); | |
txtFormat.getParagraphs().get_Item(0).getParagraphFormat().getBullet().setType(BulletType.None); | |
} | |
} | |
// Save the presentation with table | |
presentationForTable.save("PresentationTable.pptx", com.aspose.slides.SaveFormat.Pptx); | |
System.out.println("Done"); | |
} | |
} |
This code demonstrates how to create a table in PowerPoint using Java. Multiple interface classes are used for using different features e.g. the ISlide is used to access the slide, the ITable class is used for working with tables, and the IRow and ICell classes are used to work with individual cells. The ITextFrame class is the main interface that is used to set the text in a cell and set the formatting of the cells.
This tutorial has taught us how to make table in PowerPoint presentation using Java. If you want to learn the process to create an HTML from a presentation, refer to the article on how to create PowerPoint slides in HTML using Java.