/* * Acrobat メニュー項目 * PDFページを開いたとき、Acrobatの全てのメニュー項目名のリストを作成します。 * 取得した Acrobat のメニュー項目名を使用して、PDFlib で Acrobat のメニューを * 実行する際に役に立ちます。 * Acrobatのメニュー項目を実行するためには、create_action() 関数に"Named" を指 * 定し、取得したメニューの項目名を "menuname" オプションに指定します。 * JavaScript コンソールを開くための特別なAcrobatメニューコマンドを実行するために、 * Named 型のアクションを作成します。JavaScript コンソール内の全てのAcrobat の * メニュー項目名を表示するJavaScriptアクションを作成します。 * ページを開いたとき、上記で指定した2つの JavaScript アクションが供給されます。 * * 必要な製品 : PDFlib/PDFlib+PDI/PPS 9 * 必要なデータ : 無し */ package com.pdflib.cookbook.pdflib.interactive; import com.pdflib.pdflib; import com.pdflib.PDFlibException; public class acrobat_menu_items { public static void main(String argv[]) { /* 必要に応じてデータファイルがあるフォルダのパスを指定する */ String searchpath = "../input"; String outfile = "acrobat_menu_items.pdf"; String title = "Acrobat Menu Items"; pdflib p = null; int exitcode = 0; String optlist; int font, tf = -1; final String text = "Show the names of all Acrobat menu items in the JavaScript console.\n\n" + "If the JavaScript console doesn't open automatically proceed as " + "follows:\n" + "Acrobat 7: Select the \"Advanced, JavaScript, Debugger\" menu item.\n" + "Acrobat 8 and 9: Select the \"Advanced, Document Processing, " + "JavaScript Debugger\" menu item.\n" + "Acrobat X and XI: Click on \"JavaScript Debugger\" in the " + "\"JavaScript\" panel in the \"Tools\" pane."; final String list_menu_names = "function MenuList(m, level) \n" + "{\n" + " console.println(m.cName); \n" + " if (m.oChildren != null) \n" + " for (var i = 0; i < m.oChildren.length; i++) \n" + " MenuList(m.oChildren[i], level + 1); \n" + "}\n" + "var m = app.listMenuItems(); \n" + "for (var i=0; i < m.length; i++) \n" + " MenuList(m[i], 0);"; 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); font = p.load_font("Helvetica", "unicode", ""); if (font == -1) throw new Exception("Error: " + p.get_errmsg()); /* A4 横のページで作成する */ p.begin_page_ext(0, 0, "width=a4.height height=a4.width"); /* * ---------------------------- * 説明用のテキストを出力する * ---------------------------- */ tf = p.add_textflow(tf, text, "fontname=Helvetica fontsize=12 encoding=unicode leading=120%"); if (tf == -1) throw new Exception("Error: " + p.get_errmsg()); p.fit_textflow(tf, 20, 20, 550, 450, ""); /* * --------------------------------------------------------------- * ページを開いたときに起動する JavaScript のアクションを定義する * --------------------------------------------------------------- */ /* * JavaScript コンソールを開き、コンソール上でAcrobatのメニューコマンドを * 実行させるために、Named 型のアクションを作成する。 */ int console = p.create_action("Named", "menuname={EScript:JSDebugger}"); /* * Acrobatのメニュー項目を JavaScript コンソールに表示するためのアクションを * 作成する */ int menuitems = p.create_action("JavaScript", "script {" + list_menu_names + "}"); /* * ページを閉じる。 * ページを開いたときに、上記で定義したアクションを実行させるよう定義する */ optlist = "action {open={" + console + " " + menuitems + "}}"; p.end_page_ext(optlist); 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); } } }