PDFlib

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

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

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

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

影付きテキスト

PDFlib で、影付きのテキストを出力するサンプルプログラムです。

fit_textline の shadow オプションにより影付きのテキストを出力します。また、offsetサブオプションにより位置を、fillcolor により色を、gstate により影を設定するグラフィック拡張を指定します。

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

必要なデータ : 画像ファイル


/*
 * 影付きテキスト :
 * 影付きテキストの作成
 * 
 * PDFlib で、影付きのテキストラインを出力するサンプルプログラムです。
 * fit_textline の shadow オプションにより影付きのテキストラインを出力します。
 * また、オフセットサブオプションにより位置を、fillcolor により色を、gstate 
 * により影を設定するグラフィック拡張を指定します。
 *
 * 必要な製品 : PDFlib/PDFlib+PDI/PPS 9
 * 必要なデータ : 画像ファイル
 */
package com.pdflib.cookbook.pdflib.text_output;

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

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

        pdflib p = null;
        int exitcode = 0;

        final String imagefile = "cambodia_bayon3.jpg";
        final String text = "the faces of Bayon";

        final double x1 = 20, x2 = 360, yoff = 60;
        double y = 550;

        String base_shadow_opts, shadow_opts, smallfont_opts, bigfont_opts, color_opts;
        int image, gstate;
        int normalfont, boldfont;
        double bigfontsize = 35, smallfontsize = 13;

        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);

            normalfont = p.load_font("NotoSerif-Regular", "unicode", "");
            if (normalfont == -1)
                throw new Exception("Error: " + p.get_errmsg());

            boldfont = p.load_font("NotoSerif-Bold", "unicode", "");
            if (boldfont == -1)
                throw new Exception("Error: " + p.get_errmsg());

            /* 画像をロードする */
            image = p.load_image("auto", imagefile, "");
            if (image == -1)
                throw new Exception("Error: " + p.get_errmsg());

            /* 出力PDFページを作成する */
            p.begin_page_ext(0, 0, "width=a4.height height=a4.width");

            /* 大小のフォントの一般的なオプションを設定する */
            smallfont_opts = "font=" + normalfont + " fontsize="
                + smallfontsize + " ";
            bigfont_opts = "font=" + boldfont + " fontsize=" + bigfontsize
                + " ";

            /* 見出しの出力 */
            p.fit_textline("Output of fit_textline()", x1, y, smallfont_opts);
            p.fit_textline("Options of fit_textline()", x2, y, smallfont_opts);

            /*
             * ------------------------------------------------------
             * デフォルトの shadow オプションによるテキストの出力
             * ------------------------------------------------------
             */
            shadow_opts = "shadow={} ";
            p.fit_textline(text, x1, y -= yoff, bigfont_opts + shadow_opts);

            /* shadow オプションの説明用テキスト */
            p.fit_textline(shadow_opts, x2, y + bigfontsize / 4,
                    smallfont_opts);

            /*
             * -------------------------------------------------------------
             * 色々な shadow オプション (デフォルト値は {5% -5%})による
             * テキストの出力
             * -------------------------------------------------------------
             */
            shadow_opts = "shadow={offset={10% 8%}} ";
            p.fit_textline(text, x1, y -= yoff, bigfont_opts + shadow_opts);

            /* shadow オプションの説明用テキスト */
            p.fit_textline(shadow_opts, x2, y + bigfontsize / 4,
                    smallfont_opts);

            /* デフォルトの shadow オプション値による、テキストの出力 */
            shadow_opts = "shadow={offset={-10% 8%}} ";
            p.fit_textline(text, x1, y -= yoff, bigfont_opts + shadow_opts);

            /* shadow オプションの説明用テキスト */
            p.fit_textline(shadow_opts, x2, y + bigfontsize / 4,
                    smallfont_opts);

            /* デフォルトの shadow オプション値による、テキストの出力 */
            shadow_opts = "shadow={offset={-10% -8%}} ";
            p.fit_textline(text, x1, y -= yoff, bigfont_opts + shadow_opts);

            /* shadow オプションの説明用テキスト */
            p.fit_textline(shadow_opts, x2, y + bigfontsize / 4,
                    smallfont_opts);

            /*
             * -------------------------------------------
             * 色々な色での影付きテキストの出力
             * -------------------------------------------
             */
            shadow_opts = "shadow={fillcolor={rgb 0.28 0.81 0.8} offset={10% -10%}} ";

            color_opts = "fillcolor={rgb 0 0.5 0.52}";
            p.fit_textline(text, x1, y -= yoff, bigfont_opts + shadow_opts
                + color_opts);

            /* shadow オプションにより出力 */
            p.fit_textline(shadow_opts, x2, y + bigfontsize / 4,
                    smallfont_opts);

            /*
             * カレントグラフィックステータスを保存する。
             * save/restore は必ずしも必要ではないが、後で保存しておいた
             * グラフィックステータスに復帰する場合、役に立つ
             */
            p.save();

            /* 背景画像を出力する */
            p.fit_image(image, x1, 50, "boxsize={170 170} fitmethod=meet");

            /*
             * create_gstate()のオプション値"opacityfill"に0.5(透明度50%)を
             * 指定する
             */
            gstate = p.create_gstate("opacityfill=.5");

            /* 上記で指定した透明度で、白の影付きテキストを表示する */
            
            base_shadow_opts = "shadow={fillcolor={rgb 0.87 0.72 0.52} "
                + "offset={8% -8%}} gstate=";
            
            shadow_opts = base_shadow_opts + gstate;

            color_opts = " fillcolor={rgb 1 1 1}";

            p.fit_textline("the", x1, 185, bigfont_opts + shadow_opts
                + color_opts);
            p.fit_textline("faces", x1, 145, bigfont_opts + shadow_opts
                + color_opts);
            p.fit_textline("of", x1, 105, bigfont_opts + shadow_opts
                + color_opts);
            p.fit_textline("Bayon", x1, 65, bigfont_opts + shadow_opts
                + color_opts);

            /* 保存したグラフィックステータスに復帰する */
            p.restore();

            /* shadow オプションにより出力 */
            p.fit_textline(base_shadow_opts + "", x2, 145, smallfont_opts);
            p.fit_textline("(gstate is created with \"opacityfill=.5\")", x2,
                115, smallfont_opts);

            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.toString());
            exitcode = 1;
        }
        finally {
            if (p != null) {
                p.delete();
            }
            System.exit(exitcode);
        }
    }
}
(Apr 3, 2007 - Mar 22, 2019)