Embedding Video in PDF files using the Datalogics PDF Java Toolkit

Embedding Video in PDF files using the Datalogics PDF Java Toolkit

Sample of the Week:

Judicious use of video in a PDF document can make the text far easier to understand and is a great way to distribute transcripts along with the video content itself. Adobe Acrobat and some other PDF authoring tools allow end users to embed video in a PDF document. This article discusses how to use the Datalogics PDF Java Toolkit to add video to a PDF in much the same way as Acrobat.

What You Need to Know First:
When you use Adobe Acrobat to add a movie to a PDF page, you aren’t exactly adding the movie directly to the page; Acrobat attached the movie you select plus a few other files that get installed on your drive along with Acrobat. After selecting the movie tool, Acrobat presents a dialog that let’s you select the movie file and set some options. What Acrobat then does is create a RichMedia Annotation then embeds a Flash movie player SWF file, a Flash video controller SWF file, and then the movie file itself. Acrobat automatically constructs the correct set of FlashVars to tell the video player what to play and how based on your selections in the dialog.

A video player and controller are supplied as input files with this Gist along with a public domain H.264 file. You’ll need these input files to run the Gist. The path to save them to is in the code.

Because the supplied video player is based on the one that is distributed with Adobe Acrobat, it uses the same parameters and JavaScript callbacks so it is interoperable with Acrobat. The only significant difference is that it will overlay the Datalogics logo on the video. However, you can easily substitute your own video player.

Embedding the Video:
We need to start by reading in the three files that we will need to create the assets that are attached to the PDFAnnotationRichMedia Object. We need to get the files from the way they are stored on the drive to an object that can be stored in a PDF.

Once we have the files set as PDFEmbeddedFile Objects, we can add them to a list of PDFNamedEmbeddedFiles and then the viewer can reference them by name.

With the files embedded, we can set up a configuration list that instructs the viewer which of the embedded files are to be used for which purposes.

Finally, we can tell the annotation when to activate and deactivate by creating a settings dictionary.

To get started with video in PDF files, download this Gist, it’s input files which are also in the Gist, and evaluation copy of The Datalogics PDF Java Toolkit.

4 thoughts on “Embedding Video in PDF files using the Datalogics PDF Java Toolkit

  1. Hi Joel, any chance this tool kit will also allow embedding a url (like the Youtube widget you developed) or a Java app (like Processing) directly into the pdf?

    1. Steve:

      Great question and something that I was planning on blogging about later but since you brought it up… I created a new Gist to demonstrate streaming a video from a URL into the same player used in the post. You still need to embed the player and control bar and that part of the code is the same. The only significant difference is that the “source” in the FlashVars String used by the PDFRichMediaParams Object references the fully qualified URL to the video. Note: You *must* provide a URL to the video file, it can’t be a YouTube share link with this particular player. That said, this programmatic method of creating RichMedia video assets lets you add any custom player you want. According to the PDF specification, it doesn’t even need to be a Flash (.SWF) file though there’s no guarantee that Acrobat or Reader would know what to do with it.

      I haven’t updated the YouTube widget in years so I doubt that it would work anymore, however, A player that accepted YouTube links could probably be developed but that’s an exercise for another day. If such a thing were to be developed, you’d simply have to adjust the FlashVars string in the PDFRichMediaParams Object to match what was required for that particular widget.

      See: InsertRichMediaAnnotation_StreamingVideo.java

      Let me know if this was what you were looking for.


  2. Hi, embedding video with Acrobat make them looks worse and less smooth…could this, changing the default player with one that is more powerful, play them better?

    1. Hey Alberto, it is possible that changing the default player would help with performance of video playback when embedding videos in a PDF.

      The other item that will help with video playback is to have the video playback in a floating window instead of in the page content. See the documentation for this under Advanced multimedia options.

Leave a Reply

Your email address will not be published.