|
1 | | -using DocumentFormat.OpenXml; |
2 | | -using DocumentFormat.OpenXml.Presentation; |
3 | | -using A = DocumentFormat.OpenXml.Drawing; |
4 | | -using P14 = DocumentFormat.OpenXml.Office2010.PowerPoint; |
5 | | -using ShapeTree = DocumentFormat.OpenXml.Presentation.ShapeTree; |
6 | | -using ShapeProperties = DocumentFormat.OpenXml.Presentation.ShapeProperties; |
7 | | -using NonVisualDrawingProperties = DocumentFormat.OpenXml.Presentation.NonVisualDrawingProperties; |
8 | | -using NonVisualPictureProperties = DocumentFormat.OpenXml.Presentation.NonVisualPictureProperties; |
9 | | -using NonVisualPictureDrawingProperties = DocumentFormat.OpenXml.Presentation.NonVisualPictureDrawingProperties; |
10 | | -using Picture = DocumentFormat.OpenXml.Presentation.Picture; |
11 | | -using BlipFill = DocumentFormat.OpenXml.Presentation.BlipFill; |
12 | | -using DocumentFormat.OpenXml.Packaging; |
13 | | -using ApplicationNonVisualDrawingProperties = DocumentFormat.OpenXml.Presentation.ApplicationNonVisualDrawingProperties; |
14 | | -using System.IO; |
15 | | -using System.Linq; |
16 | | -using System; |
17 | | - |
18 | | -// <Snippet0> |
19 | | -AddAudio(args[0], args[1], args[2]); |
20 | | - |
21 | | -static void AddAudio(string filePath, string audioFilePath, string coverPicPath) |
22 | | -{ |
23 | | - |
24 | | - string imgEmbedId = "rId4", embedId = "rId3", mediaEmbedId = "rId2"; |
25 | | - UInt32Value shapeId = 5; |
26 | | - // <Snippet1> |
27 | | - using (PresentationDocument presentationDocument = PresentationDocument.Open(filePath, true)) |
28 | | - // </Snippet1> |
29 | | - { |
30 | | - |
31 | | - if (presentationDocument.PresentationPart == null || presentationDocument.PresentationPart.Presentation.SlideIdList == null) |
32 | | - { |
33 | | - throw new NullReferenceException("Presentation Part is empty or there are no slides in it"); |
34 | | - } |
35 | | - |
36 | | - // <Snippet2> |
37 | | - //Get presentation part |
38 | | - PresentationPart presentationPart = presentationDocument.PresentationPart; |
39 | | - |
40 | | - //Get slides ids. |
41 | | - OpenXmlElementList slidesIds = presentationPart.Presentation.SlideIdList.ChildElements; |
42 | | - |
43 | | - //Get relationsipId of the last slide |
44 | | - string? audioSlidePartRelationshipId = ((SlideId)slidesIds[slidesIds.ToArray().Length - 1]).RelationshipId; |
45 | | - |
46 | | - if (audioSlidePartRelationshipId == null) |
47 | | - { |
48 | | - throw new NullReferenceException("Slide id not found"); |
49 | | - } |
50 | | - |
51 | | - //Get slide part by relationshipID |
52 | | - SlidePart? slidePart = (SlidePart)presentationPart.GetPartById(audioSlidePartRelationshipId); |
53 | | - // </Snippet2> |
54 | | - |
55 | | - // <Snippet3> |
56 | | - // Create audio Media Data Part (content type, extension) |
57 | | - MediaDataPart mediaDataPart = presentationDocument.CreateMediaDataPart("audio/mp3", ".mp3"); |
58 | | - |
59 | | - //Get the audio file and feed the stream |
60 | | - using (Stream mediaDataPartStream = File.OpenRead(audioFilePath)) |
61 | | - { |
62 | | - mediaDataPart.FeedData(mediaDataPartStream); |
63 | | - } |
64 | | - //Adds a AudioReferenceRelationship to the MainDocumentPart |
65 | | - slidePart.AddAudioReferenceRelationship(mediaDataPart, embedId); |
66 | | - |
67 | | - //Adds a MediaReferenceRelationship to the SlideLayoutPart |
68 | | - slidePart.AddMediaReferenceRelationship(mediaDataPart, mediaEmbedId); |
69 | | - |
70 | | - NonVisualDrawingProperties nonVisualDrawingProperties = new NonVisualDrawingProperties() { Id = shapeId, Name = "audio" }; |
71 | | - A.AudioFromFile audioFromFile = new A.AudioFromFile() { Link = embedId }; |
72 | | - |
73 | | - ApplicationNonVisualDrawingProperties appNonVisualDrawingProperties = new ApplicationNonVisualDrawingProperties(); |
74 | | - appNonVisualDrawingProperties.Append(audioFromFile); |
75 | | - |
76 | | - //adds sample image to the slide with id to be used as reference in blip |
77 | | - ImagePart imagePart = slidePart.AddImagePart(ImagePartType.Png, imgEmbedId); |
78 | | - using (Stream data = File.OpenRead(coverPicPath)) |
79 | | - { |
80 | | - imagePart.FeedData(data); |
81 | | - } |
82 | | - |
83 | | - if (slidePart!.Slide!.CommonSlideData!.ShapeTree == null) |
84 | | - { |
85 | | - throw new NullReferenceException("Presentation shape tree is empty"); |
86 | | - } |
87 | | - |
88 | | - //Getting existing shape tree element from PowerPoint |
89 | | - ShapeTree shapeTree = slidePart.Slide.CommonSlideData.ShapeTree; |
90 | | - |
91 | | - // specifies the existence of a picture within a presentation. |
92 | | - // It can have non-visual properties, a picture fill as well as shape properties attached to it. |
93 | | - Picture picture = new Picture(); |
94 | | - NonVisualPictureProperties nonVisualPictureProperties = new NonVisualPictureProperties(); |
95 | | - |
96 | | - A.HyperlinkOnClick hyperlinkOnClick = new A.HyperlinkOnClick() { Id = "", Action = "ppaction://media" }; |
97 | | - nonVisualDrawingProperties.Append(hyperlinkOnClick); |
98 | | - |
99 | | - NonVisualPictureDrawingProperties nonVisualPictureDrawingProperties = new NonVisualPictureDrawingProperties(); |
100 | | - A.PictureLocks pictureLocks = new A.PictureLocks() { NoChangeAspect = true }; |
101 | | - nonVisualPictureDrawingProperties.Append(pictureLocks); |
102 | | - |
103 | | - ApplicationNonVisualDrawingPropertiesExtensionList appNonVisualDrawingPropertiesExtensionList = new ApplicationNonVisualDrawingPropertiesExtensionList(); |
104 | | - ApplicationNonVisualDrawingPropertiesExtension appNonVisualDrawingPropertiesExtension = new ApplicationNonVisualDrawingPropertiesExtension() { Uri = "{DAA4B4D4-6D71-4841-9C94-3DE7FCFB9230}" }; |
105 | | - // </Snippet3> |
106 | | - |
107 | | - // <Snippet4> |
108 | | - P14.Media media = new() { Embed = mediaEmbedId }; |
109 | | - media.AddNamespaceDeclaration("p14", "http://schemas.microsoft.com/office/powerpoint/2010/main"); |
110 | | - |
111 | | - appNonVisualDrawingPropertiesExtension.Append(media); |
112 | | - appNonVisualDrawingPropertiesExtensionList.Append(appNonVisualDrawingPropertiesExtension); |
113 | | - appNonVisualDrawingProperties.Append(appNonVisualDrawingPropertiesExtensionList); |
114 | | - |
115 | | - nonVisualPictureProperties.Append(nonVisualDrawingProperties); |
116 | | - nonVisualPictureProperties.Append(nonVisualPictureDrawingProperties); |
117 | | - nonVisualPictureProperties.Append(appNonVisualDrawingProperties); |
118 | | - |
119 | | - //Prepare shape properties to display picture |
120 | | - BlipFill blipFill = new BlipFill(); |
121 | | - A.Blip blip = new A.Blip() { Embed = imgEmbedId }; |
122 | | - // </Snippet4> |
123 | | - |
124 | | - A.Stretch stretch = new A.Stretch(); |
125 | | - A.FillRectangle fillRectangle = new A.FillRectangle(); |
126 | | - A.Transform2D transform2D = new A.Transform2D(); |
127 | | - A.Offset offset = new A.Offset() { X = 1524000L, Y = 857250L }; |
128 | | - A.Extents extents = new A.Extents() { Cx = 9144000L, Cy = 5143500L }; |
129 | | - A.PresetGeometry presetGeometry = new A.PresetGeometry() { Preset = A.ShapeTypeValues.Rectangle }; |
130 | | - A.AdjustValueList adjValueList = new A.AdjustValueList(); |
131 | | - |
132 | | - stretch.Append(fillRectangle); |
133 | | - blipFill.Append(blip); |
134 | | - blipFill.Append(stretch); |
135 | | - transform2D.Append(offset); |
136 | | - transform2D.Append(extents); |
137 | | - presetGeometry.Append(adjValueList); |
138 | | - |
139 | | - ShapeProperties shapeProperties = new ShapeProperties(); |
140 | | - shapeProperties.Append(transform2D); |
141 | | - shapeProperties.Append(presetGeometry); |
142 | | - |
143 | | - //adds all elements to the slide's shape tree |
144 | | - picture.Append(nonVisualPictureProperties); |
145 | | - picture.Append(blipFill); |
146 | | - picture.Append(shapeProperties); |
147 | | - |
148 | | - shapeTree.Append(picture); |
149 | | - |
150 | | - } |
151 | | -} |
152 | | -// </Snippet0> |
| 1 | +using DocumentFormat.OpenXml; |
| 2 | +using DocumentFormat.OpenXml.Presentation; |
| 3 | +using A = DocumentFormat.OpenXml.Drawing; |
| 4 | +using P14 = DocumentFormat.OpenXml.Office2010.PowerPoint; |
| 5 | +using ShapeTree = DocumentFormat.OpenXml.Presentation.ShapeTree; |
| 6 | +using ShapeProperties = DocumentFormat.OpenXml.Presentation.ShapeProperties; |
| 7 | +using NonVisualDrawingProperties = DocumentFormat.OpenXml.Presentation.NonVisualDrawingProperties; |
| 8 | +using NonVisualPictureProperties = DocumentFormat.OpenXml.Presentation.NonVisualPictureProperties; |
| 9 | +using NonVisualPictureDrawingProperties = DocumentFormat.OpenXml.Presentation.NonVisualPictureDrawingProperties; |
| 10 | +using Picture = DocumentFormat.OpenXml.Presentation.Picture; |
| 11 | +using BlipFill = DocumentFormat.OpenXml.Presentation.BlipFill; |
| 12 | +using DocumentFormat.OpenXml.Packaging; |
| 13 | +using ApplicationNonVisualDrawingProperties = DocumentFormat.OpenXml.Presentation.ApplicationNonVisualDrawingProperties; |
| 14 | +using System.IO; |
| 15 | +using System.Linq; |
| 16 | +using System; |
| 17 | + |
| 18 | +// <Snippet0> |
| 19 | +AddAudio(args[0], args[1], args[2]); |
| 20 | + |
| 21 | +static void AddAudio(string filePath, string audioFilePath, string coverPicPath) |
| 22 | +{ |
| 23 | + |
| 24 | + string imgEmbedId = "rId4", embedId = "rId3", mediaEmbedId = "rId2"; |
| 25 | + UInt32Value shapeId = 5; |
| 26 | + // <Snippet1> |
| 27 | + using (PresentationDocument presentationDocument = PresentationDocument.Open(filePath, true)) |
| 28 | + // </Snippet1> |
| 29 | + { |
| 30 | + |
| 31 | + if (presentationDocument.PresentationPart == null || presentationDocument.PresentationPart.Presentation.SlideIdList == null) |
| 32 | + { |
| 33 | + throw new NullReferenceException("Presentation Part is empty or there are no slides in it"); |
| 34 | + } |
| 35 | + |
| 36 | + // <Snippet2> |
| 37 | + //Get presentation part |
| 38 | + PresentationPart presentationPart = presentationDocument.PresentationPart; |
| 39 | + |
| 40 | + //Get slides ids. |
| 41 | + OpenXmlElementList slidesIds = presentationPart.Presentation.SlideIdList.ChildElements; |
| 42 | + |
| 43 | + //Get relationsipId of the last slide |
| 44 | + string? audioSlidePartRelationshipId = ((SlideId)slidesIds[slidesIds.ToArray().Length - 1]).RelationshipId; |
| 45 | + |
| 46 | + if (audioSlidePartRelationshipId == null) |
| 47 | + { |
| 48 | + throw new NullReferenceException("Slide id not found"); |
| 49 | + } |
| 50 | + |
| 51 | + //Get slide part by relationshipID |
| 52 | + SlidePart? slidePart = (SlidePart)presentationPart.GetPartById(audioSlidePartRelationshipId); |
| 53 | + // </Snippet2> |
| 54 | + |
| 55 | + // <Snippet3> |
| 56 | + // Create audio Media Data Part (content type, extension) |
| 57 | + MediaDataPart mediaDataPart = presentationDocument.CreateMediaDataPart("audio/mp3", ".mp3"); |
| 58 | + |
| 59 | + //Get the audio file and feed the stream |
| 60 | + using (Stream mediaDataPartStream = File.OpenRead(audioFilePath)) |
| 61 | + { |
| 62 | + mediaDataPart.FeedData(mediaDataPartStream); |
| 63 | + } |
| 64 | + //Adds a AudioReferenceRelationship to the MainDocumentPart |
| 65 | + slidePart.AddAudioReferenceRelationship(mediaDataPart, embedId); |
| 66 | + |
| 67 | + //Adds a MediaReferenceRelationship to the SlideLayoutPart |
| 68 | + slidePart.AddMediaReferenceRelationship(mediaDataPart, mediaEmbedId); |
| 69 | + |
| 70 | + NonVisualDrawingProperties nonVisualDrawingProperties = new NonVisualDrawingProperties() { Id = shapeId, Name = "audio" }; |
| 71 | + A.AudioFromFile audioFromFile = new A.AudioFromFile() { Link = embedId }; |
| 72 | + |
| 73 | + ApplicationNonVisualDrawingProperties appNonVisualDrawingProperties = new ApplicationNonVisualDrawingProperties(); |
| 74 | + appNonVisualDrawingProperties.Append(audioFromFile); |
| 75 | + |
| 76 | + //adds sample image to the slide with id to be used as reference in blip |
| 77 | + ImagePart imagePart = slidePart.AddImagePart(ImagePartType.Png, imgEmbedId); |
| 78 | + using (Stream data = File.OpenRead(coverPicPath)) |
| 79 | + { |
| 80 | + imagePart.FeedData(data); |
| 81 | + } |
| 82 | + |
| 83 | + if (slidePart!.Slide!.CommonSlideData!.ShapeTree == null) |
| 84 | + { |
| 85 | + throw new NullReferenceException("Presentation shape tree is empty"); |
| 86 | + } |
| 87 | + |
| 88 | + //Getting existing shape tree element from PowerPoint |
| 89 | + ShapeTree shapeTree = slidePart.Slide.CommonSlideData.ShapeTree; |
| 90 | + |
| 91 | + // specifies the existence of a picture within a presentation. |
| 92 | + // It can have non-visual properties, a picture fill as well as shape properties attached to it. |
| 93 | + Picture picture = new Picture(); |
| 94 | + NonVisualPictureProperties nonVisualPictureProperties = new NonVisualPictureProperties(); |
| 95 | + |
| 96 | + A.HyperlinkOnClick hyperlinkOnClick = new A.HyperlinkOnClick() { Id = "", Action = "ppaction://media" }; |
| 97 | + nonVisualDrawingProperties.Append(hyperlinkOnClick); |
| 98 | + |
| 99 | + NonVisualPictureDrawingProperties nonVisualPictureDrawingProperties = new NonVisualPictureDrawingProperties(); |
| 100 | + A.PictureLocks pictureLocks = new A.PictureLocks() { NoChangeAspect = true }; |
| 101 | + nonVisualPictureDrawingProperties.Append(pictureLocks); |
| 102 | + |
| 103 | + ApplicationNonVisualDrawingPropertiesExtensionList appNonVisualDrawingPropertiesExtensionList = new ApplicationNonVisualDrawingPropertiesExtensionList(); |
| 104 | + ApplicationNonVisualDrawingPropertiesExtension appNonVisualDrawingPropertiesExtension = new ApplicationNonVisualDrawingPropertiesExtension() { Uri = "{DAA4B4D4-6D71-4841-9C94-3DE7FCFB9230}" }; |
| 105 | + // </Snippet3> |
| 106 | + |
| 107 | + // <Snippet4> |
| 108 | + P14.Media media = new() { Embed = mediaEmbedId }; |
| 109 | + media.AddNamespaceDeclaration("p14", "http://schemas.microsoft.com/office/powerpoint/2010/main"); |
| 110 | + |
| 111 | + appNonVisualDrawingPropertiesExtension.Append(media); |
| 112 | + appNonVisualDrawingPropertiesExtensionList.Append(appNonVisualDrawingPropertiesExtension); |
| 113 | + appNonVisualDrawingProperties.Append(appNonVisualDrawingPropertiesExtensionList); |
| 114 | + |
| 115 | + nonVisualPictureProperties.Append(nonVisualDrawingProperties); |
| 116 | + nonVisualPictureProperties.Append(nonVisualPictureDrawingProperties); |
| 117 | + nonVisualPictureProperties.Append(appNonVisualDrawingProperties); |
| 118 | + |
| 119 | + //Prepare shape properties to display picture |
| 120 | + BlipFill blipFill = new BlipFill(); |
| 121 | + A.Blip blip = new A.Blip() { Embed = imgEmbedId }; |
| 122 | + // </Snippet4> |
| 123 | + |
| 124 | + A.Stretch stretch = new A.Stretch(); |
| 125 | + A.FillRectangle fillRectangle = new A.FillRectangle(); |
| 126 | + A.Transform2D transform2D = new A.Transform2D(); |
| 127 | + A.Offset offset = new A.Offset() { X = 1524000L, Y = 857250L }; |
| 128 | + A.Extents extents = new A.Extents() { Cx = 9144000L, Cy = 5143500L }; |
| 129 | + A.PresetGeometry presetGeometry = new A.PresetGeometry() { Preset = A.ShapeTypeValues.Rectangle }; |
| 130 | + A.AdjustValueList adjValueList = new A.AdjustValueList(); |
| 131 | + |
| 132 | + stretch.Append(fillRectangle); |
| 133 | + blipFill.Append(blip); |
| 134 | + blipFill.Append(stretch); |
| 135 | + transform2D.Append(offset); |
| 136 | + transform2D.Append(extents); |
| 137 | + presetGeometry.Append(adjValueList); |
| 138 | + |
| 139 | + ShapeProperties shapeProperties = new ShapeProperties(); |
| 140 | + shapeProperties.Append(transform2D); |
| 141 | + shapeProperties.Append(presetGeometry); |
| 142 | + |
| 143 | + //adds all elements to the slide's shape tree |
| 144 | + picture.Append(nonVisualPictureProperties); |
| 145 | + picture.Append(blipFill); |
| 146 | + picture.Append(shapeProperties); |
| 147 | + |
| 148 | + shapeTree.Append(picture); |
| 149 | + |
| 150 | + } |
| 151 | +} |
| 152 | +// </Snippet0> |
0 commit comments