PDFlib

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

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

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

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

縦方向の表示位置と均等割付

はめ込み枠で、行の縦方向の表示位置と均等割付を設定するプログラムです。

fit_textflow 関数の verticalalign オプションを使用し、はめ込み枠の縦方向に、上詰め、下詰め、中央揃え、均等割付を設定します。

また、均等割付の際、firstlinedistオプションでは、テキスト先頭行からはめ込み枠の上端までの間隔を、lastlinedist オプションでは、テキスト最終行からはめ込み枠の下端までの間隔を、指定することができます。

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


/*
 * 縦方向の表示位置と均等割付:
 * はめ込み枠で行の縦方向の表示位置と均等割付を設定します。
 * 
 * fit_textflow 関数の verticalalign オプションを使用して、はめ込み枠の
 * 縦方向の表示位置や均等割付を設定します。
 * 
 * 必要な製品 : PDFlib/PDFlib+PDI/PPS 10
 * 必要なデータ : 無し
 */
package com.pdflib.cookbook.pdflib.textflow;

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

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

        int exitcode = 0;
        pdflib p = null;
        int tf = -1, font;
        String result, cr_optlist, fit_optlist;
        final double ystart = 630;
        double x = 20, y = ystart, yoffset = 200;
        double boxheight = 190, boxwidth = 250;
        double xtext = 300, ytext = y + boxheight/2;

        try {
            p = new pdflib();

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

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

            /* 出力PDF文書を開く。第1パラメータには出力ファイル名を指定 */
            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", "unicode", "");

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

            /* 出力 PDF ページをA4サイズで作成する(1ページ目) */
            p.begin_page_ext(0, 0, "width=a4.width height=a4.height");
            
            /* フォントと、フォントサイズを指定する */
            p.setfont(font, 10);

            /* 出力するテキストを指定する */
            final String text =
                "Our paper planes are the ideal way of passing the time. We " +
                "offer revolutionary new developments of the traditional common " +
                "paper planes. If your lesson, conference, or lecture turn out " +
                "to be deadly boring, you can have a wonderful time with our " +
                "planes. All our models are folded from one paper sheet.";
            
            /* Case1~7のcreate_textflow()に指定する共通のオプションリスト 
             * "leading=120%" 行送り値をフォントサイズの120%で設定
             * "alignment=justify" 段落内の文字列を両端揃え
             */
            cr_optlist = "fontname=NotoSerif-Regular fontsize=14 " +
                "leading=120% alignment=justify";
                 
            /* ----------------------------------------------
             * Case 1: テキストをはめ込み枠に上詰めで表示する
             * ----------------------------------------------
             */
            /* 説明用のテキストを出力 */
            p.fit_textline("Case 1: Fit Textflow with default settings", 
                xtext, ytext, "");
            
            /* テキストフローオブジェクトを作成する */
            tf = p.create_textflow(text, cr_optlist);
            if (tf == -1)
                throw new Exception("Error: " + p.get_errmsg());
            
            /* オプションリストを設定する
             * "showborder" はめ込み枠の境界線を表示 
             * "verticalalign=top" はめ込み枠に上詰めで表示(デフォルト値のため省略)
             * "firstlinedist=leading" はめ込み枠上端から、テキスト先頭行のベースラ
             *  インまでの行送り値を、文字サイズにより決定し適用(デフォルト値のため
             *  省略) 
             */
            fit_optlist = "showborder";
            
            result = p.fit_textflow(tf, x, y, (x + boxwidth), (y + boxheight), 
                fit_optlist);
            
            if (!result.equals("_stop"))
            {
                /* エラーかどうかをチェックする */
            }
            p.delete_textflow(tf);
            
            
            /* -------------------------------------------------------------------
             * Case 2: テキストをはめ込み枠に上詰めで表示する。テキスト先頭行は、
             *         はめ込み枠の上端から指定した間隔で表示する。
             * -------------------------------------------------------------------
             */
            y -= yoffset;
            ytext = y + boxheight/2;
            
            /* 説明用のテキストを出力 */
            p.fit_textline("Case 2: firstlinedist=capheight", 
                xtext, ytext, "");
            
            /* テキストフローオブジェクトを作成する */
            tf = p.create_textflow(text, cr_optlist);
            if (tf == -1)
                throw new Exception("Error: " + p.get_errmsg());
            
            /* オプションリストを設定する
             * "showborder" はめ込み枠の境界線を表示
             * "verticalalign=top" はめ込み枠に上詰めで表示(デフォルト値のため省略)
             * "firstlinedist=capheight" はめ込み枠上端と、テキスト先頭行の大文字
             *  最上部のラインが接するように表示
             */
            fit_optlist = "showborder firstlinedist=capheight";
            
            result = p.fit_textflow(tf, x, y, (x + boxwidth), (y + boxheight), 
                fit_optlist);
            
            if (!result.equals("_stop"))
            {
                /* エラーかどうかをチェックする */
            }
            p.delete_textflow(tf);

            
            /* --------------------------------------------------
             * Case 3: テキストをはめ込み枠に下詰めで表示する
             * --------------------------------------------------
             */
            y -= yoffset;
            ytext = y + boxheight/2;
            
            /* 説明用のテキストを出力 */
            p.fit_textline("Case 3: verticalalign=bottom", 
                xtext, ytext, "");
            
            /* テキストフローオブジェクトを作成する */
            tf = p.create_textflow(text, cr_optlist);
            if (tf == -1)
                throw new Exception("Error: " + p.get_errmsg());
            
            /* オプションリストを設定する
             * "showborder" はめ込み枠の境界線を表示
             * "verticalalign=bottom" はめ込み枠に下詰めで表示
             */
            fit_optlist = "showborder verticalalign=bottom";
            
            result = p.fit_textflow(tf, x, y, (x + boxwidth), (y + boxheight), 
                fit_optlist);
            
            if (!result.equals("_stop"))
            {
                /* エラーかどうかをチェックする */
            }
            p.delete_textflow(tf);
            
            
            /* -----------------------------------------------------------------
             * Case 4: テキストをはめ込み枠に下詰めで表示する。テキスト最終行は、
             *         はめ込み枠の下端から指定した間隔で表示する。
             * -----------------------------------------------------------------
             */
            y -= yoffset;
            ytext = y + boxheight/2;
            
            /* 説明用のテキストを出力 */
            p.fit_textline("Case 4: verticalalign=bottom lastlinedist=descender", 
                xtext, ytext, "");
            
            /* テキストフローオブジェクトを作成する */
            tf = p.create_textflow(text, cr_optlist);
            if (tf == -1)
                throw new Exception("Error: " + p.get_errmsg());
            
            /* オプションリストを設定する
             * "showborder" はめ込み枠の境界線を表示
             * "verticalalign=bottom" はめ込み枠内に下詰めで表示
             * "lastlinedist=descender" テキスト最終行のフォントのディセンダの最下
             *  部のラインが、はめ込み枠の下端に接するように表示
             */
            fit_optlist = "showborder verticalalign=bottom lastlinedist=descender";
            
            result = p.fit_textflow(tf, x, y, (x + boxwidth), (y + boxheight), 
                fit_optlist);
            
            if (!result.equals("_stop"))
            {
                /* エラーかどうかをチェックする */
            }
            p.delete_textflow(tf);
            
            p.end_page_ext("");
            
            
            /* 出力PDFページをA4サイズで作成する(2ページ目) */
            p.begin_page_ext(0, 0, "width=a4.width height=a4.height");
            
            /* フォントと、フォントサイズを指定する */
            p.setfont(font, 10);
            
            y = ystart;
            yoffset = 230;
            ytext = y + boxheight/2;
            
            
            /* ------------------------------------------------
             * Case 5: テキストをはめ込み枠の縦中央に表示する 
             * ------------------------------------------------
             */
            
            /* 説明用のテキストを出力 */
            p.fit_textline("Case 5: ", 
                xtext, ytext, "");
            p.fit_textline("verticalalign=center firstlinedist=capheight " +
                "lastlinedist=descender", xtext, ytext-15, "");
            
            /* テキストフローオブジェクトを作成する */
            tf = p.create_textflow(text, cr_optlist);
            if (tf == -1)
                throw new Exception("Error: " + p.get_errmsg());
            
            /* オプションリストを設定する
             * "showborder" はめ込み枠の境界線を表示
             * "verticalalign=center" はめ込み枠内の縦中央に表示
             * "firstlinedist=capheight" はめ込み枠上端と、テキスト先頭行の大
             *  文字最上部のラインが接するように表示
             * "lastlinedist=descender" テキスト最終行のフォントのディセンダの
             *  最下部のラインが、はめ込み枠の下端に接するように表示
             */
            fit_optlist = "showborder verticalalign=center " +
                "firstlinedist=capheight lastlinedist=descender";
            
            result = p.fit_textflow(tf, x, y, (x + boxwidth), (y + boxheight), 
                fit_optlist);
            
            if (!result.equals("_stop"))
            {
                /* エラーかどうかをチェックする */
            }
            p.delete_textflow(tf);
            
            
            /* --------------------------------------------------------------
             * Case 6: テキストをはめ込み枠の縦方向に均等割付する
             * --------------------------------------------------------------
             */
            y -= yoffset;
            ytext = y + boxheight/2;
            
            /* 説明用のテキストを出力 */
            p.fit_textline("Case 6: verticalalign=justify linespreadlimit=200%", 
                xtext, ytext, "");
            
            /* テキストフローオブジェクトを作成する */
            tf = p.create_textflow(text, cr_optlist);
            if (tf == -1)
                throw new Exception("Error: " + p.get_errmsg());
            
            /* オプションリストを設定する
             * "showborder" はめ込み枠の境界線を表示
             * "verticalalign=justify" はめ込み枠内の縦方向にテキストを均等割付
             * "linespreadlimit=200% 縦方向の均等割付けの際、行送りの最大値をフォ
             *  ントサイズの200%で指定 
             */
            fit_optlist = "showborder verticalalign=justify linespreadlimit=200%";
            
            result = p.fit_textflow(tf, x, y, (x + boxwidth), (y + boxheight), 
                fit_optlist);
            
            if (!result.equals("_stop"))
            {
                /* エラーかどうかをチェックする */
            }
            p.delete_textflow(tf);
            
            
            /* ----------------------------------------------------------------------
             * Case 7: テキストをはめ込み枠の縦方向に均等割付し、はめ込み枠の上端から
             *         テキスト先頭行、はめ込み枠の下端からテキスト最終行を、それぞれ
             *         指定した間隔で表示する
             * ----------------------------------------------------------------------
             */
            y -= yoffset;
            ytext = y + boxheight/2;
            
            /* 説明用のテキストを出力 */
            p.fit_textline("Case 7:",
                    xtext, ytext+15, "");
            p.fit_textline("verticalalign=justify linespreadlimit=200%",
                xtext, ytext, "");
            p.fit_textline("firstlinedist=capheight lastlinedist=descender", 
                xtext, ytext-15, "");
            
            /* テキストフローオブジェクトを作成する */
            tf = p.create_textflow(text, cr_optlist);
            if (tf == -1)
                throw new Exception("Error: " + p.get_errmsg());
            
            /* オプションリストを設定する
             * "showborder " はめ込み枠の境界線を表示
             * "verticalalign=justify " はめ込み枠内の縦方向にテキストを均等割付
             * "linespreadlimit=200% " 縦方向の均等割付けの際、行送りの最大値をフォ
             *  ントサイズの200%で指定
             * "firstlinedist=capheight " はめ込み枠上端と、テキスト先頭行の大文字
             *  最上部のラインが接するように表示
             * "lastlinedist=descender " テキスト最終行のフォントのディセンダの最下
             *  部が、はめ込み枠の下端に接するように表示
             */
            fit_optlist = "showborder verticalalign=justify linespreadlimit=200% " +
                "firstlinedist=capheight lastlinedist=descender";
            
            result = p.fit_textflow(tf, x, y, (x + boxwidth), (y + boxheight), 
                fit_optlist);
            
            if (!result.equals("_stop"))
            {
                /* エラーかどうかをチェックする */
            }
            p.delete_textflow(tf);
            
            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 3, 2007 - Feb 26, 2024)