PDFlib Cookbook

cookbook

multimedia/screen_annotations

Manipulate renditions with JavaScript.

Download PHP Code  Switch to Java Code  Show Output 

<?php
/*
 * Screen annotation for video:
 * Demonstrate use of Screen annotations, Renditions and Rendition actions
 * to play video and sound. 
 *
 * Required software: PDFlib/PDFlib+PDI/PPS 10
 * Required data: video and sound files
 */


/* This is where the data files are. Adjust as necessary */
$searchpath = "../input";
$outfile = "";
$title = "Screen annotations";

$exitcode = 0;

try {
    $width=550; $height=310;
    $videofile = "mountains.mp4";	            // video file
    $soundfile = "chilled.mid";	            // sound file
    $videourl = "https://www.pdflib.com/fileadmin/cookbooks/PDFlib/PDFlib-Cookbook/input/mountains.mp4"; // video URL
                
    $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, "destination={type=fitwindow}") == 0)
        throw new Exception(
                "Error: " . $p->get_apiname() . ": " . $p->get_errmsg());

    $p->set_info("Creator", "PDFlib Cookbook");
    $p->set_info("Title", $title);

    // Create a Rendition object with the video
    $rend_video = $p->load_asset("Rendition", $videofile, "mimetype=video/mp4 name=rendition_video controller");
    if ($rend_video == 0)
        throw new Exception("Error: " . $p->get_errmsg());

    // ================================================================          
    $p->begin_page_ext(0, 0, "width=a4.width height=a4.height");

    $parent_bookmark = $p->create_bookmark("embedded video (click to play)", "open");
    
    // Create a Rendition action for playing the video
    $act = $p->create_action("Rendition", "target=annot_video operation=playfrombeginning rendition=" . $rend_video);

    // Create a Screen annotation where the video will be played
    $p->create_annotation(25, 300, 575, 610, "Screen",
        "annotcolor=lightblue title=annot_video name=annot_video action={activate=" . $act . "}");          
    
    $p->end_page_ext("");

    // ================================================================          
    $p->begin_page_ext(0, 0, "width=a4.width height=a4.height");

    $parent_bookmark = $p->create_bookmark("embedded video in floating window (click to play)", "open");
    
    // Create a Rendition object with the video
    $rend_video_floating = $p->load_asset("Rendition", $videofile,
            "mimetype=video/mp4 name=rendition_floating_window controller style=floating window={width=640 height=360}");
    if ($rend_video_floating == 0)
        throw new Exception("Error: " . $p->get_errmsg());
    
    // Create a Rendition action for playing the video
    $act = $p->create_action("Rendition", "target=annot_floating_window operation=playfrombeginning rendition=" . $rend_video_floating);

    // Create a Screen annotation where the video will be played
    $p->create_annotation(25, 300, 575, 610, "Screen",
        "annotcolor=lightblue title=annot_floating_window name=annot_floating_window action={activate=" . $act . "}");
    
    $p->end_page_ext("");

    // ================================================================           
    $p->begin_page_ext(0, 0, "width=a4.width height=a4.height");
    
    $parent_bookmark = $p->create_bookmark("video from external file (autoplay)", "open");

    // Create a Rendition object with the video loaded from an external file.
    // The extra directory name is required to record it in the PDF
    $rend_video_external = $p->load_asset("Rendition", "../input/" . $videofile, "mimetype=video/mp4 name=rendition_external external=true");
    if ($rend_video_external == 0)
        throw new Exception("Error: " . $p->get_errmsg());

    // Create a Rendition action for playing the video
    $act = $p->create_action("Rendition", "target=annot_external operation=playfrombeginning rendition=" . $rend_video_external);
    
    // Create a Screen annotation where the video will be played
    $p->create_annotation(25, 300, 575, 610, "Screen",
        "title=annot_external name=annot_external action={open=" . $act . "}");

    $p->end_page_ext("");

    // ================================================================
    $p->begin_page_ext(0, 0, "width=a4.width height=a4.height");
    
    $parent_bookmark = $p->create_bookmark("video from URL (autoplay)", "open");

    // Create a Rendition object with the video loaded from URL
    $rend_video_url = $p->load_asset("Rendition", $videourl, "mimetype=video/mp4 name=rendition_url url=true");
    if ($rend_video_url == 0)
        throw new Exception("Error: " . $p->get_errmsg());

    // Create a Rendition action for playing the video
    $act = $p->create_action("Rendition", "target=annot_video_url operation=playfrombeginning rendition=" . $rend_video_url);
    
    // Create a Screen annotation where the video will be played
    $p->create_annotation(25, 300, 575, 610, "Screen",
        "title=annot_video_url name=annot_video_url action={open=" . $act . "}");

    $p->end_page_ext("");

    // ================================================================
    $p->begin_page_ext(0, 0, "width=a4.width height=a4.height");
    
    $parent_bookmark = $p->create_bookmark("rendition actions in bookmarks", "open");

    // Create a Rendition object with the sound file
    $rend_sound = $p->load_asset("Rendition", $soundfile, "mimetype=audio/midi name=rendition_sound style=hidden");
    if ($rend_sound == 0)
        throw new Exception("Error: " . $p->get_errmsg());

    // Rendition actions for playing/stopping/pausing/resuming the sound
    $act_play   = $p->create_action("Rendition", "target=annot_bookmarks operation=play   rendition=" . $rend_sound);
    $act_stop   = $p->create_action("Rendition", "target=annot_bookmarks operation=stop   rendition=" . $rend_sound);
    $act_pause  = $p->create_action("Rendition", "target=annot_bookmarks operation=pause  rendition=" . $rend_sound);
    $act_resume = $p->create_action("Rendition", "target=annot_bookmarks operation=resume rendition=" . $rend_sound);

    // Rendition actions for playing the sound/video or resuming the previous rendition
    $act_playfrombeginning_sound = $p->create_action("Rendition", "target=annot_bookmarks operation=playfrombeginning rendition=" . $rend_sound);
    $act_playfrombeginning_video = $p->create_action("Rendition", "target=annot_bookmarks operation=playfrombeginning rendition=" . $rend_video);
    
    // Create a Screen annotation which holds the Sound or Video rendition
    $p->create_annotation(25, 300, 575, 610, "Screen", "title=annot_bookmarks name=annot_bookmarks");
    
    // Bookmarks for controlling the renditions
    $p->create_bookmark("play sound",   "action={activate=" . $act_play   . "} parent=" . $parent_bookmark);
    $p->create_bookmark("stop sound",   "action={activate=" . $act_stop   . "} parent=" . $parent_bookmark);
    $p->create_bookmark("pause sound",  "action={activate=" . $act_pause  . "} parent=" . $parent_bookmark);
    $p->create_bookmark("resume sound", "action={activate=" . $act_resume . "} parent=" . $parent_bookmark);
    $p->create_bookmark("resume paused rendition or play sound", "action={activate=" . $act_playfrombeginning_sound . "} parent=" . $parent_bookmark);
    $p->create_bookmark("resume paused rendition or play video", "action={activate=" . $act_playfrombeginning_video . "} parent=" . $parent_bookmark);

    $p->end_page_ext("");

    // ================================================================
    $p->begin_page_ext(0, 0, "width=a4.width height=a4.height");
    
    $parent_bookmark = $p->create_bookmark("rendition actions in button fields", "open");

    // Rendition actions for playing/stopping/pausing/resuming the sound
    $act_play   = $p->create_action("Rendition", "target=annot_fields operation=play   rendition=" . $rend_video);
    $act_stop   = $p->create_action("Rendition", "target=annot_fields operation=stop   rendition=" . $rend_video);
    $act_pause  = $p->create_action("Rendition", "target=annot_fields operation=pause  rendition=" . $rend_video);
    $act_resume = $p->create_action("Rendition", "target=annot_fields operation=resume rendition=" . $rend_video);
    
    // Create a Screen annotation which holds the Sound or Video rendition
    $p->create_annotation(25, 300, 575, 610, "Screen", "title=annot_fields name=annot_fields");
    
    // Buttons for controlling the renditions
    $font = $p->load_font("NotoSerif-Regular", "unicode", "");
    $p->create_field(100, 100, 200, 200, "play", "pushbutton",
            "caption=play   font=" . $font . " fontsize=24 bordercolor=blue action={activate=" . $act_play   . "}");
    $p->create_field(200, 100, 300, 200, "stop", "pushbutton",
            "caption=stop   font=" . $font . " fontsize=24 bordercolor=blue action={activate=" . $act_stop   . "}");
    $p->create_field(300, 100, 400, 200, "pause", "pushbutton",
            "caption=pause  font=" . $font . " fontsize=24 bordercolor=blue action={activate=" . $act_pause  . "}");
    $p->create_field(400, 100, 500, 200, "resume", "pushbutton",
            "caption=resume font=" . $font . " fontsize=24 bordercolor=blue action={activate=" . $act_resume . "}");

    $p->end_page_ext("");

    // ================================================================
    $p->begin_page_ext(0, 0, "width=a4.width height=a4.height");
    
    $parent_bookmark = $p->create_bookmark("rendition actions in link annotations", "open");

    // Rendition actions for playing/stopping/pausing/resuming the sound
    $act_play   = $p->create_action("Rendition", "target=annot_links operation=play   rendition=" . $rend_video);
    $act_stop   = $p->create_action("Rendition", "target=annot_links operation=stop   rendition=" . $rend_video);
    $act_pause  = $p->create_action("Rendition", "target=annot_links operation=pause  rendition=" . $rend_video);
    $act_resume = $p->create_action("Rendition", "target=annot_links operation=resume rendition=" . $rend_video);
    
    // Create a Screen annotation which holds the Sound or Video rendition
    $p->create_annotation(25, 300, 575, 610, "Screen", "title=annot_links name=annot_links");
    
    // Link annotations for controlling the renditions
    $p->fit_textline("play", 150, 150, "fontname=NotoSerif-Regular fontsize=24 position=center");
    $p->create_annotation(100, 100, 200, 200, "Link", 
            "annotcolor=blue action={activate=" . $act_play   . "}");
    $p->fit_textline("stop", 250, 150, "fontname=NotoSerif-Regular fontsize=24 position=center");
    $p->create_annotation(200, 100, 300, 200, "Link", 
            "annotcolor=blue action={activate=" . $act_stop   . "}");
    $p->fit_textline("pause", 350, 150, "fontname=NotoSerif-Regular fontsize=24 position=center");
    $p->create_annotation(300, 100, 400, 200, "Link", 
            "annotcolor=blue action={activate=" . $act_pause  . "}");
    $p->fit_textline("resume", 450, 150, "fontname=NotoSerif-Regular fontsize=24 position=center");
    $p->create_annotation(400, 100, 500, 200, "Link", 
            "annotcolor=blue action={activate=" . $act_resume . "}");

    $p->end_page_ext("");

    // ================================================================
    $p->begin_page_ext(0, 0, "width=a4.width height=a4.height");

    $parent_bookmark = $p->create_bookmark("two annotations with same rendition (autoplay)", "open");
    
    // Create a Rendition action for playing the video
    $act = $p->create_action("Rendition", "operation=playfrombeginning rendition=" . $rend_video);

    // TODO: wait for c#110
    $p->create_annotation(25, 300, 575, 610, "Screen",
        "annotcolor=lightblue title=annot_duplicate_rendition1 name=annot_duplicate_rendition1 action={open=" . $act . "}");          

    $p->create_annotation(25,   0, 575, 300, "Screen",
        "annotcolor=lightblue title=annot_duplicate_rendition2 name=annot_duplicate_rendition2 action={open=" . $act . "}");  

    $p->end_page_ext("");

    // ================================================================
    $p->begin_page_ext(0, 0, "width=a4.width height=a4.height");
    
    $parent_bookmark = $p->create_bookmark("two parallel renditions in same annotation (autoplay)", "open");

    // Create Rendition actions for playing video and sound separately
    $act_playfrombeginning_video = $p->create_action("Rendition", "target=annot_two_renditions operation=playfrombeginning rendition=" . $rend_video);
    $act_playfrombeginning_sound = $p->create_action("Rendition", "target=annot_two_renditions operation=playfrombeginning rendition=" . $rend_sound);

    // Create a Screen annotation with video and sound at the same time
    // TODO ACROBUG: currently the renditions don't play in parallel; only the video plays
    $p->create_annotation(25, 300, 575, 610, "Screen",
            "title=annot_two_renditions name=annot_two_renditions action={open={" . $act_playfrombeginning_video . " " . $act_playfrombeginning_sound ."}}");

    $p->end_page_ext("");            

    // ================================================================          
    $p->begin_page_ext(0, 0, "width=a4.width height=a4.height");

    $parent_bookmark = $p->create_bookmark("two alternative renditions in same annotations (autoplay)", "open");
    
    // Create a rendition which cannot be played because of the illegal MIME type
    $rend_video_fake = $p->load_asset("Rendition", $videofile, "mimetype=video/fake name=rendition_video_fake");
    if ($rend_video_fake == 0)
        throw new Exception("Error: " . $p->get_errmsg());
    
    // Create a Rendition action with a selector rendition containing the fake and the real (playable) video
    $act = $p->create_action("Rendition", "target=annot_video_selector operation=playfrombeginning " .
            "rendition={" . $rend_video_fake . " " . $rend_video . "}");

    // Create a Screen annotation where the video will be played
    $p->create_annotation(25, 300, 575, 610, "Screen",
        "annotcolor=lightblue title=annot_video_selector name=annot_video_selector action={open=" . $act . "}");          
    
    $p->end_page_ext("");

    
    $p->end_document("");

    $buf = $p->get_buffer();
    $len = strlen($buf);

    header("Content-type: application/pdf");
    header("Content-Length: $len");
    header("Content-Disposition: inline; filename=screen_annontations.pdf");
    print $buf;

} catch (PDFlibException $e) {
    echo("PDFlib exception occurred:\n".
        "[" . $e->get_errnum() . "] " . $e->get_apiname() .
        ": " . $e->get_errmsg() . "\n");
    exit(1);
} catch (Exception $e) {
    echo($e->getMessage());
    exit(1);
}

$p = 0;

?>