【问题标题】:Trying to get all pictures in a PowerPoint 2010 slide, but getting an error尝试获取 PowerPoint 2010 幻灯片中的所有图片,但出现错误
【发布时间】: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


    【解决方案1】:

    GetSlidePart 中的 using 语句正在关闭文档。来自documentationPresentationDocument.Dispose

    刷新并保存内容,关闭文档,释放所有资源。

    如果您将文档的打开重构到 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;
    }
    

    【讨论】:

    • 非常感谢您非常有帮助的回答!
    • 乐于帮助@kevin。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-14
    • 1970-01-01
    • 2022-10-02
    • 1970-01-01
    • 1970-01-01
    • 2018-04-22
    相关资源
    最近更新 更多