PDFlib

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

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

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

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

番号付きリスト

PDFlibで、番号付きリストを左、および右寄せで出力するサンプルプログラムです。

番号が左寄せになる番号付きリストを作成します。add_textflow の leftindent オプションには、番号のために leftindent=0% を、テキストのために leftindent=10% を指定します。

番号が右寄せになる番号付きリストを作成します。番号は、hortabmethod=ruler、ruler=3%、tabalignment=right オプションにより指定された右寄せタビュレーターを使って出力されます。

add_textflow 関数の leftindent オプションにテキストを追加するために leftindent=10% を指定します。インデント値の設定やリセットは、それぞれのパラグラフ毎に特別に設定する必要があります。よりよい方法としてテキストフロー内で提供されるインラインオプションを含んだ list と呼ばれるマクロを使用するものがあります。

必要な製品 : PDFlib/PDFlib+PDI/PPS


/*
 * 番号付きリスト :
 * PDFlibで、番号付きリストを左、および右寄せで出力する
 *
 * 番号が左寄せになる番号付きリストを作成します。add_textflow の leftindent オプションには、
 * 番号のために leftindent=0% を、テキストのために leftindent=10% を指定します。
 *
 * 番号が右寄せになる番号付きリストを作成します。番号は、hortabmethod=ruler、ruler=3%、
 * tabalignment=right オプションにより指定された右寄せタビュレーターを使って出力されます。
 * add_textflow 関数の leftindent オプションにテキストを追加するために leftindent=10% を
 * 指定します。インデント値の設定やリセットは、それぞれのパラグラフ毎に特別に設定する必要
 * があります。よりよい方法としてテキストフロー内で提供されるインラインオプションを含んだ 
 * list と呼ばれるマクロを使用するものがあります。
 *
 * 必要な製品 : PDFlib/PDFlib+PDI/PPS 10
 * 必要なデータ : 無し
 */
package com.pdflib.cookbook.pdflib.textflow;

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

public class numbered_list
{
    public static void main (String argv[])
    {
        /* 必要に応じてデータファイルがあるフォルダのパスを指定する */
        String searchpath = "../input";

        String outfile = "numbered_list.pdf";
        String title = "numbered_list";
        
        pdflib p = null;
        
        int tf = -1, i;
        final double llx=50, lly=200, urx=500, ury=700;
        String result;
        String head_optlist, num_optlist, item_optlist;
        int exitcode = 0;

        head_optlist =
            "fontname=NotoSerif-Bold fontsize=14 fillcolor={cmyk 1 0.5 0.2 0}";

        final String numbers [] = {
            "I", "II", "III", "IV"
        };

        final String items [] = {
            "Long Distance Glider\nWith this paper rocket you can send all your" +
            "messages even when sitting in a hall or in the cinema pretty near " +
            "the back.",

            "Giant Wing\nAn unbelievable sailplane! It is amazingly robust and " +
            "can even do aerobatics. But it best suited to gliding.",

            "Cone Head Rocket\nThis paper arrow can be thrown with big swing. " +
            "We launched it from the roof of a hotel. It stayed in the air a " +
            "long time and covered a considerable distance.",

            "Super Dart\nThe super dart can fly giant loops with a radius of 4 " +
            "or 5 meters and cover very long distances. Its heavy cone point is " +
            "slightly bowed upwards to get the lift required for loops."
        };

        try {
            p = new pdflib();

            p.set_option("searchpath={" + searchpath + "}");

            /* load_font() 等の戻り値を確認する */
            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);

        
            /* ----------------------------------------------------
             * 左寄せの番号リストを作成
             * ----------------------------------------------------
             */
            p.begin_page_ext(0, 0, "width=a4.width height=a4.height");

            /* テキストフローを作成する。初めに見出しを追加する */
            tf = p.add_textflow(tf, "Page 1: Numbered list with the numbers " +
                "left-aligned", head_optlist);

            /* テキストのための一般的なオプションを定義する */
            tf = p.add_textflow(tf, "",
                "fontname=NotoSerif-Regular fontsize=12 charref " +
                "alignment=justify leading=140% ");
            if (tf == -1)
                throw new Exception("Error: " + p.get_errmsg());

            /* 番号リストと、リストアイテム(テキスト)を追加する。
             * "leftindent=0%"と"leftindent=10%"オプションを使用し、テキストフローの
             * はめ込み枠の幅の0% または 10% のインデントを作成する。
             */
            num_optlist = "fillcolor={cmyk 1 0.5 0.2 0} leftindent=0%";

            item_optlist = "fillcolor={gray 0} leftindent=10%";

            for (i = 0; i < 4; i++) {
                tf = p.add_textflow(tf, "\n\n" + numbers[i], num_optlist);
                if (tf == -1)
                    throw new Exception("Error: " + p.get_errmsg());

                tf = p.add_textflow(tf, items[i], item_optlist);
                if (tf == -1)
                    throw new Exception("Error: " + p.get_errmsg());
            }

            /* テキストフローを配置する */
            result = p.fit_textflow(tf, llx, lly, urx, ury, "");
            if (!result.equals("_stop")) {
                /* チェックを行う */
            }

            p.delete_textflow(tf);

            p.end_page_ext("");

            /* -----------------------------------------------------
             * 右寄せの番号リストを作成
             * -----------------------------------------------------
             */
            p.begin_page_ext(0, 0, "width=a4.width height=a4.height");
            tf = -1;

            /* テキストフローを作成する。初めに見出しを追加する */
            tf = p.add_textflow(tf, "Page 2: Numbered list with the numbers " +
                "right-aligned", head_optlist);
            if (tf == -1)
                throw new Exception("Error: " + p.get_errmsg());

            /* 一般的なテキストのオプションを定義する。
             * 
             * タブの設定を定義する。テキストフローのはめ込み枠の幅の3%で、タブの
             * 位置を指定するには "hortabmethod=ruler" と"ruler=3%" を使用する。
             * タブ位置を右揃えにするには "tabalignment=right" を使用する。 
             */
            tf = p.add_textflow(tf, "",
                "fontname=NotoSerif-Regular fontsize=12 charref " +
                "alignment=justify leading=140% " +
                "hortabmethod=ruler ruler=3% tabalignment=right");
            if (tf == -1)
                throw new Exception("Error: " + p.get_errmsg());

            /* 番号リストのオプションを定義する */
            num_optlist =
                "fillcolor={cmyk 1 0.5 0.2 0} leftindent=0%";

            /* テキスト用のオプションリスト */
            item_optlist =
                "fillcolor={gray 0} leftindent=10%";

            /* 番号リストとテキストをテキストフローに追加する */
            for (i = 0; i < 4; i++) {
                tf = p.add_textflow(tf, "\n\n\t" + numbers[i], num_optlist);
                if (tf == -1)
                    throw new Exception("Error: " + p.get_errmsg());

                tf = p.add_textflow(tf, items[i], item_optlist);
                if (tf == -1)
                    throw new Exception("Error: " + p.get_errmsg());
            }

            /* テキストフローを配置する */
            result = p.fit_textflow(tf, llx, lly, urx, ury, "");
            if (!result.equals("_stop")) {
                /* チェックを行う */
            }

            p.delete_textflow(tf);

            p.end_page_ext("");
            
            
            /* -----------------------------------
             * マクロを使用して番号リストを作成する
             * -----------------------------------
             */
            p.begin_page_ext(0, 0, "width=a4.width height=a4.height");
            tf = -1;
            
            /* 見出しを追加する */
            p.fit_textline("Page 3: Numbered list using macros of inline options",
                llx, ury-17, head_optlist);
          
            /* インデント値の設定と解除は、段落ごとに必要なため面倒である。
             * 解決策は"list "というマクロを定義することである。
             * 便宜上"indent "というマクロを定義し、これを定数として使用する。
             *
             * "leftindent "オプションは、左余白からの距離を指定する。
             * "parindent "オプションには、負の値が設定されており、
             * 各段落の先頭行のインデントを解除する。
             * 
             * オプション "hortabsize"、"hortabmethod"、"ruler "は、"leftindent "に
             * 対応したタブ位置を指定している。
             * これらのオプションは、"leftindent "に対応するタブ位置を指定している。
             * このため、番号の後のテキストは、leftindent で指定した分だけ
             * インデントされるようになる。
             */

            String text =
                "<macro " +
                "{indent {22} " +
                "list {parindent=-&indent leftindent=&indent hortabsize=&indent " +
                "hortabmethod=ruler ruler={&indent}}" +
                "}><&list>" +
                "1.\tLong Distance Glider<nextline>With this paper rocket you " +
                "can send all your messages even when sitting in a hall or in " +
                "the cinema pretty near the back.\n\n" +
                "2.\tGiant Wing<nextline>An unbelievable sailplane! It is " +
                "amazingly robust and can even do aerobatics. But it is best " +
                "suited to gliding.\n\n" +
                "3.\tCone Head Rocket<nextline>This paper arrow can be thrown " +
                "with big swing. We launched it from the roof of a hotel. It " +
                "stayed in the air a long time and covered a considerable " +
                "distance.\n\n" +
                "4.\tSuper Dart<nextline>The super dart can fly giant loops " +
                "with a radius of 4 or 5 meters and cover very long distances. " +
                "Its heavy cone point is slightly bowed upwards to get the lift " +
                "required for loops.";
            
            String optlist =
                "fontname=NotoSerif-Regular fontsize=12 " +
                "fillcolor={gray 0} alignment=justify leading=140%";
            
            tf = p.create_textflow(text, optlist);
            if (tf == -1)
                throw new Exception("Error: " + p.get_errmsg());
            
            /* テキストフローを配置する */
            result = p.fit_textflow(tf, llx, lly, urx, ury-34, "");
            if (!result.equals("_stop")) {
                /* チェックを行う */
            }
            
            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);
            exitcode = 1;
        } finally {
            if (p != null) {
                p.delete();
            }
            System.exit(exitcode);
        }
    }
}
(Apr 3, 2007 - Feb 28, 2024)