PDFlib で、アンダーライン付きテキストを作成するサンプルプログラムです。テキストラインに標準的な線と幅と位置を持つアンダーラインを個別に配置します。また、いくつかのアンダーライン付き単語を持つテキストフローを配置します。
/*
* アンダーラインテキスト :
* アンダーラインテキストの作成
*
* PDFlib で、アンダーライン付きテキストを作成するサンプルプログラムです。テキストラインに
* 標準的な線と幅と位置を持つアンダーラインを個別に配置します。いくつかのアンダーライン付
* き単語を持つテキストフローを配置します。
*
* 必要な製品 : PDFlib/PDFlib+PDI/PPS 9
* 必要なデータ : 無し
*/
package com.pdflib.cookbook.pdflib.textflow;
import com.pdflib.pdflib;
import com.pdflib.PDFlibException;
public class underlined_text
{
public static void main (String argv[])
{
final String searchpath = "../input";
pdflib p = null;
String outfile = "underlined_text.pdf";
String title = "Underlined Text";
int tf = -1, font;
final double llx= 50, lly=50, urx=500, ury=800;
String result;
int exitcode = 0;
final String optlist =
"fontname=NotoSerif-Regular fontsize=13 encoding=unicode " +
"fillcolor={gray 0} alignment=justify charref";
/* 段に流し込むダミーテキスト。ソフトハイフンは文字参照""で表す。
* (文字参照はcharrefオプションで有効となる)
*/
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. They are exclusively folded " +
"without using any adhesive. Several models are equipped with " +
"a folded landing gear enabling a safe landing" +
" on the intended location provided that you " +
"have aimed well. Other models are able to fly loops or cover long " +
"distances. Let them start from a vista point in the mountains " +
"and see where they touch the ground. ";
try {
p = new pdflib();
/* load_font() 等でエラーが起きた場合、-1を返す */
p.set_option("errorpolicy=return");
p.set_option("searchpath={" + searchpath + "}");
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());
p.begin_page_ext(0, 0, "width=a4.width height=a4.height");
p.setfont(font, 18);
/* 標準のアンダーラインの設定で、テキストラインを作成する */
p.fit_textline("Our paper planes are the ideal way of passing the time",
llx, ury-50, "underline");
/* 個々のアンダーラインの設定で、テキストラインを作成する */
p.fit_textline("Our paper planes are the ideal way of passing the time",
llx, ury-100, "underline underlinewidth=3 underlineposition=-40%");
/* 標準のアンダーラインの設定で、テキストフローを作成する */
tf = p.create_textflow(text, optlist);
if (tf == -1)
throw new Exception("Error: " + p.get_errmsg());
result = p.fit_textflow(tf, llx, lly-200, urx, ury-200, "");
if (!result.equals("_stop")) {
/* さらに処理を行う */
}
p.delete_textflow(tf);
/* 個々のアンダーラインの設定で、テキストフローを作成する */
tf = p.create_textflow(text, optlist + " leading=160% " +
"underlinewidth=2 underlineposition=-30%");
if (tf == -1)
throw new Exception("Error: " + p.get_errmsg());
result = p.fit_textflow(tf, llx, lly-400, urx, ury-400, "");
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);
}
}
}