/* * プッシュボタン: * "Print"と"Save As" のコマンドとを実行するために、Print" と * "Save" のプッシュボタンフォームフィールドを生成します。 * * 2つのプッシュボタンフォームフィールドを生成する。 Acrobatの * コマンド "File/Print"または"File/Save As"を実行するための * 2つのアクションを作成します。 * ユーザーがそれぞれのフィールドをクリックしたとき、"Print" * または "Save As"のアクションを実行します。 * ボタンはキャプションか、テンプレートとしてロードされた画像を * 使用して表示します。 * * 必要な製品 : PDFlib/PDFlib+PDI/PPS 9 * 必要なデータ : 無し */ package com.pdflib.cookbook.pdflib.form_fields; import com.pdflib.pdflib; import com.pdflib.PDFlibException; public class form_pushbutton { public static void main (String argv[]) { /* 必要に応じてデータファイルがあるフォルダのパスを指定する */ String searchpath = "../input"; String outfile = "form_pushbutton.pdf"; String title = "Form Pushbutton"; pdflib p = null; int exitcode = 0; String optlist; String p_imagefile = "fileprint.jpg"; String s_imagefile = "filesaveas.jpg"; int font; double width=60, height=30, llx = 40, lly = 600; int pact, sact, pimg, simg; double pwidth, pheight, swidth, sheight; try { p = new pdflib(); /* load_font() 等でエラーが起きた場合、-1を返す */ 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("Helvetica", "winansi", ""); if (font == -1) throw new Exception("Error: " + p.get_errmsg()); /* "print"のイメージテンプレートをロードする */ pimg = p.load_image("auto", p_imagefile, "template=true"); if (pimg == -1) throw new Exception("Error: " + p.get_errmsg()); /* "Save As" のイメージテンプレートをロードする */ simg = p.load_image("auto", s_imagefile, "template=true"); if (simg == -1) throw new Exception("Error: " + p.get_errmsg()); /* Acrobatのコマンド "File/Print"(ファイル/印刷)を実行するための * アクションを作成する */ pact = p.create_action("Named", "menuname=Print"); /* Acrobatのコマンド "File/Save As"(ファイル/名前を付けて保存)を * 実行するためのアクションを作成する */ sact = p.create_action("Named", "menuname=SaveAs"); /* ページを始める */ p.begin_page_ext(0, 0, " width=a4.width height=a4.height"); p.setfont(font, 14); /* ------------------------------------------------------------ * Acrobatメニューコマンドを実行するための2つのプッシュボタン * フィールドを生成し、キャプションを使用してそれを表す * ------------------------------------------------------------ */ /* 説明用テキストを出力する */ p.fit_textline("Two buttons executing Acrobat menu commands and " + "represented by a caption:", llx, lly, ""); lly-=60; /* "Print" と "Save" のプッシュボタンフォームフィールドを生成する * ライトブルーの背景色を持つボタンを指定する * (backgroundcolor={rgb 0.95 0.95 1}) and * 青の境界線を指定(bordercolor={rgb 0.25 0 0.95}) 線幅はデフォルト(1) * ボタンにテキスト「Print」を付け、文字を青で塗りつぶす * (caption={Print} fillcolor={rgb 0.25 0 0.95}). * 各ボタンにツールヒントを定義する * ユーザーがフィールドエリアからマウスボタンを離したときに実行される * アクション(アクションは上記で指定)を定義する * (action={up < actionhandle>}). */ optlist = "bordercolor={rgb 0.25 0 0.95} " + "backgroundcolor={rgb 0.95 0.95 1} " + "fillcolor={rgb 0.25 0 0.95} font=" + font + " fontsize=14"; p.create_field(llx, lly, llx + width, lly + height, "print", "pushbutton", optlist + " caption={Print} action={up " + pact + "} tooltip={Print the document}"); llx+=100; p.create_field(llx, lly, llx + width, lly + height, "save", "pushbutton", optlist + " caption={Save As} action={up " + sact + "} tooltip={Save the document}"); /* ------------------------------------------------------------ * Acrobatメニューコマンドを実行するための2つのプッシュボタン * フィールドを生成し、画像を使用してそれを表す * ------------------------------------------------------------ */ llx=40; lly=400; /* 説明用テキストを出力する */ p.fit_textline("Two buttons executing Acrobat menu commands and " + "represented by an image:", llx, lly, ""); lly-=60; /* "Print"イメージの幅と高さを取得する */ pwidth = p.info_image(pimg, "imagewidth", ""); pheight = p.info_image(pimg, "imageheight", ""); /* "Save"イメージの幅と高さを取得する */ swidth = p.info_image(simg, "imagewidth", ""); sheight = p.info_image(simg, "imageheight", ""); /* "printicon" と "saveicon" のプッシュボタンフォームフィールドを * 生成する * ボタンの高さだけではなく、画像の比率に基づいてボタンの幅を計算する * ユーザーがフィールドエリアからマウスボタンを離したときに実行される * アクション(アクションは上記で指定)を定義する * (action={up < actionhandle>}). * 画像でボタンを指定する(icon=< imagehandle>). * 各ボタンにツールヒントを定義する * 画像を長方形のボタンにはめ込んでください: Acrobatの特定の動作のため、 * 境界色の指定と、線幅をゼロにセットする必要がある */ double pbuttonwidth = pwidth/pheight*height; p.create_field(llx, lly, llx + pbuttonwidth, lly + height, "printicon", "pushbutton", "action={up " + pact + "} icon=" + pimg + " tooltip={Print the document} bordercolor={gray 0} linewidth=0" + " font=" + font); llx+=100; double sbuttonwidth = swidth/sheight*height; p.create_field(llx, lly, llx + sbuttonwidth, lly + height, "saveicon", "pushbutton", "action={up " + sact + "} icon=" + simg + " tooltip={Save the document} bordercolor={gray 0} linewidth=0" + " font=" + font); p.close_image(pimg); p.close_image(simg); 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); } } }