/*
    Antenna House PDF Tool API V7.0
    Java Interface sample program

    概要：スタンプ注釈の作成

    Copyright 2015-2021 Antenna House, Inc.
*/

package Sample;

import java.io.*;
import jp.co.antenna.ptl.*;

public class AppendAnnotStamp {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        if (args.length < 3)
        {
            System.out.println("usage: java AppendAnnotStamp in-pdf-file out-pdf-file スタンプ種類 (custom-icon-pdf-file)\n");
            System.out.println("スタンプ種類\n0 : 標準  1 : 標準2  2 : カスタム(PDF)  3 : カスタム(Image)  4 : カスタム(日付印)");
            return;
        }

        String stampKind = args[2];
        switch (stampKind) {
        case "0":
        case "1":
        case "2":
        case "3":
        case "4":
            break;
        default:
            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);
                     PtlAnnots annots = page.getAnnots())
                {
                    switch (stampKind)
                    {
                    case "0":
                        // 標準
                        addPreDefinedStampAnnot(annots);
                        break;
                    case "1":
                        // 標準2
                        addPreDefinedStampAnnot2(annots);
                        break;
                    case "2":
                        // 読み込んだPDFを外観とするスタンプ
                        if (args.length < 4)
                        {
                            System.out.println("usage: java AppendAnnotStamp in-pdf-file out-pdf-file スタンプ種類 in-pdf-file");
                            return;
                        }
                        addCustomStampAnnotFromPdf(annots, args[3]);
                        break;
                    case "3":
                        // 読み込んだ画像を外観とするスタンプ
                        if (args.length < 4)
                        {
                            System.out.println("usage: java AppendAnnotStamp in-pdf-file out-pdf-file スタンプ種類 in-image-file");
                            return;
                        }
                        addCustomStampAnnotFromImage(annots, args[3]);
                        break;
                    case "4":
                        // 日付印を外観として描画
                        addCustomStampAnnotFromDrawContent(annots);
                        break;
                    }
                }
            }

            // ファイルに保存します。
            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 addPreDefinedStampAnnot(PtlAnnots annots) throws PtlException, Exception, Error
    {
        try (PtlAnnotStamp stampAnnot1 = new PtlAnnotStamp()) {
            stampAnnot1.setIconType(PtlAnnotStamp.ICON_TYPE.ICON_APPROVED);
            try(PtlRect rectSize = stampAnnot1.getRect();
                PtlRect rect = new PtlRect(10.0f, 270.0f, 10.0f + rectSize.getRight(), 270.0f + rectSize.getTop())){
                stampAnnot1.setRect(rect);
            }
            stampAnnot1.setAnnotFlags(PtlAnnot.FLAG_PRINT);
            annots.append(stampAnnot1);
        }

        try (PtlAnnotStamp stampAnnot2 = new PtlAnnotStamp()) {
            stampAnnot2.setIconType(PtlAnnotStamp.ICON_TYPE.ICON_AS_IS);
        	try(PtlRect rectSize = stampAnnot2.getRect();
        		PtlRect rect = new PtlRect(10.0f, 250.0f, 10.0f + rectSize.getRight(), 250.0f + rectSize.getTop())){
            	stampAnnot2.setRect(rect);
        	}
            stampAnnot2.setAnnotFlags(PtlAnnot.FLAG_PRINT);
            annots.append(stampAnnot2);
        }

        try (PtlAnnotStamp stampAnnot3 = new PtlAnnotStamp()) {
            stampAnnot3.setIconType(PtlAnnotStamp.ICON_TYPE.ICON_CONFIDENTIAL);
        	try(PtlRect rectSize = stampAnnot3.getRect();
        		PtlRect rect = new PtlRect(10.0f, 230.0f, 10.0f + rectSize.getRight(), 230.0f + rectSize.getTop())){
        		stampAnnot3.setRect(rect);
        	}
            stampAnnot3.setAnnotFlags(PtlAnnot.FLAG_PRINT);
            annots.append(stampAnnot3);
        }

        try (PtlAnnotStamp stampAnnot4 = new PtlAnnotStamp()) {
            stampAnnot4.setIconType(PtlAnnotStamp.ICON_TYPE.ICON_DEPARTMENTAL);
        	try(PtlRect rectSize = stampAnnot4.getRect();
        		PtlRect rect = new PtlRect(10.0f, 210.0f, 10.0f + rectSize.getRight(), 210.0f + rectSize.getTop())){
        		stampAnnot4.setRect(rect);
        	}
            stampAnnot4.setAnnotFlags(PtlAnnot.FLAG_PRINT);
            annots.append(stampAnnot4);
        }

        try (PtlAnnotStamp stampAnnot5 = new PtlAnnotStamp()) {
            stampAnnot5.setIconType(PtlAnnotStamp.ICON_TYPE.ICON_DRAFT);
        	try(PtlRect rectSize = stampAnnot5.getRect();
        		PtlRect rect = new PtlRect(10.0f, 190.0f, 10.0f + rectSize.getRight(), 190.0f + rectSize.getTop())){
        		stampAnnot5.setRect(rect);
        	}
            stampAnnot5.setAnnotFlags(PtlAnnot.FLAG_PRINT);
            annots.append(stampAnnot5);
        }

        try (PtlAnnotStamp stampAnnot6 = new PtlAnnotStamp()) {
            stampAnnot6.setIconType(PtlAnnotStamp.ICON_TYPE.ICON_EXPERIMENTAL);
        	try(PtlRect rectSize = stampAnnot6.getRect();
        		PtlRect rect = new PtlRect(10.0f, 170.0f, 10.0f + rectSize.getRight(), 170.0f + rectSize.getTop())){
        		stampAnnot6.setRect(rect);
        	}
            stampAnnot6.setAnnotFlags(PtlAnnot.FLAG_PRINT);
            annots.append(stampAnnot6);
        }

        try (PtlAnnotStamp stampAnnot7 = new PtlAnnotStamp()) {
            stampAnnot7.setIconType(PtlAnnotStamp.ICON_TYPE.ICON_EXPIRED);
            try(PtlRect rectSize = stampAnnot7.getRect();
        		PtlRect rect = new PtlRect(10.0f, 150.0f, 10.0f + rectSize.getRight(), 150.0f + rectSize.getTop())){
        		stampAnnot7.setRect(rect);
        	}
            stampAnnot7.setAnnotFlags(PtlAnnot.FLAG_PRINT);
            annots.append(stampAnnot7);
        }

        try (PtlAnnotStamp stampAnnot8 = new PtlAnnotStamp()) {
            stampAnnot8.setIconType(PtlAnnotStamp.ICON_TYPE.ICON_FINAL);
            try(PtlRect rectSize = stampAnnot8.getRect();
        		PtlRect rect = new PtlRect(10.0f, 130.0f, 10.0f + rectSize.getRight(), 130.0f + rectSize.getTop())){
        		stampAnnot8.setRect(rect);
        	}
            stampAnnot8.setAnnotFlags(PtlAnnot.FLAG_PRINT);
            annots.append(stampAnnot8);
        }

        try (PtlAnnotStamp stampAnnot9 = new PtlAnnotStamp()) {
            stampAnnot9.setIconType(PtlAnnotStamp.ICON_TYPE.ICON_FOR_COMMENT);
            try(PtlRect rectSize = stampAnnot9.getRect();
        		PtlRect rect = new PtlRect(10.0f, 110.0f, 10.0f + rectSize.getRight(), 110.0f + rectSize.getTop())){
        		stampAnnot9.setRect(rect);
        	}
            stampAnnot9.setAnnotFlags(PtlAnnot.FLAG_PRINT);
            annots.append(stampAnnot9);
        }

        try (PtlAnnotStamp stampAnnot10 = new PtlAnnotStamp()) {
            stampAnnot10.setIconType(PtlAnnotStamp.ICON_TYPE.ICON_FOR_PUBLIC_RELEASE);
            try(PtlRect rectSize = stampAnnot10.getRect();
        		PtlRect rect = new PtlRect(10.0f, 90.0f, 10.0f + rectSize.getRight(), 90.0f + rectSize.getTop())){
        		stampAnnot10.setRect(rect);
        	}
            stampAnnot10.setAnnotFlags(PtlAnnot.FLAG_PRINT);
            annots.append(stampAnnot10);
        }

        try (PtlAnnotStamp stampAnnot11 = new PtlAnnotStamp()) {
            stampAnnot11.setIconType(PtlAnnotStamp.ICON_TYPE.ICON_NOT_APPROVED);
            try(PtlRect rectSize = stampAnnot11.getRect();
        		PtlRect rect = new PtlRect(10.0f, 70.0f, 10.0f + rectSize.getRight(), 70.0f + rectSize.getTop())){
        		stampAnnot11.setRect(rect);
        	}
            stampAnnot11.setAnnotFlags(PtlAnnot.FLAG_PRINT);
            annots.append(stampAnnot11);
        }

        try (PtlAnnotStamp stampAnnot12 = new PtlAnnotStamp()) {
            stampAnnot12.setIconType(PtlAnnotStamp.ICON_TYPE.ICON_NOT_FOR_PUBLIC_RELEASE);
            try(PtlRect rectSize = stampAnnot12.getRect();
        		PtlRect rect = new PtlRect(10.0f, 50.0f, 10.0f + rectSize.getRight(), 50.0f + rectSize.getTop())){
        		stampAnnot12.setRect(rect);
        	}
            stampAnnot12.setAnnotFlags(PtlAnnot.FLAG_PRINT);
            annots.append(stampAnnot12);
        }

        try (PtlAnnotStamp stampAnnot13 = new PtlAnnotStamp()) {
            stampAnnot13.setIconType(PtlAnnotStamp.ICON_TYPE.ICON_SOLD);
            try(PtlRect rectSize = stampAnnot13.getRect();
        		PtlRect rect = new PtlRect(10.0f, 30.0f, 10.0f + rectSize.getRight(), 30.0f + rectSize.getTop())){
        		stampAnnot13.setRect(rect);
        	}
            stampAnnot13.setAnnotFlags(PtlAnnot.FLAG_PRINT);
            annots.append(stampAnnot13);
        }

        try (PtlAnnotStamp stampAnnot14 = new PtlAnnotStamp()) {
            stampAnnot14.setIconType(PtlAnnotStamp.ICON_TYPE.ICON_TOP_SECRET);
            try(PtlRect rectSize = stampAnnot14.getRect();
        		PtlRect rect = new PtlRect(10.0f, 10.0f, 10.0f + rectSize.getRight(), 10.0f + rectSize.getTop())){
        		stampAnnot14.setRect(rect);
        	}
            stampAnnot14.setAnnotFlags(PtlAnnot.FLAG_PRINT);
            annots.append(stampAnnot14);
        }
    }

    public static void addPreDefinedStampAnnot2(PtlAnnots annots) throws PtlException, Exception, Error
    {
        try (PtlAnnotStamp stampAnnot1 = new PtlAnnotStamp()) {
            stampAnnot1.setIconType(PtlAnnotStamp.ICON_TYPE.ICON_SB_APPROVED);
        	try(PtlRect rectSize = stampAnnot1.getRect();
        		PtlRect rect = new PtlRect(10.0f, 270.0f, 10.0f + rectSize.getRight(), 270.0f + rectSize.getTop())){
        		stampAnnot1.setRect(rect);
        	}
            stampAnnot1.setAnnotFlags(PtlAnnot.FLAG_PRINT);
            annots.append(stampAnnot1);
        }

        try (PtlAnnotStamp stampAnnot2 = new PtlAnnotStamp()) {
            stampAnnot2.setIconType(PtlAnnotStamp.ICON_TYPE.ICON_SB_COMPLETED);
            try(PtlRect rectSize = stampAnnot2.getRect();
        		PtlRect rect = new PtlRect(10.0f, 250.0f, 10.0f + rectSize.getRight(), 250.0f + rectSize.getTop())){
        		stampAnnot2.setRect(rect);
        	}
            stampAnnot2.setAnnotFlags(PtlAnnot.FLAG_PRINT);
            annots.append(stampAnnot2);
        }

        try (PtlAnnotStamp stampAnnot3 = new PtlAnnotStamp();) {
            stampAnnot3.setIconType(PtlAnnotStamp.ICON_TYPE.ICON_SB_CONFIDENTIAL);
            try(PtlRect rectSize = stampAnnot3.getRect();
        		PtlRect rect = new PtlRect(10.0f, 230.0f, 10.0f + rectSize.getRight(), 230.0f + rectSize.getTop())){
        		stampAnnot3.setRect(rect);
        	}
            stampAnnot3.setAnnotFlags(PtlAnnot.FLAG_PRINT);
            annots.append(stampAnnot3);
        }

        try (PtlAnnotStamp stampAnnot4 = new PtlAnnotStamp()) {
            stampAnnot4.setIconType(PtlAnnotStamp.ICON_TYPE.ICON_SB_DRAFT);
            try(PtlRect rectSize = stampAnnot4.getRect();
        		PtlRect rect = new PtlRect(10.0f, 210.0f, 10.0f + rectSize.getRight(), 210.0f + rectSize.getTop())){
        		stampAnnot4.setRect(rect);
        	}
            stampAnnot4.setAnnotFlags(PtlAnnot.FLAG_PRINT);
            annots.append(stampAnnot4);
        }

        try (PtlAnnotStamp stampAnnot5 = new PtlAnnotStamp()) {
            stampAnnot5.setIconType(PtlAnnotStamp.ICON_TYPE.ICON_SB_FINAL);
            try(PtlRect rectSize = stampAnnot5.getRect();
        		PtlRect rect = new PtlRect(10.0f, 190.0f, 10.0f + rectSize.getRight(), 190.0f + rectSize.getTop())){
        		stampAnnot5.setRect(rect);
        	}
            stampAnnot5.setAnnotFlags(PtlAnnot.FLAG_PRINT);
            annots.append(stampAnnot5);
        }

        try (PtlAnnotStamp stampAnnot6 = new PtlAnnotStamp()) {
            stampAnnot6.setIconType(PtlAnnotStamp.ICON_TYPE.ICON_SB_FOR_COMMENT);
            try(PtlRect rectSize = stampAnnot6.getRect();
        		PtlRect rect = new PtlRect(10.0f, 170.0f, 10.0f + rectSize.getRight(), 170.0f + rectSize.getTop())){
        		stampAnnot6.setRect(rect);
        	}
            stampAnnot6.setAnnotFlags(PtlAnnot.FLAG_PRINT);
            annots.append(stampAnnot6);
        }

        try (PtlAnnotStamp stampAnnot7 = new PtlAnnotStamp()) {
            stampAnnot7.setIconType(PtlAnnotStamp.ICON_TYPE.ICON_SB_FOR_PUBLIC_RELEASE);
            try(PtlRect rectSize = stampAnnot7.getRect();
        		PtlRect rect = new PtlRect(10.0f, 150.0f, 10.0f + rectSize.getRight(), 150.0f + rectSize.getTop())){
        		stampAnnot7.setRect(rect);
        	}
            stampAnnot7.setAnnotFlags(PtlAnnot.FLAG_PRINT);
            annots.append(stampAnnot7);
        }

        try (PtlAnnotStamp stampAnnot8 = new PtlAnnotStamp()) {
            stampAnnot8.setIconType(PtlAnnotStamp.ICON_TYPE.ICON_SB_INFORMATIONONLY);
            try(PtlRect rectSize = stampAnnot8.getRect();
        		PtlRect rect = new PtlRect(10.0f, 130.0f, 10.0f + rectSize.getRight(), 130.0f + rectSize.getTop())){
        		stampAnnot8.setRect(rect);
        	}
            stampAnnot8.setAnnotFlags(PtlAnnot.FLAG_PRINT);
            annots.append(stampAnnot8);
        }

        try (PtlAnnotStamp stampAnnot9 = new PtlAnnotStamp()) {
            stampAnnot9.setIconType(PtlAnnotStamp.ICON_TYPE.ICON_SB_NOT_APPROVED);
            try(PtlRect rectSize = stampAnnot9.getRect();
        		PtlRect rect = new PtlRect(10.0f, 110.0f, 10.0f + rectSize.getRight(), 110.0f + rectSize.getTop())){
        		stampAnnot9.setRect(rect);
        	}
            stampAnnot9.setAnnotFlags(PtlAnnot.FLAG_PRINT);
            annots.append(stampAnnot9);
        }

        try (PtlAnnotStamp stampAnnot10 = new PtlAnnotStamp()) {
            stampAnnot10.setIconType(PtlAnnotStamp.ICON_TYPE.ICON_SB_NOT_FOR_PUBLIC_RELEASE);
            try(PtlRect rectSize = stampAnnot10.getRect();
        		PtlRect rect = new PtlRect(10.0f, 90.0f, 10.0f + rectSize.getRight(), 90.0f + rectSize.getTop())){
        		stampAnnot10.setRect(rect);
        	}
            stampAnnot10.setAnnotFlags(PtlAnnot.FLAG_PRINT);
            annots.append(stampAnnot10);
        }

        try (PtlAnnotStamp stampAnnot11 = new PtlAnnotStamp()) {
            stampAnnot11.setIconType(PtlAnnotStamp.ICON_TYPE.ICON_SB_PRELIMINARYRESULTS);
            try(PtlRect rectSize = stampAnnot11.getRect();
        		PtlRect rect = new PtlRect(10.0f, 70.0f, 10.0f + rectSize.getRight(), 70.0f + rectSize.getTop())){
        		stampAnnot11.setRect(rect);
        	}
            stampAnnot11.setAnnotFlags(PtlAnnot.FLAG_PRINT);
            annots.append(stampAnnot11);
        }

        try (PtlAnnotStamp stampAnnot12 = new PtlAnnotStamp()) {
            stampAnnot12.setIconType(PtlAnnotStamp.ICON_TYPE.ICON_SB_VOID);
            try(PtlRect rectSize = stampAnnot12.getRect();
        		PtlRect rect = new PtlRect(10.0f, 50.0f, 10.0f + rectSize.getRight(), 50.0f + rectSize.getTop())){
        		stampAnnot12.setRect(rect);
        	}
            stampAnnot12.setAnnotFlags(PtlAnnot.FLAG_PRINT);
            annots.append(stampAnnot12);
        }
    }

    public static void addCustomStampAnnotFromPdf(PtlAnnots annots, String pathPdf) throws PtlException, Exception, Error
    {
        try (PtlAnnotStamp stampAnnot = new PtlAnnotStamp();
             PtlPDFDocument doc_custom = new PtlPDFDocument();
             PtlParamInput inputCustom = new PtlParamInput(pathPdf))     // PDFファイル
        {
            stampAnnot.setIconType(PtlAnnotStamp.ICON_TYPE.ICON_CUSTOM);
            stampAnnot.setAnnotFlags(PtlAnnot.FLAG_PRINT);

            // スタンプにするPDFファイルをロードします。
            doc_custom.load(inputCustom);

            try (PtlPages pagesCustomStamp = doc_custom.getPages())
            {
                // ページコンテナが空かどうか
                if (pagesCustomStamp.isEmpty())
                {
                    System.out.println("ページコンテナが空\n");
                    return;
                }

                // 追加された画像ページを取得する
                try (PtlPage pageCustomStamp = pagesCustomStamp.get(0)) {
                    // 画像ページを注釈に追加する
                    stampAnnot.setPage(pageCustomStamp);

                    try (PtlSize size = pageCustomStamp.getSize();
                         PtlRect rect = new PtlRect(10.0f, 100.0f, 10.0f+size.getWidth(), 100.0f+size.getHeight()))
                    {
                        stampAnnot.setRect(rect);
                    }
                }
            }

            stampAnnot.setIconName("MyIcon");

            annots.append(stampAnnot);
        }
    }

    public static void addCustomStampAnnotFromImage(PtlAnnots annots, String pathImage) throws PtlException, Exception, Error
    {
        try (PtlAnnotStamp stampAnnot = new PtlAnnotStamp()) {

            stampAnnot.setIconType(PtlAnnotStamp.ICON_TYPE.ICON_CUSTOM);
            stampAnnot.setAnnotFlags(PtlAnnot.FLAG_PRINT);

            try (PtlParamDrawImage paramDrawImage = new PtlParamDrawImage();   // 画像描画パラメータ
                 PtlParamInput inputCustom = new PtlParamInput(pathImage);     // 画像ファイル
                 PtlParamImagePage paramImagePage = new PtlParamImagePage())   // 画像ページパラメータ
            {
                // 画像描画パラメータに画像ファイルを設定
                paramDrawImage.setImageStream(inputCustom);

                // 画像ページパラメータに画像描画パラメータを設定
                paramImagePage.setImage(paramDrawImage);

                // 画像ページのサイズを画像サイズにあわせる
                paramImagePage.setPaperType(PtlParamImagePage.PAPER_TYPE.PAPER_IMAGE_SIZE);

                // 画像ページパラメータから作成したページ
                try (PtlPage pageCustomStamp = new PtlPage(paramImagePage))
                {
                    // 画像ページを注釈に追加する
                    stampAnnot.setPage(pageCustomStamp);

                    try (PtlSize size = pageCustomStamp.getSize();
                         PtlRect rect = new PtlRect(10.0f, 100.0f, 10.0f+size.getWidth(), 100.0f+size.getHeight()))
                    {
                        stampAnnot.setRect(rect);
                    }
                }
            }

            stampAnnot.setIconName("MyIcon");

            annots.append(stampAnnot);
        }
    }

    public static void addCustomStampAnnotFromDrawContent(PtlAnnots annots) throws PtlException, Exception, Error
    {
        float sizeStamp = 30.0f;
        try (PtlAnnotStamp stampAnnot = new PtlAnnotStamp();
             PtlRect rect = new PtlRect(10.0f, 100.0f, 10.0f + sizeStamp, 100.0f + sizeStamp))
        {
            stampAnnot.setRect(rect);
            stampAnnot.setIconType(PtlAnnotStamp.ICON_TYPE.ICON_CUSTOM);
            stampAnnot.setAnnotFlags(PtlAnnot.FLAG_PRINT);

            try (PtlPage pageCustomStamp = new PtlPage();
                 PtlRect rectMedia = new PtlRect(0.0f, 0.0f, sizeStamp, sizeStamp);
                 PtlContent content = pageCustomStamp.getContent())
            {
                // ページサイズ
                pageCustomStamp.setMediaBox(rectMedia);

                // コンテントに日付印を描画
                drawDateStamp(content, sizeStamp, "2014/09/8", "鈴木");

                // 画像ページを注釈に追加する
                stampAnnot.setPage(pageCustomStamp);
            }

            stampAnnot.setIconName("MyIcon");

            annots.append(stampAnnot);
        }
    }

    public static void drawDateStamp(PtlContent content, float sizeStamp, String textDate, String textName) throws PtlException, Exception, Error
    {
        try (PtlParamDrawShape paramDrawShape = new PtlParamDrawShape();
             PtlColorDeviceRGB color = new PtlColorDeviceRGB(1.0f, 0.0f, 0.0f);   // スタンプの色
             PtlColorNone colorNone = new PtlColorNone();
             PtlRect rectStamp = new PtlRect(0.0f, 0.0f, sizeStamp, sizeStamp))
        {
            // スタンプの円/線を描画するパラメーター
            paramDrawShape.setLineStyle(PtlParamDrawShape.LINE_STYLE.LINE_STYLE_SOLID);
            paramDrawShape.setLineWidth(PtlParamDrawShape.LINE_WIDTH.LINE_WIDTH_THIN);
            paramDrawShape.setLineColor(color);
            paramDrawShape.setFillColor(colorNone);

            // スタンプの円を描画
            content.drawCircle(rectStamp, paramDrawShape);

            // 円の中心から15度で線と円が交わるとする
            double rad = 15.0f * Math.PI / 180.0f;
            // 円の半径
            float r = sizeStamp / 2;
            // 円の中心から交点へのx方向の長さ
            float xx = r * (float)Math.cos(rad);
            // 円の中心から交点へのy方向の長さ
            float yy = r * (float)Math.sin(rad);
            // 上線左端のx座標
            float from_x = r - xx;
            // 上線左端のy座標
            float to_x = r + xx;
            // 下線左端のx座標
            float upper_y = r + yy;
            // 下線左端のy座標
            float lower_y = r - yy;

            try (PtlPoint posFrom1 = new PtlPoint(from_x, upper_y);
                 PtlPoint posTo1 = new PtlPoint(to_x, upper_y);
                 PtlPoint posFrom2 = new PtlPoint(from_x, lower_y);
                 PtlPoint posTo2 = new PtlPoint(to_x, lower_y))
            {
                // 円の中に上線を描画
                content.drawLine(posFrom1, posTo1, paramDrawShape);
                // 円の中に下線を描画
                content.drawLine(posFrom2, posTo2, paramDrawShape);
            }

            // 日付のフォントサイズ
            float fontSizeDate = sizeStamp / 2.15f;    // yyyy/mm/dd を想定

            try (PtlParamWriteString paramWriteStringDate = new PtlParamWriteString();
                 PtlRect rectDate = new PtlRect(from_x, lower_y, to_x, upper_y);
                 PtlParamFont font = new PtlParamFont("MS-Gothic", fontSizeDate, PtlParamFont.WEIGHT.WEIGHT_NORMAL, false, true))
            {
                paramWriteStringDate.setFont(font);
                paramWriteStringDate.setTextColor(color);
                paramWriteStringDate.setOutlineColor(color);

                // 日付を描画
                content.writeString(rectDate, PtlContent.ALIGN.ALIGN_CENTER, textDate, paramWriteStringDate);
            }

            // 下線から名前を描画する矩形までのマージン
            float margin =sizeStamp / 40.0f;

            // 名前のフォントサイズ
            float fontSizeName = sizeStamp / 1.5f;

            try (PtlParamWriteString paramWriteStringName = new PtlParamWriteString();
                 PtlRect rectName = new PtlRect(from_x, lower_y - (upper_y - lower_y) - margin, to_x, lower_y - margin);
                 PtlParamFont font = new PtlParamFont("MS-Mincho", fontSizeDate, PtlParamFont.WEIGHT.WEIGHT_NORMAL, false, true))
            {
                // 名前を書く矩形の高さ
                float heightNameBase = upper_y - lower_y;
                float heightName = fontSizeName * 25.4f / 72.0f;
                while (heightName > heightNameBase)
                {
                    fontSizeName -= 0.5f;
                    heightName = fontSizeName * 25.4f / 72.0f;
                }

                // 線の幅
                float widthLine = 0.5f;
                // 線幅を減じた円の半径
                float rr = r - widthLine;
                // 名前の高さのラジアン
                float d = (float)Math.asin(((heightName + yy + margin) / rr));
                // 名前をおさめる矩形の幅
                float widthName = rr * (float)Math.cos(d) * 2;

                // テキストの長さ
                float textWidthName = font.getStringWidth(textName);

                // 名前の長さによるフォントサイズの調整
                while (textWidthName > widthName)
                {
                    fontSizeName -= 0.5f;
                    font.setSize(fontSizeName);
                    textWidthName = font.getStringWidth(textName);
                    heightName = fontSizeName * 25.4f / 72.0f;
                    d = (float)Math.asin((heightName + yy + margin) / rr);
                    widthName = rr * (float)Math.cos(d) * 2;
                }

                paramWriteStringName.setFont(font);
                paramWriteStringName.setTextColor(color);
                paramWriteStringName.setOutlineColor(color);

                // 名前を描画
                content.writeString(rectName, PtlContent.ALIGN.ALIGN_CENTER, textName, paramWriteStringName);
            }
        }
    }
}
