How to Create Presentations in Java
Aspose.Slides FOSS for Java lets you create PowerPoint presentations entirely in Java with no dependency on Microsoft Office. This guide shows how to create a new presentation, add slides and shapes, format text, and save the result.
Step-by-Step Guide
Step 1: Add the Maven Dependency
Add Aspose.Slides FOSS to your pom.xml. Java 11 or later is required.
<dependency>
<groupId>org.aspose.slides.foss</groupId>
<artifactId>aspose-slides-foss</artifactId>
<version>1.0.0</version>
</dependency>Verify the installation:
import org.aspose.slides.foss.Presentation;
Presentation prs = new Presentation();
prs.dispose();
System.out.println("Aspose.Slides FOSS ready");Step 2: Import the Required Classes
Import the Presentation class and the SaveFormat enum needed for saving.
import org.aspose.slides.foss.Presentation;
import org.aspose.slides.foss.SaveFormat;
import org.aspose.slides.foss.ShapeType;
import org.aspose.slides.foss.ISlide;
import org.aspose.slides.foss.IAutoShape;All shape-type constants live in org.aspose.slides.foss.ShapeType. Formatting enums (FillType, NullableBool) are also in org.aspose.slides.foss.
Step 3: Create a Presentation
Use new Presentation() and wrap it in try-with-resources. A new presentation starts with one blank slide.
import org.aspose.slides.foss.Presentation;
import org.aspose.slides.foss.SaveFormat;
try (Presentation prs = new Presentation()) {
System.out.println("Slides in new presentation: " + prs.getSlides().size());
prs.save("output.pptx", SaveFormat.PPTX);
}Important: Always use try-with-resources or call prs.dispose() in a finally block. Resources will not be released correctly otherwise.
Step 4: Access a Slide
The first slide is at index 0. A blank presentation has exactly one slide.
try (Presentation prs = new Presentation()) {
ISlide slide = prs.getSlides().get(0); // zero-based index
System.out.println("Slide at index 0: " + slide);
prs.save("output.pptx", SaveFormat.PPTX);
}Step 5: Add a Shape
Use slide.getShapes().addAutoShape() to add an AutoShape. The parameters are (shapeType, x, y, width, height) all in points (1 point = 1/72 inch; standard slide is 720 x 540 pt).
import org.aspose.slides.foss.*;
try (Presentation prs = new Presentation()) {
ISlide slide = prs.getSlides().get(0);
// Rectangle at (50, 50) with 400 wide and 120 tall
IAutoShape shape = slide.getShapes().addAutoShape(
ShapeType.RECTANGLE, 50, 50, 400, 120
);
// Attach a text frame
shape.addTextFrame("Hello from Aspose.Slides FOSS!");
prs.save("with-shape.pptx", SaveFormat.PPTX);
}Step 6: Save the Presentation
Call prs.save(path, SaveFormat.PPTX) before the try block exits. PPTX is the only supported output format.
prs.save("result.pptx", SaveFormat.PPTX);The file is written atomically; if an error occurs before this call, no output file is created.
Complete Working Example
The following program creates a two-slide presentation with a title shape on the first slide and a table on the second.
import org.aspose.slides.foss.*;
public class CreatePresentation {
public static void main(String[] args) {
try (Presentation prs = new Presentation()) {
// --- Slide 1: title shape ---
ISlide slide1 = prs.getSlides().get(0);
IAutoShape title = slide1.getShapes().addAutoShape(
ShapeType.RECTANGLE, 40, 40, 640, 80
);
ITextFrame tf = title.addTextFrame("Q1 Results: Executive Summary");
IPortionFormat fmt = tf.getParagraphs().get(0)
.getPortions().get(0).getPortionFormat();
fmt.setFontHeight(32);
fmt.setFontBold(NullableBool.TRUE);
fmt.getFillFormat().setFillType(FillType.SOLID);
fmt.getFillFormat().getSolidFillColor().setColor(
Color.fromArgb(255, 0, 70, 127)
);
// --- Slide 2: table ---
prs.getSlides().addEmptySlide(prs.getLayoutSlides().get(0));
ISlide slide2 = prs.getSlides().get(1);
ITable table = slide2.getShapes().addTable(
40, 40,
new double[]{200.0, 120.0, 120.0},
new double[]{40.0, 40.0, 40.0}
);
String[] headers = {"Region", "Revenue", "Growth"};
String[][] data = {
{"North", "$1.2M", "+8%"},
{"South", "$0.9M", "+4%"},
};
for (int col = 0; col < headers.length; col++) {
table.getRows().get(0).get(col).getTextFrame().setText(headers[col]);
}
for (int row = 0; row < data.length; row++) {
for (int col = 0; col < data[row].length; col++) {
table.getRows().get(row + 1).get(col)
.getTextFrame().setText(data[row][col]);
}
}
prs.save("q1-results.pptx", SaveFormat.PPTX);
}
System.out.println("Saved q1-results.pptx");
}
}Common Issues and Fixes
IllegalStateException: Presentation already disposed
You are calling methods on a Presentation object after the try-with-resources block has exited. Always keep all work inside the try block.
UnsupportedOperationException when saving
This occurs when attempting a save format other than PPTX, or when using an unsupported feature (such as charts or animations).
Shape appears outside the visible slide area
Slides are 720 x 540 points by default. Keep x + width <= 720 and y + height <= 540.
Frequently Asked Questions
What is the default slide size?
A new Presentation() creates slides at the standard 10 x 7.5 inch (720 x 540 point) size. Changing the slide size is not yet supported in this edition.
Can I add more than one slide?
Yes. Call prs.getSlides().addEmptySlide(prs.getLayoutSlides().get(0)) to append a blank slide and access it by index:
prs.getSlides().addEmptySlide(prs.getLayoutSlides().get(0));
ISlide slide2 = prs.getSlides().get(1);Can I open an existing file and add slides?
Yes:
try (Presentation prs = new Presentation("existing.pptx")) {
prs.getSlides().addEmptySlide(prs.getLayoutSlides().get(0));
prs.save("existing.pptx", SaveFormat.PPTX);
}What formats can I save to?
Only SaveFormat.PPTX is supported. Export to PDF, HTML, SVG, or images is not available in this edition.