PDFlib Cookbook

cookbook

interactive/custom_stamp_annotation

Create a custom stamp annotation which uses a template as 'normal' icon; the template contains the stamp text

Download PHP Code  Switch to Java Code  Show Output 

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

$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("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, "");
        /* Load SVG graphics */
        $graphics = $p->load_graphics("auto", $graphicsfile, "");
        if ($graphics == 0)
            throw new Exception("Error: " . $p->get_errmsg());
        
        /* Place SVG in the lower left corner of the template */
        $p->fit_graphics($graphics, 0, 0,
            "boxsize={ " . $width . " " . $height . "} position={center} fitmethod=meet");
        $p->close_graphics($graphics);

    $p->end_template_ext(0, 0);

    // Create the annotation with template 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=custom_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;
?>