Access Slide in Presentation
Access Slides in Presentation
Aspose.Slides for Java provides Presentation class that can be used to find and access any desired slide present in the presentation. Currently, developers can access a slide in two ways:
- Accessing Slide by Index
- Accessing Slide by ID
Access Slide by Index
Presentation class represents a presentation file and exposes all slides in it as a ISlideCollection collection (that is a collection of ISlide objects). All of these slides can be accessed from this Slides collection using a slide index as shown below in the example.
// Instantiate a Presentation object that represents a presentation file
Presentation pres = new Presentation("demo.pptx");
try {
// Accessing a slide using its slide index
ISlide slide = pres.getSlides().get_Item(0);
} finally {
pres.dispose();
}
Access Slide by ID
Every slide in the presentation has a unique ID associated with it. The Presentation class exposes the getSlideById(id) method that can be used to access the slide by ID. All you need to do is to provide the valid slide ID and access that slide using getSlideById(id) method as shown below in the example.
// Instantiate a Presentation object that represents a presentation file
Presentation pres = new Presentation("demo.pptx");
try {
// Getting Slide ID
int id = (int) pres.getSlides().get_Item(0).getSlideId();
// Accessing Slide by ID
IBaseSlide slide = pres.getSlideById(id);
} finally {
pres.dispose();
}
Change Slide Position
It’s very simple to change the position of a slide in the presentation. Just follow the steps below:
- Create an instance of Presentation class.
- Obtain the reference of a slide by using its Index.
- Change the SlideNumber of the referenced slide.
- Write the modified presentation file.
In the example given below, we have changed the position of a slide (lying at the zero index position 1) of the presentation) to index 1 (Position 2).
// Instantiate Presentation class to load the source presentation file
Presentation pres = new Presentation("Presentation.pptx");
try {
// Get the slide whose position is to be changed
ISlide sld = pres.getSlides().get_Item(0);
// Set the new position for the slide
sld.setSlideNumber(2);
// Write the presentation to disk
pres.save("helloworld_Pos.pptx", SaveFormat.Pptx);
} finally {
pres.dispose();
}
The example above moves the slide (that was at position 1 to the second position and the slide that was at a second position, is moved to the first position and so on. In this way, all slides are adjusted automatically by Aspose.Slides for Java.
Set Slide Number
The new methods added to Presentation class allow to get or to set the number of the first slide in a presentation. When a new FirstSlideNumber value is specified all slide numbers are recalculated. In order to get or set the Slide Number, please follow the steps below:
- Create an instance of Presentation class
- Get the slide number
- Set the slide number
- Write the presentation as a PPTX file In the example given below, we have get and set the slide number.
// Instantiate a Presentation object that represents a presentation file
Presentation pres = new Presentation("HelloWorld.pptx");
try {
// Get the slide number
int firstSlideNumber = pres.getFirstSlideNumber();
// Set the slide number
pres.setFirstSlideNumber(10);
pres.save("Set_Slide_Number_out.pptx", SaveFormat.Pptx);
} finally {
pres.dispose();
}