簡體   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