How to Save Presentations in Java
Aspose.Slides FOSS for Java saves presentations exclusively to .pptx format using prs.save(path, SaveFormat.PPTX). This guide covers the correct save pattern, saving to a different path, and common save-related errors.
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: Open or Create a Presentation
Always use try-with-resources. The save call must happen inside the try block.
import org.aspose.slides.foss.*;
// Create new
try (Presentation prs = new Presentation()) {
prs.save("new.pptx", SaveFormat.PPTX);
}
// Open existing
try (Presentation prs = new Presentation("input.pptx")) {
prs.save("output.pptx", SaveFormat.PPTX);
}Step 3: Save After All Modifications
Place the save() call as the last statement inside the try block, after all modifications are complete.
import org.aspose.slides.foss.*;
try (Presentation prs = new Presentation()) {
ISlide slide = prs.getSlides().get(0);
IAutoShape shape = slide.getShapes().addAutoShape(
ShapeType.RECTANGLE, 50, 50, 300, 100
);
shape.addTextFrame("Hello, World!");
prs.save("output.pptx", SaveFormat.PPTX);
}Step 4: Save to a Different Path
Pass a different output path to create a new file without modifying the original:
try (Presentation prs = new Presentation("template.pptx")) {
// modify ...
prs.save("customized.pptx", SaveFormat.PPTX);
}The template.pptx file is not modified; customized.pptx is created (or overwritten if it already exists).
Step 5: Verify the Output
After the try block exits, the file is complete and closed. Check it exists:
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
Path output = Paths.get("output.pptx");
System.out.println("Saved: " + Files.exists(output)
+ ", size: " + Files.size(output) + " bytes");Supported Save Format
| Format | Enum Value | Supported |
|---|---|---|
| PPTX (Office Open XML) | SaveFormat.PPTX | Yes |
| N/A | No | |
| HTML | N/A | No |
| SVG | N/A | No |
| PNG / JPEG | N/A | No |
| ODP (OpenDocument) | N/A | No |
Only PPTX is supported. Attempting to save in any other format will raise UnsupportedOperationException.
Common Issues and Fixes
java.io.IOException: Permission denied
The output file is open in another application (e.g., PowerPoint has the file open). Close the file in other applications before saving.
File is created but appears empty or corrupted
Ensure prs.save() is called inside the try block, not after it. After the try-with-resources block exits, the Presentation object is disposed and subsequent calls will fail.
UnsupportedOperationException when saving
This occurs when attempting a save format other than PPTX, or when using an unsupported feature (such as charts or animations) during save.
Frequently Asked Questions
Can I save to the same file I opened?
Yes. Saving to the same path overwrites the original file:
try (Presentation prs = new Presentation("deck.pptx")) {
// modify ...
prs.save("deck.pptx", SaveFormat.PPTX); // overwrites original
}Can I save to an OutputStream?
Yes. The save() method accepts an OutputStream:
import java.io.FileOutputStream;
try (Presentation prs = new Presentation();
FileOutputStream fos = new FileOutputStream("output.pptx")) {
prs.save(fos, SaveFormat.PPTX);
}Does saving preserve content I haven’t modified?
Yes. Unknown XML parts from the original file are preserved verbatim. The library only serializes the parts of the document model it understands, and passes through any XML it does not recognize.