PDFlib Cookbook

cookbook

interactive/stamp_annotation

Create a stamp annotation which uses a template as normal icon.

Download PHP Code  Switch to Java Code  Show Output 

<?php
/*
 * Stamp annotation
 * 
 * Create a stamp annotation which uses a template as "normal" icon;
 * the template contains the stamp text.
 *
 * Required software: PDFlib/PDFlib+PDI/PPS 9
 * Required data: font
 */
/* This is where the data files are. Adjust as necessary */
$searchpath = dirname(__FILE__,3)."/input";
$outfile = "";
$title = "Stamp Annotation";

$exitcode = 0;

$path=0;
$width=200; $height=60;
$llx=30; $lly=500;
$lw=5.0;

try {
    $p = new pdflib();

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

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

    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 template for use as annotation icon
    $tpl = $p->begin_template_ext($width, $height, "");
        // Construct filled and stroked rectangle with rounded corners
        $path = $p->add_path_point($path,        $lw/2,         $lw/2, "move", "");
        $path = $p->add_path_point($path,        $lw/2, $height-$lw/2, "line", "");
        $path = $p->add_path_point($path, $width-$lw/2, $height-$lw/2, "line", "");
        $path = $p->add_path_point($path, $width-$lw/2,         $lw/2, "line", "");
        $p->draw_path($path, 0, 0, "fill stroke close round=5 fillcolor=lightblue strokecolor=blue linewidth=" . $lw);
        
        // Emit stamp text
        $fontopt =
            "fontname=NotoSerif-Regular encoding=unicode embedding " .
            "fontsize=30 fillcolor=white position=center boxsize={". $width . " " . $height . "}";

        $p->fit_textline("RECEIVED", 0, 0, $fontopt);
    $p->end_template_ext(0, 0);

    // Create the annotation with icon for "normal" appearance
    $p->create_annotation($llx, $lly, $llx+$width, $lly+$height,
        "Stamp", "template {normal " . $tpl . "}");

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

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

$p = 0;
?>