PDFlib

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

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

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

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

改行位置の指定

PDFlib でテキストフローを作成し、テキストの改行位置を指定して PDF を出力します。

対象製品 : PDFlib または PDFlib+PDI または PPS


/*
 * 改行位置の指定 :
 * テキストフロー内で、改行位置を指定する
 *
 * 対象製品 : PDFlib/PDFlib+PDI/PPS 10
 * 必要なデータ : 無し
 */
package com.pdflib.cookbook.pdflib.textflow;

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

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

        pdflib p = null;
        int exitcode = 0;
        int tf = -1, tf_avoid = -1;
        String result, optlist, text, text_avoid;

        try {
            p = new pdflib();

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

            /* load_font() の戻り値を確認する */
            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);

            /* A4 横でページを作成する */
            p.begin_page_ext(0, 0, "width=a4.height height=a4.width");

            /* ---------------------------------------------------------------------
             * オプションを指定せずテキストを出力する(改行なし)
             * ---------------------------------------------------------------------
             */
            text = "For more information about the Giant Wing Paper Plane see " +
                "our Web site <underline=true>www.kraxi-systems.com" +
                "<underline=false>.<nextline>Alternatively, contact us by email " +
                "via <underline=true>questions@kraxi-systems.com" +
                "<underline=false>. You'll get all information about how to fly " +
                "the Giant Wing in a thunderstorm as soon as possible.";
         
            p.fit_textline("Text without any options to avoid line breaking", 
                50, 430, "fontname=NotoSerif-Bold fontsize=12");

            /* テキストからテキストフローを作成する */
            optlist = "fontname=NotoSerif-Regular fontsize=20 leading=140%";
            tf = p.create_textflow(text, optlist);
            if (tf == -1)
                throw new Exception("Error: " + p.get_errmsg());
            
            result = p.fit_textflow(tf, 50, 100, 300, 400,
                "fitmethod=auto showborder");
            if (!result.equals("_stop"))
            {
                /* エラーをチェックする */
            }
            p.delete_textflow(tf);
            
            
            /* --------------------------------------------------------------
             * オプションを追加して、テキストを出力する(改行あり)
             * --------------------------------------------------------------
             */
            
            /* テキスト行で、ウェブアドレス名などの途中で、改行を避けるために、
             * "<avoidbreak>...<noavoidbreak>"を使用する。
             * eメールアドレス等で、特定の文字"-" や "."で改行したくない場合は、
             * "<charclass={letter {- .}}>...<charclass=default>"を指定する 
             */ 
            text_avoid = "For more information about the Giant Wing Paper Plane " +
                "see our Web site <underline=true avoidbreak>" +
                "www.kraxi-systems.com<underline=false noavoidbreak>.<nextline>" +
                "Alternatively, contact us by email via <underline=true " +
                "charclass={letter {- .}}>questions@kraxi-systems.com" +
                "<charclass={default {- .}} underline=false>. You'll get all " +
                "information about how to fly the Giant Wing in a thunderstorm " +
                "as soon as possible.";
            
            p.fit_textline("Text with \"charclass\" and \"avoidbreak\" options",
                450, 430, "fontname=NotoSerif-Bold fontsize=12");
            
            /* テキストからテキストフローを作成する */
            tf_avoid = p.create_textflow(text_avoid, optlist);
            if (tf_avoid == -1)
                throw new Exception("Error: " + p.get_errmsg());

            result = p.fit_textflow(tf_avoid, 450, 100, 700, 400,
                "fitmethod=auto showborder");
            if (!result.equals("_stop"))
            {
                /* エラーをチェックする */
            }
            p.delete_textflow(tf_avoid);

            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 22, 2024)