/* * 複数ページにわたるテキスト : * 複数ページにわたり複数行テキストを出力する * * 必要な製品 : PDFlib/PDFlib+PDI/PPS 9 * 必要なデータ : 無し */ package com.pdflib.cookbook.pdflib.textflow; import com.pdflib.pdflib; import com.pdflib.PDFlibException; public class starter_textflow { public static void main (String argv[]) { pdflib p = null; String outfile = "starter_textflow.pdf"; String title = "Starter Textflow"; int i, tf = -1; String result; final double llx1= 50, lly1=50, urx1=250, ury1=800; final double llx2=300, lly2=50, urx2=500, ury2=800; int exitcode = 0; /* 多くのコンテンツを生成するため、ダミーテキストを繰り返す */ final int count = 50; final String optlist1 = "fontname=Helvetica fontsize=10.5 encoding=unicode " + "fillcolor={gray 0} alignment=justify"; final String optlist2 = "fontname=Helvetica-Bold fontsize=14 encoding=unicode " + "fillcolor={rgb 1 0 0} charref"; /* 段に流し込むダミーテキスト。ソフトハイフンは文字参照"­"で表す。 * (文字参照はcharrefオプションで有効となる) */ final String text = "Lorem ipsum dolor sit amet, consectetur adi­pi­sicing elit, " + "sed do eius­mod tempor incidi­dunt ut labore et dolore " + "magna ali­qua. Ut enim ad minim ve­niam, quis nostrud " + "exer­citation ull­amco la­bo­ris nisi ut " + "ali­quip ex ea commodo con­sequat. Duis aute irure dolor " + "in repre­henderit in voluptate velit esse cillum dolore eu " + "fugiat nulla pari­atur. Excep­teur sint occae­cat " + "cupi­datat non proident, sunt in culpa qui officia " + "dese­runt mollit anim id est laborum. "; try { p = new pdflib(); /* 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); /* テキストフローオブジェクトの作成し、ダミーテキストを指定する。 * オプションは2種類を交互に使用する。 */ for (i=1; i<=count; i++) { String num = i + " "; tf = p.add_textflow(tf, num, optlist2); if (tf == -1) throw new Exception("Error: " + p.get_errmsg()); tf = p.add_textflow(tf, text, optlist1); if (tf == -1) throw new Exception("Error: " + p.get_errmsg()); } /* 全てのテキストが配置されるまで繰り返す。配置されるべきテキストが * まだあれば、新しいページを作成する。 * 全てのページを2段組で作成する。 */ do { /* はめ込み枠の境界線を表示する場合は "showborder" を追加する */ String optlist = "verticalalign=justify linespreadlimit=120% "; p.begin_page_ext(0, 0, "width=a4.width height=a4.height"); /* 1段目に流し込み */ result = p.fit_textflow(tf, llx1, lly1, urx1, ury1, optlist); /* さらにテキストがある場合は、2段目に流し込む */ if (!result.equals("_stop")) result = p.fit_textflow(tf, llx2, lly2, urx2, ury2,optlist); p.end_page_ext(""); /* "_boxfull" テキストがまだ残っているため、続けて残りのテキストを処理 * する必要がある。 * "_nextpage" 「新規段組の開始」として解釈される。 */ } while (result.equals("_boxfull") || result.equals("_nextpage")); /* エラーチェック */ if (!result.equals("_stop")) { /* "_boxempty" はめ込み枠が小さすぎて、テキストが全く入っていない場合 */ if (result.equals( "_boxempty")) throw new Exception ("Error: Textflow box too small"); else { /* それ以外の戻り値は「return」オプションによるユーザー終了。 * これを扱うにはそのためのコードが必要。 */ throw new Exception ("User return '" + result + "' found in Textflow"); } } p.delete_textflow(tf); 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); } } }