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

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

    Copyright 2015-2025 Antenna House, Inc.
*/

package Sample;

import jp.co.antenna.ptl.*;

public class WriteTextAngle {

	public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ
        if (args.length < 2)
        {
            System.out.println("usage: java WriteString 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();

        		for (int i = 0; i < pageno; i++)
        		{
	                // ページの取得
	                try (PtlPage page = pages.get(i))
	                {
	                    // テキスト追加
	                    writeString(page);
	                }
        		}
            }

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

                    // サイズの設定
                    font.setSize(8.0f);

                    // フォントの設定
                    writestring.setFont(font);
                }

                // 文字色設定
                try (PtlColorDeviceRGB color = new PtlColorDeviceRGB(0.5f, 1.0f, 0.8f))
                {
                    writestring.setTextColor(color);
                }

                // 文字列出力
                content.writeString(rect, PtlContent.ALIGN.ALIGN_BOTTOM_LEFT, 330.0f, "アンテナハウス株式会社", writestring);
            }
        }
    }
}
