/*
    Antenna House PDF Tool API V8.0
    Java Interface sample program
    
    概要：ページの回転と色透かし挿入 
    
    Copyright 2026- Antenna House, Inc.
*/

package Sample;

import jp.co.antenna.ptl.*;

public class CombiPgRotateColorWM {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        if (args.length < 2)
        {
            System.out.println("usage: java CombiPgRotateColorWM 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 (PtlPages pages = doc.getPages()) // ページコンテナの取得
            {
                // ページコンテナが空かどうか
                if (pages.isEmpty())
                {
                    System.out.println("ページコンテナが空\n");
                    return;
                }

                try (PtlPage page = pages.get(2))         // 3ページ目の取得
                {
                    // 回転角度設定（0, 90, 180, 270）
                    page.setRotate(90);
                }
                try (PtlPage page = pages.get(7))         // 8ページ目の取得
                {
                    // 回転角度設定（0, 90, 180, 270）
                    page.setRotate(180);
                }
            }

            // 透かしの追加
            appendWatermark(doc);

            // ファイルに保存します。
            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 appendWatermark(PtlPDFDocument doc) throws PtlException, Exception, Error
    {
        try (PtlParamWaterMarkColor watermarkcolor = new PtlParamWaterMarkColor())
        {
            //透かしの名前の設定
            watermarkcolor.setName("透かしの名前");

            //透かしを配置する矩形の設定
            watermarkcolor.setMargin(50.0f, 50.0f, 50.0f, 50.0f);

        	//先頭ページに透かしを配置するかしないかの設定 trueなら配置しない
        	watermarkcolor.setNotInFirst(true);

        	//PDF印刷時に透かしを印刷する指定を設定 false印刷しない
        	watermarkcolor.setPrintWaterMark(false);

            //透かしの不透明度の設定
            watermarkcolor.setOpacity(0.7f);

            //透かしに指定する色の設定
            try (PtlColorDeviceRGB color = new PtlColorDeviceRGB(0.0f, 1.0f, 0.0f))
            {
                watermarkcolor.setColor(color);
            }

            //透かしの設定
            doc.appendWaterMark(watermarkcolor);
        }
    }
}
