Search code examples
c#ms-officepowerpointopenxmlopenxml-sdk

Removing all slides from presentation except 2 slide using Open XML SDK


Sorry my English :)

I have some presentation

using (PresentationDocument presentationDocument = PresentationDocument.Open(@"sample.pptx", false))
{

}

which contains 10 slides. How I can remove all slides except second and save this result (presentation) in separate .pptx-file?

I explored this sample, but here show how delete one slide and I could not solved my task by that.


Solution

  • Using the methods in the sample you provided, you can just remove the first page and afterwards any pages after the new first. In code:

    private void KeepOnlySecondPage(string presentationFilePath, string onlySecondPageFilePath)
    {
       using (PresentationDocument presentationDocument = PresentationDocument.Open(presentationFilePath, false))
       {
          if (CountSlides(presentationDocument) > 0)
          {
             DeleteSlide(presentationDoucment, 0);
             while (CountSlides(presentationDocument) > 1)
             {
                DeleteSlide(presentationDocument,1);
             }
             presentationDocument.Save(onlySecondPageFilePath);
          }
        }
    }
    

    If you spend a more time studying the samples in the page you linked to and the PresentationDocument class itself, perhaps an easier approach would be to create a new PresentationDocument containing only the second page of the original.

    Also, please edit your question to include the essential parts from the links page. Links die!