/*
    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 AppendTextWatermark {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        if (args.length < 2)
        {
            System.out.println("usage: java AppendTextWatermark 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 (PtlParamWaterMarkText watermarktext = new PtlParamWaterMarkText())
        {
            // 透かしの名前の設定
            watermarktext.setName("透かしの名前");

            // 透かしを配置する矩形の設定
            try (PtlRect rect = new PtlRect(10.0f, 10.0f, 150.0f, 150.0f))
            {
                watermarktext.setRect(rect);
            }

            // 透かしの配置の設定 ALIGN_BOTTOM_LEFT = 7 /* 左下 */
            watermarktext.setAlign(PtlParamWaterMark.ALIGN.ALIGN_BOTTOM_LEFT);

            // 透かしのZオーダーの設定 ZORDER_FRONT = 1 /* 前面 */
            watermarktext.setZorder(PtlParamWaterMark.ZORDER.ZORDER_FRONT);

            // 透かしを入れるページの範囲の設定 PAGE_RANGE_ALL = 0 /* 全ページ */
            watermarktext.setPageRange(PtlParamWaterMark.PAGE_RANGE.PAGE_RANGE_ALL);

            // 透かしの不透明度の設定
            watermarktext.setOpacity(1.0f);

            // 透かしをタイリングして配置するかどうかの設定
            watermarktext.setTiling(false);

            // 透かしに指定する文字列の設定
            watermarktext.setString("PDFToolAPI TextWatermark");

            // 透かしに指定するフォントの設定
            try (PtlParamFont font = new PtlParamFont("Times New Roman", 180.0f, PtlParamFont.WEIGHT.WEIGHT_NORMAL, false, true))
            {
                watermarktext.setFont(font);
            }

            // 透かしの文字に指定する色の設定
            try (PtlColorDeviceRGB colorText = new PtlColorDeviceRGB(1.0f, 0.0f, 0.0f))
            {
                watermarktext.setTextColor(colorText);
            }

            // 透かしの文字の縁取りに指定する色の設定
            try (PtlColorDeviceRGB colorOutline = new PtlColorDeviceRGB(0.0f, 1.0f, 0.0f))
            {
                watermarktext.setOutlineColor(colorOutline);
            }

            // 透かしのテキストを対角線上に配置する設定
            watermarktext.setWriteDiagonal(false);

            // 透かしのテキストを任意の角度で配置する設定
            watermarktext.setTextAngle(15.0f);

            // 透かしの設定
            doc.appendWaterMark(watermarktext);
        }
    }
}
