PDFlib

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

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

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

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

テキストフィールドの入力フォーマット

PDFlib で、テキストフィールドに入力されたデータを、指定した形式でフォーマットするサンプルプログラムです。

  • 「mmm dd yyyy」 の形式でテキストフィールドのフォーマットを指定し、日付を表示します。
  • 「x,xxx.xx」 の形式でテキストフィールドのフォーマットを指定し、価格を表示します。

テキストフィールドをフォーマットチェックするサンプルについては、 テキストフィールドの入力チェック をご覧ください。

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


/*
 * テキストフィールドの入力フォーマット :
 * 
 * 定義された規則に従い、フィールド種別"textfield"のデータのフォーマット
 * を指定します。
 *
 * 日付を表示するため、テキストフィールドを生成し、"mmm dd yyyy" の形式で
 * フォーマットを指定します。
 * 価格を表示するため、テキストフィールドを生成し、"x,xxx.xx" の形式で
 * フォーマットを指定します。また後ろに" Euro" を付け加えます。
 *
 * 必要な製品 : PDFlib/PDFlib+PDI/PPS 9
 * 必要なデータ : 無し
 */
package com.pdflib.cookbook.pdflib.form_fields;

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

public class form_textfield_input_format
{
    public static void main (String argv[])
    {
        /* 必要に応じてデータファイルがあるフォルダのパスを指定する */
        String searchpath = "../input";
        String outfile = "form_textfield_input_format.pdf";
        String title = "Form Text Field Input Format";
        
        pdflib p = null;
        int exitcode = 0;

        String optlist;
        int font, monospaced_font, format_action;
        double fontsize, ascender, descender;
        double width=160, height=30, llx = 10, lly = 700;

        try {
            p = new pdflib();

            /* load_font() 等でエラーが起きた場合、-1を返す */
            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("Helvetica", "winansi", "");
            if (font == -1)
                throw new Exception("Error: " + p.get_errmsg());

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

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

            /* --------------------------------------------------------
             * 日付を表示するためのテキストフィールドをフォーマットする
             * --------------------------------------------------------
             *
             * フィールドの高さに合うようにフォントサイズを調整する。そして、フィールド
             * の境界線から空白を設定するためフォントサイズを小さくする(例:20%)
             */
            ascender = p.info_font(monospaced_font, "ascender", "fontsize=" +
                height);
            descender = p.info_font(monospaced_font, "descender", "fontsize=" +
                height);
            fontsize = (ascender - descender) * 0.8;

            /* "mmm dd yyyy"の形式でフォーマットするアクションを定義し、正しいフォーマット
             * での日付が入力されるようにする
             */
            format_action = p.create_action("JavaScript",
                "script={AFDate_FormatEx('mmm dd yyyy'); }");

            /* フィールド種別"textfield"の "date" を生成し、"action" オプションでJavaScript
             * のアクションを指定する
             */
            optlist = "backgroundcolor={rgb 0.95 0.95 1} bordercolor={gray 0} " +
                "currentvalue={Oct 12 2007} maxchar=11 comb=true " +
                "scrollable=false tooltip={Enter a date} font=" + monospaced_font +
                " fontsize=" + fontsize + " action={format=" + format_action + "}";

            p.create_field(llx, lly, llx + width, lly + height, "date", "textfield",
                optlist);
            lly-=40;
            
            p.setfont(font, 12);
            p.fit_textline("Change the date.", llx, lly, "");
            p.fit_textline("After pressing \"Tab\" or \"Enter\", the date is " +
                "formatted as \"mmm dd yyy\".", llx, lly-=20, "");
            lly-=100;


            /* --------------------------------------------------------
             * 価格を表示するためのテキストフィールドをフォーマットする
             * --------------------------------------------------------
             *
             * フォントサイズをフィールドの高さに合うように調整する。そして、フィールドの
             * 境界線から空白を設定するためフォントサイズを小さくする(例:20%)
             */
            ascender = p.info_font(monospaced_font, "ascender", "fontsize=" +
                height);
            descender = p.info_font(monospaced_font, "descender", "fontsize=" +
                height);
            fontsize = (ascender - descender) * 0.8;

            /* "x,xxx.xx" の形式(例:"1,200.50")でフォーマットするアクションを定義し、
             * 正しいフォーマットで数字が入力されるようにする。また、" Euro"付け加える。
             */
            format_action = p.create_action("JavaScript",
                "script={AFNumber_Format(2, 0, 0, 0, \" Euro\", false); }");

             /* フィールド種別"textfield"の "price" を生成し、"action" オプションでJavaScript
             * のアクションを指定する
             */
            optlist = "backgroundcolor={rgb 0.95 0.95 1} bordercolor={gray 0} " +
                "maxchar=10 scrollable=false tooltip={Enter the price} " +
                "font=" + font + " fontsize=" + fontsize +
                " action={format=" + format_action + "}";

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

            p.setfont(font, 12);
            p.fit_textline("Enter a price, e.g. 150.95. A maximum of 10 digits " +
                "is allowed to be entered.", llx, lly, "");
            p.fit_textline("After pressing \"Tab\" or \"Enter\", the price is " +
                "formatted as \"x,xxx.xx\" with the text \" Euro\" appended.", 
                llx, lly-=20, "");

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