Working with Sections in PowerPoint Library

3 Aug 202613 minutes to read

Sections helps to manage the slides of a PowerPoint presentation. If a presentation has many slides, you can organize the slides using sections to make the navigation easier.

Creating a section

Adding a new slide to a section

The following code example demonstrates how to add a blank slide to a section.

//Creates a PowerPoint presentation
IPresentation pptxDoc = Presentation.Create();
//Adds a section to the PowerPoint presentation
ISection section = pptxDoc.Sections.Add();
//Sets a name to the created section
section.Name = "SectionDemo";
//Adds a slide to the created section
ISlide slide = section.AddSlide(SlideLayoutType.Blank);
//Adds a text box to the slide
slide.AddTextBox(10, 10, 100, 100).TextBody.AddParagraph("Slide in SectionDemo");
//Saves the PowerPoint presentation
pptxDoc.Save("Output.pptx");
//Closes the PowerPoint presentation
pptxDoc.Close();
//Creates a PowerPoint presentation
IPresentation pptxDoc = Presentation.Create();
//Adds a section to the PowerPoint presentation
ISection section = pptxDoc.Sections.Add();
//Sets a name to the created section
section.Name = "SectionDemo";
//Adds a slide to the created section
ISlide slide = section.AddSlide(SlideLayoutType.Blank);
//Adds a text box to the slide
slide.AddTextBox(10, 10, 100, 100).TextBody.AddParagraph("Slide in SectionDemo");
//Saves the PowerPoint presentation
pptxDoc.Save("Output.pptx");
'Creates a PowerPoint presentation
Dim pptxDoc As IPresentation = Presentation.Create()
'Adds a section to the PowerPoint presentation
Dim section As ISection = pptxDoc.Sections.Add()
'Sets a name to the created section
section.Name = "SectionDemo"
'Adds a slide to the created section
Dim slide As ISlide = section.AddSlide(SlideLayoutType.Blank)
'Adds a text box to the slide
slide.AddTextBox(10, 10, 100, 100).TextBody.AddParagraph("Slide in SectionDemo")
'Saves the PowerPoint presentation
pptxDoc.Save("Output.pptx")

You can download a complete working sample from GitHub.

Adding an existing slide to a section

The following code example demonstrates how to add an existing slide to a section.

//Loads or open an PowerPoint Presentation
IPresentation pptxDoc = Presentation.Open("PPTXWithSections.PPTX");
//Creates a new section in the PowerPoint presentation
pptxDoc.Sections.Add();
//Moves the first slide to the created section
pptxDoc.Slides[0].MoveToSection(0);
//Saves the PowerPoint presentation
pptxDoc.Save("Output.pptx");
//Closes the PowerPoint presentation
pptxDoc.Close();
//Loads a PowerPoint presentation
IPresentation pptxDoc = Presentation.Open("PPTXWithoutSection.PPTX");
//Creates a new section in the PowerPoint presentation
pptxDoc.Sections.Add();
//Moves the first slide to the created section
pptxDoc.Slides[0].MoveToSection(0);
//Saves the PowerPoint presentation
pptxDoc.Save("Output.pptx");
'Loads a PowerPoint presentation
Dim pptxDoc As IPresentation = Presentation.Open("PPTXWithoutSection.PPTX")
'Creates a new section in the PowerPoint presentation
pptxDoc.Sections.Add()
'Moves the first slide to the created section
pptxDoc.Slides(0).MoveToSection(0)
'Saves the PowerPoint presentation
pptxDoc.Save("Output.pptx")

You can download a complete working sample from GitHub.

Inserting a section

The following code example demonstrates how to insert a section in a template PowerPoint presentation that contains sections

//Load a PowerPoint presentation.
IPresentation pptxDoc = Presentation.Open("PPTXWithSections.PPTX");
//Create a new section to Insert.
ISection section = pptxDoc.Sections.Add();
//Names the created section.
section.Name = "InsertedSection";
//Insert the section at second position.
pptxDoc.Sections.Insert(1, section);
//Remove the unwanted created section.
pptxDoc.Sections.RemoveAt(pptxDoc.Sections.Count - 1);
//Saves the PowerPoint presentation
pptxDoc.Save("Output.pptx");
//Closes the PowerPoint presentation
pptxDoc.Close();
//Load a PowerPoint presentation.
IPresentation pptxDoc = Presentation.Open("PPTXWithSections.PPTX");
//Create a new section to Insert.
ISection section = pptxDoc.Sections.Add();
//Names the created section.
section.Name = "InsertedSection";
//Insert the section at second position.
pptxDoc.Sections.Insert(1, section);
//Removes the unwanted section appended by Add() (now duplicated at the end).
pptxDoc.Sections.RemoveAt(pptxDoc.Sections.Count - 1);
//Saves the PowerPoint presentation
pptxDoc.Save("Output.pptx");
'Load a PowerPoint presentation.
Dim pptxDoc As IPresentation = Presentation.Open("PPTXWithSections.PPTX")
'Create a new section to Insert.
Dim section As ISection = pptxDoc.Sections.Add()
'Names the created section.
section.Name = "InsertedSection"
'Insert the section at second position.
pptxDoc.Sections.Insert(1, section)
'Remove the unwanted created section.
pptxDoc.Sections.RemoveAt(pptxDoc.Sections.Count - 1)
'Saves the PowerPoint presentation
pptxDoc.Save("Output.pptx")

You can download a complete working sample from GitHub.

Moving the sections within a PowerPoint presentation

You can move the sections within a PowerPoint presentation. The following code example demonstrates how to move a section to a specific position in the navigation pane.

//Loads or open an PowerPoint Presentation
IPresentation pptxDoc = Presentation.Open("PPTXWithSections.PPTX");
//Moves the second section to third position within the PowerPoint presentation.
pptxDoc.Sections[2].Move(3);
//Saves the PowerPoint presentation
pptxDoc.Save("Output.pptx");
//Closes the PowerPoint presentation
pptxDoc.Close();
//Loads a PowerPoint presentation
IPresentation pptxDoc = Presentation.Open("PPTXWithSections.PPTX");
//Moves the second section to third position within the PowerPoint presentation.
pptxDoc.Sections[2].Move(3);
//Saves the PowerPoint presentation
pptxDoc.Save("Output.pptx");
'Loads a PowerPoint presentation
Dim pptxDoc As IPresentation = Presentation.Open("PPTXWithSections.PPTX")
'Moves the second section to third position within the PowerPoint presentation.
pptxDoc.Sections(2).Move(3)
'Saves the PowerPoint presentation
pptxDoc.Save("Output.pptx")

You can download a complete working sample from GitHub.

Moving a slide within sections

The following code example demonstrates how to move a slide from one section to another.

//Loads or open an PowerPoint Presentation
FileStream inputStream = new FileStream("PPTXWithSections.PPTX",FileMode.Open);
IPresentation pptxDoc = Presentation.Open("PPTXWithSections.PPTX");
//Gets the first slide of second section in the PowerPoint presentation
ISlide slide = pptxDoc.Sections[1].Slides[0];
//Moves the slide to the first section
slide.MoveToSection(0);
//Saves the PowerPoint presentation
pptxDoc.Save("Output.pptx");
//Closes the PowerPoint presentation
pptxDoc.Close();
//Loads a PowerPoint presentation
IPresentation pptxDoc = Presentation.Open("PPTXWithSections.PPTX");
//Gets the first slide of second section in the PowerPoint presentation
ISlide slide = pptxDoc.Sections[1].Slides[0];
//Moves the slide to the first section
slide.MoveToSection(0);
//Saves the PowerPoint presentation
pptxDoc.Save("Output.pptx");
'Loads a PowerPoint presentation
Dim pptxDoc As IPresentation = Presentation.Open("PPTXWithSections.PPTX")
'Gets the first slide of second section in the PowerPoint presentation
Dim slide As ISlide = pptxDoc.Sections(1).Slides(0)
'Moves the slide to the first section
slide.MoveToSection(0)
'Saves the PowerPoint presentation
pptxDoc.Save("Output.pptx")

You can download a complete working sample from GitHub.

Cloning slides from a section

The following code example demonstrates how to clone the slide collection of a section and add those slides to a destination presentation.

//Loads or open an PowerPoint Presentation
FileStream inputStream = new FileStream("PPTXWithSections.PPTX",FileMode.Open);
IPresentation pptxDoc = Presentation.Open("PPTXWithSections.PPTX");
//Clones the slides in 3rd section
ISlides slides = pptxDoc.Sections[2].Clone();
//Creates a destination PowerPoint presentation instance. Existing presentations can also be used here.
IPresentation destinationPptx = Presentation.Create();
//Iterates the cloned slides and adds them to the destination presentation
foreach (ISlide slide in slides)
    destinationPptx.Slides.Add(slide);
//Saves the destination PowerPoint presentation
destinationPptx.Save("Output.pptx");
//Closes the PowerPoint presentations
sourcePptx.Close();
destinationPptx.Close();
//Loads the source PowerPoint presentation
IPresentation sourcePptx = Presentation.Open("PPTXWithSections.PPTX");
//Clones the slides in the third section
ISlides slides = sourcePptx.Sections[2].Clone();
//Creates a destination PowerPoint presentation instance. Existing presentations can also be used here.
IPresentation destinationPptx = Presentation.Create();
//Iterates the cloned slides and adds them to the destination presentation
foreach (ISlide slide in slides)
    destinationPptx.Slides.Add(slide);
//Saves the destination PowerPoint presentation
destinationPptx.Save("Output.pptx");
'Loads the source PowerPoint presentation
Dim sourcePptx As IPresentation = Presentation.Open("PPTXWithSections.PPTX")
'Clones the slides in the third section
Dim slides As ISlides = sourcePptx.Sections(2).Clone()
'Creates a destination PowerPoint presentation instance. Existing presentations can also be used here.
Dim destinationPptx As IPresentation = Presentation.Create()
'Iterates the cloned slides and adds them to the destination presentation
For Each slide As ISlide In slides
    destinationPptx.Slides.Add(slide)
Next
'Saves the destination PowerPoint presentation
destinationPptx.Save("Output.pptx")

You can download a complete working sample from GitHub.

Removing a section

The following code example demonstrates how to remove a particular section from the sections collection of a presentation. Removing a section does not delete the slides it contains; the slides are moved to the default (unnamed) section.

//Loads a PowerPoint presentation
IPresentation pptxDoc = Presentation.Open("PPTXWithSections.PPTX");
//Removes the second section from the PowerPoint presentation
pptxDoc.Sections.Remove(pptxDoc.Sections[1]);
//Saves the PowerPoint presentation
pptxDoc.Save("Output.pptx");
//Closes the PowerPoint presentation
pptxDoc.Close();
//Loads a PowerPoint presentation
IPresentation pptxDoc = Presentation.Open("PPTXWithSections.PPTX");
//Removes the second section from the PowerPoint presentation
pptxDoc.Sections.Remove(pptxDoc.Sections[1]);
//Saves the PowerPoint presentation
pptxDoc.Save("Output.pptx");
'Loads a PowerPoint presentation
Dim pptxDoc As IPresentation = Presentation.Open("PPTXWithSections.PPTX")
'Removes the second section from the PowerPoint presentation
pptxDoc.Sections.Remove(pptxDoc.Sections(1))
'Saves the PowerPoint presentation
pptxDoc.Save("Output.pptx")

You can download a complete working sample from GitHub.

Remove all sections

The following code example demonstrates how to remove the section collection from an existing PowerPoint presentation. All slides remain in the presentation as part of the default (unnamed) section.

//Loads a PowerPoint presentation
IPresentation pptxDoc = Presentation.Open("PPTXWithSections.PPTX");
//Removes all sections from the PowerPoint presentation
pptxDoc.Sections.Clear();
//Saves the PowerPoint presentation
pptxDoc.Save("Output.pptx");
//Closes the PowerPoint presentation
pptxDoc.Close();
//Loads a PowerPoint presentation
IPresentation pptxDoc = Presentation.Open("PPTXWithSections.PPTX");
//Removes all sections from the PowerPoint presentation
pptxDoc.Sections.Clear();
//Saves the PowerPoint presentation
pptxDoc.Save("Output.pptx");
'Loads a PowerPoint presentation
Dim pptxDoc As IPresentation = Presentation.Open("PPTXWithSections.PPTX")
'Removes all sections from the PowerPoint presentation
pptxDoc.Sections.Clear()
'Saves the PowerPoint presentation
pptxDoc.Save("Output.pptx")

You can download a complete working sample from GitHub.