PDFlib

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

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

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

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

タブによるリーダー

目次のように左のコンテンツと右のコンテンツの間にタブを指定することで、そのスペースをリーダーで埋める PDFlib のサンプルプログラムです。

目次を表すテキストフローを配置します。行毎に、目次ではテキストのエントリと対象となるページ番号の間にタブを含めます。タブによって定義された空間は、リーダーとして指定された文字で埋められます。


/*
 * Dot leaders with tabs:
 * Use leaders to fill the space defined by tabs between left-aligned and
 * right-aligned text, such as in a table of contents.
 * 
 * Place a Textflow representing a table of contents. In each line, the table
 * of contents contains tabs between the text entry and the corresponding
 * page number. The space defined by the tab will be filled with the
 * characters specified as leaders.
 *
 * Required software: PDFlib/PDFlib+PDI/PPS 10
 * Required data: none
 */
package com.pdflib.cookbook.pdflib.textflow;

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

public class dot_leaders_with_tabs
{
    public static void main (String argv[])
    {
        /* This is where the data files are. Adjust as necessary. */
        String searchpath = "../input";
        String outfile = "dot_leaders_with_tabs.pdf";
        String title = "Dot Leaders with Tabs";
        
        pdflib p = null;
        int exitcode = 0;
        
        int  tf = -1;
        String result;

        /* Option list for placing the Textflow: 
         * "ruler=100%" defines a tab position of 100% of the width, e.g. at
         * the right border of the text box. 
         * "hortabmethod=ruler" specifies to use the tab positions defined
         * with "ruler". 
         * "tabalignment=right" defines the tabs to be right-aligned.
         * We use the default leader character ".". To specify another
         * character(s), use the "text" suboption of the leader option, e.g.
         * "leader={text={+ }}" for defining the sequence "+ " as leaders. 
         */
        final String optlist = "fontname=NotoSerif-Regular fontsize=12 " +
             "leading=160% ruler=100% hortabmethod=ruler tabalignment=right";

        final String text = 
            "<alignment=left>Introduction<leader={alignment={grid}}>" +
            "\t7<nextline>" +
            "<alignment=left>Chapter 1<leader={alignment={grid}}>" +
            "\t25<nextline>" +
            "<alignment=left>Chapter 2<leader={alignment={grid}}>" +
            "\t107<nextline>" +
            "<alignment=left>Chapter 3<leader={alignment={grid}}>" +
            "\t219<nextline>" +
            "<alignment=left>Appendix<leader={alignment={grid}}>\t240";
           
        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);
            
            /* Create a Textflow */
            tf = p.create_textflow(text, optlist);
                if (tf == -1)
                    throw new Exception("Error: " + p.get_errmsg());
       
            /* Loop until all of the text is placed; create new pages
             * as long as more text needs to be placed.
             */
            do {
                p.begin_page_ext(0, 0, "width=a4.width height=a4.height");

                /* Place a text line with a title */
                p.fit_textline("Table of Contents", 50, 740, 
                    "fontname=NotoSerif-Bold fontsize=18");
                
                /* Place the Textflow with the table of contents */
                result = p.fit_textflow(tf, 50, 600, 500, 700, "");

                p.end_page_ext("");

                /* "_boxfull" means we must continue because there is more text;
                 * "_nextpage" is interpreted as "start new column"
                 */
            } while (result.equals("_boxfull") || result.equals("_nextpage"));

            /* Check for errors */
            if (!result.equals("_stop")) {
                /* "_boxempty" happens if the box is very small and doesn't
                 * hold any text at all.
                 */
                if (result.equals("_boxempty"))
                    throw new Exception("Error: Textflow box too small");
                else {
                    /* Any other return value is a user exit caused by
                     * the "return" option; this requires dedicated code to
                     * deal with.
                     */
                    throw new Exception("User return '" + result +
                        "' found in Textflow");
                }
            }

            p.delete_textflow(tf);

            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);
            exitcode = 1;
        } finally {
            if (p != null) {
                p.delete();
            }
            System.exit(exitcode);
        }
    }
}
(Apr 3, 2007 - Jun 20, 2024)