/*
    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 ExtractPage {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        if (args.length < 2)
        {
            System.out.println("usage: java ExtractPage in-pdf-file out-folder");
            return;
        }

        try (PtlParamInput inputFile = new PtlParamInput(args[0]);
             PtlPDFDocument doc = new PtlPDFDocument())
        {
            // PDFファイルをロード
            doc.load(inputFile);

            try (PtlDocProperty docproperty = doc.getDocProperty();// 文書プロパティの取得
                 PtlDocInfo docinfo = docproperty.getDocInfo())    // 文書情報の取得
            {
                for (int i = 0; i < doc.getPageCount(); i++)
                {
                    try (PtlPDFDocument doc_ext = new PtlPDFDocument();
                         PtlDocProperty docproperty_ext = doc_ext.getDocProperty(); // 文書プロパティの取得
                         PtlDocInfo docinfo_ext = docproperty_ext.getDocInfo();     // 文書情報の取得
                         PtlPages pages = doc_ext.getPages())
                    {
                        // タイトルをコピー
                        docinfo_ext.setTitle(docinfo.getTitle());

                        // 著者をコピー
                        docinfo_ext.setAuthor(docinfo.getAuthor());

                        // サブジェクトをコピー
                        docinfo_ext.setSubject(docinfo.getSubject());

                        // キーワードをコピー
                        docinfo_ext.setKeywords(docinfo.getKeywords());

                        // クリエータをコピー
                        docinfo_ext.setCreator(docinfo.getCreator());

                        // プロデューサをコピー
                        docinfo_ext.setProducer(docinfo.getProducer());

                        // 作成日付をコピー
                        try (PtlDate dateCreate = docinfo.getCreationDate())
                        {
                            docinfo_ext.setCreationDate(dateCreate);
                        }

                        // 更新日付をコピー
                        try (PtlDate dateMod = docinfo.getModDate())
                        {
                            docinfo_ext.setModDate(dateMod);
                        }

                        // ページ挿入オプション
                        // OPTION_COPY_OUTLINES = 0x00000004 ページ挿入時にあわせてしおりをコピーします。
                        // OPTION_COPY_ATTACHEDFILES    = 0x00000008ページ挿入時にあわせて添付ファイルをコピーします。
                        int insertoption = PtlPages.OPTION_COPY_OUTLINES | PtlPages.OPTION_COPY_ATTACHEDFILES;

                        // ページの追加(iページから1P)
                        pages.append(doc, i, 1, insertoption);

                        // 出力ファイル名
                        String outputfile = (String)args[1] + "\\output_" + i + ".pdf";

                        try (PtlParamOutput outputFile = new PtlParamOutput(outputfile))
                        {
                            // ファイルに保存します。
                            doc_ext.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("-- 完了 --");
        }
    }
}
