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.
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
//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
//view the result pdf file
This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages: