2014-10-07 1 views
0

Open XML SDK 2.0을 사용하는 작고 콘솔 프로그램이 있습니다. 기본적으로 PowerPoint 슬라이드에서 모든 그림을 가져오고 싶습니다. 나는이 프로그램을 실행하면,하지만, 나는 다음과 같은 오류가 발생합니다 :PowerPoint 2010 슬라이드의 모든 그림을 가져오고 있지만 오류가 발생했습니다.

Cannot access part because parent package was closed.

내 PPT는 하나 개의 이미지와 하나 개의 슬라이드가 있습니다. 이것은 단지 프로토 타입 프로그램입니다. Open XML SDK 2.0에 익숙하지 않아서이 오류가 무엇을 말하고 있는지, 어떻게 고쳐야하는지 잘 모르겠습니다. 나는 누군가가 올바른 방향으로 나를 가리킬 수 있기를 바랍니다. GetSlidePart에서

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 문 문서를 닫고 : 여기 내 코드입니다. PresentationDocument.Disposedocumentation에서 : 당신이 GetSlidePart의 외부로 문서의 개방을 리팩토링 경우

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

코드는 예상대로 작동합니다 :

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

+0

@kevin을 도와 드리겠습니다. – petelids

관련 문제