Adam Shakhabov
Adam Shakhabov

Reputation: 1346

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.

Upvotes: 1

Views: 724

Answers (2)

quangvuk
quangvuk

Reputation: 21

@Adam : You can try this

 // Delete all slides except Slide at Index i


                    int countSlide = CountSlides(filePath + i + ".pptx");  // Count number of slides
                    int newIndex = i; // Slide index that you want to keep it
                    while (1 < countSlide )
                    {
                        // Delete all slides before index i
                        if (newIndex > 0)
                        {
                            DeleteSlide(filePath + i + ".pptx", 0);
                            countSlide--; // Decrease number of slides after you delete it
                            newIndex--;
                        }
                        // Delete all slides after index i
                        else if (newIndex < 0)
                        {
                            DeleteSlide(filePath + i + ".pptx", 1);
                            countSlide--; // Decrease number of slides after you delete it
                            newIndex--;
                        }
                        else newIndex--;

                    }

DeleteSlide() you can find here MSDN: OpenXML Delete a slide in presentation

Upvotes: 0

Alexandru Clonțea
Alexandru Clonțea

Reputation: 1866

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!

Upvotes: 1

Related Questions