2014-10-07 60 views
0

我有一個我正在編寫的使用Open XML SDK 2.0的小型控制檯程序。我基本上只想在PowerPoint幻燈片上獲取所有圖片。當我運行該程序,不過,我得到以下錯誤:試圖獲取PowerPoint 2010幻燈片中的所有圖片,但出現錯誤

Cannot access part because parent package was closed.

我的PPT只與一個圖像一張幻燈片。這只是一個原型程序。我對Open XML SDK 2.0不熟悉,所以我不確定這個錯誤告訴我什麼以及如何解決它。我希望有人能指出我正確的方向。這裏是我的代碼:

using System; 
using System.Collections.Generic; 
using DocumentFormat.OpenXml.Presentation; 
using A = DocumentFormat.OpenXml.Drawing; 
using DocumentFormat.OpenXml.Packaging; 
using DocumentFormat.OpenXml; 
using System.Text; 
using System.Data; 
using System.Linq; 
namespace OpenXmlDemo 
{ 
    public class Program 
    { 
     static void Main(string[] args) 
     { 
      var file = @"C:\Users\kjennings\Desktop\Test PPTs\This is the Title - Copy.pptx"; 
      var index = 0; 
      var slidePart = GetSlidePart(file, index); 
      var images = slidePart.Slide.Descendants<Picture>().Select(p => p); // error occurs here 

      foreach (var image in images) 
      { 
       // Just placeholder code below. It never makes it here. 
       var pic = image; 
      } 
     } 

     public static SlidePart GetSlidePart(string docName, int slideIndex) 
     { 
      using (var ppt = PresentationDocument.Open(docName, false)) 
      { 
       // Get the relationship ID of the first slide. 
       var presentationPart = ppt.PresentationPart; 

       // Verify that the presenation part and the presenation exist, 
       if (presentationPart != null && presentationPart.Presentation != null) 
       { 
        var presentation = presentationPart.Presentation; 

        if (presentation.SlideIdList != null) 
        { 
         var slideIds = presentation.SlideIdList.ChildElements; 

         if (slideIndex < slideIds.Count) 
         { 
          // Get the relationship ID of the slide. 
          var slidePartRelationship = (slideIds[slideIndex] as SlideId).RelationshipId; 

          // Get the specified slide part from the relationship ID. 
          var slidePart = (SlidePart)presentationPart.GetPartById(slidePartRelationship); 

          return slidePart; 
         } 
        } 
       } 

       // No slide found. 
       return null; 
      } 
     } 
    } 
} 

回答

1

using聲明GetSlidePart被關閉文檔。從documentationPresentationDocument.Dispose

Flushes and saves the content, closes the document, and releases all resources.

如果重構文件到GetSlidePart外界開放你的代碼應該按預期工作:

static void Main(string[] args) 
{ 
    var file = @"C:\Users\kjennings\Desktop\Test PPTs\This is the Title - Copy.pptx"; 
    var index = 0; 

    //open the document here for use throughout the application 
    using (var ppt = PresentationDocument.Open(file, false)) 
    { 
     var slidePart = GetSlidePart(ppt, index); 
     var images = slidePart.Slide.Descendants<Picture>().Select(p => p); 

     foreach (var image in images) 
     { 
      // Just placeholder code below. It now gets here... 
      var pic = image; 
     } 
    } 
} 

//pass the open document in here... 
public static SlidePart GetSlidePart(PresentationDocument ppt, int slideIndex) 
{ 
    // Get the relationship ID of the first slide. 
    var presentationPart = ppt.PresentationPart; 

    // Verify that the presenation part and the presenation exist, 
    if (presentationPart != null && presentationPart.Presentation != null) 
    { 
     var presentation = presentationPart.Presentation; 

     if (presentation.SlideIdList != null) 
     { 
      var slideIds = presentation.SlideIdList.ChildElements; 

      if (slideIndex < slideIds.Count) 
      { 
       // Get the relationship ID of the slide. 
       var slidePartRelationship = (slideIds[slideIndex] as SlideId).RelationshipId; 

       // Get the specified slide part from the relationship ID. 
       var slidePart = (SlidePart)presentationPart.GetPartById(slidePartRelationship); 

       return slidePart; 
      } 
     } 
    } 

    // No slide found. 
    return null; 
} 
+0

非常感謝你對你非常有用的答案! – Kevin 2014-10-07 13:09:16

+0

樂於助人@kevin。 – petelids 2014-10-07 17:36:29