I need to embed a MP4 (h.264 + aac) video into a PDF file. Used code similar to an example posted in Aspose web site show below. The resulting PDF only has a link to the video and it is not embedded. Also it would be preferable to load the video from a stream rather than a local file.
Page lastPage = orig.Pages[orig.Pages.Count - 1];
Aspose.Pdf.InteractiveFeatures.Annotations.MovieAnnotation mov = new Aspose.Pdf.InteractiveFeatures.Annotations.MovieAnnotation(
lastPage,
new Aspose.Pdf.Rectangle(4f * 72f, 3.5f * 72f, 3f * 72f, 4f * 72f),
tmpfile
);
lastPage.Annotations.Add(mov);
The issues you have found earlier (filed as PDFNET-39606) have been fixed in Aspose.Pdf for .NET 17.3.0.
This message was posted using Notification2Forum from Downloads module by Aspose Notifier.
Can you post a working example? The above code still seems to just create a link and not actually embed the movie file.
Hi Mark,
Thanks for your inquiry. In reference to the resolved issue (PDFNET-39606), we have implemented a new annotation RichMediaAnnotation
in Aspose.Pdf for .NET 17.3.0. Please note that MovieAnnotation
can just play a link to the external video file (placed on disk or the internet). However, RichMediaAnnotation
is used to play media stored within the PDF document.
In fact, RichMediaAnnotation
is a container for the SWF (Flash) script. Due to license restrictions, we cannot include third-party flash scripts in our product, so you should provide your script for playing video or audio. For example, you can use VideoPlayer
and AudioPlayer
shipped with Adobe Acrobat. It is placed in the Acrobat folder Acrobat\Multimedia Skins\Players
.
Please check the following sample code for embedding video/audio files in the PDF document using RichMediaAnnotation
.
Aspose.Pdf.Document doc = new Aspose.Pdf.Document();
Page page = doc.Pages.Add();
RichMediaAnnotation rma = new RichMediaAnnotation(page, new Aspose.Pdf.Rectangle(100, 500, 300, 600));
//here we should specify a stream containing code of the video player
rma.CustomPlayer = new FileStream(@"Acrobat\Multimedia Skins\Players\Videoplayer.swf", FileMode.Open, FileAccess.Read);
//give name to video data. This data will be embedded into the document with this name and referenced from flash variables by this name.
// videoName should not contain the path to the file; this is rather "key" to access data inside of the PDF document
string videoName = "VideoTutorial.mp4";
//also we use skin for the video player
string skinName = "SkinOverAllNoFullNoCaption.swf";
//compose flash variables line for the player. please note that different players may have different formats of the flash variables line. Refer to documentation for your player.
rma.CustomFlashVariables = String.Format("source={0}&skin={1}", "VideoTutorial.mp4", skinName);
//add skin code.
rma.AddCustomData(skinName, new FileStream(@"Acrobat\Multimedia Skins\SkinOverAllNoFullNoCaption.swf", FileMode.Open, FileAccess.Read));
//set poster for video
rma.SetPoster(new FileStream(myDir + "Aspose.jpg", FileMode.Open, FileAccess.Read));
Stream fs = new FileStream(myDir + videoName, FileMode.Open, FileAccess.Read);
//set video content
rma.SetContent(videoName, fs);
//set type of the content (video)
rma.Type = RichMediaAnnotation.ContentType.Video;
//active player by click
rma.ActivateOn = RichMediaAnnotation.ActivationEvent.Click;
//update annotation data. This method should be called after all assignments/setup. This method initializes data structure of the annotation and embeds required data.
rma.Update();
//add annotation on the page.
page.Annotations.Add(rma);
doc.Save(myDir + "39606-1.pdf");
Best Regards,
@wajayme
Thank you for the suggestion but this topic covers how to embed a video in a PDF file programmatically.