/*
	Antenna House PDF Tool API V8.0
	Java Interface sample program

	概要：PDF/A,PDF/E-1への変換

	Copyright 2021-2025 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-2b  3 : PDF/A-3b  4 : PDF/A-1a  5 : PDF/A-2a  6 : PDF/A-3a  7 : PDF/A-4  8 : PDF/A-4f  9 : PDF/A-4e  10 : PDF/E-1");
            return;
        }

        PtlPDFFixUp.PDFA_TYPE type = PtlPDFFixUp.PDFA_TYPE.PDFA_2B;
        String desc = args[2];
        boolean pdfe1 = false; 
        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;
        case "3":
            type = PtlPDFFixUp.PDFA_TYPE.PDFA_3B;
            desc = "PDF/A-3b";
            break;
        case "4":
            type = PtlPDFFixUp.PDFA_TYPE.PDFA_1A;
            desc = "PDF/A-1a";
            break;
        case "5":
            type = PtlPDFFixUp.PDFA_TYPE.PDFA_2A;
            desc = "PDF/A-2a";
            break;
        case "6":
            type = PtlPDFFixUp.PDFA_TYPE.PDFA_3A;
            desc = "PDF/A-3a";
            break;
        case "7":
            type = PtlPDFFixUp.PDFA_TYPE.PDFA_4;
            desc = "PDF/A-4";
            break;
        case "8":
            type = PtlPDFFixUp.PDFA_TYPE.PDFA_4F;
            desc = "PDF/A-4f";
            break;
        case "9":
            type = PtlPDFFixUp.PDFA_TYPE.PDFA_4E;
            desc = "PDF/A-4e";
            break;
        case "10":
            pdfe1 = true;
            desc = "PDF/E-1";
            break;
        default:
            System.out.println("usage: FixUpPDFA.exe in-pdf-file out-pdf-file");
            System.out.println("規格\n1 : PDF/A-1b  2 : PDF/A-2b  3 : PDF/A-3b  4 : PDF/A-1a  5 : PDF/A-2a  6 : PDF/A-3a  7 : PDF/A-4  8 : PDF/A-4f  9 : PDF/A-4e  10 : PDF/E-1");
            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("EuroscaleCoated.icc");
             //PtlParamInput iccprgb = new PtlParamInput("AdobeRGB1998.icc"))
        {
            fix.setSaveOption(PtlPDFFixUp.SAVE_OPTION.SAVE_RECONSTRUCT);    //保存時のオプションを設定
            fix.setICCProfileCMYK(iccpcmyk);        //出力インテントのプロファイル（CMYK用）
            fix.setICCProfileRGB(iccprgb);          //出力インテントのプロファイル（RGB用）
            
            boolean bl;
            if(pdfe1)
            {
                // PDF/E-1への変換
                bl = fix.fixUpPDFE(inputFile);
            }
            else
            {
                // PDF/Aへの変換
                bl = fix.fixUpPDFA(type, 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("-- 完了 --");
        }
    }
}
