Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
c99ff5a
added c#/vb code
mkaszewiak Mar 26, 2025
bcecedb
Merge branch 'main' into addVideo
mkaszewiak Mar 26, 2025
798e91f
added code snipets
mkaszewiak Mar 28, 2025
86255f9
fixed build warnings
mkaszewiak Mar 28, 2025
c421671
revert unintended change in insert_a_new_slideto_cs.csproj
mkaszewiak Mar 28, 2025
df45c27
Reverted to match main insert_a_new_slideto_cs.csproj
mkaszewiak Mar 28, 2025
fc7e950
removed .Net framework target
mkaszewiak Mar 28, 2025
ed67d1c
Resolved conflicts
mkaszewiak Mar 28, 2025
62a8d4c
fixing typo
mkaszewiak Mar 31, 2025
7978539
Update samples/presentation/add_video/cs/Program.cs
mkaszewiak Apr 1, 2025
bc66787
Update samples/presentation/add_video/cs/Program.cs
mkaszewiak Apr 1, 2025
83d351e
Update samples/presentation/add_video/cs/Program.cs
mkaszewiak Apr 1, 2025
b869034
Update samples/presentation/add_video/cs/Program.cs
mkaszewiak Apr 1, 2025
8db9e3b
applied comments
mkaszewiak Apr 1, 2025
4283f96
Update how-to-add-a-video-to-a-slide-in-a-presentation.md
AlfredHellstern Apr 1, 2025
b35f7d8
Updated ms.assetid
mkaszewiak Apr 3, 2025
b51ceb6
Merge branch 'main' into addVideo
mkaszewiak May 12, 2025
de275e6
Merge branch 'main' into addVideo
twsouthwick May 12, 2025
6da971d
updated toc.yaml and removed typo in the file name
mkaszewiak May 13, 2025
5e9f8d3
added deleted file to the pr
mkaszewiak May 13, 2025
924ce4c
updated overview and working with comments files
mkaszewiak May 13, 2025
f4b38b3
added overview.md entry for add video
mkaszewiak May 13, 2025
2163d2f
Update docs/presentation/how-to-add-a-video-to-a-slide-in-a-presentat…
mkaszewiak May 13, 2025
e53cec1
Update docs/presentation/how-to-add-a-video-to-a-slide-in-a-presentat…
mkaszewiak May 13, 2025
780f1ee
Update docs/presentation/how-to-add-a-video-to-a-slide-in-a-presentat…
mkaszewiak May 14, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
138 changes: 138 additions & 0 deletions docs/presentation/how-to-add-a-video-to-a-slide-in-a-presentation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
---

api_name:
- Microsoft.Office.DocumentFormat.OpenXML.Packaging
api_type:
- schema
ms.assetid: 536c94b5-dd25-4173-ad6a-b72b95dd7f31
title: 'How to: Add a video to a slide in a presentation'
ms.suite: office

ms.author: o365devx
author: o365devx
ms.topic: conceptual
ms.date: 04/03/2025
ms.localizationpriority: medium
---

# Add a video to a slide in a presentation

This topic shows how to use the classes in the Open XML SDK for
Office to add a video to the first slide in a presentation
programmatically.

## Getting a Presentation Object

In the Open XML SDK, the <xref:DocumentFormat.OpenXml.Packaging.PresentationDocument> class represents a
presentation document package. To work with a presentation document,
first create an instance of the **PresentationDocument** class, and then work with
that instance. To create the class instance from the document call the
<xref:DocumentFormat.OpenXml.Packaging.PresentationDocument.Open*> method that uses a file path, and a
Boolean value as the second parameter to specify whether a document is
editable. To open a document for read/write, specify the value `true` for this parameter as shown in the following
`using` statement. In this code, the file
parameter is a string that represents the path for the file from which
you want to open the document.

### [C#](#tab/cs-1)
[!code-csharp[](../../samples/presentation/add_video/cs/Program.cs#snippet1)]

### [Visual Basic](#tab/vb-1)
[!code-vb[](../../samples/presentation/add_video/vb/Program.vb#snippet1)]
***


[!include[Using Statement](../includes/presentation/using-statement.md)] `ppt`.


## The Structure of the Video From File

The PresentationML document consists of a number of parts, among which is the Picture (`<pic/>`) element.

The following text from the [!include[ISO/IEC 29500 URL](../includes/iso-iec-29500-link.md)] specification introduces the overall form of a `PresentationML` package.

Video File (`<videoFile/>`) specifies the presence of a video file. It is defined within the non-visual properties of an object. The video shall be attached to an object as this is how it is represented within the document. The actual playing of the video however is done within the timing node list that is specified under the timing element.

Consider the following `Picture` object that has a video attached to it.

```xml
<p:pic>
<p:nvPicPr>
<p:cNvPr id="7" name="Rectangle 6">
<a:hlinkClick r:id="" action="ppaction://media"/>
</p:cNvPr>
<p:cNvPicPr>
<a:picLocks noRot="1"/>
</p:cNvPicPr>
<p:nvPr>
<a:videoFile r:link="rId1"/>
</p:nvPr>
</p:nvPicPr>
</p:pic>
```

In the above example, we see that there is a single videoFile element attached to this picture. This picture is placed within the document just as a normal picture or shape would be. The id of this picture, namely 7 in this case, is used to refer to this videoFile element from within the timing node list. The Linked relationship id is used to retrieve the actual video file for playback purposes.

&copy; [!include[ISO/IEC 29500 version](../includes/iso-iec-29500-version.md)]

The following XML Schema fragment defines the contents of videoFile.

```xml
<xsd:complexType name="CT_TLMediaNodeVideo">
<xsd:sequence>
<xsd:element name="cMediaNode" type="CT_TLCommonMediaNodeData" minOccurs="1" maxOccurs="1"/>
</xsd:sequence>
<xsd:attribute name="fullScrn" type="xsd:boolean" use="optional" default="false"/>
</xsd:complexType>
```

## How the Sample Code Works

After opening the presentation file for read/write access in the `using` statement, the code gets the presentation
part from the presentation document. Then it gets the relationship ID of
the last slide, and gets the slide part from the relationship ID.


### [C#](#tab/cs-2)
[!code-csharp[](../../samples/presentation/add_video/cs/Program.cs#snippet2)]

### [Visual Basic](#tab/vb-2)
[!code-vb[](../../samples/presentation/add_video/vb/Program.vb#snippet2)]
***

The code first creates a media data part for the video file to be added. With the video file stream open, it feeds the media data part object. Next, video and media relationship references are added to the slide using the provided embedId for future reference to the video file and mediaEmbedId for media reference.

An image part is then added with a sample picture to be used as a placeholder for the video. A picture object is created with various elements, such as Non-Visual Drawing Properties (`<cNvPr/>`), which specify non-visual canvas properties. This allows for additional information that does not affect the appearance of the picture to be stored. The `<videoFile/>` element, explained above, is also included. The HyperLinkOnClick (`<hlinkClick/>`) element specifies the on-click hyperlink information to be applied to a run of text or image. When the hyperlink text or image is clicked, the link is fetched. Non-Visual Picture Drawing Properties (`<cNvPicPr/>`) specify the non-visual properties for the picture canvas. For a detailed explanation of the elements used, please refer to [!include[ISO/IEC 29500 URL](../includes/iso-iec-29500-link.md)]

### [C#](#tab/cs-3)
[!code-csharp[](../../samples/presentation/add_video/cs/Program.cs#snippet3)]

### [Visual Basic](#tab/vb-3)
[!code-vb[](../../samples/presentation/add_video/vb/Program.vb#snippet3)]
***

Next Media(CT_Media) element is created with use of previously referenced mediaEmbedId(Embedded Picture Reference). The Blip element is also added; this element specifies the existence of an image (binary large image or picture) and contains a reference to the image data. Blip's Embed attribute is used to specify a placeholder image in the Image Part created previously.

### [C#](#tab/cs-4)
[!code-csharp[](../../samples/presentation/add_video/cs/Program.cs#snippet4)]

### [Visual Basic](#tab/vb-4)
[!code-vb[](../../samples/presentation/add_video/vb/Program.vb#snippet4)]
***

All other elements such Offset(`<off/>`), Stretch(`<stretch/>`), FillRectangle(`<fillRect/>`), are appended to the ShapeProperties(`<spPr/>`) and ShapeProperties are appended to the Picture element(`<pic/>`). Finally the picture element that incudes video is added to the ShapeTree(`<sp/>`) of the slide.

Following is the complete sample code that you can use to add video to the slide.

## Sample Code

### [C#](#tab/cs)
[!code-csharp[](../../samples/presentation/add_video/cs/Program.cs#snippet0)]

### [Visual Basic](#tab/vb)
[!code-vb[](../../samples/presentation/add_video/vb/Program.vb#snippet0)]
***

## See also

- [Open XML SDK class library reference](/office/open-xml/open-xml-sdk)
6 changes: 4 additions & 2 deletions docs/presentation/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,17 @@ This section provides how-to topics for working with presentation documents usin

- [Structure of a PresentationML document](structure-of-a-presentationml-document.md)

- [Add a comment to a slide in a presentation](how-to-add-a-comment-to-a-slide-in-a-presentation.md)
- [Add a comment to a slide in a presentation](how-to-add-a-comment-to-a-slide-in-a-presentation.md)

- [Add a video to a slide in a presentation](how-to-add-a-video-to-a-slide-in-a-presentation.md)

- [Apply a theme to a presentation](how-to-apply-a-theme-to-a-presentation.md)

- [Change the fill color of a shape in a presentation](how-to-change-the-fill-color-of-a-shape-in-a-presentation.md)

- [Create a presentation document by providing a file name](how-to-create-a-presentation-document-by-providing-a-file-name.md)

- [Delete all the comments by an author from all the slides in a presentation](how-to-delete-all-the-comments-by-an-author-from-all-the-slides-in-a-presentatio.md)
- [Delete all the comments by an author from all the slides in a presentation](how-to-delete-all-the-comments-by-an-author-from-all-the-slides-in-a-presentation.md)

- [Delete a slide from a presentation](how-to-delete-a-slide-from-a-presentation.md)

Expand Down
2 changes: 1 addition & 1 deletion docs/presentation/working-with-comments.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,4 +193,4 @@ article.
[About the Open XML SDK for Office](../about-the-open-xml-sdk.md)
[How to: Create a Presentation by Providing a File Name](how-to-create-a-presentation-document-by-providing-a-file-name.md)
[How to: Add a comment to a slide in a presentation](how-to-add-a-comment-to-a-slide-in-a-presentation.md)
[How to: Delete all the comments by an author from all the slides in a presentation](how-to-delete-all-the-comments-by-an-author-from-all-the-slides-in-a-presentatio.md)
[How to: Delete all the comments by an author from all the slides in a presentation](how-to-delete-all-the-comments-by-an-author-from-all-the-slides-in-a-presentation.md)
4 changes: 3 additions & 1 deletion docs/toc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,16 @@
href: presentation/overview.md
- name: Add a comment to a slide in a presentation
href: presentation/how-to-add-a-comment-to-a-slide-in-a-presentation.md
- name: Add a video to a slide in a presentation
href: presentation/how-to-add-a-video-to-a-slide-in-a-presentation.md
- name: Apply a theme to a presentation
href: presentation/how-to-apply-a-theme-to-a-presentation.md
- name: Change the fill color of a shape in a presentation
href: presentation/how-to-change-the-fill-color-of-a-shape-in-a-presentation.md
- name: Create a presentation document by providing a file name
href: presentation/how-to-create-a-presentation-document-by-providing-a-file-name.md
- name: Delete all the comments by an author from all the slides in a presentation
href: presentation/how-to-delete-all-the-comments-by-an-author-from-all-the-slides-in-a-presentatio.md
href: presentation/how-to-delete-all-the-comments-by-an-author-from-all-the-slides-in-a-presentation.md
- name: Delete a slide from a presentation
href: presentation/how-to-delete-a-slide-from-a-presentation.md
- name: Get all the external hyperlinks in a presentation
Expand Down
150 changes: 150 additions & 0 deletions samples/presentation/add_video/cs/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Presentation;
using A = DocumentFormat.OpenXml.Drawing;
using P14 = DocumentFormat.OpenXml.Office2010.PowerPoint;
using ShapeTree = DocumentFormat.OpenXml.Presentation.ShapeTree;
using ShapeProperties = DocumentFormat.OpenXml.Presentation.ShapeProperties;
using NonVisualDrawingProperties = DocumentFormat.OpenXml.Presentation.NonVisualDrawingProperties;
using NonVisualPictureProperties = DocumentFormat.OpenXml.Presentation.NonVisualPictureProperties;
using NonVisualPictureDrawingProperties = DocumentFormat.OpenXml.Presentation.NonVisualPictureDrawingProperties;
using Picture = DocumentFormat.OpenXml.Presentation.Picture;
using BlipFill = DocumentFormat.OpenXml.Presentation.BlipFill;
using DocumentFormat.OpenXml.Packaging;
using ApplicationNonVisualDrawingProperties = DocumentFormat.OpenXml.Presentation.ApplicationNonVisualDrawingProperties;
using System;
using System.IO;
using System.Linq;

// <Snippet0>
AddVideo(args[0], args[1], args[2]);

static void AddVideo(string filePath, string videoFilePath, string coverPicPath)
{

string imgEmbedId = "rId4", embedId = "rId3", mediaEmbedId = "rId2";
UInt32Value shapeId = 5;
// <Snippet1>
using (PresentationDocument presentationDocument = PresentationDocument.Open(filePath, true))
// </Snippet1>
{

if (presentationDocument.PresentationPart == null || presentationDocument.PresentationPart.Presentation.SlideIdList == null)
{
throw new NullReferenceException("Presentation Part is empty or there are no slides in it");
}
// <Snippet2>
//Get presentation part
PresentationPart presentationPart = presentationDocument.PresentationPart;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do the samples not use var? Not a blocker, but we should be consistent

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will stick with data types but we may discuss it on the meeting as the current samples have it mixed var and data types. Thanks

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I try to avoid var in the docs for clarity. Maybe we should go through older files and make them consistent

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should add an editorconfig that enforces it for all of them


//Get slides ids.
OpenXmlElementList slidesIds = presentationPart.Presentation.SlideIdList.ChildElements;

//Get relationsipId of the last slide
string? videoSldRelationshipId = ((SlideId) slidesIds[slidesIds.ToArray().Length - 1]).RelationshipId;

if (videoSldRelationshipId == null)
{
throw new NullReferenceException("Slide id not found");
}

//Get slide part by relationshipID
SlidePart? slidePart = (SlidePart) presentationPart.GetPartById(videoSldRelationshipId);
// </Snippet2>

// <Snippet3>
// Create video Media Data Part (content type, extension)
MediaDataPart mediaDataPart = presentationDocument.CreateMediaDataPart("video/mp4", ".mp4");

//Get the video file and feed the stream
using (Stream mediaDataPartStream = File.OpenRead(videoFilePath))
{
mediaDataPart.FeedData(mediaDataPartStream);
}
//Adds a VideoReferenceRelationship to the MainDocumentPart
slidePart.AddVideoReferenceRelationship(mediaDataPart, embedId);

//Adds a MediaReferenceRelationship to the SlideLayoutPart
slidePart.AddMediaReferenceRelationship(mediaDataPart, mediaEmbedId);

NonVisualDrawingProperties nonVisualDrawingProperties = new NonVisualDrawingProperties() { Id = shapeId, Name = "video" };
A.VideoFromFile videoFromFile = new A.VideoFromFile() { Link = embedId };

ApplicationNonVisualDrawingProperties appNonVisualDrawingProperties = new ApplicationNonVisualDrawingProperties();
appNonVisualDrawingProperties.Append(videoFromFile);

//adds sample image to the slide with id to be used as reference in blip
ImagePart imagePart = slidePart.AddImagePart(ImagePartType.Png, imgEmbedId);
using (Stream data = File.OpenRead(coverPicPath))
{
imagePart.FeedData(data);
}

if (slidePart!.Slide!.CommonSlideData!.ShapeTree == null)
{
throw new NullReferenceException("Presentation shape tree is empty");
}

//Getting existing shape tree object from PowerPoint
ShapeTree shapeTree = slidePart.Slide.CommonSlideData.ShapeTree;

// specifies the existence of a picture within a presentation.
// It can have non-visual properties, a picture fill as well as shape properties attached to it.
Picture picture = new Picture();
NonVisualPictureProperties nonVisualPictureProperties = new NonVisualPictureProperties();

A.HyperlinkOnClick hyperlinkOnClick = new A.HyperlinkOnClick() { Id = "", Action = "ppaction://media" };
nonVisualDrawingProperties.Append(hyperlinkOnClick);

NonVisualPictureDrawingProperties nonVisualPictureDrawingProperties = new NonVisualPictureDrawingProperties();
A.PictureLocks pictureLocks = new A.PictureLocks() { NoChangeAspect = true };
nonVisualPictureDrawingProperties.Append(pictureLocks);

ApplicationNonVisualDrawingPropertiesExtensionList appNonVisualDrawingPropertiesExtensionList = new ApplicationNonVisualDrawingPropertiesExtensionList();
ApplicationNonVisualDrawingPropertiesExtension appNonVisualDrawingPropertiesExtension = new ApplicationNonVisualDrawingPropertiesExtension() { Uri = "{DAA4B4D4-6D71-4841-9C94-3DE7FCFB9230}" };
// </Snippet3>

// <Snippet4>
P14.Media media = new() { Embed = mediaEmbedId };
media.AddNamespaceDeclaration("p14", "http://schemas.microsoft.com/office/powerpoint/2010/main");

appNonVisualDrawingPropertiesExtension.Append(media);
appNonVisualDrawingPropertiesExtensionList.Append(appNonVisualDrawingPropertiesExtension);
appNonVisualDrawingProperties.Append(appNonVisualDrawingPropertiesExtensionList);

nonVisualPictureProperties.Append(nonVisualDrawingProperties);
nonVisualPictureProperties.Append(nonVisualPictureDrawingProperties);
nonVisualPictureProperties.Append(appNonVisualDrawingProperties);

//Prepare shape properties to display picture
BlipFill blipFill = new BlipFill();
A.Blip blip = new A.Blip() { Embed = imgEmbedId };
// </Snippet4>

A.Stretch stretch = new A.Stretch();
A.FillRectangle fillRectangle = new A.FillRectangle();
A.Transform2D transform2D = new A.Transform2D();
A.Offset offset = new A.Offset() { X = 1524000L, Y = 857250L };
A.Extents extents = new A.Extents() { Cx = 9144000L, Cy = 5143500L };
A.PresetGeometry presetGeometry = new A.PresetGeometry() { Preset = A.ShapeTypeValues.Rectangle };
A.AdjustValueList adjValueList = new A.AdjustValueList();

stretch.Append(fillRectangle);
blipFill.Append(blip);
blipFill.Append(stretch);
transform2D.Append(offset);
transform2D.Append(extents);
presetGeometry.Append(adjValueList);

ShapeProperties shapeProperties = new ShapeProperties();
shapeProperties.Append(transform2D);
shapeProperties.Append(presetGeometry);

//adds all elements to the slide's shape tree
picture.Append(nonVisualPictureProperties);
picture.Append(blipFill);
picture.Append(shapeProperties);

shapeTree.Append(picture);
}
}
// </Snippet0>
1 change: 1 addition & 0 deletions samples/presentation/add_video/cs/add_video_cs.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<Project Sdk="Microsoft.NET.Sdk"/>
Loading