Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Creates a new PDF with a Rich Media Annotation on the first page that plays a streaming video.
* Copyright Datalogics, Inc. 2015
package pdfjt.cookbook.annotations.richmedia;
import com.adobe.pdfjt.core.types.ASName;
import com.adobe.pdfjt.core.types.ASRectangle;
import com.adobe.pdfjt.core.types.ASString;
import com.adobe.pdfjt.pdf.document.PDFDocument;
import com.adobe.pdfjt.pdf.document.PDFEmbeddedFile;
import com.adobe.pdfjt.pdf.document.PDFFileSpecification;
import com.adobe.pdfjt.pdf.document.PDFNamedEmbeddedFiles;
import com.adobe.pdfjt.pdf.document.PDFOpenOptions;
import com.adobe.pdfjt.pdf.document.PDFText;
import com.adobe.pdfjt.pdf.filters.PDFFilterFlate;
import com.adobe.pdfjt.pdf.interactive.annotation.PDFAnnotationRichMedia;
import com.adobe.pdfjt.pdf.interactive.annotation.PDFAppearance;
import com.adobe.pdfjt.pdf.multimedia.PDFRichMediaActivation;
import com.adobe.pdfjt.pdf.multimedia.PDFRichMediaConfiguration;
import com.adobe.pdfjt.pdf.multimedia.PDFRichMediaConfigurationList;
import com.adobe.pdfjt.pdf.multimedia.PDFRichMediaContent;
import com.adobe.pdfjt.pdf.multimedia.PDFRichMediaDeactivation;
import com.adobe.pdfjt.pdf.multimedia.PDFRichMediaInstance;
import com.adobe.pdfjt.pdf.multimedia.PDFRichMediaInstanceList;
import com.adobe.pdfjt.pdf.multimedia.PDFRichMediaParams;
import com.adobe.pdfjt.pdf.multimedia.PDFRichMediaSettings;
import pdfjt.util.SampleFileServices;
import java.awt.image.BufferedImage;
import javafx.scene.paint.Color;
import javax.imageio.ImageIO;
* Creates a new PDF with a Rich Media Annotation on the first page that plays a streaming video.
public class InsertRichMediaAnnotation_StreamingVideo {
* 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 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
* adds a parameter that links to the movie file itself.
* Acrobat then constructs the correct set of FlashVars to tell the video
* player what to play and how.
private static final String inputURL = "";
private static final String inputVideoURL = "";
private static final String inputPlayerName = "DatalogicsVideoPlayer.swf";
private static final String inputControllerName = "VideoControler.swf";
private static final String inputVideoPosterName = "JWST_Mission_Trailer.jpg";
private static String outputFilePath = "cookbook/Annotations/output/JWST_Mission_Trailer.pdf";
public static void main(String[] args) throws Exception {
// Create a new document that is the same size as the video.
ASRectangle pageRectangle = new ASRectangle(0, 0, 960, 720);
PDFDocument pdfDocument = PDFDocument.newInstance(pageRectangle, PDFOpenOptions.newInstance());
* We start by reading in the two files that we need to create the
* assets for the RichMedia Annotation. The video will be referenced by
* a link in the FlashVars.
URL playerURL = new URL(inputURL+"VideoPlayer/"+inputPlayerName);
File playerLocalFile = new File(inputPlayerName);
FileUtils.copyURLToFile(playerURL, playerLocalFile);
LazyRandomAccessFileByteReader playerReader = new LazyRandomAccessFileByteReader(playerLocalFile);
URL controllerURL = new URL(inputURL+"VideoPlayer/"+inputControllerName);
File controllerLocalFile = new File(inputControllerName);
FileUtils.copyURLToFile(controllerURL, controllerLocalFile);
LazyRandomAccessFileByteReader controllerReader = new LazyRandomAccessFileByteReader(controllerLocalFile);
PDFEmbeddedFile controllerFile = PDFEmbeddedFile.newInstance(pdfDocument, null, controllerReader);
PDFEmbeddedFile playerFile = PDFEmbeddedFile.newInstance(pdfDocument, null, playerReader);
controllerFile.setFilter(PDFFilterFlate.newInstance(pdfDocument, null));
playerFile.setFilter(PDFFilterFlate.newInstance(pdfDocument, null));
PDFFileSpecification playerFileSpec = PDFFileSpecification.newInstance(pdfDocument, new ASString(playerLocalFile.getName()),
PDFFileSpecification controllerFileSpec = PDFFileSpecification.newInstance(pdfDocument,
new ASString(controllerLocalFile.getName()), controllerFile);
* Once we have the files read in and converted to Objects that can be
* used in the PDF, we add them to an embedded files list.
PDFNamedEmbeddedFiles pdfNamedEmbeddedFiles = PDFNamedEmbeddedFiles.newInstance(pdfDocument);
pdfNamedEmbeddedFiles.addEntry(new ASString(controllerLocalFile.getName()), controllerFileSpec);
pdfNamedEmbeddedFiles.addEntry(new ASString(playerLocalFile.getName()), playerFileSpec);
* The we can set the assets for the Annotation
PDFRichMediaContent pdfRichMediaContent = PDFRichMediaContent.newInstance(pdfDocument);
* Now we need to create a configuration dictionary which tells the
* viewer which of the assets is the actual player.
PDFRichMediaConfiguration pdfRichMediaConfiguration = PDFRichMediaConfiguration.newInstance(pdfDocument);
* Next we need to create the set of parameters that get passed to the
* player telling it what to play and how. The player SWF needs to be
* able to interpret these FlashVars. Because the player SWF included in
* the sample is based on the one that is installed with Acrobat, it
* uses the same FlashVars. If you've developed your own player, it may
* have different FlashVars.
PDFRichMediaParams pdfRichMediaParams = PDFRichMediaParams.newInstance(pdfDocument);
Boolean autoHideSkin = true;
Color skinBackgroundColor = Color.CORNFLOWERBLUE;
Double skinBackgroundAlpha = 0.75;
Double initialVolume = .50;
"source=" + inputVideoURL +
"&skin=" + inputControllerName +
"&skinAutoHide=" + Boolean.toString(autoHideSkin) +
"&skinBackgroundColor=" + skinBackgroundColor.toString() +
"&skinBackgroundAlpha=" + skinBackgroundAlpha.toString() +
"&volume=" + initialVolume.toString()));
* Now we create an Instance dictionary, add it to the Configuration
* dictionary and then set the configuration.
PDFRichMediaInstance pdfRichMediaInstance = PDFRichMediaInstance.newInstance(pdfDocument, ASName.k_Flash, playerFileSpec);
PDFRichMediaInstanceList pdfRichMediaInstanceList = PDFRichMediaInstanceList.newInstance(pdfDocument);
PDFRichMediaConfigurationList pdfRichMediaConfigurationList = PDFRichMediaConfigurationList.newInstance(pdfDocument);
PDFPage pdfPage = pdfDocument.requirePages().getPage(0);
PDFAnnotationRichMedia pdfAnnotationRichMedia = PDFAnnotationRichMedia.newInstance(pdfDocument, pdfPage.getMediaBox(),
* Then we add the annotation to the page. At this point we have a fully
* functional embedded RichMedia movie annotation and it will
* automatically play when the page is visible. The code below here is
* optional and will allow you to control when the annotation is
* activated and deactivated.
* We can set when the RichMedia Annotation will be activated (enabled)
* and deactivated (disabled). To make this section easier to read, I've
* set up some ASNames matching the Acrobat UI.
ASName enableWhenTheContentIsClicked = ASName.k_XA;
ASName enableWhenThePageContainingTheContentIsOpened = ASName.k_PO;
ASName enableWhenThePageContainingTheContentIsVisible = ASName.k_PV;
ASName disableWhenDisableContentIsSelectedFromTheContextMenu = ASName.k_XA;
ASName disableWhenThePageContainingTheContentIsClosed = ASName.k_PC;
ASName disableWhenThePageContainingTheContentIsNotVisible = ASName.k_PI;
* Now we can create and set the RichMedia Settings Dictionary to
* control the activation and deactivation settings.
PDFRichMediaActivation pdfRichMediaActivation = PDFRichMediaActivation.newInstance(pdfDocument);
PDFRichMediaDeactivation pdfRichMediaDeactivation = PDFRichMediaDeactivation.newInstance(pdfDocument);
PDFRichMediaSettings pdfRichMediaSettings = PDFRichMediaSettings.newInstance(pdfDocument);
* Create and add an Appearance (Poster) to the Rich Media Annotation.
* This is what displays when the annotation is not active or when the
* file is being displayed in a viewer that can't show Rich Media.
BufferedImage bufferedImage = URL(inputURL+inputVideoPosterName).openStream());
PDFXObjectForm pdfXObjectForm = ImageManager.getXObjPDFImage(pdfDocument, bufferedImage, new XObjectUseOptions());
PDFAppearance pdfAppearance = PDFAppearance.newInstance(pdfDocument);
// Save out the PDF Document just created.
SampleFileServices.savePDFDocument(outputFilePath, pdfDocument);
System.out.println("Successful output to " + outputFilePath);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.