/* * フォームフィールドを PDFlib ブロックに変換 : * それぞれ異なる個別のデータを配置した PDFlib ブロックを持つ PDF ページを * 出力します。 * * PDFlibブロックツールにて、フォームフィールドを PDFlib ブロックに変換した * PDFページをインポートします。次に、テキストフィールド、チェックボックス、 * ラジオボタン等に合わせて内容を配置します。 * * 必要な製品 : PPS 9 * 必要なデータ : フォームフィールドを含むPDF文書 */ package com.pdflib.cookbook.pdflib.blocks; import com.pdflib.pdflib; import com.pdflib.PDFlibException; public class fill_converted_formfields { public static void main(String argv[]) { /* 必要に応じてデータファイルがあるフォルダのパスを指定する */ String searchpath = "../input"; String outfile = "fill_converted_formfields.pdf"; String title = "Fill Converted Form Fields"; pdflib p = null; double width, height; String infile = "form_with_blocks.pdf"; int inpage, indoc; String text_optlist; int exitcode = 0; /* 既存PDFページには、次の通りPDFlibブロックが定義されている。 * "plane model" "quantity" ・・・ テキスト * "color" "color_1" ・・・ ラジオボタン * "perforation""glossy" ・・・ チェックボックス */ 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); /* PDFlibブロックを含む既存文書を開く */ indoc = p.open_pdi_document(infile, ""); if (indoc == -1) throw new Exception("Error: " + p.get_errmsg()); /* 既存文書の1ページ目を開く */ inpage = p.open_pdi_page(indoc, 1, ""); if (inpage == -1) throw new Exception("Error: " + p.get_errmsg()); /* 既存文書1ページ目のページサイズ(幅・高さ)を取得する */ width = p.pcos_get_number(indoc, "pages[0]/width"); height = p.pcos_get_number(indoc, "pages[0]/height"); /* 出力PDFページを既存文書のサイズで作成する */ p.begin_page_ext(width, height, ""); /* 既存PDFページを出力PDFページ上に配置する */ p.fit_pdi_page(inpage, 0, 0, ""); /* "plane model"ブロックに テキスト"Long Distance Glider"を設定する */ if (p.fill_textblock(inpage, "plane model", "Long Distance Glider", "encoding=unicode") == -1) System.err.println("Warning: " + p.get_errmsg()); /* "quantity"ブロックに テキスト"1"を設定する */ if (p.fill_textblock(inpage, "quantity", "1", "encoding=unicode") == -1) System.err.println("Warning: " + p.get_errmsg()); /* "color"ブロック(ラジオボタンを示す)内に、円記号を指定する。この時 * fill_textblock()の第3パラメータには"l"を指定する。 * ZapfDingbatsフォントで、encoding=builtinを指定した場合、以下の通り * 第3パラメータに指定することで色々な記号を取得できる。 * チェック="4", 円="1", クロス="8", ダイヤモンド="u", スクエア="n" * スター="H" */ text_optlist = "fontname=ZapfDingbats encoding=builtin position=center"; if (p.fill_textblock(inpage, "color", "l", text_optlist) == -1) System.err.println("Warning: " + p.get_errmsg()); /* "color_1"ブロック(ラジオボタンを示す)に" "(スペース)を指定する。 * これにより、ブロックの境界線のみ出力され、非選択の状態となる。 * スペースを指定しない場合、ボックスの境界線は出力されない。 */ text_optlist = "fontname=ZapfDingbats encoding=builtin position=center"; if (p.fill_textblock(inpage, "color_1", " ", text_optlist) == -1) System.err.println("Warning: " + p.get_errmsg()); /* * "perforation"ブロック(チェックボックスを示す)に、チェックマークを * 出力する。 */ text_optlist = "fontname=ZapfDingbats fontsize=12 encoding=unicode " + "position=center"; if (p.fill_textblock(inpage, "perforation", "\u2714", text_optlist) == -1) System.err.println("Warning: " + p.get_errmsg()); /* "glossy"ブロック(チェックボックスを示す)に" "(スペース)を指定する。 * これによりブロックの境界線のみ出力され、空のチェックボックスを表す。 * スペースを指定しない場合、ボックスの境界線は出力されない。 */ text_optlist = "fontname=ZapfDingbats fontsize=12 encoding=unicode " + "position=center"; if (p.fill_textblock(inpage, "glossy", " ", text_optlist) == -1) System.err.println("Warning: " + p.get_errmsg()); p.end_page_ext(""); p.close_pdi_page(inpage); p.end_document(""); p.close_pdi_document(indoc); } 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); } } }