← Back to list

Add or Remove PowerPoint Slides with C#

When generating reports in batches, dynamically assembling presentations, or automating PPT templates, programmatically adding or removing…

NateBennett · 2026-05-26 05:48 · 0 claps · 2.4 min read
#csharp #powerpoint #dotnet
Open on Medium ↗
Wiki topics: DIG · Digital Marketing 🥊 · Combat Sports

Add or Remove PowerPoint Slides with C

When generating reports in batches, dynamically assembling presentations, or automating PPT templates, programmatically adding or removing slides is almost unavoidable. This article provides a ready-to-use solution using the free library Free Spire.Presentation for .NET.

Key advantages: No Microsoft Office dependency — pure managed code, suitable for server-side automation.

1. Quick Setup

Search for FreeSpire.Presentation in NuGet, or run:

PM> Install-Package FreeSpire.Presentation

Add the namespace at the top of your code

using Spire.Presentation;

2. Core API Overview

In Free Spire.Presentation, slide management revolves around the Presentation class and the Slides collection:

  • Presentation.Slides.Append() – Adds a blank slide at the end.
  • Presentation.Slides.Insert(index) – Inserts a blank slide at the specified index.
  • Presentation.Slides.Insert(index, ISlide slide) – Copies an existing slide and inserts it at the given index.
  • Presentation.Slides.RemoveAt(index) – Removes a slide by its index.
  • Presentation.Slides.Remove(ISlide value) – Removes a slide by object reference.

3. Adding Slides in PowerPoint

3.1 Create a New PPT and Remove the Default Placeholder Slide

When you create a new Presentation instance, it automatically includes one blank slide. To build a completely custom slide collection, remove it first:

using Spire.Presentation;
Presentation ppt = new Presentation();   // Contains one blank slide by default
ppt.Slides.RemoveAt(0);                  // Remove the default slide
// Now add your own slides as needed

3.2 Append a Slide at the End

Presentation ppt = new Presentation();
ppt.LoadFromFile("template.pptx");

// Append a blank slide after the last slide
ppt.Slides.Append();
ppt.SaveToFile("output.pptx", FileFormat.Pptx2019);

Make sure the file path in LoadFromFile points to a valid PPT file.

3.3 Insert a Slide at a Specific Position

Presentation ppt = new Presentation();
ppt.LoadFromFile("sample.pptx");

// Insert a blank slide as the second slide (index 0 = first slide)
ppt.Slides.Insert(1);
ppt.SaveToFile("inserted.pptx", FileFormat.Pptx2019);

3.4 Copy an Existing Slide and Insert It

You can copy a slide from the same or a different presentation — all content, layout, images, and formatting are preserved.

// Scenario 1: Copy within the same document
Presentation ppt = new Presentation();
ppt.LoadFromFile("source.pptx");
ISlide sourceSlide = ppt.Slides[0];
ppt.Slides.Append(sourceSlide);          // Copy to the end
ppt.Slides.Insert(2, sourceSlide);       // Copy and insert at index 2
ppt.SaveToFile("copied.pptx", FileFormat.Pptx2019);

// Scenario 2: Copy across documents
Presentation sourcePpt = new Presentation();
sourcePpt.LoadFromFile("source.pptx");
Presentation targetPpt = new Presentation();
targetPpt.LoadFromFile("target.pptx");
ISlide slideToCopy = sourcePpt.Slides[0];
targetPpt.Slides.Insert(0, slideToCopy); // Insert at the beginning of the target
targetPpt.SaveToFile("merged.pptx", FileFormat.Pptx2019);

4. Deleting Slides from PowerPoint

4.1 Delete by Index

Presentation ppt = new Presentation();
ppt.LoadFromFile("sample.pptx");

// Delete the first slide (index 0)
ppt.Slides.RemoveAt(0);
ppt.SaveToFile("deleted.pptx", FileFormat.Pptx2019);

⚠️ Validate the index: Check that ppt.Slides.Count is greater than the index to avoid ArgumentOutOfRangeException.

4.2 Delete by Object Reference

ISlide targetSlide = ppt.Slides[2];
ppt.Slides.Remove(targetSlide);

4.3 Delete Multiple Slides — Iterate Backwards

When deleting slides while iterating, always loop in reverse to avoid index shifts:

// Delete all slides from the end
for (int i = ppt.Slides.Count - 1; i >= 0; i--)
{
    ppt.Slides.RemoveAt(i);
}

5. Important Notes and Best Practices

  1. Zero-based indexing: Slides[0] is the first slide.
  2. Resource management: Presentation implements IDisposable. Use using statements to ensure proper disposal – especially critical in web apps or high‑frequency backend services.
  3. Error handling: Wrap file operations in try-catch blocks to handle missing files, permission issues, etc.
  4. Format compatibility: Use FileFormat.Pptx2019 for broad compatibility with modern PowerPoint. Older versions can use PPT.
  5. Free edition limitation: The free version limits processing to 10 slides per document. Evaluate whether this fits your use case.

6. Summary

With the examples above, you can dynamically add and remove PowerPoint slides in fewer than 10 lines of code — dramatically improving document automation efficiency. The free library’s API is clean, intuitive, and closely mirrors PowerPoint’s native object model, making it easy for developers to get started.

For more complex scenarios (e.g., cross‑document copying while preserving formatting, batch operations by section), you can extend these examples by calling additional API methods.


메타데이터
post_id
9048c2d0f59d
slug
add-or-remove-powerpoint-slides-with-c-9048c2d0f59d
url
https://medium.com/@natebennett2/add-or-remove-powerpoint-slides-with-c-9048c2d0f59d
canonical_url
https://medium.com/@natebennett2/add-or-remove-powerpoint-slides-with-c-9048c2d0f59d
author_url
https://medium.com/@natebennett2
status
ok
fetched_at
2026-06-09 15:37:30