PDFlib

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

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

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

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

様々な角度のテキスト

PDFlib で、水平ではなく様々な角度のテキストを出力するサンプルプログラムです。

  • 左、右、下向きに並ぶ3つのテキストラインを生成します。
  • 30度の傾きを持つテキストライン、テキストフローを生成します。
  • 30度の傾きを持ち、左向きの配置されるテキストフローを生成します。

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


/*
 * 様々な角度のテキスト :
 * 水平ではなく様々な角度のテキストを出力するサンプルプログラム。
 *
 * テキストフローは、30度の角度を持つように作成されます。テキストフローは、
 * 30度の角度や左向きの配置を持つよう生成されます。
 *
 * 必要な製品 : PDFlib/PDFlib+PDI/PPS 9
 * 必要なデータ : 無し
 */
package com.pdflib.cookbook.pdflib.textflow;

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

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

        pdflib p = null;
        int font;
        String result;
        int tf = -1;
        int exitcode = 0;
        String optlist =
            "fontname=Helvetica fontsize=14 encoding=unicode leading=120% charref";

        String textflow =
            "To fold the famous rocket looper proceed as follows:\n" +
            "Take a DIN A4 sheet. Fold it lenghtwise in the middle. Then, fold " +
            "the upper corners down. Fold the long sides inwards that the points " +
            "A and B meet on the central fold. Fold the points C and D that the " +
            "upper corners meet with the central fold as well. Fold the plane in " +
            "the middle. Fold the wings down that they close with the lower " +
            "border of the plane.";

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

            /* 1ページ目 */
            p.begin_page_ext(0, 0, "width=a4.width height=a4.height");

            font = p.load_font("Helvetica-Bold", "unicode", "");

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

            /* テキストを西向き、東向き、南向きで配置する */
            p.setfont(font, 24);
            p.fit_textline("The famous rocket looper", 100, 500, "orientate=west");

            p.setfont(font, 20);
            p.fit_textline("The famous rocket looper", 200, 500, "orientate=east");

            p.setfont(font, 16);
            p.fit_textline("The famous rocket looper", 300, 500, "orientate=south");

            /* テキストを30度回転させる */
            p.setfont(font, 16);
            p.fit_textline("The famous rocket looper", 50, 350, "rotate=30");

            /* テキストフローを使用し、テキストを30度回転させる */
            tf = p.add_textflow(tf, textflow, optlist);
            if (tf == -1)
                throw new Exception("Error: " + p.get_errmsg());

            result = p.fit_textflow(tf, 250, 50, 400, 400, "rotate=30");
            if (!result.equals("_stop"))
            {
                /* エラーかどうか、または、まだ配置すべきテキストが残っているかをチェック */
            }
            p.delete_textflow(tf);

            p.end_page_ext("");

            /* 2ページ目 */
            p.begin_page_ext(0, 0, "width=a4.width height=a4.height");

            font = p.load_font("Helvetica-Bold", "unicode", "");

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

            /* ストロークカラーを赤に設定する */
            p.setcolor("stroke", "rgb", 1, 0, 0, 0);

            /* テキストフローを使用し、テキストを30度回転させる。テキストフローのオプションに
             * "showborder" を指定し、はめ込み枠の境界線を表示する 
             */
            tf = -1;
            tf = p.add_textflow(tf, textflow, optlist);
            if (tf == -1)
                throw new Exception("Error: " + p.get_errmsg());

            result = p.fit_textflow(tf, 150, 450, 400, 650, "showborder rotate=30");
            if (!result.equals("_stop"))
            {
                /* エラーかどうか、または、まだ配置すべきテキストが残っているかをチェック */
            }
            p.delete_textflow(tf);

            p.fit_textline("rotate=30", 300, 500, "font=" + font + " fontsize=14");

            /* テキストフローで30度回転させ、テキストを西向きにする */
            tf = -1;
            tf = p.add_textflow(tf, textflow, optlist);
            if (tf == -1)
                throw new Exception("Error: " + p.get_errmsg());

            result = p.fit_textflow(tf, 150, 100, 400, 300,
                "showborder rotate=30 orientate west");
            if (!result.equals("_stop"))
            {
                /* エラーかどうか、または、まだ配置すべきテキストが残っているかをチェック */
            }
            p.delete_textflow(tf);

            p.fit_textline("rotate=30  orientate=west", 300, 150, "font=" + font +
                " fontsize=14");

            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 - May 23, 2019)