PDFlib Cookbook

cookbook

textflow/continue_textflow_in_annotation

Continue Textflow in annotation: store overflow text in a Text annotation, also called 'sticky note'.

Download PHP Code  Switch to Java Code  Show Output 

<?php
/*
 * Continue Textflow in annotation: store overflow text in a Text annotation,
 * also called "sticky note".
 * 
 * The Textflow box is filled with text, but a small space is reserved for
 * the annotation icon. If you hover of the annotation the overflow text
 * is displayed in a popu$p->
 *
 * Required software: PDFlib/PDFlib+PDI/PPS 10
 * Required data: font file
 */

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

$outfile = "";
$title = "Continue Textflow in annotation";
$exitcode = 0;

$optlist =
    "fontname=NotoSerif-Regular fontsize=12 leading=125% alignment=justify charref";

/* Dummy text for filling the box. Soft hyphens are marked with the
 * character reference "&shy;" (character references are enabled by the
 * charref option).
 */
$text = 
    "Lorem ipsum dolor sit amet, consectetur adi&shy;pi&shy;sicing elit, " .
    "sed do eius&shy;mod tempor incidi&shy;dunt ut labore et dolore " .
    "magna ali&shy;qua. Ut enim ad minim ve&shy;niam, quis nostrud " .
    "exer&shy;citation ull&shy;amco la&shy;bo&shy;ris nisi ut " .
    "ali&shy;quip ex ea commodo con&shy;sequat. Duis aute irure dolor " .
    "in repre&shy;henderit in voluptate velit esse cillum dolore eu " .
    "fugiat nulla pari&shy;atur. <fillcolor=red>Excep&shy;teur<fillcolor=black> sint occae&shy;cat " .
    "cupi&shy;datat non proident, sunt in culpa qui officia " .
    "dese&shy;runt mollit anim id est laborum. ";

try {
    $llx=50; $lly=650; $urx=250; $ury=800;
    
    $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);

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

    // Create Textflow from the available text
    $tf = $p->create_textflow($text, $optlist);
    if ($tf == 0)
        throw new Exception("Error: " . $p->get_errmsg());

    // We deliberately place only a single Textflow instance. Reserve
    // some space at the end with "createlastindent" where the annotation
    // icon is placed. Create a named matchbox for easy reference.
    $result = $p->fit_textflow($tf, $llx, $lly, $urx, $ury,
        "verticalalign=justify linespreadlimit=120% showborder " .
        "createlastindent={rightindent=20 matchbox={name=cont boxheight={ascender descender}}}");

    // If the box couldn't hold all the text we place the overflow text
    // in a Text annotation. Supply the matchbox name instead of
    // explicit coordinates.
    if ($result == "_boxfull" || $result == "_nextpage")
        $p->create_annotation(0, 0, 0, 0, "Text",
            "usematchbox=cont createrichtext={textflow=" . $tf . "} " .
            "title={Continuation...} annotcolor=red");
    
    $p->delete_textflow($tf);
    $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=continue_textflow_in_annotation.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;

?>