3

我正在处理这个例程,我需要以编程方式删除“隐藏”的 PowerPoint 幻灯片。我对 Open XML 了解不多,我修改了一段代码,该代码最初删除了一张幻灯片,其中该方法将幻灯片索引作为参数,如本文所述如何:从演示文稿中删除幻灯片 (Open XML SDK)文章。

但是,我了解到,通过 SlideParts 集合的迭代默认情况下会按照幻灯片最后一次编辑的顺序对幻灯片进行排序,而不是按照它们在演示文稿中出现的顺序。为此,必须按照使用 OpenXml SDK 迭代 SlideParts 文章中的建议遍历 SlideIdList。

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

如果我在循环中使用 SlideIdList,有谁知道如何获得 Show 属性?在代码中查看我的评论。谢谢!里绍。

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;
                    }
                }
            }

        }
    }
4

1 回答 1

3

在我发布此内容一分钟后,我意识到要获得我需要执行此操作的特定幻灯片:SlidePart slidePart = presentationPart.GetPartById(slideRelId) as SlidePart;就在 foreach 循环之前。

于 2012-11-14T17:43:32.660 回答