
リンク注釈を挿入し、アクションを指定します。
リンク注釈には、リンクをアクティブにしたときのアクションまたは移動先を指定します。リンク注釈ではアクションを指定しないで移動先のみの指定もできます。ここではアクションを設定する例を示します。PDF Tool API V6ではPDF がサポートする標準アクションのうち、GoToアクション、GoToRアクション、Launchアクション、URIアクションを指定できます。
| GoToアクション | 同一PDF内の指定された宛先(ページ、位置、および倍率)へ移動します |
| GoToRアクション | 異なるPDF内の指定された宛先(ページ、位置、および倍率)へ移動します |
| Launchアクション | アプリケーションの起動。通常はドキュメントのオープン、印刷です |
| URIアクション | 指定したURI(通常はインターネット上のハイパーテキストリンクを指す)を開きます |
GoToアクションやGoToRアクションを指定するとき、その移動先(ページ、位置、及び倍率)を同時に指定します。位置や倍率のパラメータは宛先の型に応じて変わります。具体的な指定方法については「5.1.1 PDFを開いた時の動作」のサンプルプログラムを参照してください。
| Fit型 | ページ全体がウィンドウにちょうど収まる倍率で表示します |
| FitB型 | 境界ボックス(※)全体がウィンドウにちょうど収まる倍率で表示します |
| FitBH型 | topで指定された座標がウィンドウの上端になり、境界ボックスの幅がウィンドウにちょうど収まる倍率で表示します |
| FitBV型 | leftで指定された座標がウィンドウの左端になり、境界ボックスの高さがウィンドウにちょうど収まる倍率で表示します |
| FitH型 | topで指定された座標がウィンドウの上端になり、ページ全体の幅がウィンドウにちょうど収まる倍率で表示します |
| FitR型 | top, left, bottom, rightで指定された座標がウィンドウにちょうど収まる倍率で表示します |
| FitV型 | leftで指定された座標がウィンドウの左端になり、ページ全体の高さがウィンドウに収まるように表示します |
| FitXYZ型 | top, leftで指定された座標がウィンドウの左上隅になり、zoomで指定した倍率に拡大して表示します |
※:ページの内容をすべて包含する最小の矩形
本サンプルプログラムでは、入力PDFの1ページ目に指定した種類のリンク注釈を挿入します。リンク注釈の内容はそれぞれプログラム内部であらかじめファイル名・URLを記述しています。Launchアクション、GoToRアクションの場合は実行フォルダ直下にあることを想定した仮のファイル名を指定してあり、URIアクションの場合はアンテナハウス社ホームページのURLを指定してあります。
以下のサブクラスがあり、実際にはこちらを用います。
package cookbook;
import java.io.*;
import jp.co.antenna.ptl.*;
public class AppendAnnotLink {
// そのクラスのusageを表示する関数
private static void printUsage(){
System.out.print("usage: java AppendAnnotLink in-pdf-file");
System.out.println(" out-pdf-file アクションの種類");
System.out.println("アクションの種類");
System.out.println("0 : GOTOアクションの設定");
System.out.println("1 : GOTORアクションの設定");
System.out.println("2 : Launchアクションの設定");
System.out.println("3 : URIアクションの設定");
}
public static void main(String[] args) {
if (args.length < 3){
printUsage();
return;
}
// コマンドライン引数の取得
int actionKind = Integer.parseInt(args[2]);
if((actionKind < 0) || (3 < actionKind)) {
System.out.print("アクションの種類 ");
System.out.println("は0から3の範囲の整数値で指定してください。");
printUsage();
return;
}
try (PtlParamInput inputFile = new PtlParamInput(args[0]);
PtlParamOutput outputFile = new PtlParamOutput(args[1]);
PtlPDFDocument doc = new PtlPDFDocument();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) {
// PDFファイルをロードします。
doc.load(inputFile);
try (PtlPages pages = doc.getPages()) {//ページコンテナの取得
// ページコンテナが空かどうか
if (pages.isEmpty()) {
System.out.println("ページコンテナが空\n");
return;
}
try (PtlPage page = pages.get(0)) {//1ページ目の取得
// リンク注釈追加
addAnnotLink(br, pages, page, actionKind);
}
}
// ファイルに保存します。
doc.save(outputFile);
}
...【AppendAnnotStampDefault.javaと同じ処理のため省略
・エラーメッセージ処理と出力】...
}
public static void addAnnotLink(BufferedReader br, PtlPages pages,
PtlPage page, int actionKind)
throws PtlException, Exception, Error {
try (PtlAnnots annots = page.getAnnots(); //注釈コンテナの取得
PtlAnnotLink annotlink = new PtlAnnotLink()) {//PDFのリンク注釈
// 矩形座標を設定
try (PtlRect rect = setRectCoordinate(br)) {
annotlink.setRect(rect);
}
// 内容を設定
annotlink.setTextContents("挿入されるリンク注釈");
// アクションの設定
setAction(pages, annotlink, actionKind);
// 注釈の追加
annots.append(annotlink);
}
}
public static void setAction(PtlPages pages, PtlAnnotLink annotlink, int actionKind)
throws PtlException, Exception, Error {
switch (actionKind) {
case 0:
// GOTOアクションの設定
try (PtlActionGoTo acttiongoto = new PtlActionGoTo();
PtlDestFit destfit = new PtlDestFit()) {//宛先
// 宛先ページの設定(最終ページに)
destfit.setPageNumber(pages.getCount() - 1);
// 宛先の設定
acttiongoto.setDest(destfit);
// アクションの設定
annotlink.setAction(acttiongoto);
}
break;
case 1:
// GOTORアクションの設定
try (PtlActionGoToR actiongotor = new PtlActionGoToR();
PtlDestFit destfit = new PtlDestFit()) {//宛先
// 宛先ページの設定(最終ページに)
destfit.setPageNumber(pages.getCount() - 1);
// 宛先の設定
actiongotor.setDest(destfit);
// ファイル間移動用PDFファイルを設定
actiongotor.setFileName("test.pdf");
// 新ウィンドウフラグを設定
actiongotor.setNewWindowFlag(true);
// アクションの設定
annotlink.setAction(actiongotor);
}
break;
case 2:
// Launchアクションの設定
try (PtlActionLaunch actionlaunch = new PtlActionLaunch()) {
// 起動ファイル名を設定
actionlaunch.setFileName("test.txt");
// 新ウィンドウフラグを設定
actionlaunch.setNewWindowFlag(true);
// アクションの設定
annotlink.setAction(actionlaunch);
}
break;
case 3:
// URIアクションの設定
try (PtlActionURI actionurl = new PtlActionURI()) {
// URIを設定
actionurl.setURI("http://www.antenna.co.jp/");
// アクションの設定
annotlink.setAction(actionurl);
}
break;
}
}
/**
* 矩形の各値を入力してその座標値をもつ矩形を返す関数。
* 原点はPDFの左下端。
* bottomよりtopが大きい、leftよりもrightが大きいなどの矛盾した数値は入力できない。
*
* @param br BufferedReader。数値の読み取りに使う。
* @return 指定したleft, bottom, right, topの数値を持つPtlRect
*/
public static PtlRect setRectCoordinate(BufferedReader br)
throws IOException, PtlException, Exception, Error
{
float top, bottom, left, right;
boolean isValueOkay = false;
PtlRect outputRect = new PtlRect();
while(!isValueOkay)
{
System.out.println("配置矩形の各数値を入力してください。");
System.out.print("top (mm) : ");
top = Float.parseFloat(br.readLine());
System.out.print("bottom (mm) : ");
bottom = Float.parseFloat(br.readLine());
if(top < bottom)
{
System.out.println("topの値はbottomよりも大きい値を指定して再度入力してください。");
continue;
}
System.out.print("left (mm) : ");
left = Float.parseFloat(br.readLine());
System.out.print("right (mm) : ");
right = Float.parseFloat(br.readLine());
if(right < left)
{
System.out.println("rightの値はleftよりも大きい値を指定して再度入力してください。");
continue;
}
//矩形を正しく指定できた場合の処理
isValueOkay = true;
outputRect.setLeft(left);
outputRect.setBottom(bottom);
outputRect.setRight(right);
outputRect.setTop(top);
}
return outputRect;
}
}
AppendAnnotLink.java
C:\samples>java cookbook.AppendAnnotLink usage: java AppendAnnotLink in-pdf-file out-pdf-file アクションの種類 アクションの種類 0 : GOTOアクションの設定 1 : GOTORアクションの設定 2 : Launchアクションの設定 3 : URIアクションの設定 C:\samples>java cookbook.AppendAnnotLink blank.pdf blank-link-c.pdf 3 配置矩形の各数値を入力してください。 top (mm) : 200 bottom (mm) : 150 left (mm) : 40 right (mm) : 90 -- 完了 --
サンプルプログラムで埋め込んだリンク注釈の上のマウスポインタを置いて移動先URLを表示したところです。
