/*
    Antenna House PDF Tool API V7.0
    Java Interface sample program

    概要：リンク注釈の作成

    Copyright 2015-2021 Antenna House, Inc.
*/

package Sample;

import java.io.*;
import jp.co.antenna.ptl.*;

public class AppendAnnotLink {

    public static void main(String[] args) {
        if (args.length < 3)
        {
            System.out.println("usage: java AppendAnnotLink in-pdf-file out-pdf-file アクションの種類\n");
            System.out.println("アクションの種類\n0 : GOTOアクションの設定\n1 : GOTORアクションの設定\n2 : Launchアクションの設定\n3 : URIアクションの設定");
            return;
        }

        String actionKind = args[2];
        switch (actionKind) {
        case "0":
        case "1":
        case "2":
        case "3":
            break;
        default:
            return;
        }

        try (PtlParamInput inputFile = new PtlParamInput(args[0]);
             PtlParamOutput outputFile = new PtlParamOutput(args[1]);
             PtlPDFDocument doc = new PtlPDFDocument())
        {
            // 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(pages, page, actionKind);
                }
            }

            // ファイルに保存します。
            doc.save(outputFile);
        }
        catch (PtlException pex) {
             System.out.println("PtlException : ErrorCode = " + pex.getErrorCode() + "\n  " + pex.getErrorMessage());
        }
        catch (Exception ex) {
            System.out.println(ex.getMessage());
            ex.printStackTrace();
        }
        catch (Error ex) {
            System.out.println(ex.getMessage());
            ex.printStackTrace();
        }
        finally {
            System.out.println("-- 完了 --");
        }
    }

    public static void addAnnotLink(PtlPages pages, PtlPage page, String actionKind) throws PtlException, Exception, Error
    {
        try (PtlAnnots annots = page.getAnnots(); //注釈コンテナの取得
             PtlAnnotLink annotlink = new PtlAnnotLink()) //PDFのリンク注釈
        {
            // 矩形座標を設定
            try (PtlRect rect = new PtlRect(30.0f, 30.0f, 50.0f, 50.0f))
            {
                annotlink.setRect(rect);
            }

            // 内容を設定
            annotlink.setTextContents("PDFのリンク注釈");

            // アクションの設定
            setAction(pages, annotlink, actionKind);

            // 注釈の追加
            annots.append(annotlink);
        }
    }

    public static void setAction(PtlPages pages, PtlAnnotLink annotlink, String 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;
        }
    }
}
