Sitemap

How to Automate PowerPoint Slides in C#: Add, Delete, Copy & Hide

3 min read2 days ago

--

If you are building dynamic reports, automating branded decks, or batch-cleaning a PPT template, you need to programmatically control the slides themselves: add them, delete them, clone them, and toggle visibility. This tutorial shows exactly how to do that in C# using Free Spire.Presentation for .NET — a managed library that works without any Microsoft Office or Interop dependency.

Free edition note: The free version enforces a 10-slide limit per document during read/write. It is suitable for small-to-medium automation tasks.

Quick API Overview

Before the step-by-step walkthrough, here is the core API surface you will use for every C# PowerPoint slide operation:

  • Append blank slide: Slides.Append() — adds at the end.
  • Insert blank slide: Slides.Insert(index) — uses a zero-based index.
  • Copy a slide: Slides.Append(sourceSlide) or Slides.Insert(index, sourceSlide) — preserves layout and formatting.
  • Delete by index: Slides.RemoveAt(index) — validate Count > index first.
  • Delete by reference: Slides.Remove(slideObject).
  • Hide / unhide: slide.Hidden = true / false — does not alter the collection.

The two core types are Presentation (the deck) and ISlide (an individual slide). Presentation implements IDisposable—always wrap it in a using statement when file I/O is involved.

Setup: Install via NuGet

dotnet add package FreeSpire.Presentation

All slide operations flow through Presentation.Slides. When you instantiate a new Presentation, the library adds one default blank slide. To start from zero, remove it first:

using var ppt = new Presentation();
ppt.Slides.RemoveAt(0);

How to Add Slides in C#

There are two entry points for adding blank slides:

ppt.Slides.Append();    // blank slide at the end
ppt.Slides.Insert(1); // blank slide at index 1 (second position)

Neither overload carries over content from an existing slide. If you need content, use the copy overloads in the next section.

How to Delete Slides in C# (and Avoid the Index-Shift Bug)

You can delete by index or by object reference:

ppt.Slides.RemoveAt(0);           // by index
ISlide target = ppt.Slides[2];
ppt.Slides.Remove(target); // by reference

Critical for batch deletion: iterate in reverse order. A forward loop calls RemoveAt, which shifts the indices of all remaining elements—causing skipped slides or ArgumentOutOfRangeException.

for (int i = ppt.Slides.Count - 1; i >= 0; i--)
{
if (ShouldDelete(ppt.Slides[i]))
ppt.Slides.RemoveAt(i);
}

Always check ppt.Slides.Count > index before any index-based removal.

How to Copy Slides in C# (Same Document or Cross-Document)

Copying is achieved by passing an existing ISlide into Append or Insert. Layout, images, and formatting are retained.

Get StackDepth’s stories in your inbox

Join Medium for free to get updates from this writer.

Within the same presentation:

ISlide source = ppt.Slides[0];
ppt.Slides.Append(source); // clone at the end
ppt.Slides.Insert(2, source); // clone at index 2

Across presentations: no conversion step is needed. Pass the source ISlide directly into the target collection.

using var sourcePpt = new Presentation();
sourcePpt.LoadFromFile("source.pptx");

using var targetPpt = new Presentation();
targetPpt.LoadFromFile("target.pptx");
ISlide toCopy = sourcePpt.Slides[0];
targetPpt.Slides.Insert(0, toCopy);

From a code-shape perspective, “copy” and “insert blank” differ only by whether the second argument is an ISlide.

How to Hide Slides in C#

ISlide exposes a Hidden Boolean property. It behaves exactly like PowerPoint’s “Hide Slide” feature: the slide is skipped during the slideshow but remains visible and editable in the editor. Because it does not change the Slides collection structure, no reverse iteration is required for batch hiding.

ppt.Slides[1].Hidden = true;   // hide slide 2
ppt.Slides[1].Hidden = false; // unhide

Summary

This covers the four slide-level operations you will need in most C# PowerPoint automation pipelines: add, delete, copy, and hide, all without an Office installation. Use the API cheat sheet for quick reference, remember the reverse-iteration rule for deletion.

--

--

StackDepth
StackDepth

Written by StackDepth

0 followers

Full-stack developer work across .NET, Python, JavaScript, and React, building anything from back-end services to interactive UIs.