PDFlib Cookbook

cookbook

textflow/continue_note_after_text

Insert a dot sequence automatically at the end of a Textflow fitbox after the last word which can be shown together with the dots completely inside the fitbox.

Download Java Code  Switch to PHP Code  Show Output 

/*
 * Continue note after text:
 * Insert a dot sequence or an arrow image at the end of a Textflow.
 *
 * Place a Textflow while inserting a dot sequence at the end of each fitbox.
 * Use the "width" key of info_textline() to retrieve the width of the dot 
 * sequence. Fit the Textflow with the "createlastindent" option set to the 
 * width of the dot sequence to specify appropriate empty space at the end of 
 * the fitbox. Then, fill the space between the last word and the right fitbox
 * border with the dot sequence using fit_textline(). For placing the dot 
 * sequence use the font and font size of the Textflow by retrieving it with
 * info_textflow() and the "lastfont" and "lastfontsize" options.
 * Similarly, place a Textflow but insert an arrow image at the end of each
 * fitbox.
 *
 * Required software: PDFlib/PDFlib+PDI/PPS 10
 * Required data: none
 */
package com.pdflib.cookbook.pdflib.textflow;

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

public class continue_note_after_text {
    public static void main(String argv[]) {
        /* This is where the data files are. Adjust as necessary. */
        String searchpath = "../input";
        String outfile = "continue_note_after_text.pdf";
        String title = "Continue Note after Text";

        pdflib p = null;
        int exitcode = 0;

        final String imagefile = "arrow.jpg";

        String optlist, result;
        int font, image, tf = -1;

        double textendx, textendy;
        double lastfont, lastfontsize;

        double textwidth;
        final double imagewidth = 30;

        final int fontsize = 20;

        final double llx = 100, urx = 400;
        final double lly_start = 630, ury_start = 780;
        double lly = lly_start, ury = ury_start;
        double yoffset = 190;

        final String dots = "...";

        final String tftext = 
            "Our paper planes are the ideal way of passing the time. We "
            + "offer revolutionary new developments of the traditional common "
            + "paper planes. If your lesson, conference, or lecture turn out "
            + "to be deadly boring, you can have a wonderful time with our "
            + "planes. All our models are folded from one paper sheet. They "
            + "are exclusively folded without using any adhesive. Several "
            + "mod­els are equipped with a folded landing gear enabling a "
            + "safe land­ing on the intended location pro­vided that "
            + "you have aimed well. Other models are able to fly loops or "
            + "cover long distances. Let them start from a vista point in the "
            + "mountains and see where they touch the ground. "
            + "Have a look at our new paper plane models!";

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

            if (p.begin_document(outfile, "") == -1)
                throw new Exception("Error: " + p.get_errmsg());

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

            font = p.load_font("NotoSerif-Bold", "unicode", "");
            if (font == -1)
                throw new Exception("Error: " + p.get_errmsg());

            /* Load the image */
            image = p.load_image("auto", imagefile, "");
            if (image == -1)
                throw new Exception("Error: " + p.get_errmsg());

            /*
             * ----------------------------------------------------------------
             * Place a Textflow in one ore more fitboxes while inserting
             * three dots at the end of each fitbox
             * ----------------------------------------------------------------
             */

            /* Start page */
            p.begin_page_ext(0, 0, "width=a4.width height=a4.height");

            p.setfont(font, 16);
            p.fit_textline("Page 1: Three dots will be appended at the end of "
                    + "each fitbox", 20, 810, "");

            /* Add the Textflow */
            optlist = "fontname=NotoSerif-Regular fontsize=" + fontsize
                    + " leading=120% charref alignment=justify ";

            tf = p.add_textflow(tf, tftext, optlist);
            if (tf == -1)
                throw new Exception("Error: " + p.get_errmsg());

            /*
             * Retrieve the length of the text to be inserted at the end of the
             * Textflow
             */
            optlist = "font=" + font + " fontsize=" + fontsize;
            textwidth = p.info_textline(dots, "width", optlist);

            /* Initialize the fitbox rectangle */
            lly = lly_start;
            ury = ury_start;

            /*
             * Loop until all of the text is placed; create new fitboxes as long
             * as more text needs to be placed.
             */
            do {
                /*
                 * Prepare the option list with the "createlastindent" option 
                 * set to the text width retrieved for the dots above
                 */
                p.setcolor("stroke", "rgb", 0.99, 0.44, 0.06, 0);

                optlist = "verticalalign=justify linespreadlimit=120% "
                        + "createlastindent={rightindent=" + textwidth + "} "
                        + "showborder";

                /* Place the Textflow */
                result = p.fit_textflow(tf, llx, lly, urx, ury, optlist);

                /* Check for errors */
                if (result.equals("_boxempty"))
                    throw new Exception("Error: Textflow box too small");

                if (!result.equals("_stop")) {
                    /* Get the text position at the end of the Textflow */
                    textendx = p.info_textflow(tf, "textendx");
                    textendy = p.info_textflow(tf, "textendy");

                    /*
                     * Get the font and font size used in the last text line of
                     * the fitbox
                     */
                    lastfont = p.info_textflow(tf, "lastfont");
                    lastfontsize = p.info_textflow(tf, "lastfontsize");

                    /* Place the dots at the end of the Textflow */
                    optlist = "font=" + (int) lastfont + " fontsize=" + lastfontsize;
                    p.fit_textline(dots, textendx, textendy, optlist);

                    lly -= yoffset;
                    ury -= yoffset;
                }

                /* 
                 * "_boxfull" means we must continue because there is more text 
                 */
            }
            while (result.equals("_boxfull"));

            p.delete_textflow(tf);

            p.end_page_ext("");

            /*
             * -----------------------------------------------------------------
             * Place a Textflow in one ore more fitboxes while inserting an
             * image containing an arrow at the end of each fitbox
             * -----------------------------------------------------------------
             */

            /* Start page */
            p.begin_page_ext(0, 0, "width=a4.width height=a4.height");

            tf = -1;

            p.setfont(font, 16);
            p.fit_textline(
                    "Page 2: An arrow image will be appended at the end "
                            + "of each fitbox", 20, 810, "");

            /*
             * Retrieve the length of the text to be inserted at the end of the
             * Textflow
             */
            optlist = "font=" + font + " fontsize=" + fontsize;

            /* Define the Textflow options */
            optlist = "fontname=NotoSerif-Regular fontsize=" + fontsize
                    + " leading=120% charref alignment=justify ";

            /* Add the Textflow */
            tf = p.add_textflow(tf, tftext, optlist);
            if (tf == -1)
                throw new Exception("Error: " + p.get_errmsg());

            /* Initialize the fitbox rectangle */
            lly = lly_start;
            ury = ury_start;

            /*
             * Loop until all of the text is placed; create new fitboxes as long
             * as more text needs to be placed.
             */
            do {
                /*
                 * Prepare the option list with the "createlastindent" option 
                 * set to the text width retrieved for the dots above
                 */
                p.setcolor("stroke", "rgb", 1, 0, 0, 0);

                optlist = "verticalalign=justify linespreadlimit=120% "
                        + "createlastindent={rightindent=" + imagewidth + "}";

                /* Place the Textflow */
                result = p.fit_textflow(tf, llx, lly, urx, ury, optlist);

                /* Check for errors */
                if (result.equals("_boxempty"))
                    throw new Exception("Error: Textflow box too small");

                if (!result.equals("_stop")) {
                    /* Get the text position at the end of the Textflow */
                    textendx = (int) p.info_textflow(tf, "textendx");
                    textendy = (int) p.info_textflow(tf, "textendy");

                    /* Place the image at the end of the Textflow */
                    optlist = "boxsize={" + imagewidth + " " + (fontsize * 0.8)
                            + "} " + "fitmethod=meet position={right top}";

                    p.fit_image(image, textendx, textendy, optlist);

                    lly -= yoffset;
                    ury -= yoffset;
                }

                /* 
                 * "_boxfull" means we must continue because there is more text
                 */
            }
            while (result.equals("_boxfull"));

            p.delete_textflow(tf);

            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.toString());
            exitcode = 1;
        }
        finally {
            if (p != null) {
                p.delete();
            }
            System.exit(exitcode);
        }
    }
}