Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Last active September 23, 2021 15:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aspose-com-gists/cb55517c2bd8a4b90ed613889498f270 to your computer and use it in GitHub Desktop.
Save aspose-com-gists/cb55517c2bd8a4b90ed613889498f270 to your computer and use it in GitHub Desktop.
Create and manipulate Tables in PowerPoint 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();
}
// 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();
}
// 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();
}
// 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();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment