我有一个小型控制台程序,我正在编写使用Open XML SDK 2.0。我基本上只想在PowerPoint幻灯片上获取所有图片。但是,当我运行程序时,我收到以下错误:
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;
}
}
}
}
答案 0 :(得分:1)
using
中的GetSlidePart
语句正在关闭文档。来自PresentationDocument.Dispose
的{{3}}:
刷新并保存内容,关闭文档并释放所有资源。
如果您将文档的开头重构到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;
}