set_option("errorpolicy=return"); $p->set_option("searchpath={" . $searchpath . "}"); if ($p->begin_document($outfile, "") == 0) 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 == 0) 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(""); $buf = $p->get_buffer(); $len = strlen($buf); header("Content-type: application/pdf"); header("Content-Length: $len"); header("Content-Disposition: inline; filename=form_textfield_height.pdf"); print $buf; } catch (PDFlibException $e) { echo("PDFlib exception occurred:\n". "[" . $e->get_errnum() . "] " . $e->get_apiname() . ": " . $e->get_errmsg() . "\n"); exit(1); } catch (Throwable $e) { echo("PHP exception occurred: " . $e->getMessage() . "\n"); exit(1); } $p = 0; ?>