multimedia/screen_annotations
Manipulate renditions with JavaScript.
Download Java Code Switch to PHP Code Show Output
/*
* 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
*/
package com.pdflib.cookbook.pdflib.multimedia;
import com.pdflib.pdflib;
import com.pdflib.PDFlibException;
public class screen_annotations {
public static void main(String argv[]) {
/* This is where the data files are. Adjust as necessary */
String searchpath = "../input";
String outfile = "screen_annotations.pdf";
String title = "Screen annotations";
pdflib p = null;
int exitcode = 0;
try {
final double width=550, height=310;
int act, rend_video, parent_bookmark;
String videofile = "mountains.mp4"; // video file
String soundfile = "chilled.mid"; // sound file
String videourl = // video URL
"https://www.pdflib.com/fileadmin/cookbooks/PDFlib/PDFlib-Cookbook/input/mountains.mp4";
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}") == -1)
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 == -1)
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
int 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 == -1)
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
int rend_video_external = p.load_asset("Rendition", "../input/" + videofile, "mimetype=video/mp4 name=rendition_external external=true");
if (rend_video_external == -1)
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
int rend_video_url = p.load_asset("Rendition", videourl, "mimetype=video/mp4 name=rendition_url url=true");
if (rend_video_url == -1)
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
int rend_sound = p.load_asset("Rendition", soundfile, "mimetype=audio/midi name=rendition_sound style=hidden");
if (rend_sound == -1)
throw new Exception("Error: " + p.get_errmsg());
// Rendition actions for playing/stopping/pausing/resuming the sound
int act_play = p.create_action("Rendition", "target=annot_bookmarks operation=play rendition=" + rend_sound);
int act_stop = p.create_action("Rendition", "target=annot_bookmarks operation=stop rendition=" + rend_sound);
int act_pause = p.create_action("Rendition", "target=annot_bookmarks operation=pause rendition=" + rend_sound);
int 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
int act_playfrombeginning_sound = p.create_action("Rendition", "target=annot_bookmarks operation=playfrombeginning rendition=" + rend_sound);
int 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
int 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);
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
int rend_video_fake = p.load_asset("Rendition", videofile, "mimetype=video/fake name=rendition_video_fake");
if (rend_video_fake == -1)
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("");
}
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);
}
}
}