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

    概要：テキストオブジェクトの挿入

    Copyright 2015-2025 Antenna House, Inc.
*/

package Sample;

import java.time.LocalDateTime;

import jp.co.antenna.ptl.*;

public class WriteTextPageNo {

	public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ
        if (args.length < 2)
        {
            System.out.println("usage: java WriteTextPageNo 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;
                }
                
                int pageno = pages.getCount();
                
                // 現在日時取得
                LocalDateTime nowDate = LocalDateTime.now();
                int year = nowDate.getYear();
                int month = nowDate.getMonthValue();
                int day = nowDate.getDayOfMonth();

                for (int i = 0; i < pageno; i++)
                {
                    // ページの取得
                    try (PtlPage page = pages.get(i))
                    {
                    	String str = "実行日：" + year + "/" + month + "/" + day + "  ページ番号：" + (i + 1) + "/ " + pageno;
                        // テキスト追加
                        writeString(page, str);
                    }
                }
            }

            // ファイルに保存します。
            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 writeString(PtlPage page, String str) throws PtlException, Exception, Error
    {
        // ページコンテントの取得
        try (PtlContent content = page.getContent())
        {
            PtlSize pageSize = page.getSize();
            // 文字列出力
            try (PtlRect rect1 = new PtlRect(0.0f, 0.0f, pageSize.getWidth(),pageSize.getHeight());      // 出力矩形
                 PtlParamWriteString writestring1 = new PtlParamWriteString()) // 文字の描画に使うパラメータクラス
            {
                // フォント指定に使うパラメータクラス
                try (PtlParamFont font1 = new PtlParamFont())
                {
                    // フォント名の設定
                    font1.setName("游ゴシック");

                    // サイズの設定
                    font1.setSize(12.0f);

                    // フォントの設定
                    writestring1.setFont(font1);
                }

                // 文字色設定
                writestring1.setTextColor(new PtlColorDeviceRGB(1.0f, 0.0f, 0.0f));
                writestring1.setOutlineColor(new PtlColorDeviceRGB(0.0f, 0.0f, 1.0f));

                // 文字列出力
                content.writeString(rect1, PtlContent.ALIGN.ALIGN_TOP_RIGHT, str, writestring1);
            }

        }
    }
}
