-
Notifications
You must be signed in to change notification settings - Fork 68
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add video #334
Open
mkaszewiak
wants to merge
16
commits into
OfficeDev:main
Choose a base branch
from
mkaszewiak:addVideo
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add video #334
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
c99ff5a
added c#/vb code
mkaszewiak bcecedb
Merge branch 'main' into addVideo
mkaszewiak 798e91f
added code snipets
mkaszewiak 86255f9
fixed build warnings
mkaszewiak c421671
revert unintended change in insert_a_new_slideto_cs.csproj
mkaszewiak df45c27
Reverted to match main insert_a_new_slideto_cs.csproj
mkaszewiak fc7e950
removed .Net framework target
mkaszewiak ed67d1c
Resolved conflicts
mkaszewiak 62a8d4c
fixing typo
mkaszewiak 7978539
Update samples/presentation/add_video/cs/Program.cs
mkaszewiak bc66787
Update samples/presentation/add_video/cs/Program.cs
mkaszewiak 83d351e
Update samples/presentation/add_video/cs/Program.cs
mkaszewiak b869034
Update samples/presentation/add_video/cs/Program.cs
mkaszewiak 8db9e3b
applied comments
mkaszewiak 4283f96
Update how-to-add-a-video-to-a-slide-in-a-presentation.md
AlfredHellstern b35f7d8
Updated ms.assetid
mkaszewiak File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
138 changes: 138 additions & 0 deletions
138
docs/presentation/how-to-add-a-video-to-a-slide-in-a-presentation.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
|
||
© [!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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
//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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"/> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 consistentThere was a problem hiding this comment.
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