PDFlib Cookbook

cookbook

pdfua/table_of_contents_pdfua1

Demonstrate PDF/UA list tagging.

Download PHP Code  Switch to Java Code  Show Output 

<?php
/*
 * Demonstrate tagging for a table of contents containing text, page
 * number, dot leaders and Link annotations
 *
 * required software: PDFlib/PDFlib+PDI/PPS 9.3
 * required data: font
 */

$title = "table_of_contents_pdfua1";

/* Dummy entries for the table of contents */
$toc_items = array(
    array( "1.", "Long Distance Glider",   "1" ),
    array( "2.", "Giant Wing",             "1" ),
    array( "3.", "Cone Head Rocket",       "1" ),
    array( "4.", "Super Dart",             "1" )
);
$p = null;

try {
    $p = new pdflib();
    $x0 = 50; $x1 = 70; $x2 = 450; $fontsize = 14;

    /* This is where the data files are. Adjust as necessary. */
    $searchpath = dirname(__FILE__,3)."/input";

    $p = new pdflib();

    /* all strings are expected as utf8 */
    $p->set_option("stringformat=utf8");

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

    $p->begin_document("", "pdfua=PDF/UA-1 lang=en tag={tagname=Document}");

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

    /* Automatically create spaces between chunks of text */
    $p->set_option("autospace=true");

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

    $p->create_bookmark("Tagged table of contents demo", "");

    /* Emit heading */
    $y = 700;
    $leading = 1.5 * $fontsize;
    
    /* Heading */
    $p->fit_textline("Table of contents", $x0, $y,
        "tag={tagname=H1} " .
        "fontname=NotoSerif-Regular fontsize=24 encoding=unicode embedding " .
        "fillcolor=blue");
    
    $y -= $leading;
    
    /* Start table of contents (TOC) structure element */
    $id_toc = $p->begin_item("TOC", "");

    /* Emit several table of contents items (TOCI) with substructure */
    for ($i = 0; $i < count($toc_items); $i++)
    {
        $y -= $leading;

        /* Create TOCI element with nested P, Reference and Link elements in a single call */
        $id_toci = $p->begin_item("TOCI",
            "tag={tagname=P tag={tagname=Reference direct=false tag={tagname=Link}}}");

        // Emit the chapter number and tag it as Lbl (requires PDFlib 9.3 or above)
        $p->fit_textline($toc_items[$i][0], $x0, $y,
           "fontname=NotoSerif-Regular fontsize=14 encoding=unicode embedding " .
           "boxsize={20 30} " .
           "matchbox={name={toc_entry" . $i . "} boxheight={fontsize descender}} " .
           "tag={tagname=Lbl}");


        /* Emit the text and define a named matchbox which will be used
            * to create the Link annotation. The dot leaders between text
            * and page number are automatically tagged as Artifact.
            */
        $p->fit_textline($toc_items[$i][1], $x1, $y,
            "fontname=NotoSerif-Regular fontsize=14 encoding=unicode embedding " .
            "boxsize={" . ($x2-$x1) . " 30} leader={alignment=right text={.}} " .
            "matchbox={name={toc_entry" . $i . "} boxheight={fontsize descender}}");

        /* Emit page number using a matchbox with the same name */ 
        $p->fit_textline($toc_items[$i][2], $x2, $y,
            "fontname=NotoSerif-Regular fontsize=14 encoding=unicode embedding " .
                "boxsize={20 30} position={right bottom} fillcolor=blue " .
                "matchbox={name={toc_entry" . $i . "} boxheight={fontsize descender}}");

        /* Create GoTo action (to simplify this topic we pretend to
            * jump to page 1) */
        $action = $p->create_action("GoTo", "destination={page=1}");
        
        /* Create Link annotation on named matchbox "toc_entry<i>".
            * Each matchbox consists of two rectangles (text and number).
            * This automatically creates suitable OBJR (object reference)
            * elements for each annotation.
            */
            $p->create_annotation(0, 0, 0, 0, "Link",
                "usematchbox={toc_entry" . $i . "} linewidth=0 " .
                "contents={Link to chapter start} " .
                "action={activate=" . $action .  " }");

            /* This closes Link, Reference, P and TOCI */
        $p->end_item($id_toci);
    }

    $p->end_item($id_toc);

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

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

$p = 0;
?>