Add Slides to Presentations in Java
Add a Slide to a Presentation
Aspose.Slides for Java allows developers to add empty slides to their presentation. To add an empty slide in the presentation, please follow the steps below:
- Create an instance of Presentation class.
- Instantiate ISlideCollection class by setting a reference to the Slides (collection of content Slide objects) property exposed by the Presentation object.
- Add an empty slide to the presentation at the end of the content slides collection by calling the addEmptySlide methods exposed by ISlideCollection object.
- Do some work with the newly added empty slide.
- Finally, write the presentation file using the Presentation object.
// Instantiate Presentation class that represents the presentation file
Presentation pres = new Presentation();
try {
// Instantiate SlideCollection calss
ISlideCollection slds = pres.getSlides();
for (int i = 0; i < pres.getLayoutSlides().size(); i++) {
// Add an empty slide to the Slides collection
slds.addEmptySlide(pres.getLayoutSlides().get_Item(i));
}
// Do some work on the newly added slide
// Save the PPTX file to the Disk
pres.save("EmptySlide.pptx", SaveFormat.Pptx);
} finally {
pres.dispose();
}
FAQ
Can I insert a new slide at a specific position, not just at the end?
Yes. The library supports slide collections and insert/clone operations, so you can add a slide at the required index rather than only at the end.
Are the theme/styles preserved when adding a slide based on a layout?
Yes. A layout inherits formatting from its master, and the new slide inherits from the selected layout and its associated master.
Which slide is present in a new “empty” presentation before adding slides?
A newly created presentation already contains one blank slide with index zero. This is important to consider when calculating insertion indices.
How do I choose the “right” layout for a new slide if the master has many options?
Generally choose the LayoutSlide that matches the required structure (Title and Content, Two Content, etc.). If such a layout is missing, you can add it to the master and then use it.