BASKET
Search
PDFlib

text_output/text_on_color

Download Java Code      Switch to PHP Code     Show Output PDF

/* $Id: text_on_color.java,v 1.14 2013/12/30 10:50:38 stm Exp $

 * Text on colored background:

 * Place a text line and a Textflow on a colored background

 *

 * Place a text line on a colored background using info_textline() to get the

 * dimensions of the text line. Place a text line using the "matchbox" option

 * which provides a more versatile way for text calculations.

 * Fit a Textflow on a colored background. The colored background is

 * created with a Textflow matchbox.

 *

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

 * Required data: none

 */

package com.pdflib.cookbook.pdflib.text_output;


import com.pdflib.pdflib;

import com.pdflib.PDFlibException;


public class text_on_color {

    public static void main(String argv[]) {

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

        String searchpath = "../input";

        String outfile = "text_on_color.pdf";

        String title = "Text on colored Background";


        pdflib p = null;

        int font, tf = -1, x = 50, y;

        String result, optlist;

        double width, height;


        String textline = "Giant Wing Paper Plane";


        String textflow =

            "To fold the famous rocket looper proceed as follows:\nTake a A4 "

            + "sheet. Fold it lenghtwise in the middle. Then, fold the upper "

            + "corners down. Fold the long sides inwards that the points A "

            + "and B meet on the central fold. Fold the points C and D that "

            + "the upper corners meet with the central fold as well. Fold 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_option("searchpath={" + searchpath + "}");


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

            p.set_option("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.14 $");


            /* Start Page */

            p.begin_page_ext(0, 0, "width=a4.width height=a4.height");


            /* Load font */

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


            if (font == -1)

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


            /* --------- Text line: Method I -------- */


            /*

             * Place a text line with a colored background using info_textline()

             * for retrieving the width and height of the default box for the

             * text. The height will be the capheight of the font by default. To

             * use another font property use the "matchbox" option as shown

             * below.

             */

            optlist = "font=" + font + " fontsize=40 " + "fillcolor={gray 1}";

            width = p.info_textline(textline, "width", optlist);

            height = p.info_textline(textline, "height", optlist);


            /* Draw a rectangle with exactly the retrieved width and height */

            p.setcolor("fill", "rgb", 0.0, 0.8, 0.8, 0);

            p.rect(x, 700, width, height);

            p.fill();


            /* Place the text line on the rectangle */

            p.fit_textline(textline, x, 700, optlist);


            /* --------- Text line: Method II -------- */


            /*

             * Place a text line with a colored background in a single

             * fit_textline() call. The "matchbox" option is used to specify the

             * rectangle to be colored. The "boxheight" suboption defines the

             * height of the matchbox while "offsetbottom" and "offsettop" adds

             * some space at the top and bottom. The width of the matchbox is

             * specified by the text width plus the space defined by

             * "offsetleft" and "offsetright".

             */

            optlist = "font=" + font + " fontsize=40 fillcolor={gray 1} "

                + "matchbox={fillcolor={rgb 0 0.8 0.8} "

                + "boxheight={ascender descender}}";


            /* Place the text line */

            p.fit_textline(textline, x, 600, optlist);


            /*

             * To increase the matchbox beyond the text, use "offsetbottom" and

             * "offsettop" to add some space at the top and bottom. Use

             * "offsetleft" and "offsetright" to add some space to the left and

             * right.

             */

            optlist = "font=" + font + " fontsize=40 fillcolor={gray 1} "

                + "matchbox={fillcolor={rgb 0 0.8 0.8} "

                + "boxheight={ascender descender} "

                + "offsetleft=-8 offsetright=8 offsettop=8 offsetbottom=-8}";


            /* Place the text line */

            p.fit_textline(textline, x, 500, optlist);


            /* --------- Textflow on a colored background -------- */


            /*

             * Place a white Textflow on a green background. The

             * background is created with a matchbox.

             */

            optlist = "font=" + font + " fontsize=20 fillcolor={gray 1}";


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

            if (tf == -1)

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


            /* Vertical start of the rectangle. */

            y = 430;


            /* Width for textflow */

            width = 500;


            /*

             * Now fit the textflow while creating a colored matchbox as

             * background.

             */

            result = p.fit_textflow(tf, x, 0, x + width, y,

                "matchbox={fillcolor={rgb 0.0 0.8 0.8}}");

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

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

            }


            /* Get the height of the Textflow placed. */

            height = p.info_textflow(tf, "textheight");


            /*

             * Place next rectangle by moving the vertical start point down

             * according to the height of the previous textflow plus an

             * additional offset.

             */

            y -= height + 50;


            /*

             * Fit the Textflow a second time and this time add a margin for the

             * matchbox. To rewind to the initial Textflow status use

             * "rewind=1".

             */

            result = p.fit_textflow(tf, x, 0, x + width, y,

                "rewind=1 matchbox={margin=-10 fillcolor={rgb 0.0 0.8 0.8}}");

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

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

            }


            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.toString());

        }

        finally {

            if (p != null) {

                p.delete();

            }

        }

    }

}