PDFlib Cookbook

cookbook

multimedia/javascript_for_renditions PDFlib 10

Screen annotation for video: Demonstrate use of Screen annotations, Renditions and Rendition actions to play video and sound.

Download Java Code  Switch to PHP Code  Show Output 

/*
 * JavaScript for renditions:
 * Play renditions with JavaScript.
 * 
 * The following use cases are demonstrated:
 * - Control a sound rendition from JavaScript actions in bookmarks; the
 *   rendition is played in a hidden floating window without any Screen
 *   annotation.
 * - Control a video rendition in a Screen annotation with JavaScript.
 * - Play sound and video renditions at the same time with JavaScript, also
 *   without Screen annotation.
 *
 * Required software: PDFlib/PDFlib+PDI/PPS 10
 * Required data: video and sound files
 * 
 */

package com.pdflib.cookbook.pdflib.multimedia;

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

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

        pdflib p = null;
        int exitcode = 0;

        try {
            int rend_video, parent_bookmark;
            String videofile = "mountains.mp4";	            // video file
            String soundfile = "chilled.mid";	            // sound file
                       
            p = new pdflib();
            
            // Make sure we are using an appropriate PDFlib version
            if ((int) p.get_option("major", "") < 10) {
                System.err.println("Error: Cookbook topic '" + title + "' requires PDFlib 10");
                System.exit(1);
            }

            /* 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, "destination={type=fitwindow}") == -1)
                throw new Exception(
                        "Error: " + p.get_apiname() + ": " + p.get_errmsg());

            p.set_info("Creator", "PDFlib Cookbook");
            p.set_info("Title", title);

            // ================================================================
            
            // Create a hidden rendition from the sound file and store it in the name tree
            int rend_sound = p.load_asset("Rendition", soundfile, "mimetype=audio/midi style=hidden nametree name=rendition_sound");
            if (rend_sound == -1)
                throw new Exception("Error: " + p.get_errmsg());
            
            // JavaScript for initializing the player
            String js_init_player =
				"// Initialize the player if not yet done; use as document open action\n" +
				"\n" +
				"var player = null;\n" +
				"\n" +
				"function init_player() {\n" +
				"	if (player && player.isOpen)\n" +
				"		return;\n" +
				"\n" +
				"	// Retrieve rendition by name and play it in a floating window.\n" +
				"	var rendition = this.media.getRendition(\"rendition_sound\");\n" +
				"\n" +
				"	if (rendition == null)\n" +
				"	    app.alert(\"Couldn't find Rendition\");\n" +
				"	else {\n" +
				"		var floating = {\n" +			
				"			width: 400,\n" +
				"			height: 300\n" +
				"		};\n" +
				"		player = app.media.openPlayer({\n" +
				"			rendition: rendition,\n" +
				"			settings: {\n" +
				"				floating: floating\n" +
				"			}\n" +
				"		});\n" +
				"	}\n" +
				"}";
            
            // Load JavaScript string into a virtual (PVF) file
            p.create_pvf("/pvf/javascript/init_player.js", js_init_player.getBytes(), "");

            // Create a JavaScript asset from PVF file
            int js_init_player_asset = p.load_asset("JavaScript", "/pvf/javascript/init_player.js", "");
            if (js_init_player_asset == -1)
                throw new Exception("Error: " + p.get_errmsg());
            
            // Create a JavaScript action from the asset
            int act_init = p.create_action("JavaScript", "javascript=" + js_init_player_asset);
            
            // The init action is attached to the document open action in
            // p.end_document() later to ensure that the code is available on each page
            
            p.begin_page_ext(0, 0, "width=a4.width height=a4.height");
            
            parent_bookmark = p.create_bookmark("control sound with JavaScript (click bookmarks)", "open");            

            // Simple JavaScript actions for controlling playback
            int act_play_sound   = p.create_action("JavaScript", "script={init_player(); player.play();}");
            int act_stop_sound   = p.create_action("JavaScript", "script={init_player(); player.stop();}");
            int act_pause_sound  = p.create_action("JavaScript", "script={init_player(); player.pause();}");

            // Bookmarks for controlling playback with JavaScript actions
            p.create_bookmark("play or resume",
            		"action={activate=" + act_play_sound  + "} parent=" + parent_bookmark);
            p.create_bookmark("stop",
            		"action={activate=" + act_stop_sound  + "} parent=" + parent_bookmark);
            p.create_bookmark("pause",
            		"action={activate=" + act_pause_sound + "} parent=" + parent_bookmark);
            
            p.end_page_ext(""); 

            // ================================================================
            // Create a rendition from the video file
            rend_video = p.load_asset("Rendition", videofile, "mimetype=video/mp4 name=rendition_video1 controller");
            if (rend_video == -1)
                throw new Exception("Error: " + p.get_errmsg());
            
            p.begin_page_ext(0, 0, "width=a4.width height=a4.height");

            p.create_bookmark("play video with simple JavaScript (click annotation)", "open");

            // Create a rendition action for playing the video with JavaScript
            int act = p.create_action("Rendition", "target=annot_video_javascript script={app.media.openPlayer();} rendition=" + rend_video);
            
            // Create a Screen annotation where the video will be played
            p.create_annotation(25, 300, 575, 610, "Screen",
        		"title=annot_video_javascript name=annot_video_javascript action={activate=" + act + "}");

            p.end_page_ext("");

            // ================================================================
            // Create a rendition from the video file and store it in the name tree
            // to make it accessible to JavaScript
            rend_video = p.load_asset("Rendition", videofile, "mimetype=video/mp4 nametree name=rendition_video2 controller");
            if (rend_video == -1)
                throw new Exception("Error: " + p.get_errmsg());

            // JavaScript for retrieving and playing a named rendition
            String js_video =
				"var rendition = this.media.getRendition(\"rendition_video2\");\n" +
				"\n" +
				"if (rendition == null)\n" +
				"    app.alert(\"Couldn't find rendition\");\n" +
				"else {\n" +
				"	var floating = {\n" +
				"			align: app.media.align.topCenter,\n" +
				"			canResize: app.media.canResize.yes,\n" +
				"			width: 400,\n" +
				"			height: 300\n" +
				"	};\n" +
				"	var player = app.media.openPlayer({\n" +
				"		rendition: rendition,\n" +
				"		settings: {\n" +
				"			windowType: app.media.windowType.floating,\n" +
				"			floating: floating\n" +
				"		}\n" +
				"	});\n" +
				"}\n";
            
            p.begin_page_ext(0, 0, "width=a4.width height=a4.height");

            // Load JavaScript string into a virtual (PVF) file
            p.create_pvf("/pvf/javascript/video.js", js_video.getBytes(), "");

            // Create a JavaScript asset from PVF file
            int js_video_asset = p.load_asset("JavaScript", "/pvf/javascript/video.js", "");
            if (js_video_asset == -1)
                throw new Exception("Error: " + p.get_errmsg());
            
            // Create a JavaScript action from the asset
            int act_play_video = p.create_action("JavaScript", "javascript=" + js_video_asset);

            // The floating window created by the action does not depend on the page
            p.create_bookmark("play sound and video with JavaScript (click bookmark)",
            		"action={activate={" + act_play_sound + " " + act_play_video + "}}");

            p.end_page_ext("");               
            
            // Attach the init action for the sound rendition above
            p.end_document("action={open=" + act_init + "}");
        }
        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);
        }
    }
}