/*
    Antenna House PDF Tool API V7.0
    Java Interface sample program

    概要：PDF透かしの挿入

    Copyright 2015-2021 Antenna House, Inc.
*/

package Sample;

import jp.co.antenna.ptl.*;

public class AppendPdfWatermark {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        if (args.length < 3)
        {
            System.out.println("usage: java AppendPdfWatermark in-pdf-file out-pdf-file watermark-pdf");
            return;
        }

        try (PtlParamInput inputFile = new PtlParamInput(args[0]);
             PtlParamOutput outputFile = new PtlParamOutput(args[1]);
             PtlPDFDocument doc = new PtlPDFDocument())
        {
            // PDFファイルをロード
            doc.load(inputFile);

            // 透かしの追加
            appendWatermark(doc, args[2]);

            // ファイルに保存します。
            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, String pathPdf) throws PtlException, Exception, Error
    {
        try (PtlParamWaterMarkPDF watermarkpdf = new PtlParamWaterMarkPDF())  // 透かしパラメーター
        {
            // 透かしの名前の設定
            watermarkpdf.setName("透かしの名前");

            // 透かしの配置の設定 ALIGN_CENTER = 5 /* 中央 */
            watermarkpdf.setAlign(PtlParamWaterMark.ALIGN.ALIGN_CENTER);

            // 透かしのZオーダーの設定 ZORDER_FRONT = 1 /* 前面 */
            watermarkpdf.setZorder(PtlParamWaterMark.ZORDER.ZORDER_FRONT);

            // 透かしを入れるページの範囲の設定 PAGE_RANGE_ALL = 0 /* 全ページ */
            watermarkpdf.setPageRange(PtlParamWaterMark.PAGE_RANGE.PAGE_RANGE_ALL);

            // 先頭ページに透かしを配置しない設定の範囲の設定
            watermarkpdf.setNotInFirst(false);

            // 最終ページに透かしを配置しない設定の設定
            watermarkpdf.setNotInLast(false);

            // PDF表示時に透かしを表示する指定の設定
            watermarkpdf.setDisplayWaterMark(true);

            // PDF印刷時に透かしを印刷する指定の設定
            watermarkpdf.setPrintWaterMark(true);

            // 透かしの不透明度の設定
            watermarkpdf.setOpacity(0.5f);

            // 透かしをタイリングして配置するかどうかの設定
            watermarkpdf.setTiling(false);

            try (PtlPDFDocument doc_watermark = new PtlPDFDocument();
                 PtlParamInput inputPdf = new PtlParamInput(pathPdf))     // PDFファイル
            {
                // PDFファイルをロード
                doc_watermark.load(inputPdf);

                // 透かしに使用するページの取得
                try (PtlPages pages = doc_watermark.getPages()) //ページコンテナの取得
                {
                    //ページコンテナが空かどうか
                    if (pages.isEmpty())
                    {
                        System.out.println("ページコンテナが空\n");
                        return;
                    }
                    try (PtlPage page = pages.get(0))    // 1ページ目
                    {
                        // 透かしに使用するPDF文書ページを設定
                        watermarkpdf.setPage(page);
                    }
                }
            }

            // 透かしの倍率の設定
            watermarkpdf.setScale(0.5f);

            // 透かしの設定
            doc.appendWaterMark(watermarkpdf);
        }
    }
}
