PDFlib

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

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

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

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

JavaScript アクションのためのトリガ

PDFlib でテキストフィールドに JavaScript アクションと、アクション実行のトリガを埋め込みます。

アクション実行のトリガとして、以下を create_field() の "action" オプションで指定します。

activate, keystroke, format, validate, calculate, enter, exit, down, up, focus, blur

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


/*
 * JavaScript アクションのためのトリガ :
 * フォームフィールドからJavaScript アクションをトリガとする全ての
 * 可能性を示します。
 *
 * テキストフィールド上で色々なアクションを実行するための、Javascript 
 * アクショントリガです。
 * 下記のイベントのトリガを create_field() の "action" オプションで指定
 * してください。
 * activate, keystroke, format, validate, calculate, enter, exit, down,
 * up, focus, blur.
 *
 * 必要な製品 : PDFlib/PDFlib+PDI/PPS 9
 * 必要なデータ : 無し
 */
package com.pdflib.cookbook.pdflib.form_fields;

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

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

        pdflib p = null;
        String optlist;
        int font, action;
        int llx = 100, lly = 780, urx = 400, ury = 800;
        int tx = 50, ty_off = 7;
        int exitcode = 0;

        try {
            p = new pdflib();

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

            /* load_font() 等でエラーが起きた場合、-1を返す */
            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);

            font = p.load_font("Helvetica", "winansi", "");
            if (font == -1)
                throw new Exception("Error: " + p.get_errmsg());

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

            p.setfont(font, 12);
            
            
            /* -----------------------------------------------------------------------
             * フィールドがアクティブにされた時を、JavaScript アクションのトリガとする
             * -----------------------------------------------------------------------
             */

            /* Javascript アクションを生成する */
            action = p.create_action("JavaScript", "script {" +
                "app.alert(\"Action triggered by the event 'activate' for the " +
                "text field\");}");

            /* アクションをトリガとするテキストフォールドを生成する */
            optlist = "action {activate={" + action + "} " + "} " +
                "currentvalue={Click in this field} " +
                "backgroundcolor={rgb 0.95 0.95 0.7} font=" + font;
                            
            p.create_field(llx, lly, urx, ury, "activate_field", "textfield",
                optlist);
            p.fit_textline("activate", tx, lly + ty_off,
                "fillcolor={rgb 0.47 0.47 0.34} fontsize=10");


            /* -------------------------------------------------------------------------
             * ユーザーのテキストフィールドの入力を、JavaScript アクションのトリガとする
             * -------------------------------------------------------------------------
             */

            /* Javascript アクションを生成する */
            action = p.create_action("JavaScript", "script {" +
                "app.alert(\"Action triggered by the event 'keystroke' for the " +
                "text field\");}");

            /* アクションをトリガとするテキストフォールドを生成する */
            optlist = "action {keystroke={" + action + "} " + "} " +
                "currentvalue={Press a key in this field} " +
                "backgroundcolor={rgb 0.95 0.95 0.7} font=" + font;
                            
            p.create_field(llx, lly-=50, urx, ury-=50, "keystroke_field", 
                "textfield", optlist);
            p.fit_textline("keystroke", tx, lly + ty_off,
                "fillcolor={rgb 0.47 0.47 0.34} fontsize=10");


            /* --------------------------------------------------------------------
             * フィールドがその時点の値を表示するためにフォーマットされる前に実行
             * させる JavaScript アクショントリガ。
             * これを使うと、フィールドの値をフォーマットされる前に変更することが
             * できる。
             * --------------------------------------------------------------------
             */

            /* JavaScript アクションを生成する */
            action = p.create_action("JavaScript", "script {" +
                "app.alert(\"Action triggered by the event 'format' for the text " +
                "field\");}");

            /* アクションをトリガーとするテキストフィールドを生成する */
            optlist = "action {format={" + action + "} " + "} " +
                "currentvalue={Enter text and press Return} " +
                "backgroundcolor={rgb 0.95 0.95 0.7} font=" + font;
                            
            p.create_field(llx, lly-=50, urx, ury-=50, "format_field", "textfield",
                optlist);
            p.fit_textline("format", tx, lly + ty_off,
                "fillcolor={rgb 0.47 0.47 0.34} fontsize=10");


            /* -------------------------------------------------------------------
             * フィールドの値の変更を、JavaScript アクションのトリガとする。
             * これは新しい値が有効かどうかを確認することができる。
             * -------------------------------------------------------------------
             */

            /* JavaScript アクションを生成する */
            action = p.create_action("JavaScript", "script {" +
                "app.alert(\"Action triggered by the event 'validate' for the " +
                "text field\");}");

            /* アクションをトリガとするテキストフィールドを生成する */
            optlist = "action {validate={" + action + "} " + "} " +
                "currentvalue={Enter text and press Return} " +
                "backgroundcolor={rgb 0.95 0.95 0.7} font=" + font;
                            
            p.create_field(llx, lly-=50, urx, ury-=50, "validate_field",
                "textfield", optlist);
            p.fit_textline("validate", tx, lly + ty_off,
                "fillcolor={rgb 0.47 0.47 0.34} fontsize=10");


            /* ---------------------------------------------------------------------
             * 他のフィールドの値が変更され、再計算がされた時を、JavaScript アクショ
             * ンのトリガとする
             * ---------------------------------------------------------------------
             */
            
            /* JavaScript アクションを生成する */
            action = p.create_action("JavaScript", "script {" +
                "app.alert(\"Action triggered by the event 'calculate' for the " +
                "text field\");}");

            /* アクションをトリガするテキストフィールドを生成する */
            optlist = "action {calculate={" + action + "} " + "} " +
                "currentvalue={Enter text and press Return} " +
                "backgroundcolor={rgb 0.95 0.95 0.7} font=" + font;
                            
            p.create_field(llx, lly-=50, urx, ury-=50, "calculate_field",
                "textfield", optlist);
            p.fit_textline("calculate", tx, lly + ty_off,
                "fillcolor={rgb 0.47 0.47 0.34} fontsize=10");


            /* ----------------------------------------------------------------------
             * フィールドエリア内でマウスボタンが押された時を、JavaScript アクション
             * のトリガとする
             * ----------------------------------------------------------------------
             */

            /* JavaScript アクションを生成する */
            action = p.create_action("JavaScript", "script {" +
                "app.alert(\"Action triggered by the event 'down' for the text " +
                "field\");}");

            /* アクションをトリガするテキストフィールドを生成する */
            optlist = "action {down={" + action + "} " + "} " +
                "currentvalue={Press the mouse button in this field} " +
                "backgroundcolor={rgb 0.95 0.95 0.7} font=" + font;
                            
            p.create_field(llx, lly-=50, urx, ury-=50, "down_field", "textfield",
                optlist);
            p.fit_textline("down", tx, lly + ty_off,
                "fillcolor={rgb 0.47 0.47 0.34} fontsize=10");


            /* ------------------------------------------------------------------------
             * フィールドエリア内にでマウスボタンを放した時を、JavaScript アクションの
             * トリガとする
             * ------------------------------------------------------------------------
             */

            /* JavaScript アクションを生成する */
            action = p.create_action("JavaScript", "script {" +
                "app.alert(\"Action triggered by the event 'up' for the text " +
                "field\");}");

            /* アクションをトリガするテキストフィールドを生成する */
            optlist = "action {up={" + action + "} " + "} " +
                "currentvalue={Press and then release the mouse button in this " +
                "field} backgroundcolor={rgb 0.95 0.95 0.7} font=" + font;
                            
            p.create_field(llx, lly-=50, urx, ury-=50, "up_field", "textfield",
                optlist);
            p.fit_textline("up", tx, lly + ty_off,
                "fillcolor={rgb 0.47 0.47 0.34} fontsize=10");


            /* ------------------------------------------------------------------
             * テキストフィールドが入力フォーカスを受け取った時を、JavaScript アク
             * ションのトリガとする
             * ------------------------------------------------------------------
             */

            /* JavaScript アクションを生成する */
            action = p.create_action("JavaScript", "script {" +
                "app.alert(\"Action triggered by the event 'focus' for the " +
                "text field\");}");

            /* アクションをトリガするテキストフィールドを生成する */
            optlist = "action {focus={" + action + "} " + "} " +
                "currentvalue={Click this field} " +
                "backgroundcolor={rgb 0.95 0.95 0.7} font=" + font;
                            
            p.create_field(llx, lly-=50, urx, ury-=50, "focus_field", "textfield",
                optlist);
            p.fit_textline("focus", tx, lly + ty_off,
                "fillcolor={rgb 0.47 0.47 0.34} fontsize=10");


            /* -----------------------------------------------------------------------
             * テキストフィールドが入力フォーカスを失った時を、JavaScript アクションの
             * トリガとする
             * -----------------------------------------------------------------------
             */

             /* JavaScript アクションを生成する */
            action = p.create_action("JavaScript", "script {" +
                "app.alert(\"Action triggered by the event 'blur' for the " +
                    "text field\");}");

            /* アクションをトリガするテキストフィールドを生成する */
            optlist = "action {blur={" + action + "} " + "} " +
                "currentvalue={Click this and then another field} " +
                "backgroundcolor={rgb 0.95 0.95 0.7} font=" + font;

            p.create_field(llx, lly-=50, urx, ury-=50, "blur_field", "textfield",
                optlist);
            p.fit_textline("blur", tx, lly + ty_off,
                "fillcolor={rgb 0.47 0.47 0.34} fontsize=10");


            /* -----------------------------------------------------------------------
             * マウスがフィールド内に移動した時を、JavaScript アクションのトリガとする
             * -----------------------------------------------------------------------
             */
            
            /* JavaScript アクションを生成する */
            action = p.create_action("JavaScript", "script {" +
                "app.alert(\"Action triggered by the event 'enter' for the " +
                "text field\");}");

            /* アクションをトリガするテキストフィールドを生成する */
            optlist = "action {enter={" + action + "} " + "} " +
                "currentvalue={Move the mouse into this field} " +
                "backgroundcolor={rgb 0.95 0.95 0.7} font=" + font;

            p.create_field(llx, lly-=50, urx, ury-=50, "enter_field", "textfield",
                optlist);
            p.fit_textline("enter", tx, lly + ty_off,
                "fillcolor={rgb 0.47 0.47 0.34} fontsize=10");


            /* ---------------------------------------------------------------------
             * フィールドエリアのからマウスが出た時をトリガとする JavaScript アクショ
             * ンでは "optlist = "action {exit={" + action ..." を使用する。
             * ---------------------------------------------------------------------
             */

            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);
        }
    }
}
(July 27, 2016 - May 23, 2019)