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 Java Code  Switch to PHP Code  Show Output 

/*
 * Custon stamp annotation
 * 
 * Create a custom 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
 */
package com.pdflib.cookbook.pdflib.interactive;

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

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

        int tpl;
        double width=200, height=60;
        double llx=30, lly=500;

        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, "");
	            /* Load SVG graphics */
	            int graphics = p.load_graphics("auto", graphicsfile, "");
	            if (graphics == -1)
	                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("");

        } 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);
        }
    }
}