PDFlib Cookbook

cookbook

multimedia/embedded_video updated

Create a RichMedia annotation which plays an embedded video.

Download Java Code  Switch to PHP Code  Show Output 

/*
 * Embedded video:
 * Create a RichMedia annotation which plays an embedded video.
 *
 * We don't use a Flash-based wrapper which has the following consequences:
 * - The video is played even in Acrobat DC versions which no longer support
 *   Flash.
 * - The video does not work in old Acrobat X/XI versions.
 *
 * Required software: PDFlib/PDFlib+PDI/PPS 9
 * Required data: video file
 */

package com.pdflib.cookbook.pdflib.multimedia;

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

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

        pdflib p = null;
        int exitcode = 0;

        // Video file in MP4 format
        String videofile = "mountains.mp4";
        
        String optlist;
        int templ;
        int video;

        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.
            // We display only some text; you could display an image etc.
            
            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 file as RichMedia asset
            video = p.load_asset("Video", videofile, "");
            if (video == -1)
                throw new Exception("Error: " + p.get_errmsg());
            
            /* The option list for the RichMedia annotation specifies the
             * template as static placeholder and configures the video asset.
             */

            optlist =
                "template={normal=" + templ + "} " +
                "richmedia={" +
                    "assets={ {asset=" + video + " name=" + videofile + "} } " +
                    "configuration={type=Video instances={ {asset=" + videofile + "} }}" +
                "}";
            
            // Create the RichMedia annotation with 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);
        }
    }
}