PDFlib Cookbook

cookbook

interactive/nested_bookmarks

Create bookmarks which are nested in several levels.

Download PHP Code  Switch to Java Code  Show Output 

<?php
/*
 * Nested bookmarks:
 * Create bookmarks which are nested in several levels
 * 
 * Create a title page with a top-level bookmark and provide the following
 * pages with bookmarks nested on the second level. Below each of those the
 * second level bookmarks create another bookmark which jumps to a Web site.  
 *
 * Required software: PDFlib/PDFlib+PDI/PPS 10
 * Required data: none
 */
/* This is where the data files are. Adjust as necessary. */
$searchpath = dirname(__FILE__,3)."/input";
$outfile = "";
$title = "Nested Bookmarks";

$numpages = 5; $pagewidth=200; $pageheight=100; 
$x = 20; $y = 50;

$planes =array(
    "Giant Wing",
    "Long Distance Glider",
    "Cone Head Rocket",
    "Super Dart",
    "German Bi-Plane"
);

try {
    $p = new pdflib();

    $p->set_option("searchpath={" . $searchpath . "}");

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

    if ($p->begin_document($outfile, "") == 0)
    throw new Exception("Error: " . $p->get_errmsg());

    $p->set_info("Creator", "PDFlib Cookbook");
    $p->set_info("Title", $title);
    
    /* Create the title page with a top-level bookmark */
    $p->begin_page_ext($pagewidth, $pageheight, "");
    $p->fit_textline("Kraxi Paper Planes", $x, $y, 
        "fontname=NotoSerif-Bold fontsize=14");
    $bm_planes = $p->create_bookmark("Kraxi Paper Planes", "");
    $p->end_page_ext("");

    /* Create further pages with a bookmark each which is nested below the
     * top-level bookmark created above */
    for ($i=0; $i < $numpages; $i++)
    {
    
        /* Start page */
        $p->begin_page_ext($pagewidth, $pageheight, "");
    
        /* Output some text on the page */
        $p->fit_textline($planes[$i], $x, $y, 
            "fontname=NotoSerif-Bold fontsize=14");

        /* Create a "Plane" bookmark on the page which is nested under the 
         * "Kraxi Paper Planes" bookmark */
        $bm_plane = $p->create_bookmark($planes[$i], "parent=" . $bm_planes);
    
        /* Create a "URI" action for opening a URL */
        $action = $p->create_action("URI", "url={http://www.kraxi.com}");
    
        /* Create a bookmark which jumps to be URL defined above. This
         * bookmark is nested on level three under the "Plane" bookmark
         * created above. 
         */
        $p->create_bookmark("Jump to the Kraxi Website", 
             "parent=" . $bm_plane . " action={activate=" . $action . "}");

        $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=nested_bookmarks.pdf");
    print $buf;

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

$p = 0;

?>