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

PDF Tool APIサンプルコード:DeviceN Color

機能イメージ

DeviceNカラー(特色)を用いた色指定ができます。図形やテキストの描画に利用できます。

概要

サンプルコードの概要

DeviceNを設定し、それを使用して円を描画します。

  • PtlColorSpaceSeparation: カラースペースのSeparationを表現したクラス
  • PtlColorSpaceDeviceN: カラースペースのDeviceNを表現したクラス
  • PtlColorDeviceN: PDFのDeviceN色を表現したクラス
  • PtlParamDrawShape: 線の描画に使うパラメータクラス
  • PtlParamDrawShape.setFillColor(PtlColorSpaceDeviceN): 塗りつぶし色を設定
  • PtlParamDrawShape.setLineColor(PtlColorSpaceDeviceN): 線の色を設定
  • PtlContent.drawCircle: 円形を描画

サンプルコード

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

	概要:DeviceN Color

	Copyright 2025 Antenna House,Inc.
*/

#include < PdfTk.h >

using namespace std;
using namespace PdfTk;

int main(int argc, char* argv[])
{
	if (argc < 3) {
		printf("usage: DeviceNColor.exe in-pdf-file out-pdf-file \n");
		return 1;
	}

	try {
		PtlParamInput input(argv[1]);
		PtlParamOutput output(argv[2]);

		PtlPDFDocument doc;

		// PDFファイルをロードします。
		doc.load(input);

		// ページコンテナの取得
		PtlPages& pages = doc.getPages();

		if (pages.isEmpty()) {
			printf("ページコンテナが空\n");
			return 1;
		}

		// 先頭ページの取得
		PtlPage page = pages.get(0);

		// ページコンテントの取得
		PtlContent& content = page.getContent();

		// DeviceNカラー
		PtlColorSpaceSeparation csSep1("SEP1", 0.7f, 0.8f, 1.0f, 0.9f);
		PtlColorSpaceSeparation csSep2("SEP2", 0.3f, 0.5f, 0.6f, 0.4f);
		PtlColorSpaceSeparation csSep3("SEP3", 0.4f, 0.3f, 0.5f, 0.2f);
		PtlColorSpaceSeparation csSep4("SEP4", 0.6f, 0.7f, 0.2f, 0.1f);
		PtlColorSpaceDeviceN csDeviceN(PtlColorSpaceDeviceN::COMP_Cyan | PtlColorSpaceDeviceN::COMP_Magenta | PtlColorSpaceDeviceN::COMP_Yellow | PtlColorSpaceDeviceN::COMP_Black, csSep1, csSep2, csSep3, csSep4);
		PtlColorDeviceN colorDeviceN(csDeviceN, 0.4f, 0.7f, 0.5f, 0.2f, 0.3f, 0.4f, 0.5f, 0.2f);

		PtlParamDrawShape shape;
		shape.setFillColor(colorDeviceN);
		shape.setLineColor(colorDeviceN);

		PtlRect rect(0.0f, 0.0f, 100.0f, 100.0f);
		//円形を描画
		content.drawCircle(rect, shape);

		// ファイルに保存します。
		doc.save(output);

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


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

    概要:DeviceN Color

    Copyright 2025 Antenna House,Inc.
*/

package Sample;

import jp.co.antenna.ptl.*;

public class DeviceNColor {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        if (args.length < 2)
        {
            System.out.println("usage: java DeviceNColor in-pdf-file out-pdf-file");
            return;
        }

        try (PtlParamInput input = new PtlParamInput(args[0]);
             PtlParamOutput output = new PtlParamOutput(args[1]);
             PtlPDFDocument doc = new PtlPDFDocument())
        {
            // PDFファイルをロードします。
            doc.load(input);

            // PDFファイルをロードします。
            doc.load(input);

            try (PtlPages pages = doc.getPages())
            {
                // ページコンテナが空かどうか
                if (pages.isEmpty())
                {
                    System.out.println("ページコンテナが空");
                    return;
                }

                try (PtlPage page = pages.get(0);
                    PtlContent content = page.getContent())
                {
                    try (PtlParamDrawShape shape = new PtlParamDrawShape();
                        PtlColorSpaceSeparation csSep1 = new PtlColorSpaceSeparation("SEP1", 0.7f, 0.8f, 1.0f, 0.9f);
                        PtlColorSpaceSeparation csSep2 = new PtlColorSpaceSeparation("SEP2", 0.3f, 0.5f, 0.6f, 0.4f);
                        PtlColorSpaceSeparation csSep3 = new PtlColorSpaceSeparation("SEP3", 0.4f, 0.3f, 0.5f, 0.2f);
                        PtlColorSpaceSeparation csSep4 = new PtlColorSpaceSeparation("SEP4", 0.6f, 0.7f, 0.2f, 0.1f))
                    {
                        try (PtlColorSpaceDeviceN csDeviceN = new PtlColorSpaceDeviceN(PtlColorSpaceDeviceN.COMP_Cyan | PtlColorSpaceDeviceN.COMP_Magenta | PtlColorSpaceDeviceN.COMP_Yellow | PtlColorSpaceDeviceN.COMP_Black, csSep1, csSep2, csSep3, csSep4);
                            PtlColorDeviceN color = new PtlColorDeviceN(csDeviceN, 0.4f, 0.7f, 0.5f, 0.2f, 0.3f, 0.4f, 0.5f, 0.2f);
                            PtlRect rect = new PtlRect(0.0f, 0.0f, 100.0f, 100.0f))
                        {
                            shape.setFillColor(color);
                            shape.setLineColor(color);

                            //円形を描画
                            content.drawCircle(rect, shape);
                        }
                    }
                }
            }
            doc.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("-- 完了 --");
        }
    }
}

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

	概要:DeviceN Color

	Copyright 2025 Antenna House,Inc.
*/


using System;
using PdfTkNet;

if (args.Length < 2)
{
	Console.WriteLine("usage: DeviceNColor.exe in-pdf-file out-pdf-file");
	return;
}

try
{
	using (PtlParamInput input = new PtlParamInput(args[0]))
	using (PtlParamOutput output = new PtlParamOutput(args[1]))
	using (PtlPDFDocument doc = new PtlPDFDocument())
	{

		// PDFファイルをロードします。
		doc.load(input);

		int comp = (int)(PtlColorSpaceDeviceN.DEVICEN_COMP.COMP_Cyan | PtlColorSpaceDeviceN.DEVICEN_COMP.COMP_Magenta | PtlColorSpaceDeviceN.DEVICEN_COMP.COMP_Yellow | PtlColorSpaceDeviceN.DEVICEN_COMP.COMP_Black);

		// ページコンテナの取得
		using (PtlPages pages = doc.getPages())
		using (PtlPage page = pages.get(0))
		using (PtlContent content = page.getContent())
		using (PtlColorSpaceSeparation csSep1 = new PtlColorSpaceSeparation("SEP1", 0.7f, 0.8f, 1.0f, 0.9f))
		using (PtlColorSpaceSeparation csSep2 = new PtlColorSpaceSeparation("SEP2", 0.3f, 0.5f, 0.6f, 0.4f))
		using (PtlColorSpaceSeparation csSep3 = new PtlColorSpaceSeparation("SEP3", 0.4f, 0.3f, 0.5f, 0.2f))
		using (PtlColorSpaceSeparation csSep4 = new PtlColorSpaceSeparation("SEP4", 0.6f, 0.7f, 0.2f, 0.1f))
		using (PtlColorSpaceDeviceN csDeviceN = new PtlColorSpaceDeviceN(comp, csSep1, csSep2, csSep3, csSep4))
		using (PtlColorDeviceN colorDeviceN = new PtlColorDeviceN(csDeviceN, 0.4f, 0.7f, 0.5f, 0.2f, 0.3f, 0.4f, 0.5f, 0.2f))
		using (PtlParamDrawShape shape = new PtlParamDrawShape())
		using (PtlRect rect = new PtlRect(0.0f, 0.0f, 100.0f, 100.0f))
		{
			shape.setFillColor(colorDeviceN);
			shape.setLineColor(colorDeviceN);

			//円形を描画
			content.drawCircle(rect, shape);

			doc.save(output);
		}
	}
}
catch (PtlException pex)
{
	Console.WriteLine(pex.getErrorCode() + " : " + pex.getErrorMessageJP());
	pex.Dispose();
}
catch (Exception ex)
{
	Console.WriteLine(ex.Message);
}
finally
{
	Console.WriteLine("-- 完了 --");
}

            

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

実行例

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

DeviceNColor.exe C:\in\blank.pdf C:\sav\outDeviceNColor.pdf
完了!
java -jar DeviceNColor.jar C:\in\blank.pdf C:\sav\outDeviceNColor.pdf
-- 完了 --
DeviceNColor.exe C:\in\blank.pdf C:\sav\outDeviceNColor.pdf
-- 完了 --

出力結果イメージ

設定されたDeviceNカラーを使用した円が表示されます。

出力イメージ

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

サンプルコード

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