PDFlib Cookbook

cookbook

multimedia/movie_annotations

Demonstrate a movie annotation in PDF.

Download Java Code  Switch to PHP Code  Show Output 

/*
 * RichMedia annotation for Video:
 * Demonstrate the use of a RichMedia annotation to play a Video with
 * Flash controls. 
 *
 * Required software: PDFlib/PDFlib+PDI/PPS 9
 * Required data: Movie file, Shockwave (*.swf) Video player and video skin
 */
package com.pdflib.cookbook.pdflib.multimedia;

import com.pdflib.pdflib;
import com.pdflib.PDFlibException;

public class movie_annotations {
    public static void main(String argv[]) {
        /* This is where the data files are. Adjust as necessary */
        String searchpath = "../input";
        String outfile = "movie_annotations.pdf";
        String title = "Movie Annotations";

        pdflib p = null;
        int exitcode = 0;

        // Movie file
        String moviefile = "mountains.mp4";
        
        // Auxiliary Flash files for controlling the video in PDF
        String videoplayerfile = "videoplayer.swf";
        String videoskinfile = "videoskin.swf";
        
        String optlist;
        int templ;
        int movie, videoplayer, videoskin;

        try {
            final double width=550, height=310;
            
            p = new pdflib();

            /* This means we must check return values of load_asset() etc. */
            p.set_option("errorpolicy=return");

            p.set_option("searchpath={" + searchpath + "}");

            if (p.begin_document(outfile, "compatibility=1.7ext3") == -1)
                throw new Exception(
                        "Error: " + p.get_apiname() + ": " + p.get_errmsg());

            p.set_info("Creator", "PDFlib Cookbook");
            p.set_info("Title", title);
            
            // Create template for visualizing the video before activation.
            // You could display an image; we display only a rectangle.
            
            templ = p.begin_template_ext(width, height, "");
            
            p.set_graphics_option("fillcolor=red");
            p.rect(0, 0, width, height);
            p.fill();

            p.fit_textline("Video", 10, 10,
                "fontname=NotoSerif-Regular encoding=unicode fontsize=14 " +
                "fitmethod=meet fillcolor=black position=center boxsize={" + width + " " + height + "}");

            p.end_template_ext(0, 0);

            
            p.begin_page_ext(0, 0, "width=a4.width height=a4.height");

            // Load video and auxiliary Flash files as RichMedia assets
            movie = p.load_asset("Video", moviefile, "");
            if (movie == -1)
                throw new Exception("Error: " + p.get_errmsg());
            
            videoplayer = p.load_asset("Flash", videoplayerfile, "");
            if (videoplayer == -1)
                throw new Exception("Error: " + p.get_errmsg());
            
            videoskin = p.load_asset("Flash", videoskinfile, "");
            if (videoskin == -1)
                throw new Exception("Error: " + p.get_errmsg());
            
            /* The option list for the RichMedia annotation specifies the
             * template as static placeholder, controls video embedding and
             * defines the the Flash helpers for playing.
             */

            optlist =
                "template={normal=" + templ + "} " +
                "richmedia={" +
                    "assets={" +
                        "{asset=" + movie       + " name=" + moviefile +  "} " +
                        "{asset=" + videoplayer + " name=" + videoplayerfile + "} " +
                        "{asset=" + videoskin   + " name=" + videoskinfile + "} " +
                    "} " +
                    "configuration={" +
                        "type=Video name=DefaultConfiguration " +
                            "instances={{asset=" + videoplayerfile + " " +
                                "params={binding=background " +
                                        "flashvars={source=" + moviefile +
                                                    "&skin=" + videoskinfile + "}}}} " +
                    "} " +
                "}";
            
            // Create the RichMedia annotation with Flash-controlled Video
            p.create_annotation(25, 300, 575, 610, "RichMedia", optlist);

            p.end_page_ext("");

            p.end_document("");
        }
        catch (PDFlibException e) {
            System.err.println("PDFlib exception occurred:");
            System.err.println("[" + e.get_errnum() + "] " + e.get_apiname()
                + ": " + e.get_errmsg());
            exitcode = 1;
        }
        catch (Exception e) {
            System.err.println(e);
            exitcode = 1;
        }
        finally {
            if (p != null) {
                p.delete();
            }
            System.exit(exitcode);
        }
    }
}