BASKET
Search
PDFlib

table/spread_text_over_cells

Download Java Code        Switch to PHP Code     Show Output PDF

/* $Id: spread_text_over_cells.java,v 1.6 2013/01/15 10:12:07 stm Exp $

 * Spread text over cells:

 * Spread a Textflow over several cells.

 *

 * Create a simple table with a Textflow which is spread over several cells. Use

 * the "continuetextflow" option of add_table_cell() for each Textflow cell the

 * Textflow is to be spread over.

 *

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

 * Required data: none

 */

package com.pdflib.cookbook.pdflib.table;


import com.pdflib.pdflib;

import com.pdflib.PDFlibException;


public class spread_text_over_cells

{

    public static void main (String argv[])

    {

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

    String searchpath = "../input";

    String outfile = "spread_text_over_cells.pdf";

    String title = "Spread Text over Cells";


    pdflib p = null;

    int font, boldfont, tf = -1, tbl = -1, image;

    int rowheight = 60;

    String result, optlist;

    String imagefile = "kraxi_logo.tif";

   

    /* Page width and height */

    int pagewidth = 595, pageheight = 421;

   

    /* Define the column widths of the first and the second column */

    int c1 = 80, c2 = 120;

   

    /* Define the lower left and upper right corners of the table instance.

     * The table width of 200 matches the sum of the widths of the two table

     * columns 80 + 120.

     */

    double llx = 100, lly = 100, urx = 300, ury = 350;

   

    final String tf_text =

        "Our paper planes are the ideal way of passing the time. We " +

        "offer revolution­ary new develop­ments of the " +

        "trad­itional common paper planes. If your les­son, " +

        "conference, or lect­ure turn out to be deadly boring, you can " +

        "have a wonderful time with our planes. All our models are folded " +

        "from one pap­er sheet. They are exclu­sively folded without " +

        "the use of any adhes­ive. Se­veral mod­els are " +

        "equip­ped with a folded landing gear.";


    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 + " $Revision: 1.6 $");


        /* Load the normal font */

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

        if (font == -1)

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

       

        /* Load bold font */

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

        if (boldfont == -1)

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

       

        /* Start the page */

        p.begin_page_ext(pagewidth, pageheight, "");

       

        /* Output some descriptive text */

        p.setfont(boldfont, 11);

        p.fit_textline("Use the \"textflow\" option of add_table_cell() ",

            llx, ury + 30, "");

        p.fit_textline("to spread a Textflow over several cells",

            llx, ury + 15, "");

       

       

        /* -----------------

         * Add an image cell

         * -----------------

         *

         * The image is placed in a cell starting in column 1 row 2. The column

         * width is 90 points. The cell margins are set to 4 points.

         */

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

        if (image == -1)

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


        optlist = "image=" + image + " colwidth=" + c1 + " margin=4";


        tbl = p.add_table_cell(tbl, 1, 2, "", optlist);

        if (tbl == -1)

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

       

       

        /* Add the Textflow */

        optlist = "font=" + font + " fontsize=8 leading=110% charref";

       

        tf = p.add_textflow(-1, tf_text, optlist);

        if (tf == -1)

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


        /* Prepare the option list for the Textflow cell

         *

         * Use the "continuetextflow" option to continue the Textflow in further

         * cells.

         * To avoid any space from the top add the Textflow cell use

         * "fittextflow={firstlinedist=capheight}". Then add a margin of 4

         * points.

         */

        optlist = "textflow=" + tf + " fittextflow={firstlinedist=capheight} " +

            "colwidth=" + c2 + " rowheight=" + rowheight +

            " margin=4 continuetextflow";

       

        /* Add the Textflow table cell in column 1 row 1 */

        tbl = p.add_table_cell(tbl, 1, 1, "", optlist);

        if (tbl == -1)

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

       

        /* Continue the Textflow table cell in column 2 row 2 */

        tbl = p.add_table_cell(tbl, 2, 2, "", optlist);

        if (tbl == -1)

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

       

        /* Continue the Textflow table cell in column 1 row 3 */

        tbl = p.add_table_cell(tbl, 1, 3, "", optlist);

        if (tbl == -1)

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

       

        /* Colorize the table cell in column 2 row 1 and column 2 row 3 using a

         * matchbox

         */

        optlist = "matchbox={fillcolor={rgb 0.8 0.8 0.87}}";

        tbl = p.add_table_cell(tbl, 2, 1, "", optlist);

        if (tbl == -1)

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

       

        tbl = p.add_table_cell(tbl, 2, 3, "", optlist);

        if (tbl == -1)

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


        /* Define the option list for fitting the table.

         *

         * The "stroke" option specifies the table ruling. The

         * "line=frame linewidth=0.8" suboptions define an outside ruling with a

         * line width of 0.8 and the "line=other linewidth=0.3" suboptions

         * define a cell ruling with a line width of 0.3.

         */

        optlist = "stroke={{line=frame linewidth=0.8} " +

            "{line=other linewidth=0.3}}";


        /* Place the table instance */

        result = p.fit_table(tbl, llx, lly, urx, ury, optlist);


        /* Check the result; "_stop" means all is ok */

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

            if (result.equals( "_error"))

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

            else {

                    /* Other return values require dedicated code to deal with */

            }

        }


        /* Delete the table handle. This will also delete any Textflow handles

         * used in the table

         */

        p.delete_table(tbl, "");

       

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

            }

        }

    }

}