/*
    Antenna House PDF Tool API V8.0
    Java Interface sample program

    概要：テキストオブジェクトの挿入

    Copyright 2015-2025 Antenna House, Inc.
*/

package Sample;

import jp.co.antenna.ptl.PtlColorDeviceRGB;
import jp.co.antenna.ptl.PtlContent;
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.PtlParamFont;
import jp.co.antenna.ptl.PtlParamInput;
import jp.co.antenna.ptl.PtlParamOutput;
import jp.co.antenna.ptl.PtlParamWriteString;
import jp.co.antenna.ptl.PtlRect;

public class WriteString {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        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;
                }

                // ページの取得
                try (PtlPage page = pages.get(0))
                {
                    // テキスト追加
                    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())
        {
            // 絵文字列出力■■■
            try (PtlRect rect0 = new PtlRect(0.0f, 250.0f, 300.0f, 500.0f);      // 出力矩形
                 PtlParamWriteString writestring0 = new PtlParamWriteString()) // 文字の描画に使うパラメータクラス
            {
                // フォント指定に使うパラメータクラス
                try (PtlParamFont font0 = new PtlParamFont())
                {
                    // フォント名の設定
                    font0.setName("Segoe UI Emoji");

                    // サイズの設定
                    font0.setSize(80.0f);

                    // フォントの設定
                    writestring0.setFont(font0);
                }

                // 文字色設定
                try (PtlColorDeviceRGB color0 = new PtlColorDeviceRGB(1.0f, 0.0f, 0.0f))
                {
                    writestring0.setTextColor(color0);
                }

                // 文字列出力
                content.writeString(rect0, PtlContent.ALIGN.ALIGN_CENTER, "🌝🌞🐄🐈🐉", writestring0);
            }
            
            // 文字列出力
            try (PtlRect rect1 = new PtlRect(0.0f, 0.0f, 300.0f, 250.0f);      // 出力矩形
                 PtlParamWriteString writestring1 = new PtlParamWriteString()) // 文字の描画に使うパラメータクラス
            {
                // フォント指定に使うパラメータクラス
                try (PtlParamFont font1 = new PtlParamFont())
                {
                    // フォント名の設定
                    font1.setName("ＭＳ明朝");

                    // サイズの設定
                    font1.setSize(80.0f);

                    // フォントの設定
                    writestring1.setFont(font1);
                }

                // 文字色設定
                try (PtlColorDeviceRGB color1 = new PtlColorDeviceRGB(1.0f, 0.0f, 0.0f))
                {
                    writestring1.setTextColor(color1);
                }

                // 文字列出力
                content.writeString(rect1, PtlContent.ALIGN.ALIGN_CENTER, "writeString Test 1", writestring1);
            }

            // 文字列回転出力
            try (PtlRect rect2 = new PtlRect(0.0f, 0.0f, 300.0f, 250.0f);      // 出力矩形
                 PtlParamWriteString writestring2 = new PtlParamWriteString()) // 文字の描画に使うパラメータクラス
            {
                // フォント指定に使うパラメータクラス
                try (PtlParamFont font2 = new PtlParamFont())
                {
                    // フォント名の設定
                    font2.setName("MS-Gothic");

                    // サイズの設定
                    font2.setSize(40.0f);

                    // フォントの設定
                    writestring2.setFont(font2);
                }

                // 文字色設定
                try (PtlColorDeviceRGB color2 = new PtlColorDeviceRGB(0.0f, 1.0f, 0.0f))
                {
                    writestring2.setTextColor(color2);
                }

                // 文字列出力
                content.writeString(rect2, PtlContent.ALIGN.ALIGN_BOTTOM_LEFT, 45.0f, "writeString Test 2", writestring2);
            }

            // 縦書きの文字列出力
            try (PtlRect rect3 = new PtlRect(0.0f, 0.0f, 300.0f, 200.0f);    // 出力矩形
                 PtlParamWriteString writestring3 = new PtlParamWriteString()) // 文字の描画に使うパラメータクラス
            {
                // フォント指定に使うパラメータクラス
                try (PtlParamFont font3 = new PtlParamFont())
                {
                    // フォント名の設定
                    font3.setName("MSP-Mincho");

                    // サイズの設定
                    font3.setSize(60.0f);

                    // ウエイトの設定
                    font3.setWeight(PtlParamFont.WEIGHT.WEIGHT_HEAVY);

                    // フォントの設定
                    writestring3.setFont(font3);
                }

                // 文字色設定
                try (PtlColorDeviceRGB color3 = new PtlColorDeviceRGB(0.0f, 0.0f, 1.0f))
                {
                    writestring3.setTextColor(color3);
                }

                // 文字列出力
                content.writeStringV(rect3, PtlContent.ALIGN.ALIGN_TOP_LEFT, "writeString Test 3", writestring3);
            }
        }
    }
}
