按顺序枚举PowerPoint幻灯片

时间:2013-02-19 17:34:51

标签: c#-4.0 openxml-sdk powerpoint-2010

我正在尝试使用OpenXML SDK 2.0分析现有的PowerPoint 2010 .pptx文件。

我想要实现的目标是

  • 按顺序枚举幻灯片(因为它们出现在PPTX中)
  • 从每张幻灯片中提取所有文本位

我已经开始并且到目前为止 - 我可以枚举SlideParts中的PresentationPart - 但我似乎无法找到一种方法来使其成为有序枚举 - 幻灯片以几乎任意的顺序返回......

按照PPTX文件中定义的顺序获取这些幻灯片的任何技巧?

using (PresentationDocument doc = PresentationDocument.Open(fileName, false))
{
   // Get the presentation part of the document.
   PresentationPart presentationPart = doc.PresentationPart;

   foreach (var slide in presentationPart.SlideParts)
   {
        ...
   }
}

我希望找到像SlideIDSequence数字之类的东西 - 我可以在Linq表达式中使用的某些项目或属性,如

.OrderBy(s => s.SlideID)

在该slideparts集合上。

2 个答案:

答案 0 :(得分:5)

它比我希望的要多一些 - 而且有时候文档有点粗略......

基本上,我必须枚举SlideIdList上的PresentationPart并执行一些XML-foo以从SlideId获取OpenXML演示文稿中的实际幻灯片。

有些事情:

using (PresentationDocument doc = PresentationDocument.Open(fileName, false))
{
    // Get the presentation part of the document.
    PresentationPart presentationPart = doc.PresentationPart;

    // get the SlideIdList
    var items = presentationPart.Presentation.SlideIdList;

    // enumerate over that list
    foreach (SlideId item in items)
    {
        // get the "Part" by its "RelationshipId"
        var part = presentationPart.GetPartById(item.RelationshipId);

        // this part is really a "SlidePart" and from there, we can get at the actual "Slide"
        var slide = (part as SlidePart).Slide;

        // do more stuff with your slides here!
    }
}

答案 1 :(得分:2)

我找到的最接近的是这个片段:

  

[ISO / IEC 29500-1第1版]

     

sld(演示幻灯片)

     

此元素指定幻灯片列表中的幻灯片。幻灯片列表是   用于指定幻灯片的顺序。

     

[示例:请考虑以下自定义节目,其排序为   滑动。

<p:custShowLst>
  <p:custShow name="Custom Show 1" id="0">
    <p:sldLst>
      <p:sld r:id="rId4"/>
      <p:sld r:id="rId3"/>
      <p:sld r:id="rId2"/>
      <p:sld r:id="rId5"/>
    </p:sldLst>
  </p:custShow>
</p:custShowLst>In the above example the order specified to present the slides is slide 4, then 3, 2 and finally 5. end example]

MSDN documentation for the slide class

幻灯片似乎具有rId##形式的r:id,其中##是幻灯片的编号。也许这足以让你再次去?