Screen-cast: Remove Speaker Notes from an Open XML Presentation

When sending a presentation to customers or others outside of your organization, you often want to remove speaker notes.  It is easy enough to use PowerPoint automation to remove the speaker notes, but if you are processing presentations server-side, you probably don’t want to rely on automation to accomplish this task.  Instead, it is easy enough to use the Open XML SDK to remove speaker notes.  The following screen-cast shows how.

In addition to showing how to accomplish the specific task of removing speaker notes, this screen-cast shows my approach to researching Open XML markup.

Here is the entire listing of the sample:

using System;
using System.Collections.Generic;
using System.Linq;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Presentation;
using DocumentFormat.OpenXml.Drawing;
using Presentation = DocumentFormat.OpenXml.Presentation;

class Program
{
   
static void Main(string[] args)
   
{
       
using (PresentationDocument pDoc =
           
PresentationDocument.Open("Test1b.pptx", true))
       
{
           
foreach (var slide in pDoc.PresentationPart.SlideParts)
           
{
               
NotesSlidePart notes = slide.NotesSlidePart;
               
if (notes != null)
               
{
                   
var sp = notes
                       
.NotesSlide
                       
.CommonSlideData
                       
.ShapeTree
                       
.Elements<Presentation.Shape>()
                       
.FirstOrDefault(s => {
                           
var nvSpPr = s.NonVisualShapeProperties;
                           
if (nvSpPr != null)
                           
{
                               
var nvPr = nvSpPr.ApplicationNonVisualDrawingProperties;
                               
if (nvPr != null)
                               
{
                                   
var ph = nvPr.PlaceholderShape;
                                   
if (ph != null)
                                       
return ph.Type == PlaceholderValues.Body;
                               
}
                           
}
                           
return false;
                       
});
                   
if (sp != null)
                   
{
                       
var textBody = sp.TextBody;
                       
if (textBody != null)
                       
{
                           
var firstParagraph = textBody
                               
.Elements<Paragraph>()
                               
.FirstOrDefault();
                           
if (firstParagraph != null)
                           
{
                               
List<Paragraph> subsequentParagraphs = textBody
                                   
.Elements<Paragraph>()
                                   
.Skip(1)
                                   
.ToList();
                                firstParagraph
.RemoveAllChildren();
                               
foreach (var item in subsequentParagraphs)
                                    item
.Remove();
                           
}
                       
}
                   
}
               
}
           
}
       
}
   
}
}