Recursive AI

Extract PowerPoint slides to a new PPTX in C#

OneDocument is a free, open-source Word, Excel and PowerPoint document library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

Download includes review.pptx.

Terminal
dotnet add package RecursiveAI.OneDocument.Presentations

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

C#
using RecursiveAI.OneDocument.Presentations;
using var deck =
    Presentation.Open("review.pptx");
using var excerpt = deck.ExtractSlides(1, 2);
excerpt.SaveAs("slides-2-3.pptx");

ExtractSlides(start, count) uses a zero-based start index and a slide count, so (1, 2) means slides 2–3.

Writes slides-2-3.pptx with Sales and Costs; prints nothing.

The example is the full program below: unzip it and dotnet run. To run only the code above, paste it into Program.cs of a new dotnet new console project and dotnet run.

The full program

In review.pptx, the slides are Agenda, Sales, Costs and Next steps. The full program opens the saved file and prints the slide count and text.

C#
using RecursiveAI.OneDocument.Presentations;
 
using var deck = Presentation.Open("review.pptx");
using var excerpt = deck.ExtractSlides(start: 1, count: 2);
excerpt.SaveAs("slides-2-3.pptx");
 
using var saved = Presentation.Open("slides-2-3.pptx");
Console.WriteLine($"Slides: {saved.Slides.Count}");
foreach (var slide in saved.Slides)
    foreach (var shape in slide.Shapes)
        if (!string.IsNullOrEmpty(shape.Text))
            Console.WriteLine(shape.Text);
Text
Slides: 2
Sales
Costs

An empty range or a range outside the presentation throws ArgumentOutOfRangeException before the example saves anything.

Variations

Split the whole deck into two-slide presentations. If the deck has an odd number of slides, the last file has one slide.

C#
using RecursiveAI.OneDocument.Presentations;
using var deck = Presentation.Open("review.pptx");
var parts = deck.SplitEvery(2);
try
{
    for (int i = 0; i < parts.Count; i++)
        parts[i].SaveAs($"part-{i + 1}.pptx");
}
finally
{
    foreach (var part in parts)
        part.Dispose();
}

Writes part-1.pptx (Agenda and Sales) and part-2.pptx (Costs and Next steps).

The short answer

Call ExtractSlides(start, count) on the source presentation, then call SaveAs on the returned presentation. To save slides 2 through 3, pass start: 1, count: 2. Use a different filename to keep the original PPTX on disk.

How do I extract multiple slides from PowerPoint?

Use one call for a contiguous range. If someone asks for slide numbers 4 through 7, the first index is 4 - 1 and the count is 7 - 4 + 1. The second argument is a count, not the ending index. For one slide, pass 1 as the count. To break up the whole deck into regular batches rather than selecting one range, use SplitEvery as shown above.

ExtractSlides returns a separate presentation in memory and leaves the original slides in place. Dispose the returned presentation when you finish saving it. This method doesn’t select nonadjacent slides such as 2 and 5 in one call, and it doesn’t insert the extracted slides into an existing presentation.

How do I copy PowerPoint slides without losing their formatting?

When the destination is a new PPTX, slide extraction starts with a copy of the source deck. It deletes slides outside the requested range and prunes unused masters. A slide master is the shared design for a set of slide layouts. The new deck keeps the masters used by the selected slides, including those masters’ layouts. The extracted slides don’t have to switch to a layout from a new blank presentation.

The retained slides keep their speaker notes and the parts they use. Resources used only by the removed slides or unused masters can be removed. If the job is to add slides to a deck that already exists, use InsertSlides instead; there you can choose between source and destination formatting.

All onedocument recipes