OEM販売のご相談
ご相談ください!

PDF Tool APIサンプルコード:PDF/A-1b 変換

機能イメージ

入力PDFをPDF/Aへ変換します

概要

サンプルコードの概要

入力PDFをPDF/Aへ変換します。

  • PtlPDFFixUp: PDF/A準拠のチェック変換を表現したクラス
  • PtlPDFFixUp.setSaveOption(): 保存時のオプションを設定
  • PtlPDFFixUp.setICCProfileCMYK(): 出力インテントのプロファイル(CMYK用)
  • PtlPDFFixUp.setICCProfileRGB(): 出力インテントのプロファイル(RGB用)
  • PtlPDFFixUp.fixUpPDFA(): PDF/Aへの変換
  • PtlPDFFixUp.save(output): PDF文書を保存

サンプルコード

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

	概要:PDF/A-1b,PDF/A-2bへの変換

	Copyright 2021 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-1b\n");
		return 1;
	}
	try {
		PtlPDFFixUp::PDFA_TYPE type;
		std::string desc;
		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;
		default:
			printf("usage: FixUpPDFA.exe in-pdf-file out-pdf-file 適用規格\n");
			printf("適用規格\n1 : PDF/A-1b  2 : PDF/A-1b\n");
			return 1;
		}

		PtlParamInput input(argv[1]);
		PtlParamOutput output(argv[2]);
		PtlPDFFixUp fix;
		//PtlParamInput iccpcmyk("JapanColor2001Coated.icc");
		//PtlParamInput iccprgb("sRGB2014.icc");
		PtlParamInput iccpcmyk("D:\\Develop\\AHPDFFixUp\\icc\\JapanColor2001Coated.icc");
		PtlParamInput iccprgb("D:\\Develop\\AHPDFFixUp\\icc\\sRGB2014.icc");

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

		// PDF/Aへの変換
		bool 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;
}

            
/*
	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("-- 完了 --");
        }
    }
}

            
/*
	Antenna House PDF Tool API V7.0
	.NET Interface sample program

	概要:PDF/A-1b,PDF/A-2bへの変換

	Copyright 2021 Antenna House,Inc.
*/

using System;

using PdfTkNet;

namespace FixUpPDFA
{
    class FixUpPDFA
    {
        static void Main(string[] args)
        {
            if (args.Length < 3)
            {
                Console.WriteLine("usage: FixUpPDFA.exe in-pdf-file out-pdf-file");
                Console.WriteLine("規格\n1 : PDF/A-1b  2 : PDF/A-1b");
                return;
            }

            try
            {
                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:
                    Console.WriteLine("usage: FixUpPDFA.exe in-pdf-file out-pdf-file");
                    Console.WriteLine("規格\n1 : PDF/A-1b  2 : PDF/A-1b");
                    return;
                }

                using (PtlParamInput inputFile = new PtlParamInput(args[0]))
                using (PtlParamOutput outputFile = new PtlParamOutput(args[1]))
                using (PtlPDFFixUp fix = new PtlPDFFixUp())
                using (PtlParamInput iccpcmyk = new PtlParamInput(@"D:\Develop\AHPDFFixUp\icc\JapanColor2001Coated.icc"))
                using (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への変換
                    bool bl = fix.fixUpPDFA(PtlPDFFixUp.PDFA_TYPE.PDFA_1B, inputFile);

                    if (bl)
                    {
                        Console.WriteLine(desc + " への変換に成功しました。");
                    }
                    else
                    {
                        Console.WriteLine(desc + " への変換に失敗しました。");

                        //変換エラーの内容を取得する
                        using (PtlPDFFixUpErrors fxuperrs = fix.getErrors())
                        {
                            int count = fxuperrs.getCount();
                            Console.WriteLine("エラー数 : " + count);
                            for (int i = 0; i < count; i++)
                            {
                                using (PtlPDFFixUpError fxuperr = fxuperrs.get(i))
                                {
                                    Console.WriteLine(fxuperr.getErrorCode() + " : " + fxuperr.getErrorMessageJP());
                                }
                            }
                        }
                    }

                    // PDF文書を保存
                    fix.save(outputFile);
                }
                Console.WriteLine("-- 完了 --");
            }
            catch (PtlException pex)
            {
                Console.WriteLine(pex.getErrorCode() + " : " + pex.getErrorMessageJP());
                pex.Dispose();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
}

            

サンプルコードのダウンロードはこちら

実行例

コマンドラインでの実行例

FixUpPDFA.exe C:\in\in.pdf  C:\sav\outFixUpPDFA.pdf 1
PDF/A-1b への変換に成功しました。
-- 完了 --
java -jar FixUpPDFA.jar C:\in\in.pdf  C:\sav\outFixUpPDFA.pdf 1
PDF/A-1b への変換に成功しました。
-- 完了 --
Release\net8.0\FixUpPDFA.exe C:\in\in.pdf  C:\sav\outFixUpPDFA.pdf 1
PDF/A-1b への変換に成功しました。
-- 完了 --

出力結果イメージ

出力されたPDFを確認すると「規格:PDF/A-1B」となっているのが確認できます。

出力イメージ

サンプルコードのダウンロード

サンプルコード

サンプルで使用した入出力PDF