How to Add Shapes to PowerPoint in Java
Aspose.Slides FOSS for Java supports adding AutoShapes, Tables, Connectors, and PictureFrames to presentation slides. All shape types are added through the slide.getShapes() collection.
Step-by-Step Guide
Step 1: Add the Maven Dependency
<dependency>
<groupId>org.aspose.slides.foss</groupId>
<artifactId>aspose-slides-foss</artifactId>
<version>1.0.0</version>
</dependency>Step 2: Create a Presentation
Always use try-with-resources to manage resources.
import org.aspose.slides.foss.*;
try (Presentation prs = new Presentation()) {
ISlide slide = prs.getSlides().get(0);
// ... add shapes ...
prs.save("output.pptx", SaveFormat.PPTX);
}Step 3: Add an AutoShape
slide.getShapes().addAutoShape(shapeType, x, y, width, height) places a shape at the given position and size (all in points). Use ShapeType constants to select the shape.
import org.aspose.slides.foss.*;
try (Presentation prs = new Presentation()) {
ISlide slide = prs.getSlides().get(0);
// Rectangle
IAutoShape rect = slide.getShapes().addAutoShape(
ShapeType.RECTANGLE, 50, 50, 300, 100
);
rect.addTextFrame("Rectangle shape");
// Ellipse
IAutoShape ellipse = slide.getShapes().addAutoShape(
ShapeType.ELLIPSE, 400, 50, 200, 100
);
ellipse.addTextFrame("Ellipse shape");
prs.save("autoshapes.pptx", SaveFormat.PPTX);
}Step 4: Add a Table
slide.getShapes().addTable(x, y, colWidths, rowHeights) creates a table at the specified position. Column widths and row heights are arrays of point values.
import org.aspose.slides.foss.*;
try (Presentation prs = new Presentation()) {
ISlide slide = prs.getSlides().get(0);
double[] colWidths = {150.0, 150.0, 150.0};
double[] rowHeights = {40.0, 40.0, 40.0};
ITable table = slide.getShapes().addTable(50, 200, colWidths, rowHeights);
// Set header row text
String[] headers = {"Product", "Units", "Revenue"};
for (int col = 0; col < headers.length; col++) {
table.getRows().get(0).get(col).getTextFrame().setText(headers[col]);
}
// Set data rows
String[][] rows = {
{"Widget A", "120", "$2,400"},
{"Widget B", "85", "$1,700"},
};
for (int row = 0; row < rows.length; row++) {
for (int col = 0; col < rows[row].length; col++) {
table.getRows().get(row + 1).get(col)
.getTextFrame().setText(rows[row][col]);
}
}
prs.save("table.pptx", SaveFormat.PPTX);
}Step 5: Add a Connector
Connectors link two shapes visually. Create the shapes first, then add a connector and set its start and end connection points.
import org.aspose.slides.foss.*;
try (Presentation prs = new Presentation()) {
ISlide slide = prs.getSlides().get(0);
IAutoShape box1 = slide.getShapes().addAutoShape(
ShapeType.RECTANGLE, 50, 100, 150, 60
);
box1.addTextFrame("Start");
IAutoShape box2 = slide.getShapes().addAutoShape(
ShapeType.RECTANGLE, 350, 100, 150, 60
);
box2.addTextFrame("End");
IConnector conn = slide.getShapes().addConnector(
ShapeType.BENT_CONNECTOR3, 0, 0, 10, 10
);
conn.setStartShapeConnectedTo(box1);
conn.setStartShapeConnectionSiteIndex(3); // right side of box1
conn.setEndShapeConnectedTo(box2);
conn.setEndShapeConnectionSiteIndex(1); // left side of box2
prs.save("connector.pptx", SaveFormat.PPTX);
}Connection site indices are numbered 0-3 for a rectangle: top=0, left=1, bottom=2, right=3.
Step 6: Add a Picture Frame
Embed an image and add it to the slide as a PictureFrame. Read the image bytes first, add them to the presentation’s image collection, then create the frame.
import org.aspose.slides.foss.*;
import java.nio.file.Files;
import java.nio.file.Paths;
try (Presentation prs = new Presentation()) {
byte[] imageData = Files.readAllBytes(Paths.get("logo.png"));
IPPImage image = prs.getImages().addImage(imageData);
ISlide slide = prs.getSlides().get(0);
slide.getShapes().addPictureFrame(
ShapeType.RECTANGLE, // bounding shape type
50, 50, // x, y in points
200, 150, // width, height in points
image
);
prs.save("with-image.pptx", SaveFormat.PPTX);
}Common Issues and Fixes
Shape appears outside the visible slide area
Slides are 720 x 540 points by default. Values of x or y beyond those bounds place the shape off-slide. Keep x < 720 and y < 540, and ensure x + width <= 720 and y + height <= 540.
NullPointerException when reading shape text
addAutoShape() returns the shape object directly. If you see null, check that you are not discarding the return value. Also verify getTextFrame() is not null before reading text.
Table cell text is empty after assignment
The correct method is .getTextFrame().setText(value). Access cells as table.getRows().get(rowIndex).get(colIndex).getTextFrame().setText("value").
Frequently Asked Questions
How many shapes can I add to a slide?
There is no library-imposed limit. Practical limits depend on file size and the rendering capability of your target PPTX viewer.
Can I change a shape’s position after adding it?
Yes. The shape object returned by addAutoShape() has position and size properties via getShapeFrame().
Are charts supported?
No. Charts, SmartArt, and OLE objects are not implemented in this edition and raise UnsupportedOperationException.