/*
    Antenna House PDF Tool API V7.0
    Java Interface sample program

    概要：パスの描画

    Copyright 2015-2021 Antenna House, Inc.
*/

package Sample;

import jp.co.antenna.ptl.*;

public class DrawShape {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        if (args.length < 2)
        {
            System.out.println("usage: java DrawShape 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()) //ページコンテナの取得
            {
                // ページコンテナが空かどうか
                if (pages.isEmpty())
                {
                    System.out.println("ページコンテナが空\n");
                    return;
                }

                // ページの取得
                try (PtlPage page = pages.get(0))
                {
                    // パスの描画
                    drawShape(page);
                }
            }

            // ファイルに保存します。
            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 drawShape(PtlPage page) throws PtlException, Exception, Error
    {
        try (PtlContent content = page.getContent(); // ページコンテントの取得
             PtlSize pageSize = page.getSize()) // ページサイズ
        {
            // パス出力
            try (PtlParamDrawShape paramDrawShape = new PtlParamDrawShape()) // パスの描画に使うパラメータクラス
            {
                // 丸角矩形の描画
                try (PtlRect rect = new PtlRect(10.0f, 10.0f, pageSize.getWidth()-10.0f, pageSize.getHeight()-10.0f);
                     PtlColorDeviceRGB colorFill = new PtlColorDeviceRGB(1.0f, 0.0f, 0.0f);
                     PtlColorDeviceRGB colorStroke = new PtlColorDeviceRGB(0.0f, 0.0f, 1.0f))
                {
                    paramDrawShape.setLineColor(colorStroke);
                    paramDrawShape.setFillColor(colorFill);

                    PtlParamDrawShape.LINE_STYLE lineStyle = PtlParamDrawShape.LINE_STYLE.LINE_STYLE_SOLID;
                    //int lineStyle = PtlParamDrawShape.LINE_STYLE.LINE_STYLE_DASHED;
                    paramDrawShape.setLineStyle(lineStyle);

                    //PtlParamDrawShape.LINE_WIDTH lineWidth = PtlParamDrawShape.LINE_WIDTH.LINE_WIDTH_THIN;
                    //PtlParamDrawShape.LINE_WIDTH lineWidth = PtlParamDrawShape.LINE_WIDTH.LINE_WIDTH_MIDDLE;
                    PtlParamDrawShape.LINE_WIDTH lineWidth = PtlParamDrawShape.LINE_WIDTH.LINE_WIDTH_THICK;
                    paramDrawShape.setLineWidth(lineWidth);

                    paramDrawShape.setOpacity(0.5f);
                    content.drawRoundRect(rect, 10.0f, 10.0f, paramDrawShape);
                }

                // 矩形/円の描画
                try (PtlRect rect = new PtlRect(30.0f, 30.0f, pageSize.getWidth() - 30.0f, pageSize.getHeight() - 30.0f);
                     PtlColorDeviceRGB colorFill = new PtlColorDeviceRGB(1.0f, 1.0f, 0.0f);
                     PtlColorDeviceRGB colorStroke = new PtlColorDeviceRGB(0.0f, 1.0f, 1.0f))
                {
                    paramDrawShape.setLineColor(colorStroke);
                    paramDrawShape.setFillColor(colorFill);
                	paramDrawShape.setOpacity(0.3f);
                    content.drawRect(rect, paramDrawShape);
                    content.drawCircle(rect, paramDrawShape);
                }

                // 線の描画
                try (PtlColorDeviceRGB colorStroke = new PtlColorDeviceRGB(0.0f, 1.0f, 0.0f))
                {
                    paramDrawShape.setLineColor(colorStroke);
                    try (PtlPoint from = new PtlPoint(30.0f, 30.0f);
                         PtlPoint to = new PtlPoint(pageSize.getWidth() - 30, pageSize.getHeight() - 30.0f))
                    {
                        content.drawLine(from, to, paramDrawShape);
                    }
                    try (PtlPoint from = new PtlPoint(30.0f, pageSize.getHeight() - 30.0f);
                         PtlPoint to = new PtlPoint(pageSize.getWidth() - 30.0f, 30.0f))
                    {
                        content.drawLine(from, to, paramDrawShape);
                    }
                }
            }
        }
    }
}
