PDFlib Cookbook

cookbook

interactive/acrobat_menu_items

Upon opening the page, create a full list of all menu item names in Acrobat.

Download Java Code  Switch to PHP Code  Show Output 

/*
 * Acrobat menu items
 * Upon opening the page, create a full list of all menu item names in Acrobat.
 * With the spelling retrieved, the Acrobat menu item name can be used in PDFlib
 * actions to execute Acrobat menu items.
 * 
 * One of the retrieved menu item names can be provided in the "menuname" option
 * of the create_action() function to create an action of type "Named" for 
 * executing Acrobat menu items.
 * Create an action of type "Named" to execute the special Acrobat menu command
 * for opening the JavaScript console.
 * Create a JavaScript action to show the names of all Acrobat menu items in the
 * JavaScript console.
 * For the page trigger "open", supply the two JavaScript actions defined above.
 *
 * Required software: PDFlib/PDFlib+PDI/PPS 10
 * Required data: none
 */
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[]) {
        /* This is where the data files are. Adjust as necessary. */
        String searchpath = "../input";
        String outfile = "acrobat_menu_items.pdf";
        String title = "Acrobat Menu Items";

        pdflib p = null;
        int exitcode = 0;
        String optlist;
        int 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 X and XI: Click on \"JavaScript Debugger\" in the "
            + "\"JavaScript\" panel in the \"Tools\" pane."
            + "Acrobat DC: Click on \"Tools\", search for \"JavaScript\", \"Debugger\"";

        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 + "}");

            /* This means we must check return values of load_font() etc. */
            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);

            /* Start a A4 landscape page */
            p.begin_page_ext(0, 0, "width=a4.height height=a4.width");

            /*
             * ----------------------------
             * Output some descriptive text
             * ----------------------------
             */
            tf = p.add_textflow(tf, text,
                "fontname=NotoSerif-Regular fontsize=12 leading=120%");
            if (tf == -1)
                throw new Exception("Error: " + p.get_errmsg());

            p.fit_textflow(tf, 20, 20, 550, 450, "");

            /*
             * ---------------------------------------------------------------
             * Define JavaScript actions to be triggered upon opening the page
             * ---------------------------------------------------------------
             */

            /*
             * Create an action of type "Named" to execute the Acrobat menu
             * command for opening the JavaScript console
             */
            int console = p.create_action("Named",
                "menuname={EScript:JSDebugger}");

            /*
             * Create a JavaScript action to show the names of all Acrobat menu
             * items in the JavaScript console
             */
            int menuitems = p.create_action("JavaScript", "script {"
                + list_menu_names + "}");

            /*
             * Close the page. For the page trigger "open", supply the
             * JavaScript page actions defined above.
             */
            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);
        }
    }
}