PDFlib

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

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

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

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

フォームとレイヤー

英語またはドイツ語の、テキストと、コンボボックスフィールドを持つ2つのレイヤーを作成するプログラムです。

"English"と"Deutsch"の2つのフォームフィールドボタン作成し、ボタンを押すことで、レイヤーの切り替えを行います。

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


/*
 * フォームとレイヤー:
 * 英語とドイツ語コンテンツのための2つのレイヤーを定義し、フィールドボ
 * タンを経由して表示、非表示を行います。
 *
 * いくつかのテキストと"combobox"フィールドを持つ2つのレイヤーを定義します。
 * "combobox"フィールドでは英語またはドイツ語でリスト項目を持ちます。
 * ユーザーがボタンを押した時、英語、またはドイツ語のレイヤーを表示する
 * ため "English"と"Deutsch"の2つのフォームフィールドボタンを作成します。
 *
 * 必要な製品 : PDFlib/PDFlib+PDI/PPS 9
 * 必要なデータ : 無し
 */
package com.pdflib.cookbook.pdflib.form_fields;

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

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

        pdflib p = null;
        int exitcode = 0;
        String optlist;
        int font, en_act, de_act, layerEN, layerDE;
        double width = 100, height = 18, llx = 100, lly = 600;

        try {
            p = new pdflib();

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

            /* load_font() 等でエラーが起きた場合、-1を返す */
            p.set_option("errorpolicy=return");

            /* レイヤーパネルを表示してドキュメントを開く */
            if (p.begin_document(outfile, "openmode=layers") == -1)
                throw new Exception("Error: " + p.get_errmsg());

            p.set_info("Creator", "PDFlib Cookbook");
            p.set_info("Title", title);

            /* フォントをロードする */
            font = p.load_font("Helvetica", "winansi", "");
            if (font == -1) {
                throw new Exception("Error: " + p.get_errmsg());
            }

            /* ページを始める */
            p.begin_page_ext(0, 0, "width=a4.width height=a4.height");

            /*
             * ------------------------------------------------------------------
             * "English"レイヤーを定義し、英語のテキストだけでなく、英語リストの
             * アイテムを表示する。 
             * "combobox"タイプのフォームフィールドを配置する。
             * ----------------------------------------------------------------
             * --
             */
            layerEN = p.define_layer("English", "");
            p.begin_layer(layerEN);

            /* コンボボックスのタイトルを英語で出力する */
            p.setfont(font, 12);
            p.fit_textline(
                "Choose a color from the list or enter an individual "
                    + "color:", llx, lly, "");
            lly -= 30;

            /*
             * 英語のリストアイテムを持つコンボボックスタイプの"color"フォームフィー
             * ルドを作成する。
             * もう片方のリストアイテムと高さを揃える。
             */
            optlist = "font="
                + font
                + " fontsize=14 backgroundcolor={gray 0.9} "
                + "bordercolor={gray 0.7} itemnamelist={0 1 2 3 4} currentvalue=4 "
                + "itemtextlist={yellow green blue red white} editable=true layer="
                + layerEN;

            p.create_field(llx, lly, llx + width, lly + height, "color",
                "combobox", optlist);

            /*
             * -----------------------------------------------------------------
             * "German"レイヤーを定義し、ドイツ語のテキストだけでなく、ドイツ語
             * リストのアイテムを表示する。
             * "combobox"タイプのフォームフィールドを配置する。
             * -----------------------------------------------------------------
             */
            layerDE = p.define_layer("German", "initialviewstate=false "
                + "initialprintstate=false");
            p.begin_layer(layerDE);

            lly = 600;
            p.fit_textline(
                "W\u00E4hlen Sie eine Farbe aus der Liste oder geben Sie "
                    + "eine eigene Farbe ein:", llx, lly, "");
            lly -= 30;

            /*
             * ドイツ語のリストアイテムを持つ"combobox"タイプの"farbe"フォーム
             * フィールドを作成する。
             * もう片方のリストアイテムと高さを揃える。
             */

            optlist = "font="
                + font
                + " fontsize=14 backgroundcolor={gray 0.9} "
                + "bordercolor={gray 0.7} itemnamelist={0 1 2 3 4} currentvalue=4 "
                + "itemtextlist={gelb gr\u00FCn blau rot wei\u00DF} editable=true layer="
                + layerDE;

            p.create_field(llx, lly, llx + width, lly + height, "farbe",
                "combobox", optlist);

            /*
             * "English" と "German"のレイヤーが表示される
             */
            p.set_layer_dependency("Radiobtn", "group={" + layerEN + " "
                + layerDE + "}");

            /*
             * English レイヤーを表示する "SetOCGState" アクションを作成する。
             * レイヤーが表示されている場合、German レイヤーを隠す必要はない。
             */
            en_act = p.create_action("SetOCGState", "layerstate={on " + layerEN
                + "}");

            /*
             * German レイヤーを表示する "SetOCGState" アクションを作成する。
             * レイヤーが表示されている場合、English レイヤーを隠す必要はない。
             */
            de_act = p.create_action("SetOCGState", "layerstate={on " + layerDE
                + "}");

            /*
             * "english" and "german" フォームフィールドを "pushbutton"タイプで
             * 作成する。
             * ユーザーがフォームフィールド内でマウスボタンを離したとき、他のレイ
             * ヤーのスイッチ上に定義されたアクションを使用する 
             * (action={up  }).
             */
            lly = 680;
            optlist =
                "bordercolor={rgb 0.25 0 0.95} backgroundcolor={rgb 0.95 0.95 1} "
                + "fillcolor={rgb 0.25 0 0.95} font=" + font + " fontsize=14";

            p.create_field(llx, lly, llx + width, lly + height, "english",
                "pushbutton", optlist + " caption={English} action={up "
                    + en_act + "}");

            llx += 150;

            p.create_field(llx, lly, llx + width, lly + height, "german",
                "pushbutton", optlist + " caption={Deutsch} action={up "
                    + de_act + "}");

            /* 全てのレイヤーを終了する */
            p.end_layer();

            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);
        }
    }
}
(Nov 16, 2015 - May 23, 2019)