Risho
Risho

Reputation: 2647

Sorting and deleting slides using Open XML SDK

I'm working on this routine where programatically I need to remove PowerPoint slides that are "hidden". Not knowing much about Open XML I've modified a piece of code that originaly deletes a slide where the method takes a slide index as a paramter as describbed in this How to: Delete a slide from a presentation (Open XML SDK) article.

However I've learned that the iterating through SlideParts collection will by default sort the slide in order they were last edited and not in the order they appear in the presentation. For that one must iterate through a SlideIdList as suggested in the Iterating of SlideParts with the OpenXml SDK article.

In my code which include a foreach loop that iterates through the SlideList, I need to get the slide's Show property in order to get the index of the hidden slide.

Does any one knows how I can get the Show property if I'm using a SlideIdList in my loop? See my comments in code. Thank you! Risho.

public static void DeleteSlide(string presentationFile)
    {
        using (PresentationDocument presentationDocument = PresentationDocument.Open(presentationFile, true))
        {
            // Get the presentation part from the presentation document. 
            PresentationPart presentationPart = presentationDocument.PresentationPart;

            // Get the presentation from the presentation part.
            Presentation presentation = presentationPart.Presentation;

            // Get the list of slide IDs in the presentation.
            SlideIdList slideIdList = presentation.SlideIdList;

            int slideIdx = -1;
            foreach (SlideId _slideId in presentation.SlideIdList)
            {
                slideIdx++;

                string relId = _slideId.RelationshipId.Value;

  >>>>>         // Here is where I need to checkf for Slide.Show.HasValue as
                // as the code suggests but this property belongs to a 
                // presentationDocument.PresentationPart.SlideParts object as in
                // foreach(Slide slide in presentationDocument.PresentationPart.SlideParts.

                if (slide.Slide.Show != null)
                {
                    if (slide.Slide.Show.HasValue != null)
                    {


                        // Pass the presentation to the next CountSlide method
                        // and return the slide count.
                        //return CountSlides(presentationDocument);


                        // Get the slide ID of the specified slide
                        SlideId slideId = slideIdList.ChildElements[slideIdx] as SlideId;

                        // Get the relationship ID of the slide.
                        string slideRelId = slideId.RelationshipId;

                        // Remove the slide from the slide list.
                        slideIdList.RemoveChild(slideId);

                        // Removed code that looks for a custom presentation

                        // Save the modified presentation.
                        presentation.Save();

                        // Get the slide part for the specified slide.
                        SlidePart slidePart = presentationPart.GetPartById(slideRelId) as SlidePart;

                        // Remove the slide part.
                        presentationPart.DeletePart(slidePart);
                        break;
                    }
                }
            }

        }
    }

Upvotes: 4

Views: 2278

Answers (1)

Risho
Risho

Reputation: 2647

A minute after I've posted this I realized that to get the specific slide I needed to do this: SlidePart slidePart = presentationPart.GetPartById(slideRelId) as SlidePart; just before the foreach loop.

Upvotes: 4

Related Questions