【发布时间】:2014-10-07 21:04:21
【问题描述】:
我正在编写一个使用 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;
}
}
}
}
【问题讨论】:
标签: c# openxml openxml-sdk