/*
	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_4 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        if (args.length < 1)
        {
            System.out.println("usage: java DrawTextBox_4 out-pdf-file");
            return;
        }

        try (PtlParamOutput outputFile = new PtlParamOutput(args[0]);
             PtlPDFDocument doc = new PtlPDFDocument();
             PtlOption option = new PtlOption())
        {
            option.setUnit(PtlOption.UNIT.UNIT_PT);

            String fontName = "メイリオ";
            float fontSize = 24.0f;

            try (PtlParamFont fontNormal = new PtlParamFont(fontName, fontSize, false, false, true);
                PtlColorDeviceRGB colorBlack = new PtlColorDeviceRGB(0.0f, 0.0f, 0.0f);
        		PtlPages pages = doc.getPages();
        		PtlPage newpage = new PtlPage();
                PtlSize pageSize = newpage.getSize();)
            {
            	pages.append(newpage, PtlPages.OPTION_NONE);  // 新規ページの追加
            	
                try (PtlContent content = newpage.getContent();
            		PtlRect rect = new PtlRect(0, 0, pageSize.getWidth(), pageSize.getHeight());
                    PtlTextBox textBox = content.drawTextBox(rect, PtlContent.ALIGN.ALIGN_TOP_LEFT, pageSize.getWidth(), pageSize.getHeight());
            		PtlParamWriteStringTextBox paramWriteString = new PtlParamWriteStringTextBox();)
                {
                    paramWriteString.setFont(fontNormal);
                    paramWriteString.setTextColor(colorBlack);

                    String text1 = "そのあくる日もごんは、栗をもって、兵十の家へ出かけました。兵十は物置で縄《なわ》をなっていました。それでごんは家の裏口から、こっそり中へはいりました。そのとき兵十は、ふと顔をあげました。と狐が家の中へはいったではありませんか。こないだうなぎをぬすみやがったあのごん狐めが、またいたずらをしに来たな。「ようし。」兵十は立ちあがって、納屋《なや》にかけてある火縄銃《ひなわじゅう》をとって、火薬をつめました。そして足音をしのばせてちかよって、今戸口を出ようとするごんを、ドンと、うちました。ごんは、ばたりとたおれました。兵十はかけよって来ました。家の中を見ると、土間《どま》に栗が、かためておいてあるのが目につきました。「おや」と兵十は、びっくりしてごんに目を落しました。「ごん、お前だったのか。いつも栗をくれたのは」ごんは、ぐったりと目をつぶったまま、うなずきました。兵十は火縄銃をばたりと、とり落しました。青い煙が、まだ筒口から細く出ていました。（新美南吉「ごん狐」）";
                    textBox.writeStringNL(text1, 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("-- 完了 --");
        }
	}
}

