/*
    Antenna House PDF Tool API V7.0
    Java Interface sample program

    概要：しおりの追加

    Copyright 2015-2021 Antenna House, Inc.
*/

package Sample;

import jp.co.antenna.ptl.*;

public class AppendOutline {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        if (args.length < 2)
        {
            System.out.println("usage: java AppendOutline in-pdf-file out-pdf-file");
            return;
        }

        try (PtlParamInput inputFile = new PtlParamInput(args[0]);
             PtlParamOutput outputFile = new PtlParamOutput(args[1]);
             PtlPDFDocument doc = new PtlPDFDocument())
        {
            // PDFファイルをロードします。
            doc.load(inputFile);
            
            try (PtlOutline outlineRoot = doc.getRootOutline()) //ルートアウトラインの取得
            {
                // しおりの追加
                createOutline(outlineRoot);
            }

            // ファイルに保存します。
            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("-- 完了 --");
        }
    }

    static void createOutline(PtlOutline outlineRoot) throws PtlException, Exception, Error
    {
        try (PtlOutline outlineActionGoTo = new PtlOutline())
        {
            outlineActionGoTo.setTitle("ActionGoTo DestFit");
            // アクション
            try (PtlActionGoTo actiongoto = new PtlActionGoTo(); // PtlActionGoTo : GoToアクション<現在のドキュメント内の宛先へ移動>
                 PtlDestFit destfit = new PtlDestFit())          // PtlDestFit : Fit型
            {
                // 宛先ページの設定
                destfit.setPageNumber(1);
                // 宛先の設定
                actiongoto.setDest(destfit);
                // アクションの設定
                outlineActionGoTo.setAction(actiongoto);
            }
            // アウトラインタイトルの色を設定
            try (PtlColorDeviceRGB color =  new PtlColorDeviceRGB(0.0f, 1.0f, 0.0f))
            {
                outlineActionGoTo.setColor(color);
            }
            // アウトラインフラグを設定
            outlineActionGoTo.setFlags(PtlOutline.FLAG_ITALIC);
            // PDF表示時の子アウトラインをオープンするかどうかの設定
            outlineActionGoTo.setOpen(true);
            // 子アウトラインの追加
            outlineRoot.appendLastChild(outlineActionGoTo);

            try (PtlOutline outlineActionGoToR = new PtlOutline())
            {
                outlineActionGoToR.setTitle("ActionGoToR DestFitH");
                // アクション
                try (PtlActionGoToR actiongotor = new PtlActionGoToR(); // PtlActionGoToR : GoToRアクション<("Go-to remote")他のドキュメント内への移>
                     PtlDestFitH destfith = new PtlDestFitH()) // PtlDestFitH : FitH型
                {
                    // 宛先ページの設定
                    destfith.setPageNumber(3);
                    // topの設定
                    destfith.setTopNull();
                    // dest1.setTop(NULL_VALUE_CSTR);
                    // 宛先の設定
                    actiongotor.setDest(destfith);
                    // ファイル間移動用PDFファイル名を取得（ファイル名もしくは相対パス指定）
                    actiongotor.setFileName("test.pdf");
                    // 宛先文書を新ウィンドウでオープンするかどうかのフラグを設定（true: 新ウィンドウでオープンする。false: しない）
                    actiongotor.setNewWindowFlag(false);
                    // アクションの設定
                    outlineActionGoToR.setAction(actiongotor);
                }
                // アウトラインタイトルの色を設定
                try (PtlColorDeviceRGB color =  new PtlColorDeviceRGB(0.0f, 0.0f, 1.0f))
                {
                    outlineActionGoToR.setColor(color);
                }
                // アウトラインフラグを設定
                outlineActionGoToR.setFlags(PtlOutline.FLAG_NORMAL);
                // PDF表示時の子アウトラインをオープンするかどうかの設定
                outlineActionGoToR.setOpen(false);
                // 子アウトラインの追加
                outlineActionGoTo.appendLastChild(outlineActionGoToR);
            }
        }

        try (PtlOutline outlineActionLaunch = new PtlOutline())
        {
            outlineActionLaunch.setTitle("ActionLaunch");
            // アクション
            try (PtlActionLaunch actionlaunch = new PtlActionLaunch()) // PtlActionLaunch : Launchアクション<アプリケーションの起動、通常、ファイルを開
            {
                // 起動ファイル名を設定
                actionlaunch.setFileName("test.jpg");
                // 宛先文書を新ウィンドウでオープンするかどうかのフラグを設定（true: 新ウィンドウでオープンする false: しない）
                actionlaunch.setNewWindowFlag(true);
                // アクションの設定
                outlineActionLaunch.setAction(actionlaunch);
            }
            // アウトラインタイトルの色を設定
            try (PtlColorDeviceRGB color =  new PtlColorDeviceRGB(0.0f, 1.0f, 1.0f))
            {
                outlineActionLaunch.setColor(color);
            }
            // アウトラインフラグを設定
            outlineActionLaunch.setFlags(PtlOutline.FLAG_BOLD);
            // PDF表示時の子アウトラインをオープンするかどうかの設定
            outlineActionLaunch.setOpen(false);
            // 子アウトラインの追加
            outlineRoot.appendLastChild(outlineActionLaunch);
        }

        // URIアクション    -----------------------------------
        try (PtlOutline outlineActionURI = new PtlOutline())
        {
            // アウトラインタイトル文字列を設定
            outlineActionURI.setTitle("ActionURI");
            // アクション
            try (PtlActionURI actionuri = new PtlActionURI()) // PtlActionURI : URIアクション
            {
                // URIを設定
                actionuri.setURI("http://www.antenna.co.jp/");
                // アクションの設定
                outlineActionURI.setAction(actionuri);
            }
            // アウトラインタイトルの色を設定
            try (PtlColorDeviceRGB color =  new PtlColorDeviceRGB(.01f, 0.5f, 0.0f))
            {
                outlineActionURI.setColor(color);
            }
            // アウトラインフラグを設定
            outlineActionURI.setFlags(PtlOutline.FLAG_BOLD | PtlOutline.FLAG_ITALIC);
            // PDF表示時の子アウトラインをオープンするかどうかの設定
            outlineActionURI.setOpen(true);
            // 子アウトラインの追加
            outlineRoot.appendLastChild(outlineActionURI);
        }
    }
}
