C# tutorial: embed a video in a PDF file


Embedding a video in a PDF file

iTextSharp allows you to attach different types of files in a PDF document. In the PDF File Attachment tutorial you learnt how to attached image files to a PDF document. In this tutorial, you will learn how to embed a video file to a PDF document.

To embed a video file to the PDF document, first you need to create a movie annotation to refer to the video file by using the CreateScreen method of the PdfAnnotation class. This method has five parameters: an instance of PdfWriter, a rectange refering to a location and size to display the video on the PDF document, a video title, an instance of PdfFileSpecification, a mine type, and a boolean value to tell a PDF viewer to play the video on start. When you create the instance of the PdfFileSpecification class, you will specify the PdfWriter instance, file path, file name used in the PDF, and a byte array with he file. If it is not null it takes precedence over file path.

You can create an movie annotation using the media types shown in table below. Then you need to add the movie annotation to the PDF by using the AddAnnotation method of the PdfWriter.

Extension Mine Tye
.aiff audio/aiff
.au audio/basic
.avi video/avi
.mid audio/midi
.mov video/quicktime
.mp4 video/mp4
.mp4 audio/mp4
.mpeg video/mpeg
.smil application/smil
.swf application/x-shockwave-flash

Note: The PDF viewer will warn you about possible security hazards before you can play a movie or any other multimedia file.

/create a document object
var doc = new Document();
//output file path
String outfile = "d:/pdfdoc.pdf";
//get PdfWriter object
PdfWriter writer=PdfWriter.GetInstance(doc, new FileStream(outfile, FileMode.Create));
//open the document for writing
doc.Open();
//Create an instance of PdfFileSpecification
PdfFileSpecification fs = PdfFileSpecification.FileEmbedded(writer, "d:/bailey.mpg", "bailey.mpg", null);
//create and add a movie annotation to PDF document
writer.AddAnnotation(PdfAnnotation.CreateScreen(writer, new Rectangle(200f, 700f, 400f, 800f), "Bailey", fs,"video/mpeg", true));
//close the document
doc.Close();
//view the result pdf file
System.Diagnostics.Process.Start(outfile);


pdf embed video or movie file



Comments

Sagar comment

 Sagar

When I open the PDF in mac, the video is not visible. And I also wanted to know how to make the video to start playing when scrolled to its position.
Thank you.


2017-05-25
Kirthi comment

 Kirthi

Hi,
Thanks for the code.

Can we able to annotate a video with player controls in PDF ?


2015-10-30
kevin comment

 kevin

This is a very nice Tutorial about creating pdf page.
It helped me to create an office database business card application. Thank you.


2015-03-23



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.