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

PDF Tool APIサンプルコード:文字列挿入:絵文字の挿入

機能イメージ

PDF文書の指定ページ上に絵文字を挿入します。

概要

サンプルコードの概要

PDF文書の指定ページ上に文字列を挿入します。
挿入する文字列には絵文字を使用できます。

絵文字には絵文字用のフォントを指定します。
Windowsでは "Segoe UI Emoji"フォントを指定します。
C#とJavaではそのまま絵文字を指定できましたが、C++で絵文字を使う際には文字コードで指定します。
(サンプルが外部参照エラーでビルドできない場合は「プリプロセッサ」に「WIN32」を追加してください。)

  • PtlContent: ページに描画される内容(コンテント)を表現するクラス
  • PtlParamWriteString: 文字の描画に使うパラメータクラス
  • PtlContent.writeString: 文字列を出力
  • PtlContent.writeStringV: 縦書きの文字列を出力

サンプルコード

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

	概要:テキストオブジェクトの挿入

	Copyright 2013-2025 Antenna House, Inc.
*/

#include 
#include 

using namespace PdfTk;

void writeString(PtlPage& page);

int main(int argc, char* argv[])
{
	if (argc < 3) {
		printf("usage: WriteString.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);

		// テキスト追加
		writeString(page);

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

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

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

	//絵文字列出力
	{
		//出力矩形
		PtlRect rect(0.0f, 250.0f, 300.0f, 500.0f);

		//文字の描画に使うパラメータクラス
		PtlParamWriteString writestring;

		//フォント指定に使うパラメータクラス
		PtlParamFont font;

		//フォント名の設定
		font.setName("Segoe UI Emoji");

		//サイズの設定
		font.setSize(80.0f);

		//フォントの設定
		writestring.setFont(font);

		//文字色設定
		writestring.setTextColor(PtlColorDeviceRGB(1.0f, 0.0f, 0.0f));

		std::wstring stdwstr = L"\U0001f31d\U0001f31e\U0001f404\U0001f408\U0001f409";

		//文字列出力
		content.writeString(rect, PtlContent::ALIGN_CENTER, PtlParamString((CP_UChar*)stdwstr.c_str()), writestring);
	}

	//文字列出力
	{
		//出力矩形
		PtlRect rect(0.0f, 0.0f, 300.0f, 250.0f);

		//文字の描画に使うパラメータクラス
		PtlParamWriteString writestring;

		//フォント指定に使うパラメータクラス
		PtlParamFont font;

		//フォント名の設定
	#ifdef _WIN32
		font.setName("MS明朝");
	#else
		font.setName("Times-Roman");
	#endif

		//サイズの設定
		font.setSize(80.0f);

		//フォントの設定
		writestring.setFont(font);

		//文字色設定
		writestring.setTextColor(PtlColorDeviceRGB(1.0f, 0.0f, 0.0f));

		//文字列出力
		content.writeString(rect, PtlContent::ALIGN_CENTER, "writeString Test 1", writestring);
	}

	//文字列回転出力
	{
		//出力矩形
		PtlRect rect(0.0f, 0.0f, 300.0f, 250.0f);

		//文字の描画に使うパラメータクラス
		PtlParamWriteString writestring;

		//フォント指定に使うパラメータクラス
		PtlParamFont font;

		//フォント名の設定
	#ifdef _WIN32
		font.setName("MS-Gothic");
	#else
		font.setName("Courier");
	#endif

		//サイズの設定
		font.setSize(40.0f);

		//フォントの設定
		writestring.setFont(font);

		//文字色設定
		writestring.setTextColor(PtlColorDeviceRGB(0.0f, 1.0f, 0.0f));

		//文字列出力
		content.writeString(rect, PtlContent::ALIGN_BOTTOM_LEFT, 45.0f, "writeString Test 2", writestring);
	}

	//縦書きの文字列出力
	{
		//出力矩形
		PtlRect rect(0.0f, 0.0f, 300.0f, 200.0f);

		//文字の描画に使うパラメータクラス
		PtlParamWriteString writestring;

		//フォント指定に使うパラメータクラス
		PtlParamFont font;

		//フォント名の設定
	#ifdef _WIN32
		font.setName("MSP-Mincho");
	#else
		font.setName("Helvetica");
	#endif

		//サイズの設定
		font.setSize(60.0f);

		//ウエイトの設定
		font.setWeight(PtlParamFont::WEIGHT_HEAVY);

		//フォントの設定
		writestring.setFont(font);

		//文字色設定
		writestring.setTextColor(PtlColorDeviceRGB(0.0f, 0.0f, 1.0f));

		//文字列出力
		content.writeStringV(rect, PtlContent::ALIGN_TOP_LEFT, "writeString Test 3", writestring);
	}
}

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

    概要:テキストオブジェクトの挿入

    Copyright 2015-2025 Antenna House, Inc.
*/

package Sample;

import jp.co.antenna.ptl.PtlColorDeviceRGB;
import jp.co.antenna.ptl.PtlContent;
import jp.co.antenna.ptl.PtlException;
import jp.co.antenna.ptl.PtlPDFDocument;
import jp.co.antenna.ptl.PtlPage;
import jp.co.antenna.ptl.PtlPages;
import jp.co.antenna.ptl.PtlParamFont;
import jp.co.antenna.ptl.PtlParamInput;
import jp.co.antenna.ptl.PtlParamOutput;
import jp.co.antenna.ptl.PtlParamWriteString;
import jp.co.antenna.ptl.PtlRect;

public class WriteString {

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

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

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

                // ページの取得
                try (PtlPage page = pages.get(0))
                {
                    // テキスト追加
                    writeString(page);
                }
            }

            // ファイルに保存します。
            doc.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("-- 完了 --");
        }
    }

    public static void writeString(PtlPage page) throws PtlException, Exception, Error
    {
        // ページコンテントの取得
        try (PtlContent content = page.getContent())
        {
            // 絵文字列出力■■■
            try (PtlRect rect0 = new PtlRect(0.0f, 250.0f, 300.0f, 500.0f);      // 出力矩形
                 PtlParamWriteString writestring0 = new PtlParamWriteString()) // 文字の描画に使うパラメータクラス
            {
                // フォント指定に使うパラメータクラス
                try (PtlParamFont font0 = new PtlParamFont())
                {
                    // フォント名の設定
                    font0.setName("Segoe UI Emoji");

                    // サイズの設定
                    font0.setSize(80.0f);

                    // フォントの設定
                    writestring0.setFont(font0);
                }

                // 文字色設定
                try (PtlColorDeviceRGB color0 = new PtlColorDeviceRGB(1.0f, 0.0f, 0.0f))
                {
                    writestring0.setTextColor(color0);
                }

                // 文字列出力
                content.writeString(rect0, PtlContent.ALIGN.ALIGN_CENTER, "🌝🌞🐄🐈🐉", writestring0);
            }
            
            // 文字列出力
            try (PtlRect rect1 = new PtlRect(0.0f, 0.0f, 300.0f, 250.0f);      // 出力矩形
                 PtlParamWriteString writestring1 = new PtlParamWriteString()) // 文字の描画に使うパラメータクラス
            {
                // フォント指定に使うパラメータクラス
                try (PtlParamFont font1 = new PtlParamFont())
                {
                    // フォント名の設定
                    font1.setName("MS明朝");

                    // サイズの設定
                    font1.setSize(80.0f);

                    // フォントの設定
                    writestring1.setFont(font1);
                }

                // 文字色設定
                try (PtlColorDeviceRGB color1 = new PtlColorDeviceRGB(1.0f, 0.0f, 0.0f))
                {
                    writestring1.setTextColor(color1);
                }

                // 文字列出力
                content.writeString(rect1, PtlContent.ALIGN.ALIGN_CENTER, "writeString Test 1", writestring1);
            }

            // 文字列回転出力
            try (PtlRect rect2 = new PtlRect(0.0f, 0.0f, 300.0f, 250.0f);      // 出力矩形
                 PtlParamWriteString writestring2 = new PtlParamWriteString()) // 文字の描画に使うパラメータクラス
            {
                // フォント指定に使うパラメータクラス
                try (PtlParamFont font2 = new PtlParamFont())
                {
                    // フォント名の設定
                    font2.setName("MS-Gothic");

                    // サイズの設定
                    font2.setSize(40.0f);

                    // フォントの設定
                    writestring2.setFont(font2);
                }

                // 文字色設定
                try (PtlColorDeviceRGB color2 = new PtlColorDeviceRGB(0.0f, 1.0f, 0.0f))
                {
                    writestring2.setTextColor(color2);
                }

                // 文字列出力
                content.writeString(rect2, PtlContent.ALIGN.ALIGN_BOTTOM_LEFT, 45.0f, "writeString Test 2", writestring2);
            }

            // 縦書きの文字列出力
            try (PtlRect rect3 = new PtlRect(0.0f, 0.0f, 300.0f, 200.0f);    // 出力矩形
                 PtlParamWriteString writestring3 = new PtlParamWriteString()) // 文字の描画に使うパラメータクラス
            {
                // フォント指定に使うパラメータクラス
                try (PtlParamFont font3 = new PtlParamFont())
                {
                    // フォント名の設定
                    font3.setName("MSP-Mincho");

                    // サイズの設定
                    font3.setSize(60.0f);

                    // ウエイトの設定
                    font3.setWeight(PtlParamFont.WEIGHT.WEIGHT_HEAVY);

                    // フォントの設定
                    writestring3.setFont(font3);
                }

                // 文字色設定
                try (PtlColorDeviceRGB color3 = new PtlColorDeviceRGB(0.0f, 0.0f, 1.0f))
                {
                    writestring3.setTextColor(color3);
                }

                // 文字列出力
                content.writeStringV(rect3, PtlContent.ALIGN.ALIGN_TOP_LEFT, "writeString Test 3", writestring3);
            }
        }
    }
}

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

	概要:テキストオブジェクトの挿入

	Copyright 2013-2025 Antenna House, Inc.
*/

using System;
using PdfTkNet;

namespace WriteString
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Length < 2)
            {
                Console.WriteLine("usage: WriteString.exe in-pdf-file out-pdf-file");
                return;
            }

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

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

                        using (PtlPage page = pages.get(0)) // 先頭ページ
                        {
                            // テキスト追加
                            writeString(page);
                        }
                    }

                    // ファイルに保存します。
                    doc.save(outputFile);
                }
            }
            catch (PtlException pex)
            {
                Console.WriteLine(pex.getErrorCode() + " : " + pex.getErrorMessageJP());
                pex.Dispose();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                Console.WriteLine("-- 完了 --");
            }
        }

        static void writeString(PtlPage page)
        {
            using (PtlContent content = page.getContent())  // ページコンテントの取得
            {
                // 絵文字出力
                using (PtlRect rect = new PtlRect(0.0f, 250.0f, 300.0f, 500.0f))
                using (PtlParamWriteString writestring = new PtlParamWriteString()) // 文字の描画に使うパラメータクラス
                {
                    // フォント指定に使うパラメータクラス
                    using (PtlParamFont font = new PtlParamFont())
                    {
                        // フォント名の設定
                        font.setName("Segoe UI Emoji");

                        // サイズの設定
                        font.setSize(80.0f);

                        // フォントの設定
                        writestring.setFont(font);
                    }
                    // 文字色設定
                    using (PtlColorDeviceRGB colorText = new PtlColorDeviceRGB(1.0f, 0.0f, 0.0f))
                    {
                        writestring.setTextColor(colorText);
                    }
                    // 文字列出力
                    content.writeString(rect, PtlContent.ALIGN.ALIGN_CENTER, "🌝🌞🐄🐈🐉", writestring);
                }

                // 文字列出力
                using (PtlRect rect = new PtlRect(0.0f, 0.0f, 300.0f, 250.0f))
                using (PtlParamWriteString writestring = new PtlParamWriteString()) // 文字の描画に使うパラメータクラス
                {
                    // フォント指定に使うパラメータクラス
                    using (PtlParamFont font = new PtlParamFont())
                    {
                        // フォント名の設定
                        font.setName("MS明朝");

                        // サイズの設定
                        font.setSize(80.0f);

                        // フォントの設定
                        writestring.setFont(font);
                    }
                    // 文字色設定
                    using (PtlColorDeviceRGB colorText = new PtlColorDeviceRGB(1.0f, 0.0f, 0.0f))
                    {
                        writestring.setTextColor(colorText);
                    }
                    // 文字列出力
                    content.writeString(rect, PtlContent.ALIGN.ALIGN_CENTER, "writeString Test 1", writestring);
                }

                // 文字列回転出力
                using (PtlRect rect = new PtlRect(0.0f, 0.0f, 300.0f, 250.0f))
                using (PtlParamWriteString writestring = new PtlParamWriteString()) // 文字の描画に使うパラメータクラス
                {
                    // フォント指定に使うパラメータクラス
                    using (PtlParamFont font = new PtlParamFont())
                    {
                        // フォント名の設定
                        font.setName("MS-Gothic");

                        // サイズの設定
                        font.setSize(40.0f);

                        // フォントの設定
                        writestring.setFont(font);
                    }
                    // 文字色設定
                    using (PtlColorDeviceRGB colorText = new PtlColorDeviceRGB(0.0f, 1.0f, 0.0f))
                    {
                        writestring.setTextColor(colorText);
                    }
                    // 文字列出力
                    content.writeString(rect, PtlContent.ALIGN.ALIGN_BOTTOM_LEFT, 45.0f, "writeString Test 2", writestring);
                }

                // 縦書きの文字列出力
                using (PtlRect rect = new PtlRect(0.0f, 0.0f, 300.0f, 200.0f))
                using (PtlParamWriteString writestring = new PtlParamWriteString()) // 文字の描画に使うパラメータクラス
                {
                    // フォント指定に使うパラメータクラス
                    using (PtlParamFont font = new PtlParamFont())
                    {
                        // フォント名の設定
                        font.setName("MSP-Mincho");

                        // サイズの設定
                        font.setSize(60.0f);

                        // ウエイトの設定
                        font.setWeight(PtlParamFont.WEIGHT.WEIGHT_HEAVY);

                        // フォントの設定
                        writestring.setFont(font);
                    }
                    // 文字色設定
                    using (PtlColorDeviceRGB colorText = new PtlColorDeviceRGB(0.0f, 0.0f, 1.0f))
                    {
                        writestring.setTextColor(colorText);
                    }
                    // 文字列出力
                    content.writeStringV(rect, PtlContent.ALIGN.ALIGN_TOP_LEFT, "writeString Test 3", writestring);
                }
            }
        }
    }
}

            

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

実行例

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

WriteString.exe C:\in\blankA3.pdf C:\sav\outWriteString.pdf
完了!
java -jar WriteString.jar C:\in\blankA3.pdf C:\sav\outWriteString.pdf
-- 完了 --
WriteString.exe C:\in\blankA3.pdf C:\sav\outWriteString.pdf
-- 完了 --

出力結果イメージ

このサンプルでは横書きで「🌝🌞🐄🐈🐉」「writeString Test 1」が追加されます。
45度回転した文字列「writeString Test 2」と
縦書きの「writeString Test 3」も追加されています。

出力イメージ

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

サンプルコード

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