/*
	Antenna House PDF Tool API V8.0
	Java Interface sample program

	概要：矩形内で折り返す文字列

	Copyright 2025- Antenna House,Inc.
*/

package Sample;

import jp.co.antenna.ptl.*;

public class DrawTextBox_3 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        if (args.length < 2)
        {
            System.out.println("usage: java DrawTextBox_3 in-pdf-file out-pdf-file");
            return;
        }

        try (PtlParamInput inputFile = new PtlParamInput(args[0]);
             PtlParamOutput outputFile = new PtlParamOutput(args[1]);
             PtlPDFDocument doc = new PtlPDFDocument();
             PtlOption option = new PtlOption())
        {
            option.setUnit(PtlOption.UNIT.UNIT_PT);

            doc.load(inputFile);

            String fontName = "メイリオ";
            float fontSize = 24.0f;

            try (PtlParamFont fontNormal = new PtlParamFont(fontName, fontSize, false, false, true);
                 PtlParamFont fontBoldItalic = new PtlParamFont(fontName, fontSize, true, true, true);
                 PtlColorDeviceRGB colorBlack = new PtlColorDeviceRGB(0.0f, 0.0f, 0.0f);
                 PtlColorDeviceRGB colorRed = new PtlColorDeviceRGB(1.0f, 0.0f, 0.0f);
        		 PtlPages pages = doc.getPages();
                 PtlPage page = pages.get(0);     //1ページ目
                 PtlSize pageSize = page.getSize();
                 PtlContent content = page.getContent();
                 PtlRect rect = new PtlRect(20, 20, pageSize.getWidth() - 20, pageSize.getHeight() - 20);
                 PtlTextBox textBox = content.drawTextBox(rect, PtlContent.ALIGN.ALIGN_TOP_LEFT, 400, 400);)
            {
                // TextBoxの縁取りを付ける
                textBox.setOutlineColor(colorRed);

                // 縁取りがテキストを囲むサイズに変わるよう指定
                textBox.fitToBBox(true);

                try (PtlParamWriteStringTextBox paramWriteString = new PtlParamWriteStringTextBox();)
                {

                    paramWriteString.setFont(fontNormal);
                    paramWriteString.setTextColor(colorBlack);

                    String text1 = "長いテキストは折り返されます。";
                    textBox.writeString(text1, paramWriteString);   // 文字列を出力

                    String text2 = "「これは、私が小さいときに、村の茂平というおじいさんからきいたお話です。むかしは、私たちの村のちかくの、中山というところに小さなお城があって、中山さまというおとのさまが、おられたそうです。その中山から、少しはなれた山の中に、「ごん狐」という狐がいました。」（新美南吉「ごん狐」）";
                    textBox.writeStringNL(text2, paramWriteString); // 文字列を出力して改行

                    textBox.writeNL();  // 改行

                    String text3 = ("英字は途中で折り返されません。TextBoxはPtlParamWriteStringTextBoxを使います。");
                    textBox.writeStringNL(text3, paramWriteString); // 文字列を出力して改行
                    
                }
                textBox.terminate();

                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("-- 完了 --");
        }
	}
}

