/*
    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 DrawForm {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        if (args.length < 3)
        {
            System.out.println("usage: java DrawForm in-pdf-file out-pdf-file in-pdf-file2");
            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("ページコンテナが空");
                    return;
                }

                // １ページ目の取得
                try (PtlPage page = pages.get(0))
                {
                    // フォームの描画
                    drawForm(page, args[2]);
                }
            }

            // ファイルに保存します。
            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 drawForm(PtlPage page, String pathPDF) throws PtlException, Exception, Error
    {
        try (PtlContent content = page.getContent(); // ページコンテントの取得
             PtlParamInput inputFile2 = new PtlParamInput(pathPDF);  //挿入するPDF
             PtlPDFDocument doc2 = new PtlPDFDocument();
             PtlRect rect = new PtlRect(0.0f, 0.0f, 100.0f, 100.0f)) // 描画矩形
        {
            // PDFファイルをロードします。
            doc2.load(inputFile2);

            try (PtlPages pages2 = doc2.getPages()) // ページコンテナの取得
            {
                // ページコンテナが空かどうか
                if (pages2.isEmpty())
                {
                    System.out.println("ページコンテナが空");
                    return;
                }

                // １ページ目の取得
                try (PtlPage pageInsert = pages2.get(0))
                {
                    // フォームの描画
                    content.drawForm(rect, PtlContent.ALIGN.ALIGN_CENTER, pageInsert);
                }
            }
        }
    }
}
