/*
	Antenna House PDF Tool API V7.0
	Java Interface sample program

	概要：PDF/A-1b,PDF/A-2bへの変換

	Copyright 2021 Antenna House,Inc.
*/

package Sample;

import jp.co.antenna.ptl.*;

public class FixUpPDFA {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        if (args.length < 3)
        {
            System.out.println("usage: java FixUpPDFA in-pdf-file out-pdf-file");
            System.out.println("規格\n1 : PDF/A-1b  2 : PDF/A-1b");
            return;
        }

        PtlPDFFixUp.PDFA_TYPE type;
        String desc = args[2];
        switch (args[2])
        {
        case "1":
            type = PtlPDFFixUp.PDFA_TYPE.PDFA_1B;
            desc = "PDF/A-1b";
            break;
        case "2":
            type = PtlPDFFixUp.PDFA_TYPE.PDFA_2B;
            desc = "PDF/A-2b";
            break;
        default:
            System.out.println("usage: FixUpPDFA.exe in-pdf-file out-pdf-file");
            System.out.println("規格\n1 : PDF/A-1b  2 : PDF/A-1b");
            return;
        }

        try (PtlParamInput inputFile = new PtlParamInput(args[0]);
             PtlParamOutput outputFile = new PtlParamOutput(args[1]);
             PtlPDFFixUp fix = new PtlPDFFixUp();
             //PtlParamInput iccpcmyk = new PtlParamInput("JapanColor2001Coated.icc");
             //PtlParamInput iccprgb = new PtlParamInput("sRGB2014.icc"))
             PtlParamInput iccpcmyk = new PtlParamInput("D:\\Develop\\AHPDFFixUp\\icc\\JapanColor2001Coated.icc");
             PtlParamInput iccprgb = new PtlParamInput("D:\\Develop\\AHPDFFixUp\\icc\\sRGB2014.icc"))
        {
            fix.setSaveOption(PtlPDFFixUp.SAVE_OPTION.SAVE_RECONSTRUCT);    //保存時のオプションを設定
            fix.setICCProfileCMYK(iccpcmyk);        //出力インテントのプロファイル（CMYK用）
            fix.setICCProfileRGB(iccprgb);          //出力インテントのプロファイル（RGB用）
            // PDF/Aへの変換
            boolean bl = fix.fixUpPDFA(PtlPDFFixUp.PDFA_TYPE.PDFA_1B, inputFile);

            if (bl)
            {
                System.out.println(desc + " への変換に成功しました。");
            }
            else
            {
                System.out.println(desc + " への変換に失敗しました。");

                //変換エラーの内容を取得する
                try (PtlPDFFixUpErrors fxuperrs = fix.getErrors())
                {
	                int count = fxuperrs.getCount();
	                System.out.println("エラー数 ： " + count);
	                for (int i = 0; i < count; i++)
	                {
	                    try (PtlPDFFixUpError fxuperr = fxuperrs.get(i))
	                    {
	                    	System.out.println(fxuperr.getErrorCode() + " : " + fxuperr.getErrorMessageJP());
	                    }
	                }
	            }
            }

            // PDF文書を保存
            fix.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("-- 完了 --");
        }
    }
}
