简体   繁体   English

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

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

I would like to take a PowerPoint slide (the "source"), and insert it into another PowerPoint slide (the "target") that already contains some content, at a specific position in the source PowerPoint slide.我想拍摄一张 PowerPoint 幻灯片(“源”),并将其插入到另一个 PowerPoint 幻灯片(“目标”)中,该幻灯片已包含一些内容,位于源 PowerPoint 幻灯片中的特定 position 处。

I've tried several ways to research code that does this, but I keep getting results for merging slides into PowerPoint presentations, which is not what I want.我尝试了几种方法来研究执行此操作的代码,但我不断获得将幻灯片合并到 PowerPoint 演示文稿中的结果,这不是我想要的。 I want to take an existing slide and insert it into another, much like one would insert a picture into an existing slide.我想取一张现有的幻灯片并将其插入另一张幻灯片,就像将图片插入现有幻灯片一样。

I have code that another coworker wrote that clones all of the elements from the source slide, but it is convoluted and uses different code variations for different element types.我有另一个同事编写的代码,它克隆了源幻灯片中的所有元素,但它很复杂,并且对不同的元素类型使用不同的代码变体。 Here is a representative sample of that code:这是该代码的代表性示例:

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

The code continues with an else-if ladder containing blocks of code for element type names ending with .GroupShape , .GraphicFrame , .Shape , and .ConnectionShape , concluding with a catchall else at the bottom.代码以 else-if 阶梯继续,其中包含以.GroupShape.Shape.GraphicFrame.ConnectionShape结尾的元素类型名称的代码块,并在底部以一个 catchall else 结尾。

The problem is this code doesn't process some types of objects properly.问题是这段代码不能正确处理某些类型的对象。 For one thing, it doesn't process drawings at all (perhaps because some of them originated from an older version of PowerPoint), and when it does, it does things like change the color of the drawing.一方面,它根本不处理绘图(可能是因为其中一些来自旧版本的 PowerPoint),当它处理时,它会做一些事情,比如改变绘图的颜色。

What I was hoping is that there was a more fundamental way (ie simpler, generic code) to embed a source PowerPoint slide into another, treating it like a single object, without looking at element types within the source PowerPoint specifically.我希望有一种更基本的方法(即更简单的通用代码)将源 PowerPoint 幻灯片嵌入到另一个中,将其视为单个 object,而无需专门查看源 PowerPoint 中的元素类型。

Alternatively, what would be the way to process drawings or images in ordinary "shapes" that don't identify themselves specifically as images?或者,有什么方法可以处理普通“形状”的图纸或图像,这些“形状”并没有明确地将自己标识为图像?

This is the code that solved the specific problem I was describing above:这是解决我上面描述的特定问题的代码:

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 + ")");
    }
}

Which, when applied to elementTypeName.EndsWith(".shape");其中,当应用于elementTypeName.EndsWith(".shape"); produces exactly the result I want.产生我想要的结果。

For composing complete slides into a presentation (which doesn't require some of the generation mechanics that we do), OpenXmlPowerTools is a much better approach.对于将完整的幻灯片组合成演示文稿(这不需要我们所做的某些生成机制), OpenXmlPowerTools是一种更好的方法。

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

相关问题 如何使用 OpenXML SDK 在 Powerpoint 幻灯片中隐藏文本? - How to hide a text inside a Powerpoint slide using OpenXML SDK? 如何使用 Powershell 在现有的 Powerpoint 幻灯片中插入文本 - How to insert text to an existing Powerpoint Slide using Powershell 如何使用xml将幻灯片的背景插入PowerPoint - how to insert slide's background into powerpoint using xml 使用 OpenXML SDK 从 Powerpoint 演示文稿的主幻灯片中删除所有未使用的幻灯片布局 - Remove all unused Slide Layout from my Master Slide from Powerpoint Presentation using OpenXML SDK 如何将 powerpoint 幻灯片注释导出到单个文本文件? - How do I export powerpoint slide notes to individual text files? 如何在 RMarkdown 的 PowerPoint 幻灯片上包含多个数字? - How do I include muliple figures on a PowerPoint slide in RMarkdown? 如何将 Excel 图表复制到 PowerPoint 幻灯片中? - How do I copy an Excel chart into a PowerPoint slide? Interop.PowerPoint:如何设置另一张幻灯片的幻灯片背景? - Interop.PowerPoint: How set slide background from another slide? PowerPoint幻灯片上的OpenXML自动刷新连接器 - OpenXML refresh connectors on a PowerPoint Slide automatically 如何将模板从另一个 PowerPoint 复制到幻灯片中? - How to copy template into slide from another PowerPoint?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM