/*
    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 AppendAnnotCoordinateSample {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        if (args.length < 2)
        {
            System.out.println("usage: java AppendAnnotCoordinateSample 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();)
            {
            	PtlPage page = pages.get(0);

                try (PtlAnnots annots = page.getAnnots())
                {
                    try (PtlQuadPoint quad = new PtlQuadPoint())
                    {
                        boolean verticalWriting = false;

                        // 「編集、共有、印刷が行える操作説明書」
                        try (PtlQuadPoint quad1 = new PtlQuadPoint())
                        {
                            // left bottom right top
                            // 26, 234, 133, 243
                            quad1.setTopLeft(new PtlPoint(26, 243));
                            quad1.setTopRight(new PtlPoint(133, 243));
                            quad1.setBottomLeft(new PtlPoint(26, 234));
                            quad1.setBottomRight(new PtlPoint(133, 234));

                        	// 波状下線釈追加
                           addAnnotSquiggly(annots, quad1, verticalWriting);
                        }

                        // 「実際に操作してみましょう」
                       try (PtlQuadPoint quad2 = new PtlQuadPoint())
                       {
                           // left bottom right top
                           // 136, 189, 179, 195
                           quad2.setTopLeft(new PtlPoint(136, 195));
                           quad2.setTopRight(new PtlPoint(179, 195));
                           quad2.setBottomLeft(new PtlPoint(136, 189));
                           quad2.setBottomRight(new PtlPoint(179, 189));

	                       // 下線注釈
	                       addAnnotUnderline(annots, quad2, verticalWriting);
                       }

                       // 「ほんの少しのヘルプで雄弁に文章を書く」
                       try (PtlQuadPoint quad3 = new PtlQuadPoint())
                       {
                           // left bottom right top
                           // 24, 108, 167, 116
                           quad3.setTopLeft(new PtlPoint(24, 116));
                           quad3.setTopRight(new PtlPoint(167, 116));
                           quad3.setBottomLeft(new PtlPoint(24, 108));
                           quad3.setBottomRight(new PtlPoint(167, 108));
                    	   
	                       // 取り消し線注釈
	                       addAnnotStrikeOut(annots, quad3, verticalWriting);
                       }
                    }
                }
            }
            // ファイルに保存します。
            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 addAnnotSquiggly(PtlAnnots annots, PtlQuadPoint quadPoint, boolean verticalWriting) throws PtlException, Exception, Error
    {
    	try (PtlAnnotSquiggly annotSquiggly = new PtlAnnotSquiggly())     // 波状下線注釈
        {

            // 座標を設定 座標の単位はmmで原点(0,0)は左下
            try (PtlQuadPoints quadPoints = annotSquiggly.getQuadPoints())
            {
                quadPoints.append(quadPoint);
            }
        	//テキストを縦書き方向に囲むかどうかを設定
        	annotSquiggly.setVerticalDirection(verticalWriting);
        	
            // 内容を設定（注釈用に表示されるテキスト・可読な形式での注釈コンテンツの代替説明）
            annotSquiggly.setTextContents("波状下線サンプル");

            // 日時の設定(2025/01/01 00:00:00)
            try (PtlDate date = new PtlDate(2025, 1, 1, 0, 0, 0))
            {
                annotSquiggly.setDate(date);
            }

            // 注釈フラグを設定（論理和） FLAG_NOROTATE = 0x00000010, /* 注釈の外観をページにあわせて回転しません。 */
            annotSquiggly.setAnnotFlags(PtlAnnot.FLAG_NOROTATE);

            // 色を設定
            try (PtlColorDeviceRGB color =  new PtlColorDeviceRGB(1.0f, 0.0f, 0.0f))
            {
                annotSquiggly.setColor(color);
            }

            // 内部色を設定
            try (PtlColorDeviceRGB color =  new PtlColorDeviceRGB(1.0f, 1.0f, 0.0f))
            {
                annotSquiggly.setInteriorColor(color);
            }

            // 境界線スタイルを設定 BORDER_SOLID = 1, /* 実線(注釈を囲む実線の矩形) */
            annotSquiggly.setBorderStyle(PtlAnnot.BORDER_STYLE.BORDER_SOLID);

            // 境界線幅を設定 BORDER_WIDTH_THIN = 1, /* 細い */
            annotSquiggly.setBorderWidth(PtlAnnot.BORDER_LINE_WIDTH.BORDER_WIDTH_THIN);

            // 不透明度を設定 0.0 ～ 1.0。0.0が透明、1.0が不透明
            annotSquiggly.setMarkUpCA(0.8f);

            // 注釈の追加
            annots.append(annotSquiggly);
        }
    }

    public static void addAnnotStrikeOut(PtlAnnots annots, PtlQuadPoint quadPoint, boolean verticalWriting) throws PtlException, Exception, Error
    {
		try (PtlAnnotStrikeOut annotStrikeOut = new PtlAnnotStrikeOut())     // 取り消し線注釈
        {
            // 座標を設定 座標の単位はmmで原点(0,0)は左下
        	try (PtlQuadPoints quadPoints = annotStrikeOut.getQuadPoints())
        	{
        	    quadPoints.append(quadPoint);
        	}
        	
        	//テキストを縦書き方向に囲むかどうかを設定
        	annotStrikeOut.setVerticalDirection(verticalWriting);

            // 内容を設定（注釈用に表示されるテキスト・可読な形式での注釈コンテンツの代替説明）
            annotStrikeOut.setTextContents("取り消し線注釈サンプル");

            // 日時の設定(2025/01/01 00:00:00)
            try (PtlDate date = new PtlDate(2025, 1, 1, 0, 0, 0))
            {
                annotStrikeOut.setDate(date);
            }

            // 注釈フラグを設定（論理和） FLAG_NOROTATE = 0x00000010, /* 注釈の外観をページにあわせて回転しません。 */
            annotStrikeOut.setAnnotFlags(PtlAnnot.FLAG_NOROTATE);

            // 色を設定
            try (PtlColorDeviceRGB color =  new PtlColorDeviceRGB(1.0f, 0.0f, 0.0f))
            {
                annotStrikeOut.setColor(color);
            }

            // 内部色を設定
            try (PtlColorDeviceRGB color =  new PtlColorDeviceRGB(1.0f, 1.0f, 0.0f))
            {
                annotStrikeOut.setInteriorColor(color);
            }

            // 境界線スタイルを設定 BORDER_SOLID = 1, /* 実線(注釈を囲む実線の矩形) */
            annotStrikeOut.setBorderStyle(PtlAnnot.BORDER_STYLE.BORDER_SOLID);

            // 境界線幅を設定 BORDER_WIDTH_THIN = 1, /* 細い */
            annotStrikeOut.setBorderWidth(PtlAnnot.BORDER_LINE_WIDTH.BORDER_WIDTH_THIN);

            // 不透明度を設定 0.0 ～ 1.0。0.0が透明、1.0が不透明
            annotStrikeOut.setMarkUpCA(0.8f);

            // 注釈の追加
            annots.append(annotStrikeOut);
        }
    }

    public static void addAnnotUnderline(PtlAnnots annots, PtlQuadPoint quadPoint, boolean verticalWriting) throws PtlException, Exception, Error
    {
		try (PtlAnnotUnderline annotUnderline = new PtlAnnotUnderline())     // 下線注釈
        {
            // 座標を設定 座標の単位はmmで原点(0,0)は左下
        	try (PtlQuadPoints quadPoints = annotUnderline.getQuadPoints())
        	{
        	    quadPoints.append(quadPoint);
        	}
        	
        	//テキストを縦書き方向に囲むかどうかを設定
        	annotUnderline.setVerticalDirection(verticalWriting);

            // 内容を設定（注釈用に表示されるテキスト・可読な形式での注釈コンテンツの代替説明）
            annotUnderline.setTextContents("下線注釈サンプル");

            // 日時の設定(2025/01/01 00:00:00)
            try (PtlDate date = new PtlDate(2025, 1, 1, 0, 0, 0))
            {
                annotUnderline.setDate(date);
            }

            // 注釈フラグを設定（論理和） FLAG_NOROTATE = 0x00000010, /* 注釈の外観をページにあわせて回転しません。 */
            annotUnderline.setAnnotFlags(PtlAnnot.FLAG_NOROTATE);

            // 色を設定
            try (PtlColorDeviceRGB color =  new PtlColorDeviceRGB(1.0f, 0.0f, 0.0f))
            {
                annotUnderline.setColor(color);
            }

            // 内部色を設定
            try (PtlColorDeviceRGB color =  new PtlColorDeviceRGB(1.0f, 1.0f, 0.0f))
            {
                annotUnderline.setInteriorColor(color);
            }

            // 境界線スタイルを設定 BORDER_SOLID = 1, /* 実線(注釈を囲む実線の矩形) */
            annotUnderline.setBorderStyle(PtlAnnot.BORDER_STYLE.BORDER_SOLID);

            // 境界線幅を設定 BORDER_WIDTH_THIN = 1, /* 細い */
            annotUnderline.setBorderWidth(PtlAnnot.BORDER_LINE_WIDTH.BORDER_WIDTH_THIN);

            // 不透明度を設定 0.0 ～ 1.0。0.0が透明、1.0が不透明
            annotUnderline.setMarkUpCA(0.8f);

            // 注釈の追加
            annots.append(annotUnderline);
        }
    }

}
