PDFlib Cookbook

cookbook

interactive/stamp_annotation new

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

Download Java Code  Switch to PHP Code  Show Output 

/*
 * 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
 */
package com.pdflib.cookbook.pdflib.interactive;

import com.pdflib.pdflib;
import com.pdflib.PDFlibException;

public class stamp_annotation
{
    public static void main (String argv[])
    {
        /* This is where the data files are. Adjust as necessary */
        String searchpath = "../input";
        String outfile = "stamp_annotation.pdf";
        String title = "Stamp Annotation";
        
        pdflib p = null;
        int exitcode = 0;

        String fontopt;
        int tpl, path=-1;
        double width=200, height=60;
        double llx=30, lly=500;
        double lw=5;

        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, "") == -1)
                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("");

        } catch (PDFlibException e) {
            System.err.println("PDFlib exception occurred:");
            System.err.println("[" + e.get_errnum() + "] " + e.get_apiname()
                + ": " + e.get_errmsg());
            exitcode = 1;
        } catch (Exception e) {
            System.err.println(e);
            exitcode = 1;
        } finally {
            if (p != null) {
                p.delete();
            }
            System.exit(exitcode);
        }
    }
}