如何使用 Open XML SDK 在 PPTx 中插入视频?

How to Insert Video in PPTx using Open XML SDK?

我正在尝试创建一个基于 link (Working with presentation documents using the Open XML SDK 2.5) 的 PPTx 文件,并且能够创建包含图像、形状和标题的幻灯片文件。但我想将视频插入到新幻灯片中。有人可以帮我解决这个问题吗?

我从 Open XML SDK 2.5 Tool 得到了解决方案。以下是生成 C# 代码的步骤。

  • 首先创建一个包含视频的 Powerpoint 演示文稿并保存。
  • 使用打开 XML SDK 工具打开 PPTx 文件并单击 "Reflect Code"。

此操作将为您提供几千行 C# 代码。您可以直接使用代码或进行一些修改以实现可重用性。我做了同样的事情,下面是我的最终输出。

using DocumentFormat.OpenXml.Packaging;
using System.IO;
using System.Linq;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Presentation;
using System.IO;
using System.Linq;
using A = DocumentFormat.OpenXml.Drawing;
using P14 = DocumentFormat.OpenXml.Office2010.PowerPoint;

namespace PPTResearch
{
class Program
{
    int PicID = 915, slideCount;
    string imgEmbedId, videoEmbedId, mediaEmbedId, slidpartId;

    static void Main(string[] args)
    {
        string filepath = "D:\Temp\PPT\9.pptx";
        (new Program()).AddVideoToPresentation(filepath, @"D:\Videos\Movavi Library\Example.wmv", @"D:\Temp\VIDEO.png");
    }

    public void AddVideoToPresentation(string pptFilePath, string videoFilePath, string videoClipImagePath)
    {
        if (File.Exists(pptFilePath))
            File.Delete(pptFilePath);
        (new BasePresentation()).CreateBlankPresentation(pptFilePath);

        using (PresentationDocument presentationDocument = PresentationDocument.Open(pptFilePath, true))
        {
            PresentationPart presentationPart = presentationDocument.PresentationPart;

            slideCount = presentationPart.Presentation.SlideIdList.Count();
            imgEmbedId = string.Format("imgId{0}{1}", slideCount, PicID++);
            videoEmbedId = string.Format("vidId{0}{1}", slideCount, PicID++);
            mediaEmbedId = string.Format("medId{0}{1}", slideCount, PicID++);
            slidpartId = string.Format("sldId{0}{1}", slideCount, PicID++);

            (new BasePresentation()).InsertSlide(presentationPart, "");
            SlidePart slidepart = presentationDocument.PresentationPart.SlideParts.LastOrDefault();
            GenerateSlidePartDetails(slidepart);

            ImagePart imagePart = slidepart.AddNewPart<ImagePart>("image/png", imgEmbedId);
            System.IO.Stream data = File.OpenRead(videoClipImagePath);
            imagePart.FeedData(data);
            data.Close();

            MediaDataPart mediaDataPart1 = presentationDocument.CreateMediaDataPart("video/x-ms-wmv", "wmv");
            System.IO.Stream mediaDataPart1Stream = File.OpenRead(videoFilePath);
            mediaDataPart1.FeedData(mediaDataPart1Stream);
            mediaDataPart1Stream.Close();

            slidepart.AddVideoReferenceRelationship(mediaDataPart1, videoEmbedId);
            slidepart.AddMediaReferenceRelationship(mediaDataPart1, mediaEmbedId);

            slidepart.Slide.Save();
        }
    }

    private void GenerateSlidePartDetails(SlidePart slidepart)
    {
        Slide slide = new Slide();

        CommonSlideData commonSlideData1 = new CommonSlideData();
        ShapeTree shapeTree1 = new ShapeTree();

        NonVisualGroupShapeProperties nonVisualGroupShapeProperties1 = new NonVisualGroupShapeProperties();
        NonVisualDrawingProperties nonVisualDrawingProperties1 = new NonVisualDrawingProperties() { Id = (UInt32Value)1U, Name = "" };
        NonVisualGroupShapeDrawingProperties nonVisualGroupShapeDrawingProperties1 = new NonVisualGroupShapeDrawingProperties();
        ApplicationNonVisualDrawingProperties applicationNonVisualDrawingProperties1 = new ApplicationNonVisualDrawingProperties();

        nonVisualGroupShapeProperties1.Append(nonVisualDrawingProperties1);
        nonVisualGroupShapeProperties1.Append(nonVisualGroupShapeDrawingProperties1);
        nonVisualGroupShapeProperties1.Append(applicationNonVisualDrawingProperties1);

        GroupShapeProperties groupShapeProperties1 = new GroupShapeProperties();

        A.TransformGroup transformGroup1 = new A.TransformGroup();
        A.Offset offset1 = new A.Offset() { X = 0L, Y = 0L };
        A.Extents extents1 = new A.Extents() { Cx = 0L, Cy = 0L };
        A.ChildOffset childOffset1 = new A.ChildOffset() { X = 0L, Y = 0L };
        A.ChildExtents childExtents1 = new A.ChildExtents() { Cx = 0L, Cy = 0L };

        transformGroup1.Append(offset1);
        transformGroup1.Append(extents1);
        transformGroup1.Append(childOffset1);
        transformGroup1.Append(childExtents1);

        groupShapeProperties1.Append(transformGroup1);

        Picture picture1 = new Picture();
        NonVisualPictureProperties nonVisualPictureProperties1 = new NonVisualPictureProperties();

        NonVisualDrawingProperties nonVisualDrawingProperties2 = new NonVisualDrawingProperties() { Id = (UInt32Value)3U, Name = videoEmbedId + ".wmv" };
        A.HyperlinkOnClick hyperlinkOnClick1 = new A.HyperlinkOnClick() { Id = "", Action = "ppaction://media" };
        nonVisualDrawingProperties2.Append(hyperlinkOnClick1);

        NonVisualPictureDrawingProperties nonVisualPictureDrawingProperties1 = new NonVisualPictureDrawingProperties();
        A.PictureLocks pictureLocks1 = new A.PictureLocks() { NoChangeAspect = true };
        nonVisualPictureDrawingProperties1.Append(pictureLocks1);

        ApplicationNonVisualDrawingProperties applicationNonVisualDrawingProperties2 = new ApplicationNonVisualDrawingProperties();
        A.VideoFromFile videoFromFile1 = new A.VideoFromFile() { Link = videoEmbedId };

        ApplicationNonVisualDrawingPropertiesExtensionList applicationNonVisualDrawingPropertiesExtensionList1 = new ApplicationNonVisualDrawingPropertiesExtensionList();
        ApplicationNonVisualDrawingPropertiesExtension applicationNonVisualDrawingPropertiesExtension1 = new ApplicationNonVisualDrawingPropertiesExtension() { Uri = "{DAA4B4D4-6D71-4841-9C94-3DE7FCFB9230}" };

        P14.Media media1 = new P14.Media() { Embed = mediaEmbedId };
        media1.AddNamespaceDeclaration("p14", "http://schemas.microsoft.com/office/powerpoint/2010/main");
        applicationNonVisualDrawingPropertiesExtension1.Append(media1);
        applicationNonVisualDrawingPropertiesExtensionList1.Append(applicationNonVisualDrawingPropertiesExtension1);

        applicationNonVisualDrawingProperties2.Append(videoFromFile1);
        applicationNonVisualDrawingProperties2.Append(applicationNonVisualDrawingPropertiesExtensionList1);

        nonVisualPictureProperties1.Append(nonVisualDrawingProperties2);
        nonVisualPictureProperties1.Append(nonVisualPictureDrawingProperties1);
        nonVisualPictureProperties1.Append(applicationNonVisualDrawingProperties2);

        BlipFill blipFill1 = new BlipFill();
        A.Blip blip1 = new A.Blip() { Embed = imgEmbedId };

        A.Stretch stretch1 = new A.Stretch();
        A.FillRectangle fillRectangle1 = new A.FillRectangle();

        stretch1.Append(fillRectangle1);

        blipFill1.Append(blip1);
        blipFill1.Append(stretch1);

        ShapeProperties shapeProperties1 = new ShapeProperties();

        A.Transform2D transform2D1 = new A.Transform2D();
        A.Offset offset2 = new A.Offset() { X = 0L, Y = 0L };
        A.Extents extents2 = new A.Extents() { Cx = 9144000L, Cy = 5143500L };

        transform2D1.Append(offset2);
        transform2D1.Append(extents2);

        A.PresetGeometry presetGeometry1 = new A.PresetGeometry() { Preset = A.ShapeTypeValues.Rectangle };
        A.AdjustValueList adjustValueList1 = new A.AdjustValueList();

        presetGeometry1.Append(adjustValueList1);

        shapeProperties1.Append(transform2D1);
        shapeProperties1.Append(presetGeometry1);

        picture1.Append(nonVisualPictureProperties1);
        picture1.Append(blipFill1);
        picture1.Append(shapeProperties1);

        shapeTree1.Append(nonVisualGroupShapeProperties1);
        shapeTree1.Append(groupShapeProperties1);
        shapeTree1.Append(picture1);

        CommonSlideDataExtensionList commonSlideDataExtensionList1 = new CommonSlideDataExtensionList();

        CommonSlideDataExtension commonSlideDataExtension1 = new CommonSlideDataExtension() { Uri = "{BB962C8B-B14F-4D97-AF65-F5344CB8AC3E}" };

        P14.CreationId creationId1 = new P14.CreationId() { Val = (UInt32Value)4033567156U };
        creationId1.AddNamespaceDeclaration("p14", "http://schemas.microsoft.com/office/powerpoint/2010/main");

        commonSlideDataExtension1.Append(creationId1);

        commonSlideDataExtensionList1.Append(commonSlideDataExtension1);

        commonSlideData1.Append(shapeTree1);
        commonSlideData1.Append(commonSlideDataExtensionList1);

        ColorMapOverride colorMapOverride1 = new ColorMapOverride();
        A.MasterColorMapping masterColorMapping1 = new A.MasterColorMapping();

        colorMapOverride1.Append(masterColorMapping1);

        Timing timing1 = new Timing();

        TimeNodeList timeNodeList1 = new TimeNodeList();

        ParallelTimeNode parallelTimeNode1 = new ParallelTimeNode();

        CommonTimeNode commonTimeNode1 = new CommonTimeNode() { Id = (UInt32Value)1U, Duration = "indefinite", Restart = TimeNodeRestartValues.Never, NodeType = TimeNodeValues.TmingRoot };

        ChildTimeNodeList childTimeNodeList1 = new ChildTimeNodeList();

        SequenceTimeNode sequenceTimeNode1 = new SequenceTimeNode() { Concurrent = true, NextAction = NextActionValues.Seek };

        CommonTimeNode commonTimeNode2 = new CommonTimeNode() { Id = (UInt32Value)2U, Restart = TimeNodeRestartValues.WhenNotActive, Fill = TimeNodeFillValues.Hold, EventFilter = "cancelBubble", NodeType = TimeNodeValues.InteractiveSequence };

        StartConditionList startConditionList1 = new StartConditionList();

        Condition condition1 = new Condition() { Event = TriggerEventValues.OnClick, Delay = "0" };

        TargetElement targetElement1 = new TargetElement();
        ShapeTarget shapeTarget1 = new ShapeTarget() { ShapeId = "3" };

        targetElement1.Append(shapeTarget1);

        condition1.Append(targetElement1);

        startConditionList1.Append(condition1);

        EndSync endSync1 = new EndSync() { Event = TriggerEventValues.End, Delay = "0" };
        RuntimeNodeTrigger runtimeNodeTrigger1 = new RuntimeNodeTrigger() { Val = TriggerRuntimeNodeValues.All };

        endSync1.Append(runtimeNodeTrigger1);

        ChildTimeNodeList childTimeNodeList2 = new ChildTimeNodeList();

        ParallelTimeNode parallelTimeNode2 = new ParallelTimeNode();

        CommonTimeNode commonTimeNode3 = new CommonTimeNode() { Id = (UInt32Value)3U, Fill = TimeNodeFillValues.Hold };

        StartConditionList startConditionList2 = new StartConditionList();
        Condition condition2 = new Condition() { Delay = "0" };

        startConditionList2.Append(condition2);

        ChildTimeNodeList childTimeNodeList3 = new ChildTimeNodeList();

        ParallelTimeNode parallelTimeNode3 = new ParallelTimeNode();

        CommonTimeNode commonTimeNode4 = new CommonTimeNode() { Id = (UInt32Value)4U, Fill = TimeNodeFillValues.Hold };

        StartConditionList startConditionList3 = new StartConditionList();
        Condition condition3 = new Condition() { Delay = "0" };

        startConditionList3.Append(condition3);

        ChildTimeNodeList childTimeNodeList4 = new ChildTimeNodeList();

        ParallelTimeNode parallelTimeNode4 = new ParallelTimeNode();

        CommonTimeNode commonTimeNode5 = new CommonTimeNode() { Id = (UInt32Value)5U, PresetId = 2, PresetClass = TimeNodePresetClassValues.MediaCall, PresetSubtype = 0, Fill = TimeNodeFillValues.Hold, NodeType = TimeNodeValues.ClickEffect };

        StartConditionList startConditionList4 = new StartConditionList();
        Condition condition4 = new Condition() { Delay = "0" };

        startConditionList4.Append(condition4);

        ChildTimeNodeList childTimeNodeList5 = new ChildTimeNodeList();

        Command command1 = new Command() { Type = CommandValues.Call, CommandName = "togglePause" };

        CommonBehavior commonBehavior1 = new CommonBehavior();
        CommonTimeNode commonTimeNode6 = new CommonTimeNode() { Id = (UInt32Value)6U, Duration = "1", Fill = TimeNodeFillValues.Hold };

        TargetElement targetElement2 = new TargetElement();
        ShapeTarget shapeTarget2 = new ShapeTarget() { ShapeId = "3" };

        targetElement2.Append(shapeTarget2);

        commonBehavior1.Append(commonTimeNode6);
        commonBehavior1.Append(targetElement2);

        command1.Append(commonBehavior1);

        childTimeNodeList5.Append(command1);

        commonTimeNode5.Append(startConditionList4);
        commonTimeNode5.Append(childTimeNodeList5);

        parallelTimeNode4.Append(commonTimeNode5);

        childTimeNodeList4.Append(parallelTimeNode4);

        commonTimeNode4.Append(startConditionList3);
        commonTimeNode4.Append(childTimeNodeList4);

        parallelTimeNode3.Append(commonTimeNode4);

        childTimeNodeList3.Append(parallelTimeNode3);

        commonTimeNode3.Append(startConditionList2);
        commonTimeNode3.Append(childTimeNodeList3);

        parallelTimeNode2.Append(commonTimeNode3);

        childTimeNodeList2.Append(parallelTimeNode2);

        commonTimeNode2.Append(startConditionList1);
        commonTimeNode2.Append(endSync1);
        commonTimeNode2.Append(childTimeNodeList2);

        NextConditionList nextConditionList1 = new NextConditionList();

        Condition condition5 = new Condition() { Event = TriggerEventValues.OnClick, Delay = "0" };

        TargetElement targetElement3 = new TargetElement();
        ShapeTarget shapeTarget3 = new ShapeTarget() { ShapeId = "3" };

        targetElement3.Append(shapeTarget3);

        condition5.Append(targetElement3);

        nextConditionList1.Append(condition5);

        sequenceTimeNode1.Append(commonTimeNode2);
        sequenceTimeNode1.Append(nextConditionList1);

        Video video1 = new Video();

        CommonMediaNode commonMediaNode1 = new CommonMediaNode() { Volume = 80000 };

        CommonTimeNode commonTimeNode7 = new CommonTimeNode() { Id = (UInt32Value)7U, Fill = TimeNodeFillValues.Hold, Display = false };

        StartConditionList startConditionList5 = new StartConditionList();
        Condition condition6 = new Condition() { Delay = "indefinite" };

        startConditionList5.Append(condition6);

        commonTimeNode7.Append(startConditionList5);

        TargetElement targetElement4 = new TargetElement();
        ShapeTarget shapeTarget4 = new ShapeTarget() { ShapeId = "3" };

        targetElement4.Append(shapeTarget4);

        commonMediaNode1.Append(commonTimeNode7);
        commonMediaNode1.Append(targetElement4);

        video1.Append(commonMediaNode1);

        childTimeNodeList1.Append(sequenceTimeNode1);
        childTimeNodeList1.Append(video1);

        commonTimeNode1.Append(childTimeNodeList1);

        parallelTimeNode1.Append(commonTimeNode1);

        timeNodeList1.Append(parallelTimeNode1);

        timing1.Append(timeNodeList1);

        slide.Append(commonSlideData1);
        slide.Append(colorMapOverride1);
        slide.Append(timing1);

        slidepart.Slide = slide;
    }
}
}

如果您有其他解决方案,请在此处 post。