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

PDF Tool APIサンプルコード:文字列追加:ページ左下に角度のある文字列を追加する

機能イメージ

ページ左下に角度のある文字列を追加します。

概要

サンプルコードの概要

入力PDFの左下に330度回転した文字を追加します。

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

サンプルコード

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

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

	Copyright 2013-2025 Antenna House, Inc.
*/

#include < PdfTk.h >
#include < stdio.h >

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;
		}
		int pageno = pages.getCount();

		for (int i = 0; i < pageno; i++)
		{
			// ページの取得
			PtlPage page = pages.get(i);

			// テキスト追加
			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();

	//文字列回転出力
	{
		PtlSize pagesize = page.getSize();

		//出力矩形
		PtlRect rect(0.0f, 0.0f, pagesize.getWidth(), pagesize.getHeight());

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

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

		//フォント名の設定
		font.setName("游ゴシック");

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

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

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

		//文字列出力
		content.writeString(rect, PtlContent::ALIGN_BOTTOM_LEFT, 330.0f, "アンテナハウス株式会社", 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.*;

public class WriteTextAngle {

	public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ
        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;
                }

        		int pageno = pages.getCount();

        		for (int i = 0; i < pageno; i++)
        		{
	                // ページの取得
	                try (PtlPage page = pages.get(i))
	                {
	                    // テキスト追加
	                    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())
        {
    		PtlSize pagesize = page.getSize();
        	
            // 文字列回転出力
            try (PtlRect rect = new PtlRect(0.0f, 0.0f, pagesize.getWidth(), pagesize.getHeight());      // 出力矩形
                 PtlParamWriteString writestring = new PtlParamWriteString()) // 文字の描画に使うパラメータクラス
            {
                // フォント指定に使うパラメータクラス
                try (PtlParamFont font = new PtlParamFont())
                {
                    // フォント名の設定
                    font.setName("游ゴシック");

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

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

                // 文字色設定
                try (PtlColorDeviceRGB color = new PtlColorDeviceRGB(0.5f, 1.0f, 0.8f))
                {
                    writestring.setTextColor(color);
                }

                // 文字列出力
                content.writeString(rect, PtlContent.ALIGN.ALIGN_BOTTOM_LEFT, 330.0f, "アンテナハウス株式会社", writestring);
            }
        }
    }
}

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

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

	Copyright 2013-2025 Antenna House, Inc.
*/

using PdfTkNet;
using System;
using System.Xml;

namespace WriteTextAngle
{
    class WriteTextAngle
    {
        static void Main(string[] args)
        {
            if (args.Length < 2)
            {
                Console.WriteLine("usage: WriteTextAngle.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;
                        }
                        int pageno = pages.getCount();

                        for (int i = 0; i < pageno; i++)
                        {
                            using (PtlPage page = pages.get(i)) // 先頭ページ
                            {
                                // テキスト追加
                                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())  // ページコンテントの取得
            {
                PtlSize pagesize = page.getSize();

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

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

                        // フォントの設定
                        writestring.setFont(font);
                    }
                    // 文字色設定
                    using (PtlColorDeviceRGB colorText = new PtlColorDeviceRGB(0.5f, 1.0f, 0.8f))
                    {
                        writestring.setTextColor(colorText);
                    }
                    // 文字列出力
                    content.writeString(rect, PtlContent.ALIGN.ALIGN_BOTTOM_LEFT, 330.0f, "アンテナハウス株式会社", writestring);
                }
            }
        }
    }
}

            
AHPDFToolCmd80.exe -writeText -text "アンテナハウス株式会社" -align 7 -angle 330 -colorText 0.5 1 0.8 -font -name 游ゴシック -size 8 -d C:\in\in.pdf -o C:\sav\outWriteTextAngle.pdf
            

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

実行例

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

WriteTextAngle.exe C:\in\in.pdf C:\sav\outWriteTextAngle.pdf
完了!
java -jar WriteTextAngle.jar C:\in\in.pdf C:\sav\outWriteTextAngle.pdf
-- 完了 --
WriteTextAngle.exe C:\in\in.pdf C:\sav\outWriteTextAngle.pdf
-- 完了 --
AHPDFToolCmd80.exe -writeText -text "アンテナハウス株式会社" -align 7 -angle 330 -colorText 0.5 1 0.8 -font -name 游ゴシック -size 8 -d C:\in\in.pdf -o C:\sav\outWriteTextAngle.pdf
 use time 0.335000s

出力結果イメージ

各ページの左下に330度回転した文字が挿入されます。

出力イメージ

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

サンプルコード

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