Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JoelGeraci-Datalogics/65d1f75a05737f88b40b to your computer and use it in GitHub Desktop.
Save JoelGeraci-Datalogics/65d1f75a05737f88b40b to your computer and use it in GitHub Desktop.
Creates a new PDF with a 3D Annotation on the first page
/*
* Copyright Datalogics, Inc. 2015
*/
package pdfjt.cookbook.annotations.richmedia;
import com.adobe.internal.io.LazyRandomAccessFileByteReader;
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.graphics.xobject.PDFXObjectForm;
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 com.adobe.pdfjt.pdf.page.PDFPage;
import com.adobe.pdfjt.services.imageconversion.ImageManager;
import com.adobe.pdfjt.services.xobjhandler.XObjectUseOptions;
import pdfjt.util.SampleFileServices;
import java.awt.image.BufferedImage;
import java.io.File;
import javafx.scene.paint.Color;
import javax.imageio.ImageIO;
/**
* Creates a new PDF with a 3D Annotation on the first page.
*/
public class InsertRichMediaAnnotation_Video {
/*
* 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
* 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 inputPath = "cookbook/Annotations/input/video/";
private static final String inputVideoName = "NYC_Traffic_H264.mp4";
private static final String inputPlayerName = "DatalogicsVideoPlayer.swf";
private static final String inputControllerName = "VideoControler.swf";
private static final String inputVideoPosterName = "NYC_Traffic_H264.jpg";
private static String outputFilePath = "cookbook/Annotations/output/NYC_Traffic_H264.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, 1920, 1080);
PDFDocument pdfDocument = PDFDocument.newInstance(pageRectangle, PDFOpenOptions.newInstance());
/*
* We start by reading in the three files that we need to create the
* assets for the RichMedia Annotation.
*/
File inputVideoFile = new File(inputPath + inputVideoName);
File inputControllerFile = new File(inputPath + inputControllerName);
File inputPlayerFile = new File(inputPath + inputPlayerName);
LazyRandomAccessFileByteReader videoReader = new LazyRandomAccessFileByteReader(inputVideoFile);
LazyRandomAccessFileByteReader controllerReader = new LazyRandomAccessFileByteReader(inputControllerFile);
LazyRandomAccessFileByteReader playerReader = new LazyRandomAccessFileByteReader(inputPlayerFile);
PDFEmbeddedFile videoFile = PDFEmbeddedFile.newInstance(pdfDocument, null, videoReader);
PDFEmbeddedFile controllerFile = PDFEmbeddedFile.newInstance(pdfDocument, null, controllerReader);
PDFEmbeddedFile playerFile = PDFEmbeddedFile.newInstance(pdfDocument, null, playerReader);
videoFile.setFilter(PDFFilterFlate.newInstance(pdfDocument, null));
controllerFile.setFilter(PDFFilterFlate.newInstance(pdfDocument, null));
playerFile.setFilter(PDFFilterFlate.newInstance(pdfDocument, null));
PDFFileSpecification videoFileSpec = PDFFileSpecification.newInstance(pdfDocument, new ASString(inputVideoFile.getName()),
videoFile);
PDFFileSpecification controllerFileSpec = PDFFileSpecification.newInstance(pdfDocument,
new ASString(inputControllerFile.getName()), controllerFile);
PDFFileSpecification playerFileSpec = PDFFileSpecification.newInstance(pdfDocument, new ASString(inputPlayerFile.getName()),
playerFile);
/*
* 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(inputVideoFile.getName()), videoFileSpec);
pdfNamedEmbeddedFiles.addEntry(new ASString(inputControllerFile.getName()), controllerFileSpec);
pdfNamedEmbeddedFiles.addEntry(new ASString(inputPlayerFile.getName()), playerFileSpec);
/*
* The we can set the assets for the Annotation
*/
PDFRichMediaContent pdfRichMediaContent = PDFRichMediaContent.newInstance(pdfDocument);
pdfRichMediaContent.setAssets(pdfNamedEmbeddedFiles);
/*
* 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);
pdfRichMediaConfiguration.setSubtype(PDFRichMediaConfiguration.k_Flash);
/*
* 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;
pdfRichMediaParams.setFlashVars(PDFText.createString(pdfDocument,
"source=" + inputVideoName +
"&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);
pdfRichMediaInstance.setParams(pdfRichMediaParams);
PDFRichMediaInstanceList pdfRichMediaInstanceList = PDFRichMediaInstanceList.newInstance(pdfDocument);
pdfRichMediaInstanceList.add(pdfRichMediaInstance);
pdfRichMediaConfiguration.setInstances(pdfRichMediaInstanceList);
PDFRichMediaConfigurationList pdfRichMediaConfigurationList = PDFRichMediaConfigurationList.newInstance(pdfDocument);
pdfRichMediaConfigurationList.add(pdfRichMediaConfiguration);
pdfRichMediaContent.setConfigurations(pdfRichMediaConfigurationList);
PDFPage pdfPage = pdfDocument.requirePages().getPage(0);
PDFAnnotationRichMedia pdfAnnotationRichMedia = PDFAnnotationRichMedia.newInstance(pdfDocument, pdfPage.getMediaBox(),
pdfRichMediaContent);
/*
* 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.
*/
pdfPage.addAnnotation(pdfAnnotationRichMedia);
/*
* 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);
pdfRichMediaActivation.setCondition(enableWhenTheContentIsClicked);
PDFRichMediaDeactivation pdfRichMediaDeactivation = PDFRichMediaDeactivation.newInstance(pdfDocument);
pdfRichMediaDeactivation.setCondition(disableWhenThePageContainingTheContentIsClosed);
PDFRichMediaSettings pdfRichMediaSettings = PDFRichMediaSettings.newInstance(pdfDocument);
pdfRichMediaSettings.setActivation(pdfRichMediaActivation);
pdfRichMediaSettings.setDeactivation(pdfRichMediaDeactivation);
pdfAnnotationRichMedia.setRichMediaSettings(pdfRichMediaSettings);
/*
* 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 = ImageIO.read(new File(inputPath + inputVideoPosterName));
PDFXObjectForm pdfXObjectForm = ImageManager.getXObjPDFImage(pdfDocument, bufferedImage, new XObjectUseOptions());
PDFAppearance pdfAppearance = PDFAppearance.newInstance(pdfDocument);
pdfAppearance.setNormalAppearance(pdfXObjectForm);
pdfAnnotationRichMedia.setAppearance(pdfAppearance);
// Save out the PDF Document just created.
SampleFileServices.savePDFDocument(outputFilePath, pdfDocument);
System.out.println("Successful output to " + outputFilePath);
}
}
This file has been truncated, but you can view the full file.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment