public void TestDataReferenceRelationshipsAreClonedCorrectly() { using var stream = CreatePresentationDocumentWithDataReference(); using var ppt = PresentationDocument.Open(stream, true); Assert.NotNull(ppt.PresentationPart); Assert.NotNull(ppt.PresentationPart.Presentation); Assert.Single(ppt.PresentationPart.SlideParts); var slidePart = ppt.PresentationPart.SlideParts.First(); Assert.NotEmpty(slidePart.DataPartReferenceRelationships); var clonedStream = new MemoryStream(); ppt.Clone(clonedStream); using var clonedPpt = PresentationDocument.Open(clonedStream, false); Assert.NotNull(clonedPpt.PresentationPart); Assert.NotNull(clonedPpt.PresentationPart.Presentation); Assert.Single(clonedPpt.PresentationPart.SlideParts); var clonedSlidePart = clonedPpt.PresentationPart.SlideParts.First(); Assert.NotEmpty(clonedSlidePart.DataPartReferenceRelationships); // Additional test for related 1issue #949 // Verify that the id requested in AddDataPartReferenceRelationship was used. var mediaDataPart = new MediaDataPart(ppt.InternalOpenXmlPackage, MediaDataPartType.Mp3); MediaReferenceRelationship mrr = slidePart.AddMediaReferenceRelationship(mediaDataPart, "rId4"); Assert.Equal("rId4", mrr.Id); }
private void ReplaceSlideAudioAnnotationForRelationship(PresentationDocument presentationDocument, string slideRelationshipId, byte[] audioFile) { var presentationPart = presentationDocument.PresentationPart; SlidePart slidePart = (SlidePart)presentationPart.GetPartById(slideRelationshipId); MediaDataPart mediaPart = (MediaDataPart)slidePart.DataPartReferenceRelationships .FirstOrDefault(dpr => (dpr.DataPart?.ContentType == "audio/x-wav" || dpr.DataPart?.ContentType == "audio/mp4") && dpr.RelationshipType.EndsWith("/relationships/media"))?.DataPart; if (mediaPart != null) { this._logger.Verbose($"Replacing contents of '{mediaPart.Uri}' media part."); byte[] encodedAAC = new byte[0]; if (mediaPart.ContentType == "audio/mp4") { this._logger.Verbose("Converting replacement audio from Wav to Mp4 AAC."); encodedAAC = _audioEncoder.FromWav(audioFile); } using (var mpStream = mediaPart.GetStream()) using (var writer = new BinaryWriter(mpStream)) { if (encodedAAC.LongLength > 0) { writer.Write(encodedAAC); } else { writer.Write(audioFile); } } this._logger.Verbose("Updating slide duration to match new audio."); using (var memStream = new MemoryStream(audioFile)) using (IWaveSource wavSource = new CSCore.Codecs.WAV.WaveFileReader(memStream)) { TimeSpan totalTime = wavSource.GetLength(); this._logger.Verbose($"New audio length: {totalTime.TotalMilliseconds}ms."); UpdateSlideAdvanceAfterTime(slidePart, totalTime.TotalMilliseconds); } } else { this._logger.Warning("Current slide has no media in it."); this._logger.Verbose("Could not find any parts with '/relationships/media' type and 'audio/x-wav' or 'audio/mp4' content types."); } }
private static MemoryStream CreatePresentationDocumentWithDataReference() { var stream = new MemoryStream(); using (var ppt = PresentationDocument.Create(stream, PresentationDocumentType.Presentation)) { var mainPart = ppt.AddPresentationPart(); var slide = new Slide(); var slidePart = mainPart.AddNewPart <SlidePart>(); var dataPart = new MediaDataPart(ppt.InternalOpenXmlPackage, MediaDataPartType.Mp3); var dataPartReferenceRelationship = DataPartReferenceRelationship.Create(slidePart, dataPart, MediaReferenceRelationship.RelationshipTypeConst, "rId2"); slidePart.AddDataPartReferenceRelationship(dataPartReferenceRelationship); slide.Save(slidePart); mainPart.Presentation = new Presentation.Presentation(slide); mainPart.Presentation.Save(); } stream.Seek(0, SeekOrigin.Begin); return(stream); }
public void AddVideo(SlidePart slidepart, string videoFilePath, string videoCoverPath, D.Transform2D transform) { Slide slide = slidepart.Slide; ShapeTree shapeTree1 = slidepart.Slide.CommonSlideData.ShapeTree; var ptrlid = Doc.PresentationPart.GetIdOfPart(slidepart); var picID = AnalysisHelper.GetMaxId(shapeTree1); string imgEmbedId = string.Format("imgId{0}{1}{2}", ptrlid, picID, 1); string videoEmbedId = string.Format("vidId{0}{1}{2}", ptrlid, picID, 2); string mediaEmbedId = string.Format("medId{0}{1}{2}", ptrlid, picID, 3); Picture picture1 = new Picture(); NonVisualPictureProperties nonVisualPictureProperties1 = new NonVisualPictureProperties(); NonVisualDrawingProperties nonVisualDrawingProperties2 = new NonVisualDrawingProperties() { Id = (UInt32Value)3U, Name = videoEmbedId + "" }; D.HyperlinkOnClick hyperlinkOnClick1 = new D.HyperlinkOnClick() { Id = "", Action = "ppaction://media" }; nonVisualDrawingProperties2.Append(hyperlinkOnClick1); NonVisualPictureDrawingProperties nonVisualPictureDrawingProperties1 = new NonVisualPictureDrawingProperties(); D.PictureLocks pictureLocks1 = new D.PictureLocks() { NoChangeAspect = true }; nonVisualPictureDrawingProperties1.Append(pictureLocks1); ApplicationNonVisualDrawingProperties applicationNonVisualDrawingProperties2 = new ApplicationNonVisualDrawingProperties(); D.VideoFromFile videoFromFile1 = new D.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(); D.Blip blip1 = new D.Blip() { Embed = imgEmbedId }; D.Stretch stretch1 = new D.Stretch(); D.FillRectangle fillRectangle1 = new D.FillRectangle(); stretch1.Append(fillRectangle1); blipFill1.Append(blip1); blipFill1.Append(stretch1); ShapeProperties shapeProperties1 = new ShapeProperties(); D.PresetGeometry presetGeometry1 = new D.PresetGeometry() { Preset = D.ShapeTypeValues.Rectangle }; D.AdjustValueList adjustValueList1 = new D.AdjustValueList(); presetGeometry1.Append(adjustValueList1); shapeProperties1.Append(transform); shapeProperties1.Append(presetGeometry1); picture1.Append(nonVisualPictureProperties1); picture1.Append(blipFill1); picture1.Append(shapeProperties1); shapeTree1.Append(picture1); if (!(slide.Timing?.ChildElements?.Count > 0)) { AnalysisHelper.InitTiming(slide); } ImagePart imagePart = slidepart.AddImagePart(AnalysisHelper.GetImagePartType(videoCoverPath), imgEmbedId); using (var data = File.OpenRead(videoCoverPath)) { imagePart.FeedData(data); }; Doc.PartExtensionProvider.AddPartExtension("video/mp4", ".mp4"); MediaDataPart mediaDataPart1 = Doc.CreateMediaDataPart("video/mp4", ".mp4"); using (System.IO.Stream mediaDataPart1Stream = File.OpenRead(videoFilePath)) { mediaDataPart1.FeedData(mediaDataPart1Stream); } slidepart.AddVideoReferenceRelationship(mediaDataPart1, videoEmbedId); slidepart.AddMediaReferenceRelationship(mediaDataPart1, mediaEmbedId); slide.Save(); }