PDFlib を使い、PDF にテーブルを配置します。PDFlib では、テーブルハンドルにセルを追加する形でテーブルを作成し、出来たテーブルを矩形領域内に配置します。
/*
* テーブルの基本:
* 複数ページにまたがるテーブルの作成
*
* 必要な製品: PDFlib/PDFlib+PDI/PPS 9
* 必要なデータ: イメージファイル
*/
package com.pdflib.cookbook.pdflib.table;
import com.pdflib.pdflib;
import com.pdflib.PDFlibException;
public class starter_table {
public static void main(String argv[]) {
/* 必要に応じてデータファイルがあるフォルダのパスを指定する */
String searchpath = "../input";
String outfile = "starter_table.pdf";
String title = "Starter Table";
String imagefile = "nesrin.jpg";
int row, col, font, image, tf = -1, tbl = -1;
final int rowmax = 50, colmax = 5;
int exitcode = 0;
pdflib p = null;
final double llx = 50, lly = 50, urx = 550, ury = 800;
String headertext = "Table header (centered across all columns)";
String result;
String optlist;
/* テキストフローでセルを埋めるためのダミーテキスト */
final String tf_text = "Lorem ipsum dolor sit amet, consectetur "
+ "adipisicing elit, sed do eiusmod tempor "
+ "incididunt ut labore et dolore magna aliqua. Ut enim ad "
+ "minim veniam, quis nostrud exercitation ullamco "
+ "laboris nisi ut aliquip ex ea commodo "
+ "consequat. Duis aute irure dolor in reprehenderit in "
+ "voluptate velit esse cillum dolore eu fugiat nulla pariatur. "
+ "Excepteur sint occaecat cupidatat non proident, sunt "
+ "in culpa qui officia deserunt mollit anim id est laborum. ";
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);
/* -------------------- テーブルにセルを追加する -------------------- */
/* ---------- 1行目: テーブルセル (全ての列にまたがる) */
row = 1;
col = 1;
font = p.load_font("NotoSerif-Bold", "unicode", "");
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, "vertical line", 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, "some color", optlist);
if (tbl == -1)
throw new Exception("Error adding cell: " + p.get_errmsg());
/* ----- テキストフローを使用した複数行テキスト */
col++;
font = p.load_font("NotoSerif-Regular", "unicode", "");
if (font == -1)
throw new Exception("Error: " + p.get_errmsg());
optlist = "charref fontname=NotoSerif-Regular encoding=unicode 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, "entry void", 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 " + col + "/Row " + 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");
/*
* 1 行おきに塗りつぶす; すべてのテーブルセルに線を引く。
* セルの境界線を視覚化するには、"showcells showborder" を追加する。
*/
optlist = "header=1 rowheightdefault=auto "
+ "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 はすべてOKを意味する */
if (!result.equals("_stop")) {
if (result.equals("_error")) {
throw new Exception("Error when placing table: "
+ p.get_errmsg());
}
else {
/*
* それ以外の戻り値はすべて専用のコードで扱う必要がある
*/
throw new Exception("User return found in Table");
}
}
/* テーブルで使用されている テキストフローハンドルも削除する */
p.delete_table(tbl, "");
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);
}
}
}