PDFlib

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

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

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

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

JavaScript によるテキストフィールドへの入力

JavaScript を使用して PDF のテキストフォームフィールドに値を入力するプログラムです。

日付を表示するテキストフィールドを生成し、ページを開いたとき、JavaScript アクションを使用して、現在の日付をフォームフィールドに表示します。

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

 
/*
 * JavaScript によるテキストフィールドへの入力 :
 * JavaScript を使用してテキストフォームフィールドに値を入力します。
 *
 * 日付を表示するテキストフィールドを生成する。ページを開いたとき、JavaScript 
 * アクションを使用して、現在の日付をフォームフィールドに表示します。
 *
 * 必要な製品 : PDFlib/PDFlib+PDI/PPS 10
 * 必要なデータ : 無し
 */
package com.pdflib.cookbook.pdflib.form_fields;

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

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

        pdflib p = null;
        int exitcode = 0;

        String optlist;
        int font;
        double width=160, height=30, llx = 10, lly = 700;

        try {
            p = new pdflib();

            /* load_font() 等の戻り値を確認する */
            p.set_option("errorpolicy=return");

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

            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("NotoSerif-Regular", "winansi", 
                               "simplefont nosubsetting");
            if (font == -1)
                throw new Exception("Error: " + p.get_errmsg());

            /* --------------------------------------------------------------------
            * ページを始める時のアクションとして、現在の日付をフォームフィールド
            * "date"に入力するため、JavaScriptを定義する。
            * --------------------------------------------------------------------
            *
            * "script" オプションには、現在の日付を "mm dd yyyy"の書式(例:"Apr 15 2020")
            * でテキストフィールドに表示するための JavaScript の断片をアクションリストして
            * 定義する。
            */
            optlist = "script={var d = util.printd('mmm dd yyyy', new Date());\n" +
                "var date = this.getField('date');\ndate.value = d;}";

            int show_date = p.create_action("JavaScript", optlist);

            /* 次に、ページを生成する。"action"オプションに、ページを開くときの処理として、
             * 上記で指定した"show_date"アクションを指定する。
             */
            p.begin_page_ext(0, 0, "width=a4.width height=a4.height action={open " +
                show_date + "}");

            /* ------------------------------------
             * テキストフィールド "date" を生成する
             * ------------------------------------
             *
             * フォームフィールド種別 "textfield" の フィールド "date" にライトブルーの
             * 背景色、黒の境界線で指定する。
             * 入力できる最大文字数を11文字に設定する(maxchar=11)
             * テキストがフィールドを満たしたとき、それ以上は入力を受け付けないようにする 
             * (scrollable=false)
             * フィールドはキャラクタごとに等間隔に、サブフィールドに分割される(comb=true)
             */
            optlist = "backgroundcolor={lightblue} bordercolor={gray 0} " +
                "maxchar=11 scrollable=false font=" + font + " fontsize=18";

            p.create_field(llx, lly, llx + width, lly + height, "date", "textfield",
                optlist);
            lly-=40;

            p.fit_textline("When opening the page the current date is " +
                "automatically displayed in the field.", llx, lly, 
                "fontname=NotoSerif-Regular fontsize=12");

            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);
        }
    }
}
(Feb 22, 2016 - Feb 28, 2024)