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

    概要：ページ抽出 別々のPDFとして保存

    Copyright 2015-2026 Antenna House, Inc.
*/

package Sample;

import jp.co.antenna.ptl.*;

public class ExtractDividePage {

	public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ

        if (args.length < 2)
        {
            System.out.println("usage: java ExtractDividePage in-pdf-file out-pdf page-renge");
            return;
        }

        try (PtlParamInput inputFile = new PtlParamInput(args[0]);
    		//PtlParamOutput outputFile = new PtlParamOutput(args[1]);
			PtlPDFDocument doc = new PtlPDFDocument())
        {
    		String outputfile = args[1];
        	// PDFファイルをロード
            doc.load(inputFile);

            try (PtlDocProperty docproperty = doc.getDocProperty();	// 文書プロパティの取得
	            PtlDocInfo docinfo = docproperty.getDocInfo();) 	// 文書情報の取得
    		{
                // 出力ページ範囲
               String pageRange = (String)args[2];
               String[] Ranges = pageRange.split(",");
               int rc = Ranges.length;
               for (int i = 0; i < rc; 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_NONE  = 0x00000000 オプションはありません。
		                int insertoption = PtlPages.OPTION_NONE;
		                
                        String suffix;
	                	String tmp = Ranges[i];
	                    if (tmp.contains("-"))
	                    {
	                    	String[] pg = tmp.split("-");
	                        int tmpStrt = Integer.parseInt(pg[0]);
	                        int tmpEnd = Integer.parseInt(pg[1]);
	
	                        // ページの追加(tmpStrtページからtmpEndページ)
	                        pages.append(doc, tmpStrt, (tmpEnd - tmpStrt + 1), insertoption);
	                        suffix = "_" + tmpStrt + "-" + tmpEnd + ".pdf";
	                    }
	                    else
	                    {
	                        int tmpStrt = Integer.parseInt(tmp);
	                        // ページの追加(tmpStrtページから1P)
	                        pages.append(doc, tmpStrt, 1, insertoption);
	                        suffix = "_" + tmpStrt + ".pdf";
	                    }
		
						// 出力ファイル名
	                    PtlParamOutput output = new PtlParamOutput(outputfile.substring(0, outputfile.length() - 5) + suffix);	                    
		                // ファイルに保存します。
		                doc_ext.save(output);
	                }
	            }
            }
        }
        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("-- 完了 --");
        }
	}

}
