/* * テキストフィールドの高さ : * フォントサイズからテキストフィールドの高さを決定します。そして、逆も * また同じ。 * * フィールドの高さを指定し、フォントサイズは指定せずに、テキストフィールド * を生成します。 * 指定されたフィールドの高さでテキストフィールド作り、フィールドの高さから適 * 切なフォントサイズを計算します。 * 指定されたフォントサイズでテキストフィールドを作り、フォントサイズから * 適切なフィールドの高さを計算します。 * * 必要な製品 : PDFlib/PDFlib+PDI/PPS 10 * 必要なデータ : 無し */ package com.pdflib.cookbook.pdflib.form_fields; import com.pdflib.pdflib; import com.pdflib.PDFlibException; public class form_textfield_height { public static void main (String argv[]) { /* 必要に応じてデータファイルがあるフォルダのパスを指定する */ String searchpath = "../input"; String outfile = "form_textfield_height.pdf"; String title = "Form Textfield Height"; pdflib p = null; int exitcode = 0; String optlist; int font; double fontsize; double ascender, descender; double width=160, height=30; double llx = 10, lly = 700; String text = "Enter text here"; try { p = new pdflib(); /* load_font() 等の戻り値を確認する */ 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", "winansi", "simplefont nosubsetting"); if (font == -1) throw new Exception("Error: " + p.get_errmsg()); p.begin_page_ext(0, 0, "width=a4.width height=a4.height"); /* -------------------------------------------------------------------- * フォントのサイズは指定せず、フィールドの高さを所定のサイズで指定する * -------------------------------------------------------------------- * * フォームフィールド種別"textfield"の"date" フィールドを生成し、背景色に * ブルーライト、境界線に黒を指定する。 * 入力できる最大文字数を25文字に指定する(maxchar=25) * もし、この他にオプションを指定しなければ、フォントサイズは自動的に調整 * され、テキストフィールドにはめ込まれる。 * これは、より多くのテキストを入力すれば、テキストはより小さくなることを * 意味している。 */ optlist = "backgroundcolor={rgb 0.95 0.95 1} bordercolor={gray 0} " + "maxchar=25 currentvalue={" + text + "} font=" + font; p.create_field(llx, lly, llx + width, lly + height, "date", "textfield", optlist); lly-=40; p.fit_textline("Field height given, no font size specified; a " + "maximum of 25 characters is allowed.", llx, lly, "font=" + font + " fontsize=12"); p.fit_textline("Acrobat automatically decreases the font size when " + "more text is entered.", llx, lly-=20, "font=" + font + " fontsize=12"); lly-=100; /* -------------------------------------------------------------- * 与えられたフィールドの高さから、適切なフォントサイズを計算する * -------------------------------------------------------------- */ /*テキストフィールド "date2" を所定のサイズで生成する。フィールドの * 高さに合うようにフォントサイズを調整する: * フィールドの高さと等しい仮定的なフォントサイズのアセンダ値、ディセ * ンダ値(通常は非奨励)を取得し、アセンダとディセンダーの合計した値を * フォントサイズとして使用する。 * また、フィールド境界から若干の空のスペースを残すために、20% フォント * サイズを小さくする。 */ ascender = p.info_font(font, "ascender", "fontsize=" + height); descender = p.info_font(font, "descender", "fontsize=" + height); fontsize = (ascender - descender) * 0.8; optlist = "backgroundcolor={rgb 0.95 0.95 1} bordercolor={gray 0} " + "currentvalue={" + text + "} font=" + font + " fontsize=" + fontsize; p.create_field(llx, lly, llx + width, lly + height, "date2", "textfield", optlist); lly-=40; p.fit_textline("Field height of 30 is given. Acrobat uses an " + "appropriate font size.", llx, lly, "font=" + font + " fontsize=12"); lly-=100; /* -------------------------------------------------------------- * 与えられたフォントサイズから、適切なフィールドの高さを計算する * -------------------------------------------------------------- */ /* テキストフィールド "date3" を所定のサイズで生成する。この場合では、 * フォントサイズは24で与えられ、フィールドの高さは適切に選択される * べきである : フォントサイズを 24 に指定し、アセンダ値、ディセンダ * 値(通常は非奨励)を取得する。この取得した値は、フィールドの高さを * 決定するのに有効となる。 * * フォントとベースラインについてのAcrobatの挙動は明らかではないので、 * フィールドの高さは、小さくなる。これを回避するため、フィールドの高さ * の適切な割合のマージン(例:50%)を追加する。 */ ascender = p.info_font(font, "ascender", "fontsize=24"); descender = p.info_font(font, "descender", "fontsize=24"); height = (ascender - descender) * 1.5; optlist = "backgroundcolor={rgb 0.95 0.95 1} bordercolor={gray 0} " + "currentvalue={" + text + "} font=" + font + " fontsize=24"; p.create_field(llx, lly, llx + width, lly + height, "date3", "textfield", optlist); lly-=40; p.fit_textline("Font size of 24 is given. We calculate the field " + "height based on the font's ascender and descender.", llx, lly, "font=" + font + " fontsize=12"); 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); } } }