使用Open XML SDK对幻灯片进行排序和删除

时间:2012-11-14 17:28:34

标签: c# openxml openxml-sdk

我正在编写此例程,在编程中我需要删除隐藏"隐藏的PowerPoint幻灯片。对Open XML不太了解我已经修改了一段代码,该代码最初删除了一个幻灯片,其中该方法将幻灯片索引作为参数,如本How to: Delete a slide from a presentation (Open XML SDK)文章中所述。

但是我已经了解到,默认情况下,通过SlideParts集合进行迭代将按照上次编辑的顺序对幻灯片进行排序,而不是按照它们在演示文稿中出现的顺序排序。因此,必须按照Iterating of SlideParts with the OpenXml SDK 文章中的建议迭代SlideIdList。

在我的代码中包含一个遍历SlideList的foreach循环,我需要获取幻灯片的Show属性以获取隐藏幻灯片的索引。

如果我在循环中使用SlideIdList,是否有人知道如何获取Show属性?在代码中查看我的评论。谢谢! 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;
                    }
                }
            }

        }
    }

1 个答案:

答案 0 :(得分:2)

我发布这篇文章后一分钟,我意识到要获得特定的幻灯片,我需要这样做:SlidePart slidePart = presentationPart.GetPartById(slideRelId) as SlidePart;就在foreach循环之前。