import java.io.*;
import com.pdflib.pdflib;
import com.pdflib.PDFlibException;
public class starter_table
{
public static void main (String argv[])
{
/* データファイルの場所です。必要なように適合させます */
String searchpath = "../data";
String imagefile = "nesrin.jpg";
int i, row, col, font, image, tf=-1, tbl=-1;
final int rowmax=50, colmax=5;
pdflib p = null;
final double llx= 50, lly=50, urx=550, ury=800;
String headertext = "テーブルヘッダー(全ての列の中心)";
String result;
String optlist;
/* マルチラインなTextflowのセルのダミーテキスト */
final String tf_text =
"PDFlibは、Adobe PDF(Portable Document Format)形式ファイルをプログ
ラムで生成するためのプログラムライブラリーです。文書を作るイメージ
で手順に従って関数を呼んでいけばPDF文書を生成するプログラムを容易
に作成することができます。PDFlibはさまざまなOS上のさまざまな言語を
サポートしているので、きっと日頃お使いになっている開発環境を使って
PDFを作成することができるでしょう。";
try {
p = new pdflib();
p.set_parameter("SearchPath", searchpath);
/* load_font()等から返る値を調べなければいけないことを
* 表しています。
*/
p.set_parameter("errorpolicy", "return");
if (p.begin_document("starter_table.pdf", "") == -1)
throw new Exception("Error: " + p.get_errmsg());
p.set_info("Creator", "PDFlib starter sample");
p.set_info("Title", "starter_table");
/* -------------------- テーブルセルを追加 -------------------- */
/* ---------- 行1: テーブルヘッダー (全ての行を測る) */
row = 1; col = 1;
font = p.load_font("HeiseiMin-W3", "UniJIS-UCS2-H", "");
if (font == -1)
throw new Exception("Error: " + p.get_errmsg());
optlist = "fittextline={position=center font=" + font +
" fontsize=14} " + "colspan=" + colmax;
tbl = p.add_table_cell(tbl, col, row, headertext, optlist);
if (tbl == -1)
throw new Exception("Error adding cell: " + p.get_errmsg());
/* ---------- 行2: 様々な種類の内容 */
/* ----- 簡単なテキストセル */
row++; col=1;
optlist =
"fittextline={font=" + font + " fontsize=10 orientate=west}";
tbl = p.add_table_cell(tbl, col, row, "縦文字", optlist);
if (tbl == -1)
throw new Exception("Error adding cell: " + p.get_errmsg());
/* ----- 背景の配色 */
col++;
optlist =
"fittextline={font=" + font + " fontsize=10} " +
"matchbox={fillcolor={rgb 0.9 0.5 0}}";
tbl = p.add_table_cell(tbl, col, row, "同色", optlist);
if (tbl == -1)
throw new Exception("Error adding cell: " + p.get_errmsg());
/* ----- Textflowのマルチラインテキスト */
col++;
font = p.load_font("HeiseiMin-W3", "UniJIS-UCS2-H", "");
if (font == -1)
throw new Exception("Error: " + p.get_errmsg());
optlist =
"charref fontname=HeiseiMin-W3 encoding=UniJIS-UCS2-H" +
" fontsize=8 ";
tf = p.add_textflow(tf, tf_text, optlist);
if (tf == -1)
throw new Exception("Error: " + p.get_errmsg());
optlist = "margin=2 textflow=" + tf;
tbl = p.add_table_cell(tbl, col, row, "", optlist);
if (tbl == -1)
throw new Exception("Error adding cell: " + p.get_errmsg());
/* ----- 回転イメージ */
col++;
image = p.load_image("auto", imagefile, "");
if (image == -1)
throw new Exception ("Couldn't load image: " + p.get_errmsg());
optlist = "image=" + image + " fitimage={orientate=west}";
tbl = p.add_table_cell(tbl, col, row, "", optlist);
if (tbl == -1)
throw new Exception("Error adding cell: " + p.get_errmsg());
/* ----- 対角線の種類 */
col++;
optlist = "fittextline={font=" + font + " fontsize=10 stamp=ll2ur}";
tbl = p.add_table_cell(tbl, col, row, "空入力", optlist);
if (tbl == -1)
throw new Exception("Error adding cell: " + p.get_errmsg());
/* ---------- 3行目とそれらの数字より上で満たす */
for (row++; row <= rowmax; row++)
{
for (col = 1; col <= colmax; col++)
{
String num;
num = col + "列目" + " / " + row + "行目" ;
optlist =
"colwidth=20% fittextline={font=" + font + " fontsize=10}";
tbl = p.add_table_cell(tbl, col, row, num, optlist);
if (tbl == -1)
throw new Exception("Error adding cell: " +
p.get_errmsg());
}
}
/* ---------- 一つかそれ以上のページにテーブルを配置 ---------- */
/* 全てのテーブルが配置されるまでループし、それ以上の
* テーブル段階が配置を必要とする間に新しいページを作っていきます。
*/
do {
p.begin_page_ext(0, 0, "width=a4.width height=a4.height");
/* 他の全ての行が変化し、全てのテーブルセルに線を描きます
* "showcells showborder"をセルの境界を視覚化するために
* 追加します。
*/
optlist =
"header=1 fill={{area=rowodd fillcolor={gray 0.9}}} " +
"stroke={{line=other}} ";
/* テーブルを配置する */
result = p.fit_table(tbl, llx, lly, urx, ury, optlist);
if (result.equals("_error"))
throw new Exception ("Couldn't place table : " +
p.get_errmsg());
p.end_page_ext("");
} while (result.equals("_boxfull"));
/* 結果を調べる。"_stop"は全てが良いことを表す。 */
if (!result.equals("_stop"))
{
if (result.equals( "_error"))
{
throw new Exception ("Error when placing table: " +
p.get_errmsg());
}
else
{
/* 他のいくつかの戻り値は、"return"オプションによって
* ユーザーが終了を引き起こしたもので、処理をする
* 精巧なコードを必要とします。
*/
throw new Exception ("User return found in Textflow");
}
}
/* テーブルで使われたTextflowのハンドルも消去されるでしょう*/
p.delete_table(tbl, "");
p.end_document("");
} catch (PDFlibException e) {
System.err.print("PDFlib exception occurred:\n");
System.err.print("[" + e.get_errnum() + "] " + e.get_apiname() +
": " + e.get_errmsg() + "\n");
} catch (Exception e) {
System.err.println(e.getMessage());
} finally {
if (p != null) {
p.delete();
}
}
}
}
|