繁体   English   中英

如何使用 OpenXML 将幻灯片插入另一个 PowerPoint 幻灯片?

[英]How do I insert a slide into another PowerPoint slide using OpenXML?

我想拍摄一张 PowerPoint 幻灯片(“源”),并将其插入到另一个 PowerPoint 幻灯片(“目标”)中,该幻灯片已包含一些内容,位于源 PowerPoint 幻灯片中的特定 position 处。

我尝试了几种方法来研究执行此操作的代码,但我不断获得将幻灯片合并到 PowerPoint 演示文稿中的结果,这不是我想要的。 我想取一张现有的幻灯片并将其插入另一张幻灯片,就像将图片插入现有幻灯片一样。

我有另一个同事编写的代码,它克隆了源幻灯片中的所有元素,但它很复杂,并且对不同的元素类型使用不同的代码变体。 这是该代码的代表性示例:

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

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

        // Adjust the offsets so it is positioned correctly.
        ((Picture)elementClone).ShapeProperties.Transform2D.Offset.X += (Int64)shapeStruct.OffsetX;
        ((Picture)elementClone).ShapeProperties.Transform2D.Offset.Y += (Int64)shapeStruct.OffsetY;

        // Get the shape tree that we're adding the clone to and append to it.
        ShapeTree shapeTree = slideCard.CommonSlideData.ShapeTree;
        shapeTree.Append(elementClone);

        string rId = ((Picture)element).BlipFill.Blip.Embed.Value;

        ImagePart imagePart = (ImagePart)slideInstProc.SlidePart.GetPartById(rId);
        string contentType = imagePart.ContentType;

        // Locate the same object we cloned over to the slide.
        var blip = ((Picture)elementClone).BlipFill.Blip;

        slidePart = slideCard.SlidePart;

        try
        {
            ImagePart imagePart1 = slidePart.AddImagePart(contentType, rId);
            imagePart1.FeedData(imagePart.GetStream());
        }
        catch (XmlException)
        {
            //Console.WriteLine(xe.ToString());
            Console.WriteLine("Duplicate rId (" + rId + ")");
        }
    }
    if (elementTypeName.EndsWith(".GroupShape"))
    {
        ... etc

代码以 else-if 阶梯继续,其中包含以.GroupShape.Shape.GraphicFrame.ConnectionShape结尾的元素类型名称的代码块,并在底部以一个 catchall else 结尾。

问题是这段代码不能正确处理某些类型的对象。 一方面,它根本不处理绘图(可能是因为其中一些来自旧版本的 PowerPoint),当它处理时,它会做一些事情,比如改变绘图的颜色。

我希望有一种更基本的方法(即更简单的通用代码)将源 PowerPoint 幻灯片嵌入到另一个中,将其视为单个 object,而无需专门查看源 PowerPoint 中的元素类型。

或者,有什么方法可以处理普通“形状”的图纸或图像,这些“形状”并没有明确地将自己标识为图像?

这是解决我上面描述的特定问题的代码:

using A = DocumentFormat.OpenXml.Drawing;

foreach(A.BlipFill blipFill in shape.Descendants<A.BlipFill>())
{
    string rId = blipFill.Blip.Embed.Value;
    ImagePart imagePart = (ImagePart)slideInstProc.SlidePart.GetPartById(rId);
    string contentType = imagePart.ContentType;

    try
    {
        ImagePart imagePart1 = slidePart.AddImagePart(contentType, rId);
        imagePart1.FeedData(imagePart.GetStream());
    }
    catch (XmlException)
    {
        Console.WriteLine("Duplicate rId (" + rId + ")");
    }
}

其中,当应用于elementTypeName.EndsWith(".shape"); 产生我想要的结果。

对于将完整的幻灯片组合成演示文稿(这不需要我们所做的某些生成机制), OpenXmlPowerTools是一种更好的方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM