/*
    Antenna House PDF Tool API V8.0
    Java Interface sample program

    概要：画像透かし・PDF透かし：透かし画像・PDFをカスタム角度で挿入

    Copyright 2025- Antenna House, Inc.
*/
package Sample;

import jp.co.antenna.ptl.PtlException;
import jp.co.antenna.ptl.PtlPDFDocument;
import jp.co.antenna.ptl.PtlPage;
import jp.co.antenna.ptl.PtlPages;
import jp.co.antenna.ptl.PtlParamInput;
import jp.co.antenna.ptl.PtlParamOutput;
import jp.co.antenna.ptl.PtlParamWaterMark;
import jp.co.antenna.ptl.PtlParamWaterMarkImage;
import jp.co.antenna.ptl.PtlParamWaterMarkPDF;

public class AppendWatermarkSetAngle {

    /**
     * @param args the command line arguments
     */	
	public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ
        if (args.length < 3)
        {
            System.out.println("usage: java AppendImageWatermark in-pdf-file out-pdf-file watermark-image-file");
            return;
        }

        try (PtlParamInput inputFile = new PtlParamInput(args[0]);
             PtlParamOutput outputFile = new PtlParamOutput(args[1]);
             PtlPDFDocument doc = new PtlPDFDocument())
        {
            // PDFファイルをロード
            doc.load(inputFile);

            // 透かしの追加
            appendWatermarkImage(doc, args[2]);
        	appendWatermarkPDF(doc, args[3]);

            // ファイルに保存します。
            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 appendWatermarkImage(PtlPDFDocument doc, String pathImage) throws PtlException, Exception, Error
    {
        try (PtlParamWaterMarkImage watermarkimage = new PtlParamWaterMarkImage())  // 透かしパラメーター
        {
            try (PtlParamInput inputImage = new PtlParamInput(pathImage))      // 画像ファイル
            {
                // 画像描画パラメータに画像ファイルを設定
                watermarkimage.setImageStream(inputImage);
            }

            // 透かしの名前の設定
            watermarkimage.setName("透かしの名前");

            // 透かしを配置するときの余白の設定
            watermarkimage.setMargin(10.0f, 10.0f, 10.0f, 10.0f);

            // 透かしの配置の設定 ALIGN_TOP_LEFT = 1 /* 左上 */
            watermarkimage.setAlign(PtlParamWaterMark.ALIGN.ALIGN_TOP_LEFT);

            // 透かしのZオーダーの設定 ZORDER_FRONT = 1 /* 前面 */
            watermarkimage.setZorder(PtlParamWaterMark.ZORDER.ZORDER_FRONT);

            // 透かしを入れるページの範囲の設定 PAGE_RANGE_ODD = 3 /* 奇数ページ */
            watermarkimage.setPageRange(PtlParamWaterMark.PAGE_RANGE.PAGE_RANGE_ODD);

        	// 透かしの角度の設定
        	watermarkimage.setAngle(-15);

            // 透かしの不透明度の設定
            watermarkimage.setOpacity(0.9f);

            // 透かしをタイリングして配置するかどうかの設定
            watermarkimage.setTiling(false);

            // 透かしの倍率の設定
            watermarkimage.setScale(0.8f);

            // 透かしの設定
            doc.appendWaterMark(watermarkimage);
        }
    }

    public static void appendWatermarkPDF(PtlPDFDocument doc, String pathPdf) throws PtlException, Exception, Error
    {
        try (PtlParamWaterMarkPDF watermarkpdf = new PtlParamWaterMarkPDF())  // 透かしパラメーター
        {
            // 透かしの名前の設定
            watermarkpdf.setName("透かしの名前");

        	// 透かしの配置の設定 ALIGN_BOTTOM_RIGHT = 9 /* 右下 */
            watermarkpdf.setAlign(PtlParamWaterMark.ALIGN.ALIGN_TOP_RIGHT);

            // 透かしのZオーダーの設定 ZORDER_FRONT = 1 /* 前面 */
            watermarkpdf.setZorder(PtlParamWaterMark.ZORDER.ZORDER_FRONT);

        	// 透かしを入れるページの範囲の設定 PAGE_RANGE_EVEN = 4 /* 偶数ページ */
            watermarkpdf.setPageRange(PtlParamWaterMark.PAGE_RANGE.PAGE_RANGE_EVEN);

            // 先頭ページに透かしを配置しない設定の範囲の設定
            watermarkpdf.setNotInFirst(false);

            // 最終ページに透かしを配置しない設定の設定
            watermarkpdf.setNotInLast(false);

            // PDF表示時に透かしを表示する指定の設定
            watermarkpdf.setDisplayWaterMark(true);

            // PDF印刷時に透かしを印刷する指定の設定
            watermarkpdf.setPrintWaterMark(true);
            
        	// 透かしの角度の設定
        	watermarkpdf.setAngle(15);

            // 透かしの不透明度の設定
            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);
        }
    }
}
