Access Slide in Presentation

Aspose.Slides allows you to access slides in two ways: by index and by ID.

Access Slide by Index

All slides in a presentation are arranged numerically based on the slide position starting from 0. The first slide is accessible through index 0; the second slide is accessed through index 1; etc.

The Presentation class, representing a presentation file, exposes all slides as an ISlideCollection collection (collection of ISlide objects). This C++ code shows you how to access a slide through its index:

	// The path to the documents directory.
	const String templatePath = u"../templates/AddSlides.pptx";

	// Instantiates the Presentation class
	SharedPtr<Presentation> pres = MakeObject<Presentation>(templatePath);

	// Get a slide's reference through its index
	SharedPtr<ISlide> slide = pres->get_Slides()->idx_get(0);

Access Slide by ID

Each slide in a presentation has a unique ID associated with it. You can use the GetSlideById() method (exposed by the Presentation class) to target that ID. This C++ code shows you how to provide a valid slide ID and access that slide through the GetSlideById() method:

	// The path to the documents directory.
	const String templatePath = u"../templates/AddSlides.pptx";

	// Instantiates the Presentation class
	SharedPtr<Presentation> pres = MakeObject<Presentation>(templatePath);

	// Gets a slide ID
	int id = pres->get_Slides()->idx_get(0)->get_SlideId();

	// Accesses the slide through its ID
	SharedPtr<IBaseSlide> slide = pres->GetSlideById(id);

Change Slide Position

Aspose.Slides allow you to change a slide position. For example, you can specify that the first slide should become the second slide.

  1. Create an instance of the Presentation class.
  2. Get the slide’s reference (whose position you want to change) through its index
  3. Set a new position for the slide through the set_SlideNumber() property.
  4. Save the modified presentation.

This C++ code demonstrates an operation in which the slide in position 1 is moved to position 2:

	// The path to the documents directory.
	const String templatePath = u"../templates/AddSlides.pptx";
	const String outPath = u"../out/ChangeSlidePosition.pptx";

	// Instantiates the Presentation class
	SharedPtr<Presentation> pres = MakeObject<Presentation>(templatePath);

	// Gets the slide whose position will be changed
	SharedPtr<ISlide> slide = pres->get_Slides()->idx_get(0);

	// Sets the new position for the slide
	slide->set_SlideNumber(2);

	// Saves the modified presentation
	pres->Save(outPath, Aspose::Slides::Export::SaveFormat::Pptx);

The first slide became the second; the second slide became the first. When you change a slide’s position, other slides are automatically adjusted.

Set Slide Number

Using the set_FirstSlideNumber() property (exposed by the Presentation class), you can specify a new number for the first slide in a presentation. This operation causes other slide numbers to be recalculated.

  1. Create an instance of the Presentation class.
  2. Get the slide number.
  3. Set the slide number.
  4. Save the modified presentation.

This C++ code demonstrates an operation where the first slide number is set to 10:

	// The path to the documents directory.
	const String outPath = u"../out/SetSlideNumber_out.pptx";
	const String templatePath = u"../templates/AccessSlides.pptx";

	//Instantiates the Presentation class
	SharedPtr<Presentation> pres = MakeObject<Presentation>(templatePath);

	// Gets the slide number
	int firstSlideNumber = pres->get_FirstSlideNumber();

	// Sets the slide number
	pres->set_FirstSlideNumber(2);
	
	// Saves the modified presentation
	pres->Save(outPath, Aspose::Slides::Export::SaveFormat::Pptx);

If you prefer to skip the first slide, you can start the numbering from the second slide (and hide the numbering for the first slide) this way:

auto presentation = System::MakeObject<Presentation>();

auto layoutSlide = presentation->get_LayoutSlides()->GetByType(SlideLayoutType::Blank);

auto slides = presentation->get_Slides();
slides->AddEmptySlide(layoutSlide);
slides->AddEmptySlide(layoutSlide);
slides->AddEmptySlide(layoutSlide);

// Sets the number for the first presentation slide
presentation->set_FirstSlideNumber(0);

// Shows slide numbers for all slides
presentation->get_HeaderFooterManager()->SetAllSlideNumbersVisibility(true);

// Hides the slide number for the first slide
slides->idx_get(0)->get_HeaderFooterManager()->SetSlideNumberVisibility(false);

// Saves the modified presentation
presentation->Save(u"output.pptx", SaveFormat::Pptx);