/*
    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 AppendColorWatermark {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        if (args.length < 2)
        {
            System.out.println("usage: java AppendColorWatermark 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);

            // 透かしの追加
            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(10.0f, 10.0f, 10.0f, 10.0f);

            //透かしのZオーダーの設定 ZORDER_FRONT = 2 /* 背面 */
            watermarkcolor.setZorder(PtlParamWaterMark.ZORDER.ZORDER_BACK);

            //透かしを入れるページの範囲の設定 PAGE_RANGE_ALL = 0 /* 全ページ */
            watermarkcolor.setPageRange(PtlParamWaterMark.PAGE_RANGE.PAGE_RANGE_ALL);

            //透かしの不透明度の設定
            watermarkcolor.setOpacity(0.4f);

            //透かしに指定する色の設定
            try (PtlColorDeviceRGB color = new PtlColorDeviceRGB(1.0f, 0.0f, 0.0f))
            {
                watermarkcolor.setColor(color);
            }

            //透かしの設定
            doc.appendWaterMark(watermarkcolor);
        }
    }
}
