PDFlib Cookbook

cookbook

table/table_row_height

Define the height of the rows in a table.

Download Java Code  Switch to PHP Code  Show Output 

/*
 * Table row height:
 * Define the height of the rows in a table
 * 
 * Demonstrate how to use the "rowheight" option of add_table_cell() to 
 * define the height of the rows in a table.
 * 
 * Required software: PDFlib/PDFlib+PDI/PPS 9
 * Required data: image file
 */
package com.pdflib.cookbook.pdflib.table;

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

public class table_row_height
{
    public static void main (String argv[])
    {
        /* This is where the data files are. Adjust as necessary. */
        String searchpath = "../input";
        String outfile = "table_row_height.pdf";
        String title = "Table Row Height";

        pdflib p = null;
        String celloptlist, imgoptlist, taboptlist;
        int tbl = -1;
        String result;
        int exitcode = 0;
        
        /* Image files and handles*/
        int image1, image2;
        final String imagefile1 = "new.jpg";
        final String imagefile2 = "new.jpg";
        
        /* Text handles */
        int tf1 = -1, tf2 = -1; 
        
        /* x and y coordinates for the output of descriptive text */
        int ystart = 800;
        int ytext = ystart;
        int yoff = 15;
        int x = 20;
        
        /* Font handles and sizes */
        int normalfont, boldfont;
        int fontsize = 14;
        
        /* Coordinates of the lower left and upper right corners of the table 
         * fitbox. The width of the fitbox matches the sum of the widths of the
         * three table columns. 
         */
        double tab_llx = 100, tab_lly = 200, tab_urx = 450, tab_ury = 600;
        
        double rowheight1 = (tab_ury - tab_lly) / 2;
        double rowheight2 = (tab_ury - tab_lly) / 4;
        
        /* Textflows. Soft hyphens are marked with the character reference "­"
         * (character references are enabled by the "charref" option). */
        String tftext1 = 
            "Long Distance Glider: With this paper rocket you can send all your " +
            "messages even when sitting in a hall or in the cinema pretty near " +
            "the back.";
        
        String tftext2 =
            "Giant Wing: An unbeliev­able sailplane! It is amaz­ingly " +
            "robust and can even do aerobatics.";
        
       
        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);
            
            /* Load two images */
            image1 = p.load_image("auto", imagefile1, "");
            if (image1 == -1)
                throw new Exception("Error: " + p.get_errmsg());
            
            image2 = p.load_image("auto", imagefile2, "");
            if (image2 == -1)
                throw new Exception("Error: " + p.get_errmsg());

            /* Load the font */
            boldfont = p.load_font("NotoSerif-Bold", "unicode", "");
            if (boldfont == -1)
                    throw new Exception("Error: " + p.get_errmsg());
            
            normalfont = p.load_font("NotoSerif-Regular", "unicode", "");
            if (normalfont == -1)
                    throw new Exception("Error: " + p.get_errmsg());
            
            final String tfoptlist = "font=" + normalfont + " fontsize=" + 
                fontsize + " leading=110% charref";

            
            /* ---------------------------------------------------------------------
             * First case:
             * Create and fit a table containing two rows. The table width and 
             * height are given but no row height is defined. The row height will
             * be calculated automatically as a half, a third, a quarter etc. of the
             * table height, depending of the number of rows available. This means
             * that each row will always have the same size.
             * ---------------------------------------------------------------------
             */
            p.begin_page_ext(0, 0, "width=a4.width height=a4.height");
            
            /* In the first column of the first row, add a table cell containing a
             * Textflow
             */
            
            /* In the first column of the first row, add an image cell */
            imgoptlist = "image=" + image1;
            tbl = p.add_table_cell(tbl, 1, 1, "", imgoptlist);
            if (tbl == -1)
                throw new Exception("Error: " + p.get_errmsg());
            
            /* Add the Textflow to be placed in the table cell */
            tf1 = p.add_textflow(-1, tftext1, tfoptlist);
            if (tf1 == -1)
                throw new Exception("Error: " + p.get_errmsg());
            
            /* In the second column of the first row, add a Textflow table cell */
            celloptlist = "textflow=" + tf1;
            tbl = p.add_table_cell(tbl, 2, 1, "", celloptlist);
            if (tbl == -1)
                throw new Exception("Error: " + p.get_errmsg());
            
            /* In the first column of the second row, add an image cell */
            imgoptlist = "image=" + image2;
            tbl = p.add_table_cell(tbl, 1, 2, "", imgoptlist);
            if (tbl == -1)
                throw new Exception("Error: " + p.get_errmsg());
            
            /* Add the Textflow to be placed in the table cell */
            tf2 = p.add_textflow(-1, tftext2, tfoptlist);
            if (tf2 == -1)
                throw new Exception("Error: " + p.get_errmsg());
            
            /* In the second column of the second row, add a Textflow table cell */
            celloptlist = "textflow=" + tf2;
            tbl = p.add_table_cell(tbl, 2, 2, "", celloptlist);
            if (tbl == -1)
                throw new Exception("Error: " + p.get_errmsg());
            
            /* Set the stroke color which is used for testing to illustrate the
             * borders of the table fitbox with the "showborder" option below
             */
            p.setcolor("stroke", "rgb", 1.0, 0.5, 0.0, 0.0);
            
            /* Fit the table
             * The "stroke" option and the suboptions "line=frame linewidth=0.8" 
             * define the ruling of the table frame with a line width of 0.8.
             * Using "line=other linewidth=0.3" the ruling of all cells is specified
             * with a line width of 0.3.
             * "showborder" is only used for testing to illustrate the borders of
             * the table fitbox.
             */
            taboptlist = 
                "stroke={{line=frame linewidth=0.8} {line=other linewidth=0.3}}" +
                " showborder";
            
            result = p.fit_table(tbl, tab_llx, tab_lly, tab_urx, tab_ury, 
                taboptlist);
            
            /* 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 */
                    }
            }
            
            /* Output some descriptive text */
            p.setfont(boldfont, 14);
            p.fit_textline("Table size is defined as " + (int)(tab_urx - tab_llx) +  
                " x " + (int)(tab_ury - tab_lly), x, ytext, "");
            
            ytext -= yoff;
            p.fit_textline("No row height is defined", x, ytext, "");
            
            ytext -= 2 * yoff;
            p.fit_textline("Result:", x, ytext, "");
            ytext -= yoff;
            p.fit_textline("Row height will be calculated automatically",
                x, ytext, "");
            
            ytext -= yoff;
            p.fit_textline("With two rows available it will be half of the table " +
                "height", x, ytext, "");
            ytext -= yoff;
            p.fit_textline("(with three rows it will be a third of the table " +
                "height, etc.)", x, ytext, "");
            ytext -= yoff;
            p.fit_textline("As a result each row will have the same height", 
                x, ytext, "");
            
            p.end_page_ext("");
            
            
            /* ----------------------------------------------------------------
             * Second case:
             * Create and fit a table containing two rows. The table width and 
             * height are given. The row height of each row is set to a minimum
             * height of 1. In this case, the row height is calculated as it is 
             * needed to fit the object completely. 
             * ----------------------------------------------------------------
             */
            p.begin_page_ext(0, 0, "width=a4.width height=a4.height");
            ytext = ystart;
            tf1 = -1;
            tf2 = -1;
            tbl = -1;
            
            /* In the first column of the first row, add an image cell */
            imgoptlist = "image=" + image1;
            tbl = p.add_table_cell(tbl, 1, 1, "", imgoptlist);
            if (tbl == -1)
                throw new Exception("Error: " + p.get_errmsg());
            
            /* Add the Textflow to be placed in the table cell */
            tf1 = p.add_textflow(-1, tftext1, tfoptlist);
            if (tf1 == -1)
                throw new Exception("Error: " + p.get_errmsg());
            
            /* In the second column of the first row, add a Textflow table cell.
             * Use the "rowheight" option to set the minimum row height to 1. The
             * row height will be increased automatically as the object needs more
             * space to be fit completely.
             */
            celloptlist = "textflow=" + tf1 + " rowheight=1";
            tbl = p.add_table_cell(tbl, 2, 1, "", celloptlist);
            if (tbl == -1)
                throw new Exception("Error: " + p.get_errmsg());
            
            /* In the first column of the second row, add an image cell */
            imgoptlist = "image=" + image2;
            tbl = p.add_table_cell(tbl, 1, 2, "", imgoptlist);
            if (tbl == -1)
                throw new Exception("Error: " + p.get_errmsg());
            
            /* Add the Textflow to be placed in the table cell */
            tf2 = p.add_textflow(-1, tftext2, tfoptlist);
            if (tf2 == -1)
                throw new Exception("Error: " + p.get_errmsg());
            
            /* In the second column of the second row, add a Textflow table cell.
             * Use the "rowheight" option to set the minimum row height to 1. The
             * row height will be increased automatically as the object needs more
             * space to be fit completely.
             */
            celloptlist = "textflow=" + tf2 + " rowheight=1";
            tbl = p.add_table_cell(tbl, 2, 2, "", celloptlist);
            if (tbl == -1)
                throw new Exception("Error: " + p.get_errmsg());
            
            /* Set the stroke color which is used for testing to illustrate the
             * borders of the table fitbos with the "showborder" option below
             */
            p.setcolor("stroke", "rgb", 1.0, 0.5, 0.0, 0.0);
            
            /* Fit the table
             * The "stroke" option and the suboptions "line=frame linewidth=0.8" 
             * define the ruling of the table frame with a line width of 0.8.
             * Using "line=other linewidth=0.3" the ruling of all cells is specified
             * with a line width of 0.3.
             * "showborder" is only used for testing to illustrate the borders of
             * the table fitbox.
             */
            taboptlist = 
                "stroke={{line=frame linewidth=0.8} {line=other linewidth=0.3}}" +
                " showborder";
            
            result = p.fit_table(tbl, tab_llx, tab_lly, tab_urx, tab_ury, 
                taboptlist);
            
            /* 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 */
                }
            }
            
            /* Output some descriptive text */
            p.setfont(boldfont, 14);
            p.fit_textline("Table size is defined as " + (int)(tab_urx - tab_llx) +
                " x " + (int)(tab_ury - tab_lly), x, ytext, "");
            
            ytext -= yoff;
            p.fit_textline("For the first row rowheight=1 is defined", 
                x, ytext, "");
            ytext -= yoff;
            p.fit_textline("For the second row rowheight=1 is defined",
                x, ytext, "");
            
            ytext -= 2 * yoff;
            p.fit_textline("Result:", x, ytext, "");
            
            ytext -= yoff;
            p.fit_textline("rowheight=1 defines the minimum height of the row. " +
                "The height will ", x, ytext, "");
            
            ytext -= yoff;
            p.fit_textline("be increased automatically as the object needs more " +
                "space to be fit", x, ytext, "");
                  
            p.end_page_ext("");
            
            
            /* ---------------------------------------------------------------------
             * Third case:
             * Create and fit a table containing two rows. The table width and 
             * height are given. The row height of each row is defined as percentage 
             * of the overall table height.
             * ---------------------------------------------------------------------
             */
            p.begin_page_ext(0, 0, "width=a4.width height=a4.height");
            ytext = ystart;
            tf1 = -1;
            tf2 = -1;
            tbl = -1;
            
            /* In the first column of the first row, add an image cell */
            imgoptlist = "image=" + image1;
            tbl = p.add_table_cell(tbl, 1, 1, "", imgoptlist);
            if (tbl == -1)
                throw new Exception("Error: " + p.get_errmsg());
            
            /* Add the Textflow to be placed in the table cell */
            tf1 = p.add_textflow(-1, tftext1, tfoptlist);
            if (tf1 == -1)
                throw new Exception("Error: " + p.get_errmsg());
            
            /* In the second column of the first row, add a Textflow table cell */
            celloptlist = "textflow=" + tf1 + " rowheight=70%";
            tbl = p.add_table_cell(tbl, 2, 1, "", celloptlist);
            if (tbl == -1)
                throw new Exception("Error: " + p.get_errmsg());
            
            /* In the first column of the second row, add an image cell */
            imgoptlist = "image=" + image2;
            tbl = p.add_table_cell(tbl, 1, 2, "", imgoptlist);
            if (tbl == -1)
                throw new Exception("Error: " + p.get_errmsg());
            
            /* Add the Textflow to be placed in the table cell */
            tf2 = p.add_textflow(-1, tftext2, tfoptlist);
            if (tf2 == -1)
                throw new Exception("Error: " + p.get_errmsg());
            
            /* In the second column of the second row, add a Textflow table cell */
            celloptlist = "textflow=" + tf2 + " rowheight=30%";
            tbl = p.add_table_cell(tbl, 2, 2, "", celloptlist);
            if (tbl == -1)
                throw new Exception("Error: " + p.get_errmsg());
            
            /* Set the stroke color which is used for testing to illustrate the
             * borders of the table fitbos with the "showborder" option below
             */
            p.setcolor("stroke", "rgb", 1.0, 0.5, 0.0, 0.0);
            
            /* Fit the table
             * The "stroke" option and the suboptions "line=frame linewidth=0.8" 
             * define the ruling of the table frame with a line width of 0.8.
             * Using "line=other linewidth=0.3" the ruling of all cells is specified
             * with a line width of 0.3.
             * "showborder" is only used for testing to illustrate the borders of
             * the table fitbox.
             */
            taboptlist = 
                "stroke={{line=frame linewidth=0.8} {line=other linewidth=0.3}}" +
                " showborder";
            
            result = p.fit_table(tbl, tab_llx, tab_lly, tab_urx, tab_ury, 
                taboptlist);
            
            /* 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 */
                }
            }
            
            /* Output some descriptive text */
            p.setfont(boldfont, 14);
            p.fit_textline("Table size is defined as " + (int)(tab_urx - tab_llx) +
                " x " + (int)(tab_ury - tab_lly), x, ytext, "");
            
            ytext -= yoff;
            p.fit_textline("For the first row rowheight=70% is defined",
                x, ytext, "");
            ytext -= yoff;
            p.fit_textline("For the second row rowheight=30% is defined",
                x, ytext, "");
            
            ytext -= 2 * yoff;
            p.fit_textline("Result:", x, ytext, "");
            
            ytext -= yoff;
            p.fit_textline("The height of the first row will be 70% of " +
                "the table height", x, ytext, "");
            
            ytext -= yoff;
            p.fit_textline("The height of the second row will be 30% of the table" +
                " height", x, ytext, "");
                  
            p.end_page_ext("");
            
            
            /* -------------------------------------------------------------------
             * Fourth case:
             * Create and fit a table containing two rows. The table width and 
             * height are given. The row height of each row is defined as a fixed 
             * value.
             * -------------------------------------------------------------------
             */
            p.begin_page_ext(0, 0, "width=a4.width height=a4.height");
            ytext = ystart;
            tf1 = -1;
            tf2 = -1;
            tbl = -1;
            
            /* In the first column of the first row, add an image cell */
            imgoptlist = "image=" + image1;
            tbl = p.add_table_cell(tbl, 1, 1, "", imgoptlist);
            if (tbl == -1)
                throw new Exception("Error: " + p.get_errmsg());
            
            /* Add the Textflow to be placed in the table cell */
            tf1 = p.add_textflow(-1, tftext1, tfoptlist);
            if (tf1 == -1)
                throw new Exception("Error: " + p.get_errmsg());
            
            /* In the second column of the first row, add a Textflow table cell */
            celloptlist = "textflow=" + tf1 + " rowheight=" + rowheight1;
            tbl = p.add_table_cell(tbl, 2, 1, "", celloptlist);
            if (tbl == -1)
                throw new Exception("Error: " + p.get_errmsg());
            
            /* In the first column of the second row, add an image cell */
            imgoptlist = "image=" + image2;
            tbl = p.add_table_cell(tbl, 1, 2, "", imgoptlist);
            if (tbl == -1)
                throw new Exception("Error: " + p.get_errmsg());
            
            /* Add the Textflow to be placed in the table cell */
            tf2 = p.add_textflow(-1, tftext2, tfoptlist);
            if (tf2 == -1)
                throw new Exception("Error: " + p.get_errmsg());
            
            /* In the second column of the second row, add a Textflow table cell */
            celloptlist = "textflow=" + tf2 + " rowheight=" + rowheight2;
            tbl = p.add_table_cell(tbl, 2, 2, "", celloptlist);
            if (tbl == -1)
                throw new Exception("Error: " + p.get_errmsg());
            
            /* Set the stroke color which is used for testing to illustrate the
             * borders of the table fitbos with the "showborder" option below
             */
            p.setcolor("stroke", "rgb", 1.0, 0.5, 0.0, 0.0);
            
            /* Fit the table
             * The "stroke" option and the suboptions "line=frame linewidth=0.8" 
             * define the ruling of the table frame with a line width of 0.8.
             * Using "line=other linewidth=0.3" the ruling of all cells is specified
             * with a line width of 0.3.
             * "showborder" is only used for testing to illustrate the borders of
             * the table fitbox.
             */
            taboptlist = 
                "stroke={{line=frame linewidth=0.8} {line=other linewidth=0.3}}" +
                " showborder";
            
            result = p.fit_table(tbl, tab_llx, tab_lly, tab_urx, tab_ury, 
                taboptlist);
            
            /* 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 */
                }
            }
            
            /* Output some descriptive text */
            p.setfont(boldfont, 14);
            p.fit_textline("Table size is given as " + (int)(tab_urx - tab_llx) + 
                " x " + (int)(tab_ury - tab_lly), x, ytext, "");
            
            ytext -= yoff;
            p.fit_textline("For the first row rowheight=" + (int)rowheight1 + 
                " is defined", x, ytext, "");
            ytext -= yoff;
            p.fit_textline("For the second row rowheight="  + (int)rowheight2 +
                " is defined", x, ytext, "");
            
            ytext -= 2 * yoff;
            p.fit_textline("Result:", x, ytext, "");
            
            ytext -= yoff;
            p.fit_textline("rowheight defines the minimum row height. Since the " +
                "objects need less space it ", x, ytext, "");
            
            ytext -= yoff;
            p.fit_textline("does not have to be increased. Parts of the table " +
                "fitbox will be left empty", x, ytext, "");
                  
            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);
        }
    }
}