PDFlib
BASKET
PDFlib

text_output/simple_stamp

Create a stamp across the page which runs diagonally from one edge to the other. Place a text line as outlines like a stamp in the background of a Textflow. Place a text line like a stamp with a certain opacity on an image.

Download Java Code      Show Output PDF

/* $Id: simple_stamp.java,v 1.16 2007/10/30 16:16:33 katja Exp $

 * Simple stamp:

 * Create a stamp across the page which runs diagonally from one edge to the

 * other

 *

 * Place a text line as outlines like a stamp in the background of a Textflow.

 * Place a text line like a stamp with a certain opacity on an image.

 *

 * Required software: PDFlib/PDFlib+PDI/PPS 7

 * Required data: none

 */

package com.pdflib.cookbook.pdflib.text_output;


import com.pdflib.pdflib;

import com.pdflib.PDFlibException;


public class simple_stamp

{

    public static void main (String argv[])

    {

    /* This is where the data files are. Adjust as necessary. */

    String searchpath = "../input";

    String outfile = "simple_stamp.pdf";

    String title = "Simple Stamp";


    pdflib p = null;

    int font, image, gstate;

    String imagefile = "nesrin.jpg";

    String result;

    int tf = -1;

    final double llx=50, lly=50;


    final String optlist =

        "fontname=Helvetica fontsize=24 encoding=unicode leading=120% charref";


    final String textflow =

        "To fold the famous rocket looper proceed as follows:\n" +

        "Take a DIN A4 sheet.\nFold it lenghtwise in the middle.\nThen, fold " +

        "the upper corners down.\nFold the long sides inwards that the " +

        "points A and B meet on the central fold.\nFold the points C and D " +

        "that the upper corners meet with the central fold as well." +

        "\nFold the plane in the middle. Fold the wings down that they close " +

        "with the lower border of the plane.";


    try {

        p = new pdflib();

        p.set_parameter("SearchPath", searchpath);


        /* This means we must check return values of load_font() etc. */

        p.set_parameter("errorpolicy", "return");


        /* Set an output path according to the name of the topic */

        if (p.begin_document(outfile, "") == -1)

            throw new Exception("Error: " + p.get_errmsg());


        p.set_info("Creator", "PDFlib Cookbook");

        p.set_info("Title", title + " $Revision: 1.16 $");


        /* Page 1 */

        p.begin_page_ext(595, 842, "");


        /* For PDFlib Lite: change "unicode" to "winansi" */

        font = p.load_font("Helvetica-Bold", "unicode", "");


        if (font == -1)

            throw new Exception("Error: " + p.get_errmsg());


        /* Fit the text line as outlines like a stamp in the specified box. The

         * stamp will be placed diagonally from the upper left to the lower

         * right (stamp=ul2lr).

         */

        p.fit_textline("The Famous Rocket Looper", llx, lly, "font=" + font +

            " strokecolor={rgb 1 0 0} textrendering=1 boxsize={500 750} " +

            "strokecolor={rgb 0.5 0 1} stamp=ul2lr");


        /* Place the Textflow on top of the stamp */

        tf = p.add_textflow(tf, textflow, optlist);

        if (tf == -1)

            throw new Exception("Error: " + p.get_errmsg());


        result = p.fit_textflow(tf, 100, 100, 400, 700, "");

        if (!result.equals("_stop"))

        {

            /* Check for errors or more text to be placed */

        }


        p.end_page_ext("");


        /* Page 2 */

        p.begin_page_ext(350, 220, "");


        /* Load and place the image first to have it in the background

        */

        image = p.load_image("auto", imagefile, "");


        if (image == -1)

        throw new Exception("Error: " + p.get_errmsg());


        p.fit_image(image, 0, 0, "scale=0.5");


        /* Save the current graphics state */

        p.save();


        /* Set the opacity in the current graphics state,

         * for the stamp to appear slightly transparent

         */

        gstate = p.create_gstate("opacityfill=0.5");

        p.set_gstate(gstate);


        /* Fit the text line as outlines like a stamp in the specified box.

         * The stamp will be place diagonally from the lower left to the upper

         * right (stamp=ll2ur). It will be drawn in a transparent white

         * according to the opacity set for the current graphics state.

         */

        p.fit_textline("Our Test Image", 30, 10, "font=" + font +

            " fillcolor={rgb 1 1 1} boxsize={300 200} fontsize=1 stamp=ll2ur");


        p.restore();


        p.end_page_ext("");


        p.end_document("");


        } catch (PDFlibException e) {

            System.err.print("PDFlib exception occurred:\n");

            System.err.print("[" + e.get_errnum() + "] " + e.get_apiname() +

                ": " + e.get_errmsg() + "\n");

        } catch (Exception e) {

            System.err.println(e.getMessage());

        } finally {

            if (p != null) {

                p.delete();

            }

        }

    }

}