/*
    Antenna House PDF Tool API V8.0
    Java Interface sample program

    概要：テキスト検索して下線、波線、取り消し線注釈

    Copyright 2025 Antenna House, Inc.
*/
package Sample;

import jp.co.antenna.ptl.PtlAnnot;
import jp.co.antenna.ptl.PtlAnnotHighlight;
import jp.co.antenna.ptl.PtlAnnotSquiggly;
import jp.co.antenna.ptl.PtlAnnotStrikeOut;
import jp.co.antenna.ptl.PtlAnnotUnderline;
import jp.co.antenna.ptl.PtlAnnots;
import jp.co.antenna.ptl.PtlColorDeviceRGB;
import jp.co.antenna.ptl.PtlDate;
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.PtlParamInput;
import jp.co.antenna.ptl.PtlParamOutput;
import jp.co.antenna.ptl.PtlParamSearchText;
import jp.co.antenna.ptl.PtlQuadPoint;
import jp.co.antenna.ptl.PtlQuadPoints;
import jp.co.antenna.ptl.PtlSearchTextResult;
import jp.co.antenna.ptl.PtlSearchTextResultDetail;
import jp.co.antenna.ptl.PtlSearchTextResultDetails;
import jp.co.antenna.ptl.PtlSearchTextResults;

public class SearchTextAndAppendAnnot {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        if (args.length < 3)
        {
            System.out.println("usage: java SearchTextAndAppendAnnot in-pdf-file out-pdf-file keyword");
            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 (PtlParamSearchText paramSearchText = new PtlParamSearchText())
                {
                    // 検索設定
                    paramSearchText.appendText(args[2]);

                    // 検索実行して検索結果コンテナ取得
                    try (PtlSearchTextResults results = doc.searchText(paramSearchText))
                    {
                        int numResults = results.getCount();
                        for (int i = 0; i < numResults; ++i)
                        {
                            // 検索結果取得
                            try (PtlSearchTextResult result = results.get(i))
                            {
                                // 検索に使用したキーワード
                                String keyword = result.getKeyword();

                                // ヒットしたページ番号
                                int pageNumber = result.getPageNumber();
                                try (PtlPage page = pages.get(pageNumber);
                                     PtlAnnots annots = page.getAnnots())
                                {
                                    // 検索結果詳細コンテナ取得
                                    try (PtlSearchTextResultDetails details = result.getResultDetails())
                                    {
                                        int numDetails = details.getCount();
                                        for (int j = 0; j < numDetails; ++j)
                                        {
                                            // 検索結果詳細取得
                                            try (PtlSearchTextResultDetail resultDetail = details.get(j))
                                            {
                                                // 検索に使用したキーワードの部分文字列
                                                String keywordD = resultDetail.getKeyword();

                                                try (PtlAnnotHighlight annot = new PtlAnnotHighlight();
                                                     PtlQuadPoints quads = annot.getQuadPoints();
                                                // 検索に使用したキーワードの部分文字列を囲むQuadPoint
                                                	PtlQuadPoint quad = resultDetail.getQuadPoint())
                                                {
                                                    quads.append(quad);
                                                    // 検索に使用したキーワードの部分文字列が縦書きか
                                                    boolean verticalWriting = resultDetail.isVerticalWriting();
                                                    //annot.setVerticalDirection(verticalWriting);
                                        			//注釈の追加
                                        			switch(i % 3)
                                        			{
                                        			case 0:
                                                   	// 波状下線釈追加
                                                       addAnnotSquiggly(annots, quad, verticalWriting);
                                        				break;
                                        			case 1:
                                                       // 取り消し線注釈
                                                       addAnnotStrikeOut(annots, quad, verticalWriting);
                                        				break;
                                        			case 2:
                                                       // 下線注釈
                                                       addAnnotUnderline(annots, quad, verticalWriting);
                                        				break;
                                        			}


                                                    //annots.append(annot);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

            // ファイルに保存します。
            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);
        }
    }

}
