/* * 背景色上のテキスト : * 背景に色をもつテキスト行とテキストフローを配置する * * 背景に色を持つテキスト行を配置します。info_textline()を使用し、テキスト行の * サイズを取得します。 * テキスト行を配置するのに、"matchbox" オプションを使えば、より多彩な方法でテキ * ストを配置できます。背景色で塗られた上にテキストフローを表示することもできます。 * * 必要な製品 : PDFlib/PDFlib+PDI/PPS 9 * 必要なデータ : 無し */ package com.pdflib.cookbook.pdflib.textflow; import com.pdflib.pdflib; import com.pdflib.PDFlibException; public class text_on_color { public static void main(String argv[]) { /* 必要に応じてデータファイルがあるフォルダのパスを指定する */ String searchpath = "../input"; String outfile = "text_on_color.pdf"; String title = "Text on colored Background"; pdflib p = null; int font, tf = -1, x = 50, y; String result, optlist; double width, height; int exitcode = 0; String textline = "Giant Wing Paper Plane"; String textflow = "To fold the famous rocket looper proceed as follows:\nTake a 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); /* 出力文書を開く */ p.begin_page_ext(0, 0, "width=a4.width height=a4.height"); /* フォントをロードする */ font = p.load_font("Helvetica", "unicode", ""); if (font == -1) throw new Exception("Error: " + p.get_errmsg()); /* --------- テキストラインを使用した出力方法 I -------- */ /* * info_textline()よりテキストのデフォルトの幅と高さを取得し、背景色の上に * テキストを配置する。 * フォントのデフォルトの高さは、キャップハイトを用いる。 * 他のフォントプロパティを使用する場合は、 "matchbox"オプションを使用する。 */ optlist = "font=" + font + " fontsize=40 " + "fillcolor={gray 1}"; width = p.info_textline(textline, "width", optlist); height = p.info_textline(textline, "height", optlist); /* 取得した幅と高さで、矩形を描き、塗りつぶす */ p.setcolor("fill", "rgb", 0.0, 0.8, 0.8, 0); p.rect(x, 700, width, height); p.fill(); /* 矩形の上にテキストを配置する */ p.fit_textline(textline, x, 700, optlist); /* --------- テキストラインを使用した出力方法 II -------- */ /* * fit_textline で塗りつぶした矩形の背景にテキストを配置する。 * "matchbox" オプションは、背景の矩形を作成するために使用する。 * マッチボックスの高さは"boxheight"で指定する。 * また、"offsetbottom" "offsettop"は上部と下部からの余白を追加し、 * "offsetleft"と"offsetright"では、テキストの右端、左端からの余白を * 定義する。 */ optlist = "font=" + font + " fontsize=40 fillcolor={gray 1} " + "matchbox={fillcolor={rgb 0 0.8 0.8} " + "boxheight={ascender descender}}"; /* テキストを配置する */ p.fit_textline(textline, x, 600, optlist); /* * マッチボックスの上部と下部に余白を追加するには "offsetbottom" * "offsettop"を指定する。 * 左右に余白を追加するには "offsetleft" と "offsetright" をそれぞれ * 指定する */ optlist = "font=" + font + " fontsize=40 fillcolor={gray 1} " + "matchbox={fillcolor={rgb 0 0.8 0.8} " + "boxheight={ascender descender} " + "offsetleft=-8 offsetright=8 offsettop=8 offsetbottom=-8}"; /* テキストラインを配置する */ p.fit_textline(textline, x, 500, optlist); /* --------- テキストフローで、塗りつぶした背景色の上にテキストを出力 -------- */ /* * テキストフローで緑の矩形の背景を作成する。 * 背景はマッチボックスで作成する。 */ optlist = "font=" + font + " fontsize=20 fillcolor={gray 1}"; tf = p.add_textflow(tf, textflow, optlist); if (tf == -1) throw new Exception("Error: " + p.get_errmsg()); /* 矩形の高さ */ y = 430; /* 矩形の幅 */ width = 500; /* * テキストフローを使用し、マッチボックスの背景を塗りつぶす。 */ result = p.fit_textflow(tf, x, 0, x + width, y, "matchbox={fillcolor={rgb 0.0 0.8 0.8}}"); if (!result.equals("_stop")) { /* エラーかどうか、または、まだ配置すべきテキストが残って * いるかをチェック */ } /* テキスト全体の外接枠の高さ */ height = p.info_textflow(tf, "textheight"); /* * 次に表示する矩形は、テキストの高さも含めて、垂直に下方向に移動 * するため、y座標を以下の通り設定する */ y -= height + 50; /* * 再度、fit_textflowを呼び出し、margin を指定し、マッチボックスの * 上下左右の余白を追加する。"rewind=1" で、fit_textflow( ) を最初 * に呼び出した時の前の状態へ戻す。 */ result = p.fit_textflow(tf, x, 0, x + width, y, "rewind=1 matchbox={margin=-10 fillcolor={rgb 0.0 0.8 0.8}}"); if (!result.equals("_stop")) { /* エラーかどうか、または、まだ配置すべきテキストが残っているか * をチェック */ } 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); } } }