/* * リンク注釈 : * 画像やテキスト行で、PDFファイルやWebページやを開くためのリンクを生成したり、 * JavaScriptを実行します。 * * 画像上で、URLを開くためのリンク注釈を生成します。 * テキスト行上で、URLを開くためのリンク注釈を生成します。 * テキスト行上で、他のPDFファイルへジャンプするためのリンク注釈を生成します。 * テキスト行上で、JavaScriptを実行してメッセージボックスを表示するリンク注釈 * を生成します。 * * 必要な製品 : PDFlib/PDFlib+PDI/PPS 9 * 必要なデータ : 無し */ package com.pdflib.cookbook.pdflib.interactive; import com.pdflib.pdflib; import com.pdflib.PDFlibException; public class link_annotations { public static void main (String argv[]) { /* 必要に応じてデータファイルがあるフォルダのパスを指定する */ String searchpath = "../input"; String outfile = "link_annotations.pdf"; String title = "Link Annotations"; pdflib p = null; int exitcode = 0; /* リンクによって参照されるPDFファイル */ String pdffile = "../input/PLOP-datasheet.pdf"; /* リンクによって参照されるURL */ String url = "http://www.pdflib.com"; /* ロードした画像にもリンクを生成 */ String imagefile = "websurfer.jpg"; String optlist; int font, action, image, x = 20, y = 400; 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", "unicode", ""); if (font == -1) throw new Exception("Error: " + p.get_errmsg()); p.begin_page_ext(0, 0, "width=a4.height height=a4.width"); /* --- 画像上で、URLを開くためのリンク注釈を生成 --- */ /* 画像を配置し、範囲枠 "image_matchbox" を介して画像の寸法を * 決定する。 */ optlist = "boxsize={50 50} fitmethod=meet matchbox={name=image_matchbox}"; image = p.load_image("auto", imagefile, ""); if (image == -1) throw new Exception("Error: " + p.get_errmsg()); p.fit_image(image, x, y, optlist); p.close_image(image); /* URL を開くための "URL" アクションを生成する */ optlist = "url={" + url + "}"; action = p.create_action("URI", optlist); /* 範囲枠 "image_matchbox" を使用し, "URI" アクションによって、リンク * 注釈を生成する。長方形座標の0は範囲枠の座標に置き換えられる。 */ optlist = "action={activate " + action + "} linewidth=0 " + "usematchbox={image_matchbox}"; p.create_annotation(0, 0, 0, 0, "Link", optlist); /* --- テキスト行上で、URLを開くためのリンク注釈を生成 --- */ /* テキスト行を配置し、範囲枠 "text_matchbox" を介してテキストの寸法を * 決定する。 */ optlist = "font=" + font + " fontsize=20 " + "matchbox={name=text_matchbox} " + "fillcolor={rgb 0 0.6 0.6} strokecolor={rgb 0 0.6 0.6} underline"; p.fit_textline("Go to " + url, x, y-=60, optlist); /* URL を開くための "URL" アクションを生成する */ optlist = "url={" + url + "}"; action = p.create_action("URI", optlist); /* 範囲枠 "text_matchbox" を使用し, "URI" アクションによって、リンク * 注釈を生成する。長方形座標の0は範囲枠の座標に置き換えられる。 */ optlist = "action={activate " + action + "} linewidth=0 " + "usematchbox={text_matchbox}"; p.create_annotation(0, 0, 0, 0, "Link", optlist); /* --- テキスト行上で、他のPDFファイルへジャンプするためのリンク注釈を生成 --- */ /* テキスト行を配置し、範囲枠 "goto_matchbox" を介してテキストの寸法を * 決定する。 */ optlist = "font=" + font + " fontsize=20 " + "matchbox={name=goto_matchbox} " + "fillcolor={rgb 0.6 0.6 0} strokecolor={rgb 0.6 0.6 0} underline"; p.fit_textline("Jump to file \"" + pdffile + "\"", x, y-=100, optlist); /* PDF ファイルにジャンプするための "GoToR" アクションを生成する */ optlist = "filename={" + pdffile + "}" + " destination {page 1 type fitwindow} newwindow"; action = p.create_action("GoToR", optlist); /* 範囲枠 "text_matchbox" を使用し, "GoToR" アクションによって、リンク * 注釈を生成する。長方形座標の0は範囲枠の座標に置き換えられる。 */ optlist = "action={activate " + action + "} linewidth=0 " + "usematchbox={goto_matchbox}"; p.create_annotation(0, 0, 0, 0, "Link", optlist); /* --- テキスト行上で、JavaScriptを実行してメッセージボックスを --- * * --- 表示するリンク注釈を生成 --- */ /* テキスト行を配置し、範囲枠 "js_matchbox" を介してテキストの寸法を * 決定する。 */ optlist = "font=" + font + " fontsize=16 " + "matchbox={name=js_matchbox} " + "fillcolor={rgb 0.6 0 0.6} strokecolor={rgb 0.6 0 0.6} underline"; p.fit_textline("Launch JavaScript", x, y-=100, optlist); /* "JavaScript"アクションを生成し、メッセージボックスを表示する。 */ optlist = "script={app.alert(\"JavaScript works!\")}"; int js_action = p.create_action("JavaScript", optlist); /* 範囲枠 "text_matchbox" を使用し, "JavaScript" アクションによって、リンク * 注釈を生成する。長方形座標の0は範囲枠の座標に置き換えられる。 */ optlist = "action={activate " + js_action + "} linewidth=0 " + "usematchbox={js_matchbox}"; p.create_annotation(0, 0, 0, 0, "Link", optlist); 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); } } }