﻿/*
	Antenna House PDF Tool API V8.0
	C++ Interface sample program

	概要：PDF/A,PDF/E-1への変換

	Copyright 2021-2025 Antenna House,Inc.
*/
#include <PdfTk.h>

using namespace std;
using namespace PdfTk;

int main(int argc, char* argv[])
{
	if (argc < 4)
	{
		printf("usage: FixUpPDFA.exe in-pdf-file out-pdf-file 規格\n");
		printf("規格\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/E-1\n");
		return 1;
	}
	try {
		PtlPDFFixUp::PDFA_TYPE type;
		std::string desc;
		bool pdfe1 = false;

		const char* kind = argv[3];
		switch (kind[0]) {
		case '1':
			type = PtlPDFFixUp::PDFA_1B;
			desc = "PDF/A-1b";
			break;
		case '2':
			type = PtlPDFFixUp::PDFA_2B;
			desc = "PDF/A-2b";
			break;
		case '3':
			type = PtlPDFFixUp::PDFA_3B;
			desc = "PDF/A-3b";
			break;
		case '4':
			type = PtlPDFFixUp::PDFA_1A;
			desc = "PDF/A-1a";
			break;

		case '5':
			type = PtlPDFFixUp::PDFA_2A;
			desc = "PDF/A-2a";
			break;
		case '6':
			type = PtlPDFFixUp::PDFA_3A;
			desc = "PDF/A-3a";
			break;

		case '7':
			type = PtlPDFFixUp::PDFA_4;
			desc = "PDF/A-4";
			break;
		case '8':
			pdfe1 = true;
			desc = "PDF/E-1";
			break;

		default:
			printf("usage: FixUpPDFA.exe in-pdf-file out-pdf-file 規格\n");
			printf("規格\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/E-1\n");
			return 1;
		}

		PtlParamInput input(argv[1]);
		PtlParamOutput output(argv[2]);
		PtlPDFFixUp fix;
		PtlParamInput iccpcmyk("JapanColor2001Coated.icc");
		PtlParamInput iccprgb("sRGB2014.icc");

		fix.setSaveOption(PtlPDFFixUp::SAVE_OPTION::SAVE_RECONSTRUCT);	//保存時のオプションを設定
		fix.setICCProfileCMYK(iccpcmyk);		//出力インテントのプロファイル（CMYK用）
		fix.setICCProfileRGB(iccprgb);			//出力インテントのプロファイル（RGB用）

		bool bl;
		//
		if (pdfe1)
		{
			bl = fix.fixUpPDFE(input);
		}
		else
		{
			// PDF/Aへの変換
			bl = fix.fixUpPDFA(type, input);
		}
		if (bl)
		{
			printf("%s への変換に成功しました。\n", desc.c_str());
		}
		else
		{
			printf("%s への変換に失敗しました。\n", desc.c_str());

			//変換エラーの内容を取得する
			PtlPDFFixUpErrors fxuperrs = fix.getErrors();
			int count = fxuperrs.getCount();
			printf("エラー数 ： %d\n",  count);
			for (int i = 0; i < count; i++)
			{
				PtlPDFFixUpError fxuperr = fxuperrs.get(i);
				fprintf(stderr, "%d\n : %s\n", fxuperr.getErrorCode(), fxuperr.getErrorMessageJP().c_str());
			}
		}

		// PDF文書を保存
		fix.save(output);

		printf("-- 完了 --\n");
	}
	catch (PtlException e)
	{
		fprintf(stderr, "Error code : %d\n %s\n", e.getErrorCode(), e.getErrorMessage().c_str());
		return 1;
	}
	return 0;
}
