将 OLE 对象从一张幻灯片复制到另一张幻灯片会损坏生成的 PowerPoint

Rob*_*vey 8 c# powerpoint openxml

我有将一张 PowerPoint 幻灯片的内容复制到另一张幻灯片的代码。下面是如何处理图像的示例。

foreach (OpenXmlElement element in sourceSlide.CommonSlideData.ShapeTree.ChildElements.ToList())
{
    string elementType = element.GetType().ToString();

    if (elementType.EndsWith(".Picture"))
    {
        // Deep clone the element.
        elementClone = element.CloneNode(true);
        var picture = (Picture)elementClone;

        // Get the picture's original rId
        var blip = picture.BlipFill.Blip;
        string rId = blip.Embed.Value;
        
        // Retrieve the ImagePart from the original slide by rId
        ImagePart sourceImagePart = (ImagePart)sourceSlide.SlidePart.GetPartById(rId);

        // Add the image part to the new slide, letting OpenXml generate the new rId
        ImagePart targetImagePart = targetSlidePart.AddImagePart(sourceImagePart.ContentType);

        // And copy the image data.
        targetImagePart.FeedData(sourceImagePart.GetStream());

        // Retrieve the new ID from the target image part,
        string id = targetSlidePart.GetIdOfPart(targetImagePart);

        // and assign it to the picture.
        blip.Embed.Value = id;

        // Get the shape tree that we're adding the clone to and append to it.
        ShapeTree shapeTree = targetSlide.CommonSlideData.ShapeTree;
        shapeTree.Append(elementClone);
    }
Run Code Online (Sandbox Code Playgroud)

这段代码工作正常。对于像Graphic Frames这样的其他场景,它看起来有点不同,因为每个Graphic Frames可以包含多个图片对象。

// Go thru all the Picture objects in this GraphicFrame.
foreach (var sourcePicture in element.Descendants<Picture>())
{
    string rId = sourcePicture.BlipFill.Blip.Embed.Value;
    ImagePart sourceImagePart = (ImagePart)sourceSlide.SlidePart.GetPartById(rId);
    var contentType = sourceImagePart.ContentType;

    var targetPicture = elementClone.Descendants<Picture>().First(x => x.BlipFill.Blip.Embed.Value == rId);
    var targetBlip = targetPicture.BlipFill.Blip;

    ImagePart targetImagePart = targetSlidePart.AddImagePart(contentType);
    targetImagePart.FeedData(sourceImagePart.GetStream());
    string id = targetSlidePart.GetIdOfPart(targetImagePart);
    targetBlip.Embed.Value = id;
}
Run Code Online (Sandbox Code Playgroud)

现在我需要对 OLE 对象做同样的事情。

// Go thru all the embedded objects in this GraphicFrame.
foreach (var oleObject in element.Descendants<OleObject>())
{
    // Get the rId of the embedded OLE object.
    string rId = oleObject.Id;

    // Get the EmbeddedPart from the source slide.
    var embeddedOleObj = sourceSlide.SlidePart.GetPartById(rId);

    // Get the content type.
    var contentType = embeddedOleObj.ContentType;

    // Create the Target Part.  Let OpenXML assign an rId.
    var targetObjectPart = targetSlide.SlidePart.AddNewPart<EmbeddedObjectPart>(contentType, null);

    // Get the embedded OLE object data from the original object.
    var objectStream = embeddedOleObj.GetStream();

    // And give it to the ObjectPart.
    targetObjectPart.FeedData(objectStream);

    // Get the new rId and assign it to the OLE Object.
    string id = targetSlidePart.GetIdOfPart(targetObjectPart);
    oleObject.Id = id;
}
Run Code Online (Sandbox Code Playgroud)

但它没有用。生成的 PowerPoint 已损坏。

我究竟做错了什么?


注意: 除了rIdOLE 对象中的处理之外,所有代码都有效。我知道它有效,因为如果我只是将原始rId对象从源对象传递到目标对象部分,如下所示:

var targetObjectPart = targetSlide.SlidePart
   .AddNewPart<EmbeddedObjectPart>(contentType, rId);
Run Code Online (Sandbox Code Playgroud)

只要目标幻灯片中不存在它,它就会正常运行rId这显然不会像我需要的那样每次都起作用。

源幻灯片和目标幻灯片来自不同的 PPTX 文件。我们使用的是 OpenXML,而不是 Office Interop。

mce*_*nak 2

由于您没有提供完整的代码,因此很难判断出了什么问题。
我的猜测是您没有修改正确的对象。

在图片的代码示例中,您正在创建和修改elementClone.
在 ole 对象的代码示例中,您正在使用和修改oleObject(它是 的后代element),并且从上下文中并不清楚它是源文档的一部分还是目标文档的一部分。


您可以尝试这个最小的示例:

  • 使用带有一个嵌入 ole 对象的新 pptxc:\testdata\input.pptx
  • 使用新的 pptx(空白的)c:\testdata\output.pptx

运行代码后,我能够打开输出文档中嵌入的 ole 对象。

using DocumentFormat.OpenXml.Presentation;
using DocumentFormat.OpenXml.Packaging;
using System.Linq;

namespace ooxml
{
    class Program
    {
        static void Main(string[] args)
        {            
            CopyOle("c:\\testdata\\input.pptx", "c:\\testdata\\output.pptx");
        }

        private static void CopyOle(string inputFile, string outputFile)
        {
            using (PresentationDocument sourceDocument = PresentationDocument.Open(inputFile, true))
            {
                using (PresentationDocument targetDocument = PresentationDocument.Open(outputFile, true))
                {
                    var sourceSlidePart = sourceDocument.PresentationPart.SlideParts.First();
                    var targetSlidePart = targetDocument.PresentationPart.SlideParts.First();

                    
                    foreach (var element in sourceSlidePart.Slide.CommonSlideData.ShapeTree.ChildElements)
                    {
                        //clones an element, does not copy the actual relationship target (e.g. ppt\embeddings\oleObject1.bin)
                        var elementClone = element.CloneNode(true);                      
                        
                        //for each cloned OleObject, fix its relationship
                        foreach(var clonedOleObject in elementClone.Descendants<OleObject>())
                        {
                            //find the original EmbeddedObjectPart in the source document
                            //(we can use the id from the clonedOleObject to do that, since it contained the same id
                            // as the source ole object)
                            var sourceObjectPart = sourceSlidePart.GetPartById(clonedOleObject.Id);

                            //create a new EmbeddedObjectPart in the target document and copy the data from the original EmbeddedObjectPart
                            var targetObjectPart = targetSlidePart.AddEmbeddedObjectPart(sourceObjectPart.ContentType);
                            targetObjectPart.FeedData(sourceObjectPart.GetStream());

                            //update the relationship target on the clonedOleObject to point to the newly created EmbeddedObjectPath
                            clonedOleObject.Id = targetSlidePart.GetIdOfPart(targetObjectPart);
                        }

                        //add cloned element to the document
                        targetSlidePart.Slide.CommonSlideData.ShapeTree.Append(elementClone);
                    }
                    targetDocument.PresentationPart.Presentation.Save();
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

至于故障排除,OOXML Tools chrome 扩展很有帮助。
它允许比较两个文档的结构,因此更容易分析出了什么问题。

例子:

  • 如果您只克隆所有元素,您会发现 /ppt/embeddings/* 和 /ppt/media/* 将丢失 在此输入图像描述
  • 或者您可以检查关系是否正确(例如输入文档使用“rId1”引用嵌入数据,输出文档使用“R3a2fa0c37eaa42b5”) 在此输入图像描述

    在此输入图像描述