PDFlib

高度なPDFアプリケーションの開発を支援する定番プログラムライブラリー Supported by インフォテック株式会社

PDFlib サンプル集(クックブック)

本サンプルプログラムは、PDF 文書生成ライブラリーの実装である PDFlib の基本的な機能を実際のプログラムで紹介したものです。

本サイトでダウンロードした PDFlib は、一部機能の制限を除き、評価版として無償でお使いいただけます。

テキストを複数のテーブルセルに分割して配置

テーブルのセルに複数行のテキストを配置する際、配置しきれなかった分を別のセルに配置することができます。

テキストフローハンドル tf を PDF_add_table_cell() に渡す際に continuetextflow オプションを渡します。continuetextflow を受け取ったセルでは、tf の未配置のテキストを可能なだけ配置します。

必要な製品:PDFlib または PDFlib+PDI または PPS


/*
 * 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";
    int exitcode = 0;
    
    /* 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);

        /* 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.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);
        }
    }
}
(Sep 18, 2013 - Oct 23, 2022)