2025 AI Readiness Report: Survey Insights on Enterprise AI Maturity – Now Available!

Working with PDF RichMedia Annotations

By Apryse | 2013 Sep 11

Sanity Image
Read time

7 min

RichMedia annotations can be used to embed videos and sound files in PDF documents. When opened in a compliant reader, the user can play them back directly as part of the page content, usually by just clicking on the video. This blog will examine how a RichMedia annotation is embedded in PDF, how PDFNet can be used to extract and create the movie data for playback.

How to Open and Playback a PDF RichMedia Annotation

Copied to clipboard

The simplest way to play back a movie annotation is to take advantage of the host operating system’s APIs that are capable of playing video content. For example, both iOS and Android have built in controls that take video files and present them to the user with controls to start and stop playback. The input files for these controls can be created by extracting the raw video data from the RichMedia annotation and saving it (temporarily) to disk. The basic steps are:

  1. Extract the movie file from the RichMedia annotation and save it to disk. (You may want to check that the operating system supports the particular video’s encoding.)
  2. Add a video playback widget directly over the annotation.
  3. Begin playback.

Extracting the Media File

Copied to clipboard

Here is a closeup look at what is actually inside the PDF file. This low-level content is abstracted by PDFNet, but it may be of interest to know what’s going on under the hood. The media file information can be found in the “Assets” name tree, which is inside the RichMediaPresentation dictionary of the RichMedia annotation entry. This raw PDF content shows what’s in the Assets entry:

To extract the video.mp4 file we need to iterate through the annotation assets tree until we find the video content. The following code performs this (code is in Java, but any of PDFNet’s supported languages could be used as well):

Note: the code snippets that follow below assume that a pdftron.PDF.PDFViewCtrl view is being used to display PDF documents, represented as the “mPDFView” member variable.

The method getRichMediaFile() iterates through all the annotations of the page being viewed, checking for the first one that has the RichMedia type. If the annotation is found, it tries to extract the media to a (temporary) file. The isMediaFileSupported() method is used to do a pre-check on the file and see if it is supported on the current platform (for example, the supported formats on Android can be seen here).

Playing Back the Media File

Copied to clipboard

Once you extract the file it can then be passed to appropriate APIs that are available on your platform. For example, you can open the file using an external media player, or in case you are creating an Android app, you can use a VideoView widget, etc.

The code below shows how to playback the video file using a VideoView widget on Android. The VideoView is sized and positioned according to the annotation on the page (another option, for example, would be to launch an external media player):

The setupAndPlayMedia() creates a VideoView widget object and its size and position are calculated based on the RichMedia annotation present in the page using the adjustViewPosition() method. For example, you can use the following code to start and stop playing back a RichMedia annotation:

Creating a RichMedia Annotation

Copied to clipboard

The code snippet below shows how to create a RichMedia annotation using the PDFNet API. It will create a new document with a RichMedia annotation placed in the lower left corner of the first page. The annotation object is first created, and then filled with all the information required by the PDF specification.

Further Reading

Copied to clipboard

More information on RichMedia annotations can also be found in our forums, including the following two discussions:

Sanity Image

Apryse

Share this post

email
linkedIn
twitter