PDFlib

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

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

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

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

テキストフィールドのレイアウト

PDFlib で、テキストフィールドのレイアウトを指定するサンプルプログラムです。

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

 
/*
 * テキストフィールドのレイアウト:
 * テキストフィールドのレイアウトを指定し、日付を表示します。
 *
 * 日付を保存するため、等間隔のサブフィールドでテキストフィールドを作成します。
 * 以下のフィールドプロパティを定義します。
 * フォント、フォントサイズ、境界線、塗り色、初期値、入力できる最大文字数、
 * スクロール動作、ツールチップ
 *
 * 必要な製品 : PDFlib/PDFlib+PDI/PPS 9
 * 必要なデータ : 無し
 */
package com.pdflib.cookbook.pdflib.form_fields;

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

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

        String optlist;
        int font, monospaced_font;
        double fontsize, ascender, descender;
        double width=140, 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;

            /* フォームフィールド種別"textfield"の"date" フィールドを生成し、背景色にライト
             * ブルー、境界線に黒を指定する。
             * 日付を初期表示する(currentvalue={Sep 10 2007}) 
             * 入力できる最大文字数を11文字に指定する(maxchar=11)
             * テキストがフィールドを満たしたとき、それ以上は入力を受け付けないようにする
             * (scrollable=false)
             * フィールドはキャラクタごとに等間隔に、サブフィールドに分割される(comb=true)
             * ツールチップは、ユーザーがフィールド上にカーソルを移動したとき表示する
             * (tooltip={Enter a date})
             */
            optlist = "backgroundcolor={rgb 0.95 0.95 1} bordercolor={gray 0} " +
            "currentvalue={Sep 10 2007} maxchar=11 scrollable=false comb=true " +
            "tooltip={Enter a date} font=" + monospaced_font +
            " fontsize=" + fontsize;

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

            p.setfont(font, 12);
            p.fit_textline("Form text field with an initial value set. The " +
                "characters are placed in equidistant subfields.", llx, lly, "");
            p.fit_textline("A maximum of 11 characters is allowed. The text is " +
                "not allowed to scroll out of the window.", llx, lly-=20, "");
            p.fit_textline("A tooltip is displayed when the mouse moves over the " +
                "field.", 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);
        }
    }
}
(Apr 15, 2016 - May 23, 2019)